Skip to content

Commit

Permalink
spotless
Browse files Browse the repository at this point in the history
Signed-off-by: Kiran Prakash <[email protected]>
  • Loading branch information
kiranprakash154 committed Mar 29, 2024
1 parent bf11aad commit 09ecf8a
Showing 1 changed file with 55 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -982,61 +982,61 @@ public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throw
assertEquals(expectedThirdCachedEntrySize, currentMemorySize);
}

// when staleness threshold is high, it should NOT clean-up
public void testStaleKeysCleanup_ThresholdUpdates() throws Exception {
Instant start = Instant.now();
long thresholdInMillis = 1_500;
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.SETTING_INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING, 0.90)
.put(IndicesRequestCache.SETTING_INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING, TimeValue.timeValueMillis(thresholdInMillis))
);
String index = "index";
Client client = client(node);
setupIndex(client, index);

// create first cache entry
createCacheEntry(client, index, "hello");
assertCacheState(client, index, 0, 1);
long expectedFirstCachedItemEntrySize = getRequestCacheStats(client, index).getMemorySizeInBytes();
assertTrue(expectedFirstCachedItemEntrySize > 0);

// create second cache entry
createCacheEntry(client, index, "there");
assertCacheState(client, "index", 0, 2);
assertEquals(expectedFirstCachedItemEntrySize * 2, getRequestCacheStats(client, "index").getMemorySizeInBytes());

// force refresh so that it creates 2 stale keys in the cache for the cache cleaner to pick up.
flushAndRefresh("index");
client().prepareIndex("index").setId("1").setSource("k", "good bye");
ensureSearchable("index");

// create another entry
createCacheEntry(client, index, "hello1");
assertCacheState(client, "index", 0, 3);
long cacheSizeBeforeCleanup = getRequestCacheStats(client, "index").getMemorySizeInBytes();
assertTrue(cacheSizeBeforeCleanup > expectedFirstCachedItemEntrySize * 2);
assertEquals(cacheSizeBeforeCleanup, expectedFirstCachedItemEntrySize * 3, 2);

Instant end = Instant.now();
long elapsedTimeMillis = Duration.between(start, end).toMillis();
// if this test is flaky, increase the sleep time.
long sleepTime = (thresholdInMillis - elapsedTimeMillis) + 2_000;
Thread.sleep(sleepTime);

// cache cleaner should have skipped the cleanup
long cacheSizeAfterCleanup = getRequestCacheStats(client, "index").getMemorySizeInBytes();
assertEquals(cacheSizeBeforeCleanup, cacheSizeAfterCleanup);

// Set indices.requests.cache.cleanup.staleness_threshold to "10%"
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.persistentSettings(Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), 0.10));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());

Thread.sleep(1_500);
cacheSizeAfterCleanup = getRequestCacheStats(client, "index").getMemorySizeInBytes();
assertTrue(cacheSizeBeforeCleanup > cacheSizeAfterCleanup);
}
// when staleness threshold is high, it should NOT clean-up
public void testStaleKeysCleanup_ThresholdUpdates() throws Exception {
Instant start = Instant.now();
long thresholdInMillis = 1_500;
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.SETTING_INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING, 0.90)
.put(IndicesRequestCache.SETTING_INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING, TimeValue.timeValueMillis(thresholdInMillis))
);
String index = "index";
Client client = client(node);
setupIndex(client, index);

// create first cache entry
createCacheEntry(client, index, "hello");
assertCacheState(client, index, 0, 1);
long expectedFirstCachedItemEntrySize = getRequestCacheStats(client, index).getMemorySizeInBytes();
assertTrue(expectedFirstCachedItemEntrySize > 0);

// create second cache entry
createCacheEntry(client, index, "there");
assertCacheState(client, "index", 0, 2);
assertEquals(expectedFirstCachedItemEntrySize * 2, getRequestCacheStats(client, "index").getMemorySizeInBytes());

// force refresh so that it creates 2 stale keys in the cache for the cache cleaner to pick up.
flushAndRefresh("index");
client().prepareIndex("index").setId("1").setSource("k", "good bye");
ensureSearchable("index");

// create another entry
createCacheEntry(client, index, "hello1");
assertCacheState(client, "index", 0, 3);
long cacheSizeBeforeCleanup = getRequestCacheStats(client, "index").getMemorySizeInBytes();
assertTrue(cacheSizeBeforeCleanup > expectedFirstCachedItemEntrySize * 2);
assertEquals(cacheSizeBeforeCleanup, expectedFirstCachedItemEntrySize * 3, 2);

Instant end = Instant.now();
long elapsedTimeMillis = Duration.between(start, end).toMillis();
// if this test is flaky, increase the sleep time.
long sleepTime = (thresholdInMillis - elapsedTimeMillis) + 2_000;
Thread.sleep(sleepTime);

// cache cleaner should have skipped the cleanup
long cacheSizeAfterCleanup = getRequestCacheStats(client, "index").getMemorySizeInBytes();
assertEquals(cacheSizeBeforeCleanup, cacheSizeAfterCleanup);

// Set indices.requests.cache.cleanup.staleness_threshold to "10%"
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.persistentSettings(Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), 0.10));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());

Thread.sleep(1_500);
cacheSizeAfterCleanup = getRequestCacheStats(client, "index").getMemorySizeInBytes();
assertTrue(cacheSizeBeforeCleanup > cacheSizeAfterCleanup);
}

private void setupIndex(Client client, String index) throws Exception {
assertAcked(
Expand Down

0 comments on commit 09ecf8a

Please sign in to comment.