Package org.elasticsearch.indices
Class IndicesService
java.lang.Object
org.elasticsearch.common.component.AbstractLifecycleComponent
org.elasticsearch.indices.IndicesService
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<IndexService>
,LifecycleComponent
,org.elasticsearch.core.Releasable
,IndexService.ShardStoreDeleter
,IndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
public class IndicesService
extends AbstractLifecycleComponent
implements IndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>, IndexService.ShardStoreDeleter
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
result type returned bysignaling different reasons why a shard can / cannot be deleted
Nested classes/interfaces inherited from interface org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices
IndicesClusterStateService.AllocatedIndices.IndexRemovalReason
-
Field Summary
Fields inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
lifecycle
-
Constructor Summary
ConstructorDescriptionIndicesService(Settings settings, PluginsService pluginsService, NodeEnvironment nodeEnv, org.elasticsearch.common.xcontent.NamedXContentRegistry xContentRegistry, AnalysisRegistry analysisRegistry, IndexNameExpressionResolver indexNameExpressionResolver, MapperRegistry mapperRegistry, NamedWriteableRegistry namedWriteableRegistry, ThreadPool threadPool, IndexScopedSettings indexScopedSettings, CircuitBreakerService circuitBreakerService, BigArrays bigArrays, ScriptService scriptService, ClusterService clusterService, Client client, MetaStateService metaStateService, Collection<Function<IndexSettings,Optional<EngineFactory>>> engineFactoryProviders, Map<String,IndexStorePlugin.DirectoryFactory> directoryFactories, ValuesSourceRegistry valuesSourceRegistry, Map<String,IndexStorePlugin.RecoveryStateFactory> recoveryStateFactories, List<IndexStorePlugin.IndexFoldersDeletionListener> indexFoldersDeletionListeners, Map<String,IndexStorePlugin.SnapshotCommitSupplier> snapshotCommitSuppliers, CheckedBiConsumer<ShardSearchRequest,StreamOutput,IOException> requestCacheKeyDifferentiator)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addPendingDelete(Index index, IndexSettings settings)
Adds a pending delete for the given index.void
addPendingDelete(ShardId shardId, IndexSettings settings)
Adds a pending delete for the given index shard.boolean
boolean
awaitClose(long timeout, TimeUnit timeUnit)
Wait for thisIndicesService
to be effectively closed.buildAliasFilter(ClusterState state, String index, Set<String> resolvedExpressions)
boolean
canCache(ShardSearchRequest request, SearchContext context)
Can the shard request be cached at all?boolean
canDeleteIndexContents(Index index, IndexSettings indexSettings)
This method returns true if the current node is allowed to delete the given index.canDeleteShardContent(ShardId shardId, IndexSettings indexSettings)
ReturnsShardDeletionCheckResult
signaling whether the shards content for the given shard can be deleted.void
clearIndexShardCache(ShardId shardId, boolean queryCache, boolean fieldDataCache, boolean requestCache, String... fields)
Clears the caches for the given shard id if the shard is still allocated on this nodecreateIndex(IndexMetadata indexMetadata, List<IndexEventListener> builtInListeners, boolean writeDanglingIndices)
Creates a newIndexService
for the given metadata.createIndexMapperService(IndexMetadata indexMetadata)
creates a new mapper service for the given index, in order to do administrative work like mapping updates.createShard(ShardRouting shardRouting, PeerRecoveryTargetService recoveryTargetService, PeerRecoveryTargetService.RecoveryListener recoveryListener, RepositoriesService repositoriesService, Consumer<IndexShard.ShardFailure> onShardFailure, Consumer<ShardId> globalCheckpointSyncer, RetentionLeaseSyncer retentionLeaseSyncer, DiscoveryNode targetNode, DiscoveryNode sourceNode)
Creates a shard for the specified shard routing and starts recovery.void
deleteShardStore(String reason, ShardLock lock, IndexSettings indexSettings)
Deletes the shard with an already acquired shard lock.void
deleteShardStore(String reason, ShardId shardId, ClusterState clusterState)
This method deletes the shard contents on disk for the given shard ID.void
deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata, ClusterState clusterState)
Deletes an index that is not assigned to this node.protected void
doClose()
protected void
doStart()
protected void
doStop()
Returns the registered metadata field names for all compatible versions.getCoordinatorRewriteContextProvider(LongSupplier nowInMillis)
Returns a function which given an index name, returns a predicate which fields must match in order to be returned by get mappings, get index, get field mappings and field capabilities API.getMetadataFields(Version version)
Returns the registered metadata field names for the provided compatibleVersion
.getRewriteContext(LongSupplier nowInMillis)
Returns a newQueryRewriteContext
with the givennow
providergetTimestampFieldType(Index index)
boolean
boolean
Checks if all pending deletes have completed.indexService(Index index)
Returns an IndexService for the specified index if exists otherwise returnsnull
.indexServiceSafe(Index index)
Returns an IndexService for the specified index if exists otherwise aIndexNotFoundException
is thrown.boolean
iterator()
void
loadIntoContext(ShardSearchRequest request, SearchContext context, QueryPhase queryPhase)
Loads the cache result, computing it if needed by executing the query phase and otherwise deserializing the cached value into thecontext's query result
.void
processPendingDeletes(Index index, IndexSettings indexSettings, org.elasticsearch.core.TimeValue timeout)
Processes all pending deletes for the given index.void
removeIndex(Index index, IndicesClusterStateService.AllocatedIndices.IndexRemovalReason reason, String extraInfo)
Removes the given index from this service and releases all associated resources.stats(CommonStatsFlags flags)
verifyIndexIsDeleted(Index index, ClusterState clusterState)
Verify that the contents on disk for the given index is deleted; if not, delete the contents.void
verifyIndexMetadata(IndexMetadata metadata, IndexMetadata metadataUpdate)
This method verifies that the givenmetadata
holds sane values to create anIndexService
.<T, E extends Exception>
TwithTempIndexService(IndexMetadata indexMetadata, org.elasticsearch.core.CheckedFunction<IndexService,T,E> indexServiceConsumer)
Methods inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
addLifecycleListener, close, lifecycleState, removeLifecycleListener, start, stop
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices
getShardOrNull
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
INDICES_SHARDS_CLOSED_TIMEOUT
- See Also:
- Constant Field Values
-
INDICES_CACHE_CLEAN_INTERVAL_SETTING
-
INDICES_ID_FIELD_DATA_ENABLED_SETTING
-
WRITE_DANGLING_INDICES_INFO_SETTING
-
-
Constructor Details
-
IndicesService
public IndicesService(Settings settings, PluginsService pluginsService, NodeEnvironment nodeEnv, org.elasticsearch.common.xcontent.NamedXContentRegistry xContentRegistry, AnalysisRegistry analysisRegistry, IndexNameExpressionResolver indexNameExpressionResolver, MapperRegistry mapperRegistry, NamedWriteableRegistry namedWriteableRegistry, ThreadPool threadPool, IndexScopedSettings indexScopedSettings, CircuitBreakerService circuitBreakerService, BigArrays bigArrays, ScriptService scriptService, ClusterService clusterService, Client client, MetaStateService metaStateService, Collection<Function<IndexSettings,Optional<EngineFactory>>> engineFactoryProviders, Map<String,IndexStorePlugin.DirectoryFactory> directoryFactories, ValuesSourceRegistry valuesSourceRegistry, Map<String,IndexStorePlugin.RecoveryStateFactory> recoveryStateFactories, List<IndexStorePlugin.IndexFoldersDeletionListener> indexFoldersDeletionListeners, Map<String,IndexStorePlugin.SnapshotCommitSupplier> snapshotCommitSuppliers, CheckedBiConsumer<ShardSearchRequest,StreamOutput,IOException> requestCacheKeyDifferentiator)
-
-
Method Details
-
doStart
protected void doStart()- Specified by:
doStart
in classAbstractLifecycleComponent
-
clusterService
-
doStop
protected void doStop()- Specified by:
doStop
in classAbstractLifecycleComponent
-
doClose
- Specified by:
doClose
in classAbstractLifecycleComponent
- Throws:
IOException
-
awaitClose
Wait for thisIndicesService
to be effectively closed. When this returnstrue
, all shards and shard stores are closed and all shardclosed listeners
have run. However someshard closed listeners
might not have run.- Returns:
- true if all shards closed within the given timeout, false otherwise
- Throws:
InterruptedException
- if the current thread got interrupted while waiting for shards to close
-
stats
-
iterator
- Specified by:
iterator
in interfaceIterable<IndexService>
-
hasIndex
-
indexService
Returns an IndexService for the specified index if exists otherwise returnsnull
.- Specified by:
indexService
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
-
indexServiceSafe
Returns an IndexService for the specified index if exists otherwise aIndexNotFoundException
is thrown. -
createIndex
public IndexService createIndex(IndexMetadata indexMetadata, List<IndexEventListener> builtInListeners, boolean writeDanglingIndices) throws IOExceptionCreates a newIndexService
for the given metadata.- Specified by:
createIndex
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
- Parameters:
indexMetadata
- the index metadata to create the index forbuiltInListeners
- a list of built-in lifecycleIndexEventListener
that should should be used along side with the per-index listenerswriteDanglingIndices
- whether dangling indices information should be written- Throws:
ResourceAlreadyExistsException
- if the index already exists.IOException
-
withTempIndexService
public <T, E extends Exception> T withTempIndexService(IndexMetadata indexMetadata, org.elasticsearch.core.CheckedFunction<IndexService,T,E> indexServiceConsumer) throws IOException, E- Throws:
IOException
E extends Exception
-
createIndexMapperService
creates a new mapper service for the given index, in order to do administrative work like mapping updates. This *should not* be used for document parsing. Doing so will result in an exception. Note: the returnedMapperService
should be closed when unneeded.- Throws:
IOException
-
verifyIndexMetadata
public void verifyIndexMetadata(IndexMetadata metadata, IndexMetadata metadataUpdate) throws IOExceptionThis method verifies that the givenmetadata
holds sane values to create anIndexService
. This method tries to update the meta data of the createdIndexService
if the givenmetadataUpdate
is different from the givenmetadata
. This method will throw an exception if the creation or the update fails. The createdIndexService
will not be registered and will be closed immediately.- Throws:
IOException
-
createShard
public IndexShard createShard(ShardRouting shardRouting, PeerRecoveryTargetService recoveryTargetService, PeerRecoveryTargetService.RecoveryListener recoveryListener, RepositoriesService repositoriesService, Consumer<IndexShard.ShardFailure> onShardFailure, Consumer<ShardId> globalCheckpointSyncer, RetentionLeaseSyncer retentionLeaseSyncer, DiscoveryNode targetNode, DiscoveryNode sourceNode) throws IOExceptionDescription copied from interface:IndicesClusterStateService.AllocatedIndices
Creates a shard for the specified shard routing and starts recovery.- Specified by:
createShard
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
- Parameters:
shardRouting
- the shard routingrecoveryTargetService
- recovery service for the targetrecoveryListener
- a callback when recovery changes state (finishes or fails)repositoriesService
- service responsible for snapshot/restoreonShardFailure
- a callback when this shard failsglobalCheckpointSyncer
- a callback when this shard syncs the global checkpointretentionLeaseSyncer
- a callback when this shard syncs retention leasestargetNode
- the node where this shard will be recoveredsourceNode
- the source node to recover this shard from (it might be null)- Returns:
- a new shard
- Throws:
IOException
- if an I/O exception occurs when creating the shard
-
removeIndex
public void removeIndex(Index index, IndicesClusterStateService.AllocatedIndices.IndexRemovalReason reason, String extraInfo)Description copied from interface:IndicesClusterStateService.AllocatedIndices
Removes the given index from this service and releases all associated resources. Persistent parts of the index like the shards files, state and transaction logs are kept around in the case of a disaster recovery.- Specified by:
removeIndex
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
- Parameters:
index
- the index to removereason
- the reason to remove the indexextraInfo
- extra information that will be used for logging and reporting
-
getIndicesFieldDataCache
-
getCircuitBreakerService
-
getIndicesQueryCache
-
deleteUnassignedIndex
public void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata, ClusterState clusterState)Deletes an index that is not assigned to this node. This method cleans up all disk folders relating to the index but does not deal with in-memory structures. For those callremoveIndex(Index, IndexRemovalReason, String)
- Specified by:
deleteUnassignedIndex
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
-
deleteShardStore
public void deleteShardStore(String reason, ShardLock lock, IndexSettings indexSettings) throws IOExceptionDeletes the shard with an already acquired shard lock.- Specified by:
deleteShardStore
in interfaceIndexService.ShardStoreDeleter
- Parameters:
reason
- the reason for the shard deletionlock
- the lock of the shard to deleteindexSettings
- the shards index settings.- Throws:
IOException
- if an IOException occurs
-
deleteShardStore
public void deleteShardStore(String reason, ShardId shardId, ClusterState clusterState) throws IOException, ShardLockObtainFailedExceptionThis method deletes the shard contents on disk for the given shard ID. This method will fail if the shard deleting is prevented bycanDeleteShardContent(ShardId, IndexSettings)
of if the shards lock can not be acquired. On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove the index folder as well.- Parameters:
reason
- the reason for the shard deletionshardId
- the shards ID to deleteclusterState
- . This is required to access the indexes settings etc.- Throws:
IOException
- if an IOException occursShardLockObtainFailedException
-
canDeleteIndexContents
This method returns true if the current node is allowed to delete the given index. This is the case if the index is deleted in the metadata or there is no allocation on the local node and the index isn't on a shared file system.- Parameters:
index
-Index
to check whether deletion is allowedindexSettings
-IndexSettings
for the given index- Returns:
- true if the index can be deleted on this node
-
verifyIndexIsDeleted
Verify that the contents on disk for the given index is deleted; if not, delete the contents. This method assumes that an index is already deleted in the cluster state and/or explicitly through index tombstones.- Specified by:
verifyIndexIsDeleted
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
- Parameters:
index
-Index
to make sure its deleted from diskclusterState
-ClusterState
to ensure the index is not part of it- Returns:
- IndexMetadata for the index loaded from disk
-
canDeleteShardContent
public IndicesService.ShardDeletionCheckResult canDeleteShardContent(ShardId shardId, IndexSettings indexSettings)ReturnsShardDeletionCheckResult
signaling whether the shards content for the given shard can be deleted.- Parameters:
shardId
- the shard to delete.indexSettings
- the shards's relevantIndexSettings
. This is required to access the indexes settings etc.
-
addPendingDelete
Adds a pending delete for the given index shard.- Specified by:
addPendingDelete
in interfaceIndexService.ShardStoreDeleter
-
addPendingDelete
Adds a pending delete for the given index. -
processPendingDeletes
public void processPendingDeletes(Index index, IndexSettings indexSettings, org.elasticsearch.core.TimeValue timeout) throws IOException, InterruptedException, ShardLockObtainFailedExceptionProcesses all pending deletes for the given index. This method will acquire all locks for the given index and will process all pending deletes for this index. Pending deletes might occur if the OS doesn't allow deletion of files because they are used by a different process ie. on Windows where files might still be open by a virus scanner. On a shared filesystem a replica might not have been closed when the primary is deleted causing problems on delete calls so we schedule there deletes later.- Specified by:
processPendingDeletes
in interfaceIndicesClusterStateService.AllocatedIndices<IndexShard,IndexService>
- Parameters:
index
- the index to process the pending deletes fortimeout
- the timeout used for processing pending deletes- Throws:
IOException
InterruptedException
ShardLockObtainFailedException
-
hasUncompletedPendingDeletes
public boolean hasUncompletedPendingDeletes()Checks if all pending deletes have completed. Used by tests to ensure we don't check directory contents while deletion still ongoing. * The reason is that, on Windows, browsing the directory contents can interfere with the deletion process and delay it unnecessarily. -
getAnalysis
-
canCache
Can the shard request be cached at all? -
loadIntoContext
public void loadIntoContext(ShardSearchRequest request, SearchContext context, QueryPhase queryPhase) throws ExceptionLoads the cache result, computing it if needed by executing the query phase and otherwise deserializing the cached value into thecontext's query result
. The combination of load + compute allows to have a single load operation that will cause other requests with the same key to wait till its loaded an reuse the same cache.- Throws:
Exception
-
getTotalIndexingBufferBytes
-
buildAliasFilter
public AliasFilter buildAliasFilter(ClusterState state, String index, Set<String> resolvedExpressions) -
getRewriteContext
Returns a newQueryRewriteContext
with the givennow
provider -
getCoordinatorRewriteContextProvider
public CoordinatorRewriteContextProvider getCoordinatorRewriteContextProvider(LongSupplier nowInMillis) -
clearIndexShardCache
public void clearIndexShardCache(ShardId shardId, boolean queryCache, boolean fieldDataCache, boolean requestCache, String... fields)Clears the caches for the given shard id if the shard is still allocated on this node -
getFieldFilter
Returns a function which given an index name, returns a predicate which fields must match in order to be returned by get mappings, get index, get field mappings and field capabilities API. Useful to filter the fields that such API return. The predicate receives the field name as input argument. In case multiple plugins register a field filter throughMapperPlugin.getFieldFilter()
, only fields that match all the registered filters will be returned by get mappings, get index, get field mappings and field capabilities API. -
getMetadataFields
Returns the registered metadata field names for the provided compatibleVersion
. -
getAllMetadataFields
Returns the registered metadata field names for all compatible versions. -
isIdFieldDataEnabled
public boolean isIdFieldDataEnabled() -
allPendingDanglingIndicesWritten
public boolean allPendingDanglingIndicesWritten() -
getTimestampFieldType
- Returns:
- the field type of the
@timestamp
field of the given index, ornull
if: - the index is not found, - the field is not found, or - the field is not a timestamp field.
-