Skip to content

Commit b951a37

Browse files
committed
refactor(admin): apply retention cleanup review feedback (#7717)
1 parent 40a6214 commit b951a37

6 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ShenyuAdminConfiguration.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.apache.shenyu.admin.config;
1919

20+
import org.apache.shenyu.admin.config.properties.InstanceRetentionProperties;
2021
import org.apache.shenyu.admin.service.converter.SelectorHandleConverter;
2122
import org.apache.shenyu.admin.service.converter.SelectorHandleConverterFactor;
2223
import org.apache.shenyu.admin.service.publish.InstanceInfoReportEventPublisher;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2325
import org.springframework.context.ApplicationEventPublisher;
2426
import org.springframework.context.annotation.Bean;
2527
import org.springframework.context.annotation.Configuration;
@@ -39,6 +41,7 @@
3941
*/
4042
@Configuration
4143
@EnableTransactionManagement
44+
@EnableConfigurationProperties(InstanceRetentionProperties.class)
4245
public class ShenyuAdminConfiguration {
4346

4447
/**

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/InstanceRetentionProperties.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
package org.apache.shenyu.admin.config.properties;
1919

2020
import org.springframework.boot.context.properties.ConfigurationProperties;
21-
import org.springframework.context.annotation.Configuration;
2221

2322
import java.time.Duration;
2423

2524
/**
2625
* Instance retention properties, used to bound the records kept for expired instances.
2726
*/
28-
@Configuration
2927
@ConfigurationProperties(prefix = "shenyu.instance-retention")
3028
public class InstanceRetentionProperties {
3129

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/InstanceCheckService.java‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class InstanceCheckService {
5757

5858
private static final int MAX_HISTORY_SIZE = 20;
5959

60+
private static final long DB_CLEANUP_INTERVAL_MS = TimeUnit.HOURS.toMillis(1);
61+
6062
private static final Logger LOG = LoggerFactory.getLogger(InstanceCheckService.class);
6163

6264
private ScheduledThreadPoolExecutor executor;
@@ -75,6 +77,8 @@ public class InstanceCheckService {
7577

7678
private final Map<Integer, Deque<Long>> stateHistoryMap;
7779

80+
private volatile long lastDbCleanupTime;
81+
7882
public InstanceCheckService(final InstanceInfoService instanceInfoService,
7983
final InstanceRetentionProperties instanceRetentionProperties) {
8084
this.scheduledTime = 10;
@@ -190,12 +194,13 @@ private void doCheck() {
190194
* The cleanup runs only on the node that owns the liveness checks (the master Admin in a cluster),
191195
* and the database delete is guarded by the same cutoff so a renewed identity is never removed.
192196
*/
193-
private void purgeExpiredInstances() {
197+
void purgeExpiredInstances() {
194198
if (!instanceRetentionProperties.isEnabled()) {
195199
return;
196200
}
201+
long now = System.currentTimeMillis();
197202
long ttlMillis = instanceRetentionProperties.getTtl().toMillis();
198-
long cutoff = System.currentTimeMillis() - ttlMillis;
203+
long cutoff = now - ttlMillis;
199204
if (cutoff < 0) {
200205
return;
201206
}
@@ -219,14 +224,21 @@ private void purgeExpiredInstances() {
219224
if (purgedCount > 0) {
220225
LOG.info("[instanceHealthInfo]{} expired instance record(s) removed from memory", purgedCount);
221226
}
227+
// The in-memory purge needs the liveness cadence, but the bulk delete does not:
228+
// run the database cleanup at most once per interval instead of on every check cycle.
229+
if (now - lastDbCleanupTime < DB_CLEANUP_INTERVAL_MS) {
230+
return;
231+
}
232+
lastDbCleanupTime = now;
222233
int deletedRows = instanceInfoService.deleteExpired(cutoff);
223234
if (deletedRows > 0) {
224235
LOG.info("[instanceHealthInfo]{} expired instance record(s) removed from database", deletedRows);
225236
}
226237
}
227238

228239
private boolean shouldPurge(final InstanceInfoVO instance, final long cutoff) {
229-
return Objects.equals(InstanceStatusEnum.DELETED.getCode(), instance.getInstanceState())
240+
return Objects.nonNull(instance.getInstanceState())
241+
&& InstanceStatusEnum.DELETED.getCode() == instance.getInstanceState()
230242
&& instance.getLastHeartBeatTime() < cutoff;
231243
}
232244

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/InstanceInfoServiceImpl.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public InstanceInfoVO findById(final String id) {
111111
}
112112

113113
@Override
114+
// Scheduled bulk delete: deliberately outside any transaction so it does not pin a long-lived
115+
// transaction/undo for the whole maintenance cycle. Do not switch it to REQUIRES_NEW.
114116
@Transactional(propagation = Propagation.NOT_SUPPORTED)
115117
public int deleteExpired(final long cutoff) {
116118
return instanceInfoMapper.deleteExpired(cutoff);

‎shenyu-admin/src/main/resources/mappers/instance-info-sqlmap.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
WHERE id = #{id,jdbcType=VARCHAR}
189189
</delete>
190190

191+
<!-- instance_state = 0 is the DELETED state, see org.apache.shenyu.common.enums.InstanceStatusEnum. -->
191192
<delete id="deleteExpired">
192193
DELETE FROM instance_info
193194
WHERE instance_state = 0

‎shenyu-admin/src/test/java/org/apache/shenyu/admin/service/impl/InstanceCheckServiceTest.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void testRetentionCleanupDisabledByDefault() {
301301
cached.setInstanceState(InstanceStatusEnum.DELETED.getCode());
302302
cached.setLastHeartBeatTime(System.currentTimeMillis() - Duration.ofDays(30).toMillis());
303303

304-
ReflectionTestUtils.invokeMethod(instanceCheckService, "purgeExpiredInstances");
304+
instanceCheckService.purgeExpiredInstances();
305305

306306
assertNotNull(instanceCheckService.getInstanceHealthBeatInfo(dto));
307307
verify(instanceInfoService, never()).deleteExpired(anyLong());
@@ -324,7 +324,7 @@ void testRetentionCleanupRemovesOnlyExpiredDeletedInstances() {
324324
freshDeleted.setInstanceState(InstanceStatusEnum.DELETED.getCode());
325325
InstanceInfoVO online = instanceCheckService.getInstanceHealthBeatInfo(onlineDTO);
326326

327-
ReflectionTestUtils.invokeMethod(instanceCheckService, "purgeExpiredInstances");
327+
instanceCheckService.purgeExpiredInstances();
328328

329329
assertNull(instanceCheckService.getInstanceHealthBeatInfo(expiredDTO));
330330
assertNotNull(instanceCheckService.getInstanceHealthBeatInfo(freshDeletedDTO));
@@ -344,7 +344,7 @@ void testRetentionCleanupKeepsRenewedIdentity() {
344344

345345
// the instance heartbeats again before the cleanup cycle runs, so its cached record is kept
346346
instanceCheckService.handleBeatInfo(dto);
347-
ReflectionTestUtils.invokeMethod(instanceCheckService, "purgeExpiredInstances");
347+
instanceCheckService.purgeExpiredInstances();
348348

349349
assertNotNull(instanceCheckService.getInstanceHealthBeatInfo(dto));
350350
}
@@ -359,12 +359,28 @@ void testRetentionCleanupRemovesLegacyRowsWithoutHeartbeat() {
359359
when(instanceInfoService.list()).thenReturn(Collections.singletonList(legacy));
360360
instanceCheckService.fetchInstanceData();
361361

362-
ReflectionTestUtils.invokeMethod(instanceCheckService, "purgeExpiredInstances");
362+
instanceCheckService.purgeExpiredInstances();
363363

364364
assertNull(instanceCheckService.getInstanceHealthBeatInfo("11.0.0.1:8090@http#nsH"));
365365
verify(instanceInfoService).deleteExpired(anyLong());
366366
}
367367

368+
@Test
369+
void testDatabaseSweepIsThrottledToConfiguredInterval() {
370+
when(instanceRetentionProperties.isEnabled()).thenReturn(true);
371+
when(instanceRetentionProperties.getTtl()).thenReturn(Duration.ofDays(7));
372+
373+
instanceCheckService.purgeExpiredInstances();
374+
instanceCheckService.purgeExpiredInstances();
375+
verify(instanceInfoService, times(1)).deleteExpired(anyLong());
376+
377+
// the database sweep runs again only after the cleanup interval has elapsed
378+
ReflectionTestUtils.setField(instanceCheckService, "lastDbCleanupTime",
379+
System.currentTimeMillis() - Duration.ofHours(2).toMillis());
380+
instanceCheckService.purgeExpiredInstances();
381+
verify(instanceInfoService, times(2)).deleteExpired(anyLong());
382+
}
383+
368384
private InstanceInfoVO buildVO(final String ip, final String port, final String type, final String ns) {
369385
InstanceInfoVO v = new InstanceInfoVO();
370386
v.setInstanceIp(ip);
@@ -389,3 +405,4 @@ private InstanceBeatInfoDTO buildDTO(final String ip, final String port, final S
389405
return dto;
390406
}
391407
}
408+

0 commit comments

Comments
 (0)