Search in sources :

Example 1 with RemovalNotification

use of org.opensearch.common.cache.RemovalNotification in project OpenSearch by opensearch-project.

the class IndicesServiceCloseTests method testCloseWhileOngoingRequestUsesRequestCache.

public void testCloseWhileOngoingRequestUsesRequestCache() throws Exception {
    Node node = startNode();
    IndicesService indicesService = node.injector().getInstance(IndicesService.class);
    assertEquals(1, indicesService.indicesRefCount.refCount());
    assertAcked(node.client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)));
    node.client().prepareIndex("test").setId("1").setSource(Collections.singletonMap("foo", 3L)).get();
    OpenSearchAssertions.assertAllSuccessful(node.client().admin().indices().prepareRefresh("test").get());
    assertEquals(2, indicesService.indicesRefCount.refCount());
    IndicesRequestCache cache = indicesService.indicesRequestCache;
    IndexService indexService = indicesService.iterator().next();
    IndexShard shard = indexService.getShard(0);
    Engine.Searcher searcher = shard.acquireSearcher("test");
    assertEquals(1, searcher.getIndexReader().maxDoc());
    node.close();
    assertEquals(1, indicesService.indicesRefCount.refCount());
    assertEquals(0L, cache.count());
    IndicesRequestCache.CacheEntity cacheEntity = new IndicesRequestCache.CacheEntity() {

        @Override
        public long ramBytesUsed() {
            return 42;
        }

        @Override
        public void onCached(Key key, BytesReference value) {
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public Object getCacheIdentity() {
            return this;
        }

        @Override
        public void onHit() {
        }

        @Override
        public void onMiss() {
        }

        @Override
        public void onRemoval(RemovalNotification<Key, BytesReference> notification) {
        }
    };
    cache.getOrCompute(cacheEntity, () -> new BytesArray("bar"), searcher.getDirectoryReader(), new BytesArray("foo"));
    assertEquals(1L, cache.count());
    searcher.close();
    assertEquals(0, indicesService.indicesRefCount.refCount());
    assertEquals(0L, cache.count());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) BytesArray(org.opensearch.common.bytes.BytesArray) IndexService(org.opensearch.index.IndexService) Node(org.opensearch.node.Node) MockNode(org.opensearch.node.MockNode) NodeRoles.dataNode(org.opensearch.test.NodeRoles.dataNode) IndexShard(org.opensearch.index.shard.IndexShard) RemovalNotification(org.opensearch.common.cache.RemovalNotification) Engine(org.opensearch.index.engine.Engine) Key(org.opensearch.indices.IndicesRequestCache.Key)

Aggregations

BytesArray (org.opensearch.common.bytes.BytesArray)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 RemovalNotification (org.opensearch.common.cache.RemovalNotification)1 IndexService (org.opensearch.index.IndexService)1 Engine (org.opensearch.index.engine.Engine)1 IndexShard (org.opensearch.index.shard.IndexShard)1 Key (org.opensearch.indices.IndicesRequestCache.Key)1 MockNode (org.opensearch.node.MockNode)1 Node (org.opensearch.node.Node)1 NodeRoles.dataNode (org.opensearch.test.NodeRoles.dataNode)1