Search in sources :

Example 6 with IndicesQueryCache

use of org.elasticsearch.indices.IndicesQueryCache in project elasticsearch by elastic.

the class IndicesQueryCacheTests method testTwoShards.

public void testTwoShards() throws IOException {
    Directory dir1 = newDirectory();
    IndexWriter w1 = new IndexWriter(dir1, newIndexWriterConfig());
    w1.addDocument(new Document());
    DirectoryReader r1 = DirectoryReader.open(w1);
    w1.close();
    ShardId shard1 = new ShardId("index", "_na_", 0);
    r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
    IndexSearcher s1 = new IndexSearcher(r1);
    s1.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);
    Directory dir2 = newDirectory();
    IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
    w2.addDocument(new Document());
    DirectoryReader r2 = DirectoryReader.open(w2);
    w2.close();
    ShardId shard2 = new ShardId("index", "_na_", 1);
    r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
    IndexSearcher s2 = new IndexSearcher(r2);
    s2.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);
    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10).put(IndicesQueryCache.INDICES_QUERIES_CACHE_ALL_SEGMENTS_SETTING.getKey(), true).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s1.setQueryCache(cache);
    s2.setQueryCache(cache);
    assertEquals(1, s1.count(new DummyQuery(0)));
    QueryCacheStats stats1 = cache.getStats(shard1);
    assertEquals(1L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());
    QueryCacheStats stats2 = cache.getStats(shard2);
    assertEquals(0L, stats2.getCacheSize());
    assertEquals(0L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(0L, stats2.getMissCount());
    assertEquals(1, s2.count(new DummyQuery(0)));
    stats1 = cache.getStats(shard1);
    assertEquals(1L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());
    stats2 = cache.getStats(shard2);
    assertEquals(1L, stats2.getCacheSize());
    assertEquals(1L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(1L, stats2.getMissCount());
    for (int i = 0; i < 20; ++i) {
        assertEquals(1, s2.count(new DummyQuery(i)));
    }
    stats1 = cache.getStats(shard1);
    // evicted
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());
    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());
    IOUtils.close(r1, dir1);
    // no changes
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());
    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());
    cache.onClose(shard1);
    // forgot everything about shard1
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(0L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(0L, stats1.getMissCount());
    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());
    IOUtils.close(r2, dir2);
    cache.onClose(shard2);
    // forgot everything about shard2
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(0L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(0L, stats1.getMissCount());
    stats2 = cache.getStats(shard2);
    assertEquals(0L, stats2.getCacheSize());
    assertEquals(0L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(0L, stats2.getMissCount());
    // this triggers some assertions
    cache.close();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndicesQueryCache(org.elasticsearch.indices.IndicesQueryCache) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) QueryCacheStats(org.elasticsearch.index.cache.query.QueryCacheStats) Document(org.apache.lucene.document.Document) Settings(org.elasticsearch.common.settings.Settings) Directory(org.apache.lucene.store.Directory)

Aggregations

IndicesQueryCache (org.elasticsearch.indices.IndicesQueryCache)6 Document (org.apache.lucene.document.Document)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Directory (org.apache.lucene.store.Directory)3 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)3 Settings (org.elasticsearch.common.settings.Settings)3 QueryCacheStats (org.elasticsearch.index.cache.query.QueryCacheStats)3 ShardId (org.elasticsearch.index.shard.ShardId)3 CommonStats (org.elasticsearch.action.admin.indices.stats.CommonStats)1 CommonStatsFlags (org.elasticsearch.action.admin.indices.stats.CommonStatsFlags)1 ShardStats (org.elasticsearch.action.admin.indices.stats.ShardStats)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 BigArrays (org.elasticsearch.common.util.BigArrays)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Environment (org.elasticsearch.env.Environment)1 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)1 DisabledQueryCache (org.elasticsearch.index.cache.query.DisabledQueryCache)1