/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.index; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.FilterDirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReader; import org.apache.lucene.search.similarities.BM25Similarity; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.store.MMapDirectory; import org.apache.lucene.util.Constants; import org.opensearch.Version; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.routing.ShardRouting; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.CheckedFunction; import org.opensearch.common.SetOnce; import org.opensearch.common.TriFunction; import org.opensearch.core.common.io.stream.NamedWriteableRegistry; import org.opensearch.common.logging.DeprecationLogger; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Setting.Property; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.BigArrays; import org.opensearch.core.index.Index; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.common.util.io.IOUtils; import org.opensearch.env.NodeEnvironment; import org.opensearch.index.analysis.AnalysisRegistry; import org.opensearch.index.analysis.IndexAnalyzers; import org.opensearch.index.cache.query.DisabledQueryCache; import org.opensearch.index.cache.query.IndexQueryCache; import org.opensearch.index.cache.query.QueryCache; import org.opensearch.index.engine.Engine; import org.opensearch.index.engine.EngineConfigFactory; import org.opensearch.index.engine.EngineFactory; import org.opensearch.index.mapper.MapperService; import org.opensearch.index.shard.IndexEventListener; import org.opensearch.index.shard.IndexingOperationListener; import org.opensearch.index.shard.SearchOperationListener; import org.opensearch.index.similarity.SimilarityService; import org.opensearch.index.store.FsDirectoryFactory; import org.opensearch.index.store.remote.directory.RemoteSnapshotDirectoryFactory; import org.opensearch.index.store.remote.filecache.FileCache; import org.opensearch.index.translog.TranslogFactory; import org.opensearch.indices.IndicesQueryCache; import org.opensearch.core.indices.breaker.CircuitBreakerService; import org.opensearch.indices.fielddata.cache.IndicesFieldDataCache; import org.opensearch.indices.mapper.MapperRegistry; import org.opensearch.indices.recovery.RecoveryState; import org.opensearch.plugins.IndexStorePlugin; import org.opensearch.repositories.RepositoriesService; import org.opensearch.script.ScriptService; import org.opensearch.search.aggregations.support.ValuesSourceRegistry; import org.opensearch.threadpool.ThreadPool; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiFunction; import java.util.function.BooleanSupplier; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; /** * IndexModule represents the central extension point for index level custom implementations like: *
* Note: an index might be created on a node multiple times. For instance if the last shard from an index is * relocated to another node the internal representation will be destroyed which includes the registered listeners. * Once the node holds at least one shard of an index all modules are reloaded and listeners are registered again. * Listeners can't be unregistered they will stay alive for the entire time the index is allocated on a node. *
*/ public void addIndexEventListener(IndexEventListener listener) { ensureNotFrozen(); if (listener == null) { throw new IllegalArgumentException("listener must not be null"); } if (indexEventListeners.contains(listener)) { throw new IllegalArgumentException("listener already added"); } this.indexEventListeners.add(listener); } /** * Adds an {@link SearchOperationListener} for this index. All listeners added here * are maintained for the entire index lifecycle on this node. Once an index is closed or deleted these * listeners go out of scope. ** Note: an index might be created on a node multiple times. For instance if the last shard from an index is * relocated to another node the internal representation will be destroyed which includes the registered listeners. * Once the node holds at least one shard of an index all modules are reloaded and listeners are registered again. * Listeners can't be unregistered they will stay alive for the entire time the index is allocated on a node. *
*/ public void addSearchOperationListener(SearchOperationListener listener) { ensureNotFrozen(); if (listener == null) { throw new IllegalArgumentException("listener must not be null"); } if (searchOperationListeners.contains(listener)) { throw new IllegalArgumentException("listener already added"); } this.searchOperationListeners.add(listener); } /** * Adds an {@link IndexingOperationListener} for this index. All listeners added here * are maintained for the entire index lifecycle on this node. Once an index is closed or deleted these * listeners go out of scope. ** Note: an index might be created on a node multiple times. For instance if the last shard from an index is * relocated to another node the internal representation will be destroyed which includes the registered listeners. * Once the node holds at least one shard of an index all modules are reloaded and listeners are registered again. * Listeners can't be unregistered they will stay alive for the entire time the index is allocated on a node. *
*/ public void addIndexOperationListener(IndexingOperationListener listener) { ensureNotFrozen(); if (listener == null) { throw new IllegalArgumentException("listener must not be null"); } if (indexOperationListeners.contains(listener)) { throw new IllegalArgumentException("listener already added"); } this.indexOperationListeners.add(listener); } /** * Registers the given {@link Similarity} with the given name. * The function takes as parameters:* The {@link CheckedFunction} is invoked each time a {@link Engine.Searcher} is requested to do an operation, * for example search, and must return a new directory reader wrapping the provided directory reader or if no * wrapping was performed the provided directory reader. * The wrapped reader can filter out document just like delete documents etc. but must not change any term or * document content. * NOTE: The index reader wrapper ({@link CheckedFunction}) has a per-request lifecycle, * must delegate {@link IndexReader#getReaderCacheHelper()}, {@link LeafReader#getCoreCacheHelper()} * and must be an instance of {@link FilterDirectoryReader} that eventually exposes the original reader * via {@link FilterDirectoryReader#getDelegate()}. * The returned reader is closed once it goes out of scope. *
*/ public void setReaderWrapper( Function