Skip to content

Commit 9b87c4b

Browse files
Fix testReturnsOnlyRequestedStats after merging 124902
1 parent d8eb439 commit 9b87c4b

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,20 @@
4343
import java.util.concurrent.atomic.AtomicBoolean;
4444
import java.util.concurrent.atomic.AtomicInteger;
4545
import java.util.concurrent.atomic.AtomicReference;
46-
import java.util.function.Consumer;
4746

4847
import static org.hamcrest.Matchers.anEmptyMap;
4948
import static org.hamcrest.Matchers.containsString;
5049
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
5150
import static org.hamcrest.Matchers.not;
5251
import static org.mockito.Mockito.mock;
53-
5452
import static org.mockito.Mockito.times;
5553
import static org.mockito.Mockito.verify;
5654
import static org.mockito.Mockito.when;
5755

5856
public class TransportGetAllocationStatsActionTests extends ESTestCase {
5957

6058
private long startTimeMillis;
61-
private TimeValue cacheTTL;
59+
private TimeValue allocationStatsCacheTTL;
6260
private ControlledRelativeTimeThreadPool threadPool;
6361
private ClusterService clusterService;
6462
private TransportService transportService;
@@ -71,12 +69,14 @@ public class TransportGetAllocationStatsActionTests extends ESTestCase {
7169
public void setUp() throws Exception {
7270
super.setUp();
7371
startTimeMillis = 0L;
74-
cacheTTL = TimeValue.timeValueMinutes(1);
72+
allocationStatsCacheTTL = TimeValue.timeValueMinutes(1);
7573
threadPool = new ControlledRelativeTimeThreadPool(TransportClusterAllocationExplainActionTests.class.getName(), startTimeMillis);
7674
clusterService = ClusterServiceUtils.createClusterService(
7775
threadPool,
7876
new ClusterSettings(
79-
Settings.builder().put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), cacheTTL.toString()).build(),
77+
Settings.builder()
78+
.put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), allocationStatsCacheTTL.toString())
79+
.build(),
8080
ClusterSettings.BUILT_IN_CLUSTER_SETTINGS
8181
)
8282
);
@@ -107,7 +107,17 @@ public void tearDown() throws Exception {
107107
transportService.close();
108108
}
109109

110+
private void disableAllocationStatsCache() {
111+
setAllocationStatsCacheTTL(TimeValue.ZERO);
112+
}
113+
114+
private void setAllocationStatsCacheTTL(TimeValue ttl) {
115+
clusterService.getClusterSettings()
116+
.applySettings(Settings.builder().put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), ttl.toString()).build());
117+
};
118+
110119
public void testReturnsOnlyRequestedStats() throws Exception {
120+
disableAllocationStatsCache();
111121
int expectedNumberOfStatsServiceCalls = 0;
112122

113123
for (final var metrics : List.of(
@@ -149,6 +159,7 @@ public void testReturnsOnlyRequestedStats() throws Exception {
149159
}
150160

151161
public void testDeduplicatesStatsComputations() throws InterruptedException {
162+
disableAllocationStatsCache();
152163
final var requestCounter = new AtomicInteger();
153164
final var isExecuting = new AtomicBoolean();
154165
when(allocationStatsService.stats()).thenAnswer(invocation -> {
@@ -205,13 +216,6 @@ public void testGetStatsWithCachingEnabled() throws Exception {
205216
when(allocationStatsService.stats()).thenReturn(stats);
206217
};
207218

208-
final Consumer<TimeValue> setCacheTTL = (ttl) -> {
209-
clusterService.getClusterSettings()
210-
.applySettings(
211-
Settings.builder().put(TransportGetAllocationStatsAction.CACHE_TTL_SETTING.getKey(), ttl.toString()).build()
212-
);
213-
};
214-
215219
final CheckedConsumer<ActionListener<Void>, Exception> threadTask = l -> {
216220
final var request = new TransportGetAllocationStatsAction.Request(
217221
TEST_REQUEST_TIMEOUT,
@@ -233,22 +237,22 @@ public void testGetStatsWithCachingEnabled() throws Exception {
233237
verify(allocationStatsService, times(++numExpectedAllocationStatsServiceCalls)).stats();
234238

235239
// Force the cached stats to expire.
236-
threadPool.setCurrentTimeInMillis(startTimeMillis + (2 * cacheTTL.getMillis()));
240+
threadPool.setCurrentTimeInMillis(startTimeMillis + (2 * allocationStatsCacheTTL.getMillis()));
237241

238242
// Expect a single call to the stats service on the cache miss.
239243
resetExpectedAllocationStats.run();
240244
ESTestCase.startInParallel(between(1, 5), threadNumber -> safeAwait(threadTask));
241245
verify(allocationStatsService, times(++numExpectedAllocationStatsServiceCalls)).stats();
242246

243247
// Update the TTL setting to disable the cache, we expect a service call each time.
244-
setCacheTTL.accept(TimeValue.ZERO);
248+
setAllocationStatsCacheTTL(TimeValue.ZERO);
245249
threadTask.accept(ActionListener.noop());
246250
threadTask.accept(ActionListener.noop());
247251
numExpectedAllocationStatsServiceCalls += 2;
248252
verify(allocationStatsService, times(numExpectedAllocationStatsServiceCalls)).stats();
249253

250254
// Re-enable the cache, only one thread should call the stats service.
251-
setCacheTTL.accept(TimeValue.timeValueMinutes(5));
255+
setAllocationStatsCacheTTL(TimeValue.timeValueMinutes(5));
252256
resetExpectedAllocationStats.run();
253257
ESTestCase.startInParallel(between(1, 5), threadNumber -> safeAwait(threadTask));
254258
verify(allocationStatsService, times(++numExpectedAllocationStatsServiceCalls)).stats();

0 commit comments

Comments
 (0)