Search in sources :

Example 1 with DisabledQueryCache

use of org.opensearch.index.cache.query.DisabledQueryCache in project OpenSearch by opensearch-project.

the class IndexModuleTests method testDisableQueryCacheHasPrecedenceOverForceQueryCache.

public void testDisableQueryCacheHasPrecedenceOverForceQueryCache() throws IOException {
    Settings settings = Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_ENABLED_SETTING.getKey(), false).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
    final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings);
    IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry);
    module.forceQueryCacheProvider((a, b) -> new CustomQueryCache(null));
    IndexService indexService = newIndexService(module);
    assertTrue(indexService.cache().query() instanceof DisabledQueryCache);
    indexService.close("simon says", false);
}
Also used : Settings(org.opensearch.common.settings.Settings) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache)

Example 2 with DisabledQueryCache

use of org.opensearch.index.cache.query.DisabledQueryCache in project OpenSearch by opensearch-project.

the class IndexModule method newIndexService.

public IndexService newIndexService(IndexService.IndexCreationContext indexCreationContext, NodeEnvironment environment, NamedXContentRegistry xContentRegistry, IndexService.ShardStoreDeleter shardStoreDeleter, CircuitBreakerService circuitBreakerService, BigArrays bigArrays, ThreadPool threadPool, ScriptService scriptService, ClusterService clusterService, Client client, IndicesQueryCache indicesQueryCache, MapperRegistry mapperRegistry, IndicesFieldDataCache indicesFieldDataCache, NamedWriteableRegistry namedWriteableRegistry, BooleanSupplier idFieldDataEnabled, ValuesSourceRegistry valuesSourceRegistry) throws IOException {
    final IndexEventListener eventListener = freeze();
    Function<IndexService, CheckedFunction<DirectoryReader, DirectoryReader, IOException>> readerWrapperFactory = indexReaderWrapper.get() == null ? (shard) -> null : indexReaderWrapper.get();
    eventListener.beforeIndexCreated(indexSettings.getIndex(), indexSettings.getSettings());
    final IndexStorePlugin.DirectoryFactory directoryFactory = getDirectoryFactory(indexSettings, directoryFactories);
    final IndexStorePlugin.RecoveryStateFactory recoveryStateFactory = getRecoveryStateFactory(indexSettings, recoveryStateFactories);
    QueryCache queryCache = null;
    IndexAnalyzers indexAnalyzers = null;
    boolean success = false;
    try {
        if (indexSettings.getValue(INDEX_QUERY_CACHE_ENABLED_SETTING)) {
            BiFunction<IndexSettings, IndicesQueryCache, QueryCache> queryCacheProvider = forceQueryCacheProvider.get();
            if (queryCacheProvider == null) {
                queryCache = new IndexQueryCache(indexSettings, indicesQueryCache);
            } else {
                queryCache = queryCacheProvider.apply(indexSettings, indicesQueryCache);
            }
        } else {
            queryCache = new DisabledQueryCache(indexSettings);
        }
        if (IndexService.needsMapperService(indexSettings, indexCreationContext)) {
            indexAnalyzers = analysisRegistry.build(indexSettings);
        }
        final IndexService indexService = new IndexService(indexSettings, indexCreationContext, environment, xContentRegistry, new SimilarityService(indexSettings, scriptService, similarities), shardStoreDeleter, indexAnalyzers, engineFactory, engineConfigFactory, circuitBreakerService, bigArrays, threadPool, scriptService, clusterService, client, queryCache, directoryFactory, eventListener, readerWrapperFactory, mapperRegistry, indicesFieldDataCache, searchOperationListeners, indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled, allowExpensiveQueries, expressionResolver, valuesSourceRegistry, recoveryStateFactory);
        success = true;
        return indexService;
    } finally {
        if (success == false) {
            IOUtils.closeWhileHandlingException(queryCache, indexAnalyzers);
        }
    }
}
Also used : IndicesQueryCache(org.opensearch.indices.IndicesQueryCache) QueryCache(org.opensearch.index.cache.query.QueryCache) IndicesQueryCache(org.opensearch.indices.IndicesQueryCache) IndexQueryCache(org.opensearch.index.cache.query.IndexQueryCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) IndexQueryCache(org.opensearch.index.cache.query.IndexQueryCache) IndexEventListener(org.opensearch.index.shard.IndexEventListener) SimilarityService(org.opensearch.index.similarity.SimilarityService) IndexStorePlugin(org.opensearch.plugins.IndexStorePlugin) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) CheckedFunction(org.opensearch.common.CheckedFunction)

Example 3 with DisabledQueryCache

use of org.opensearch.index.cache.query.DisabledQueryCache in project OpenSearch by opensearch-project.

the class IndexShardTestCase method newShard.

/**
 * creates a new initializing shard.
 * @param routing                       shard routing to use
 * @param shardPath                     path to use for shard data
 * @param indexMetadata                 indexMetadata for the shard, including any mapping
 * @param storeProvider                 an optional custom store provider to use. If null a default file based store will be created
 * @param indexReaderWrapper            an optional wrapper to be used during search
 * @param globalCheckpointSyncer        callback for syncing global checkpoints
 * @param indexEventListener            index event listener
 * @param listeners                     an optional set of listeners to add to the shard
 */
protected IndexShard newShard(ShardRouting routing, ShardPath shardPath, IndexMetadata indexMetadata, @Nullable CheckedFunction<IndexSettings, Store, IOException> storeProvider, @Nullable CheckedFunction<DirectoryReader, DirectoryReader, IOException> indexReaderWrapper, @Nullable EngineFactory engineFactory, @Nullable EngineConfigFactory engineConfigFactory, Runnable globalCheckpointSyncer, RetentionLeaseSyncer retentionLeaseSyncer, IndexEventListener indexEventListener, IndexingOperationListener... listeners) throws IOException {
    final Settings nodeSettings = Settings.builder().put("node.name", routing.currentNodeId()).build();
    final IndexSettings indexSettings = new IndexSettings(indexMetadata, nodeSettings);
    final IndexShard indexShard;
    if (storeProvider == null) {
        storeProvider = is -> createStore(is, shardPath);
    }
    final Store store = storeProvider.apply(indexSettings);
    boolean success = false;
    try {
        IndexCache indexCache = new IndexCache(indexSettings, new DisabledQueryCache(indexSettings), null);
        MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), indexSettings.getSettings(), "index");
        mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_RECOVERY);
        SimilarityService similarityService = new SimilarityService(indexSettings, null, Collections.emptyMap());
        final Engine.Warmer warmer = createTestWarmer(indexSettings);
        ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
        CircuitBreakerService breakerService = new HierarchyCircuitBreakerService(nodeSettings, Collections.emptyList(), clusterSettings);
        indexShard = new IndexShard(routing, indexSettings, shardPath, store, () -> null, indexCache, mapperService, similarityService, engineFactory, engineConfigFactory, indexEventListener, indexReaderWrapper, threadPool, BigArrays.NON_RECYCLING_INSTANCE, warmer, Collections.emptyList(), Arrays.asList(listeners), globalCheckpointSyncer, retentionLeaseSyncer, breakerService);
        indexShard.addShardFailureCallback(DEFAULT_SHARD_FAILURE_HANDLER);
        success = true;
    } finally {
        if (success == false) {
            IOUtils.close(store);
        }
    }
    return indexShard;
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) IndexSettings(org.opensearch.index.IndexSettings) Store(org.opensearch.index.store.Store) IndexCache(org.opensearch.index.cache.IndexCache) SimilarityService(org.opensearch.index.similarity.SimilarityService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) MapperService(org.opensearch.index.mapper.MapperService) Engine(org.opensearch.index.engine.Engine)

Example 4 with DisabledQueryCache

use of org.opensearch.index.cache.query.DisabledQueryCache in project OpenSearch by opensearch-project.

the class AggregatorTestCase method createSearchContext.

protected SearchContext createSearchContext(IndexSearcher indexSearcher, IndexSettings indexSettings, Query query, MultiBucketConsumer bucketConsumer, CircuitBreakerService circuitBreakerService, MappedFieldType... fieldTypes) throws IOException {
    QueryCache queryCache = new DisabledQueryCache(indexSettings);
    QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {

        @Override
        public void onUse(Query query) {
        }

        @Override
        public boolean shouldCache(Query query) {
            // never cache a query
            return false;
        }
    };
    ContextIndexSearcher contextIndexSearcher = new ContextIndexSearcher(indexSearcher.getIndexReader(), indexSearcher.getSimilarity(), queryCache, queryCachingPolicy, false);
    SearchContext searchContext = mock(SearchContext.class);
    when(searchContext.numberOfShards()).thenReturn(1);
    when(searchContext.searcher()).thenReturn(contextIndexSearcher);
    when(searchContext.fetchPhase()).thenReturn(new FetchPhase(Arrays.asList(new FetchSourcePhase(), new FetchDocValuesPhase())));
    when(searchContext.bitsetFilterCache()).thenReturn(new BitsetFilterCache(indexSettings, mock(Listener.class)));
    IndexShard indexShard = mock(IndexShard.class);
    when(indexShard.shardId()).thenReturn(new ShardId("test", "test", 0));
    when(searchContext.indexShard()).thenReturn(indexShard);
    when(searchContext.aggregations()).thenReturn(new SearchContextAggregations(AggregatorFactories.EMPTY, bucketConsumer));
    when(searchContext.query()).thenReturn(query);
    /*
         * Always use the circuit breaking big arrays instance so that the CircuitBreakerService
         * we're passed gets a chance to break.
         */
    BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), circuitBreakerService).withCircuitBreaking();
    when(searchContext.bigArrays()).thenReturn(bigArrays);
    // TODO: now just needed for top_hits, this will need to be revised for other agg unit tests:
    MapperService mapperService = mapperServiceMock();
    when(mapperService.getIndexSettings()).thenReturn(indexSettings);
    when(mapperService.hasNested()).thenReturn(false);
    when(searchContext.mapperService()).thenReturn(mapperService);
    IndexFieldDataService ifds = new IndexFieldDataService(indexSettings, new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() {
    }), circuitBreakerService, mapperService);
    QueryShardContext queryShardContext = queryShardContextMock(contextIndexSearcher, mapperService, indexSettings, circuitBreakerService, bigArrays);
    when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
    when(queryShardContext.getObjectMapper(anyString())).thenAnswer(invocation -> {
        String fieldName = (String) invocation.getArguments()[0];
        if (fieldName.startsWith(NESTEDFIELD_PREFIX)) {
            BuilderContext context = new BuilderContext(indexSettings.getSettings(), new ContentPath());
            return new ObjectMapper.Builder<>(fieldName).nested(Nested.newNested()).build(context);
        }
        return null;
    });
    Map<String, MappedFieldType> fieldNameToType = new HashMap<>();
    fieldNameToType.putAll(Arrays.stream(fieldTypes).filter(Objects::nonNull).collect(Collectors.toMap(MappedFieldType::name, Function.identity())));
    fieldNameToType.putAll(getFieldAliases(fieldTypes));
    registerFieldTypes(searchContext, mapperService, fieldNameToType);
    doAnswer(invocation -> {
        /* Store the release-ables so we can release them at the end of the test case. This is important because aggregations don't
             * close their sub-aggregations. This is fairly similar to what the production code does. */
        releasables.add((Releasable) invocation.getArguments()[0]);
        return null;
    }).when(searchContext).addReleasable(any());
    return searchContext;
}
Also used : QueryCachingPolicy(org.apache.lucene.search.QueryCachingPolicy) QueryCache(org.apache.lucene.search.QueryCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) Listener(org.opensearch.index.cache.bitset.BitsetFilterCache.Listener) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Builder(org.opensearch.search.aggregations.AggregatorFactories.Builder) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) ContextIndexSearcher(org.opensearch.search.internal.ContextIndexSearcher) SearchContext(org.opensearch.search.internal.SearchContext) MockBigArrays(org.opensearch.common.util.MockBigArrays) Mockito.anyString(org.mockito.Mockito.anyString) ShardId(org.opensearch.index.shard.ShardId) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexShard(org.opensearch.index.shard.IndexShard) FetchSourcePhase(org.opensearch.search.fetch.subphase.FetchSourcePhase) FetchDocValuesPhase(org.opensearch.search.fetch.subphase.FetchDocValuesPhase) ContentPath(org.opensearch.index.mapper.ContentPath) IndicesFieldDataCache(org.opensearch.indices.fielddata.cache.IndicesFieldDataCache) MockBigArrays(org.opensearch.common.util.MockBigArrays) BigArrays(org.opensearch.common.util.BigArrays) IndexFieldDataService(org.opensearch.index.fielddata.IndexFieldDataService) FetchPhase(org.opensearch.search.fetch.FetchPhase) Objects(java.util.Objects) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) MapperService(org.opensearch.index.mapper.MapperService)

Aggregations

DisabledQueryCache (org.opensearch.index.cache.query.DisabledQueryCache)4 Settings (org.opensearch.common.settings.Settings)2 MapperService (org.opensearch.index.mapper.MapperService)2 SimilarityService (org.opensearch.index.similarity.SimilarityService)2 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 QueryCache (org.apache.lucene.search.QueryCache)1 QueryCachingPolicy (org.apache.lucene.search.QueryCachingPolicy)1 Mockito.anyString (org.mockito.Mockito.anyString)1 CheckedFunction (org.opensearch.common.CheckedFunction)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 BigArrays (org.opensearch.common.util.BigArrays)1 MockBigArrays (org.opensearch.common.util.MockBigArrays)1 MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 IndexSettings (org.opensearch.index.IndexSettings)1 IndexAnalyzers (org.opensearch.index.analysis.IndexAnalyzers)1 IndexCache (org.opensearch.index.cache.IndexCache)1