Skip to content

Commit 341cd9c

Browse files
Address code review comments
1 parent ae438b9 commit 341cd9c

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetAllocationStatsAction.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,22 @@ private static class AllocationStatsCache {
224224

225225
void setTTL(TimeValue ttl) {
226226
ttlMillis = ttl.millis();
227-
228227
if (ttlMillis == 0L) {
229228
cachedStats.set(null);
230229
}
231230
}
232231

233232
Map<String, NodeAllocationStats> get() {
234-
235233
if (ttlMillis == 0L) {
236234
return null;
237235
}
238236

237+
// We don't set the atomic ref to null here upon expiration since we know it is about to be replaced with a fresh instance.
239238
final var stats = cachedStats.get();
240-
241-
if (stats == null || threadPool.relativeTimeInMillis() - stats.timestampMillis > ttlMillis) {
242-
return null;
243-
}
244-
245-
return stats.stats;
239+
return stats == null || threadPool.relativeTimeInMillis() - stats.timestampMillis > ttlMillis ? null : stats.stats;
246240
}
247241

248242
void put(Map<String, NodeAllocationStats> stats) {
249-
250243
if (ttlMillis > 0L) {
251244
cachedStats.set(new CachedAllocationStats(stats, threadPool.relativeTimeInMillis()));
252245
}

server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetAllocationStatsActionTests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public void setUp() throws Exception {
7474
clusterService = ClusterServiceUtils.createClusterService(
7575
threadPool,
7676
new ClusterSettings(
77-
Settings.builder()
78-
.put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), allocationStatsCacheTTL.toString())
79-
.build(),
77+
Settings.builder().put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), allocationStatsCacheTTL).build(),
8078
ClusterSettings.BUILT_IN_CLUSTER_SETTINGS
8179
)
8280
);
@@ -113,7 +111,7 @@ private void disableAllocationStatsCache() {
113111

114112
private void setAllocationStatsCacheTTL(TimeValue ttl) {
115113
clusterService.getClusterSettings()
116-
.applySettings(Settings.builder().put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), ttl.toString()).build());
114+
.applySettings(Settings.builder().put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), ttl).build());
117115
};
118116

119117
public void testReturnsOnlyRequestedStats() throws Exception {
@@ -223,21 +221,24 @@ public void testGetStatsWithCachingEnabled() throws Exception {
223221
EnumSet.of(Metric.ALLOCATIONS)
224222
);
225223

226-
final var future = new PlainActionFuture<TransportGetAllocationStatsAction.Response>();
227-
action.masterOperation(mock(Task.class), request, ClusterState.EMPTY_STATE, future);
228-
final var response = future.get();
229-
assertSame("Expected the cached allocation stats to be returned", response.getNodeAllocationStats(), allocationStats.get());
230-
231-
l.onResponse(null);
224+
action.masterOperation(mock(Task.class), request, ClusterState.EMPTY_STATE, l.map(response -> {
225+
assertSame("Expected the cached allocation stats to be returned", response.getNodeAllocationStats(), allocationStats.get());
226+
return null;
227+
}));
232228
};
233229

234230
// Initial cache miss, all threads should get the same value.
235231
resetExpectedAllocationStats.run();
236232
ESTestCase.startInParallel(between(1, 5), threadNumber -> safeAwait(threadTask));
237233
verify(allocationStatsService, times(++numExpectedAllocationStatsServiceCalls)).stats();
238234

235+
// Advance the clock to a time less than or equal to the TTL and verify we still get the cached stats.
236+
threadPool.setCurrentTimeInMillis(startTimeMillis + between(0, (int) allocationStatsCacheTTL.millis()));
237+
ESTestCase.startInParallel(between(1, 5), threadNumber -> safeAwait(threadTask));
238+
verify(allocationStatsService, times(numExpectedAllocationStatsServiceCalls)).stats();
239+
239240
// Force the cached stats to expire.
240-
threadPool.setCurrentTimeInMillis(startTimeMillis + (2 * allocationStatsCacheTTL.getMillis()));
241+
threadPool.setCurrentTimeInMillis(startTimeMillis + allocationStatsCacheTTL.getMillis() + 1);
241242

242243
// Expect a single call to the stats service on the cache miss.
243244
resetExpectedAllocationStats.run();
@@ -246,8 +247,8 @@ public void testGetStatsWithCachingEnabled() throws Exception {
246247

247248
// Update the TTL setting to disable the cache, we expect a service call each time.
248249
setAllocationStatsCacheTTL(TimeValue.ZERO);
249-
threadTask.accept(ActionListener.noop());
250-
threadTask.accept(ActionListener.noop());
250+
safeAwait(threadTask);
251+
safeAwait(threadTask);
251252
numExpectedAllocationStatsServiceCalls += 2;
252253
verify(allocationStatsService, times(numExpectedAllocationStatsServiceCalls)).stats();
253254

0 commit comments

Comments
 (0)