Skip to content

Commit 772a952

Browse files
Add IRC specific cache cleaner and remove from IndicesService
Signed-off-by: Kiran Prakash <awskiran@amazon.com>
1 parent f2e58a8 commit 772a952

File tree

2 files changed

+72
-25
lines changed

2 files changed

+72
-25
lines changed

server/src/main/java/org/opensearch/indices/IndicesRequestCache.java

+68-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import org.opensearch.common.cache.RemovalNotification;
4848
import org.opensearch.common.cache.service.CacheService;
4949
import org.opensearch.common.cache.store.config.CacheConfig;
50+
import org.opensearch.common.lease.Releasable;
51+
import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
5052
import org.opensearch.common.lucene.index.OpenSearchDirectoryReader;
5153
import org.opensearch.common.settings.Setting;
5254
import org.opensearch.common.settings.Setting.Property;
@@ -61,6 +63,7 @@
6163
import org.opensearch.core.common.unit.ByteSizeValue;
6264
import org.opensearch.core.index.shard.ShardId;
6365
import org.opensearch.index.shard.IndexShard;
66+
import org.opensearch.threadpool.ThreadPool;
6467

6568
import java.io.Closeable;
6669
import java.io.IOException;
@@ -95,7 +98,10 @@
9598
*
9699
* @opensearch.internal
97100
*/
98-
public final class IndicesRequestCache implements RemovalListener<IndicesRequestCache.Key, BytesReference>, Closeable {
101+
public final class IndicesRequestCache extends AbstractLifecycleComponent
102+
implements
103+
RemovalListener<IndicesRequestCache.Key, BytesReference>,
104+
Closeable {
99105

100106
private static final Logger logger = LogManager.getLogger(IndicesRequestCache.class);
101107

@@ -138,13 +144,26 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
138144
private final TimeValue expire;
139145
private final ICache<Key, BytesReference> cache;
140146
private final Function<ShardId, Optional<CacheEntity>> cacheEntityLookup;
141-
142-
IndicesRequestCache(Settings settings, Function<ShardId, Optional<CacheEntity>> cacheEntityFunction, CacheService cacheService) {
147+
// pkg-private for testing
148+
final IndicesRequestCacheCleanupManager cacheCleanupManager;
149+
private final IndicesRequestCacheCleaner cacheCleaner;
150+
private final TimeValue cleanInterval;
151+
private final ThreadPool threadpool;
152+
153+
IndicesRequestCache(
154+
Settings settings,
155+
Function<ShardId, Optional<CacheEntity>> cacheEntityFunction,
156+
CacheService cacheService,
157+
ThreadPool threadPool
158+
) {
143159
this.size = INDICES_CACHE_QUERY_SIZE.get(settings);
144160
this.expire = INDICES_CACHE_QUERY_EXPIRE.exists(settings) ? INDICES_CACHE_QUERY_EXPIRE.get(settings) : null;
161+
this.cleanInterval = INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING.get(settings);
145162
long sizeInBytes = size.getBytes();
146163
ToLongBiFunction<Key, BytesReference> weigher = (k, v) -> k.ramBytesUsed() + v.ramBytesUsed();
147164
this.cacheCleanupManager = new IndicesRequestCacheCleanupManager(getStalenessThreshold(settings));
165+
this.threadpool = threadPool;
166+
this.cacheCleaner = new IndicesRequestCacheCleaner(this, this.threadpool, this.cleanInterval);
148167
this.cacheEntityLookup = cacheEntityFunction;
149168
this.cache = cacheService.createCache(
150169
new CacheConfig.Builder<Key, BytesReference>().setSettings(settings)
@@ -159,6 +178,19 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
159178
);
160179
}
161180

181+
@Override
182+
protected void doStart() {
183+
threadpool.schedule(this.cacheCleaner, this.cleanInterval, ThreadPool.Names.SAME);
184+
}
185+
186+
@Override
187+
protected void doStop() {
188+
cacheCleaner.close();
189+
}
190+
191+
@Override
192+
protected void doClose() throws IOException {}
193+
162194
@Override
163195
public void close() {
164196
cache.invalidateAll();
@@ -230,6 +262,39 @@ void invalidate(IndicesService.IndexShardCacheEntity cacheEntity, DirectoryReade
230262
cache.invalidate(new Key(((IndexShard) cacheEntity.getCacheIdentity()).shardId(), cacheKey, readerCacheKeyId));
231263
}
232264

265+
private final class IndicesRequestCacheCleaner implements Runnable, Releasable {
266+
267+
private final IndicesRequestCache indicesRequestCache;
268+
private final ThreadPool threadPool;
269+
private final TimeValue interval;
270+
271+
IndicesRequestCacheCleaner(IndicesRequestCache indicesRequestCache, ThreadPool threadPool, TimeValue interval) {
272+
this.indicesRequestCache = indicesRequestCache;
273+
this.threadPool = threadPool;
274+
this.interval = interval;
275+
}
276+
277+
private final AtomicBoolean closed = new AtomicBoolean(false);
278+
279+
@Override
280+
public void run() {
281+
try {
282+
this.indicesRequestCache.cacheCleanupManager.cleanCache();
283+
} catch (Exception e) {
284+
logger.warn("Exception during periodic indices request cache cleanup:", e);
285+
}
286+
// Reschedule itself to run again if not closed
287+
if (closed.get() == false) {
288+
threadPool.scheduleUnlessShuttingDown(interval, ThreadPool.Names.SAME, this);
289+
}
290+
}
291+
292+
@Override
293+
public void close() {
294+
closed.compareAndSet(false, true);
295+
}
296+
}
297+
233298
/**
234299
* Loader for the request cache
235300
*

server/src/main/java/org/opensearch/indices/IndicesService.java

+4-22
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public IndicesService(
414414
return Optional.empty();
415415
}
416416
return Optional.of(new IndexShardCacheEntity(indexService.getShard(shardId.id())));
417-
}), cacheService);
417+
}), cacheService, threadPool);
418418
this.indicesQueryCache = new IndicesQueryCache(settings);
419419
this.mapperRegistry = mapperRegistry;
420420
this.namedWriteableRegistry = namedWriteableRegistry;
@@ -443,7 +443,7 @@ public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, lon
443443
}
444444
});
445445
this.cleanInterval = INDICES_CACHE_CLEAN_INTERVAL_SETTING.get(settings);
446-
this.cacheCleaner = new CacheCleaner(indicesFieldDataCache, indicesRequestCache, logger, threadPool, this.cleanInterval);
446+
this.cacheCleaner = new CacheCleaner(indicesFieldDataCache, logger, threadPool, this.cleanInterval);
447447
this.metaStateService = metaStateService;
448448
this.engineFactoryProviders = engineFactoryProviders;
449449

@@ -1587,17 +1587,9 @@ private static final class CacheCleaner implements Runnable, Releasable {
15871587
private final ThreadPool threadPool;
15881588
private final TimeValue interval;
15891589
private final AtomicBoolean closed = new AtomicBoolean(false);
1590-
private final IndicesRequestCache requestCache;
1591-
1592-
CacheCleaner(
1593-
IndicesFieldDataCache cache,
1594-
IndicesRequestCache requestCache,
1595-
Logger logger,
1596-
ThreadPool threadPool,
1597-
TimeValue interval
1598-
) {
1590+
1591+
CacheCleaner(IndicesFieldDataCache cache, Logger logger, ThreadPool threadPool, TimeValue interval) {
15991592
this.cache = cache;
1600-
this.requestCache = requestCache;
16011593
this.logger = logger;
16021594
this.threadPool = threadPool;
16031595
this.interval = interval;
@@ -1620,16 +1612,6 @@ public void run() {
16201612
TimeValue.nsecToMSec(System.nanoTime() - startTimeNS)
16211613
);
16221614
}
1623-
1624-
try {
1625-
this.requestCache.cleanCache();
1626-
} catch (Exception e) {
1627-
logger.warn("Exception during periodic request cache cleanup:", e);
1628-
}
1629-
// Reschedule itself to run again if not closed
1630-
if (closed.get() == false) {
1631-
threadPool.scheduleUnlessShuttingDown(interval, ThreadPool.Names.SAME, this);
1632-
}
16331615
}
16341616

16351617
@Override

0 commit comments

Comments
 (0)