Class Store
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.elasticsearch.core.RefCounted
,IndexShardComponent
Note: If you use a store it's reference count should be increased before using it by calling #incRef and a corresponding #decRef must be called in a try/finally block to release the store again ie.:
store.incRef(); try { // use the store... } finally { store.decRef(); }
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Represents a snapshot of the current directory build from the latest Lucene commit.static interface
A listener that is executed once the store is closed and all references to it are releasedstatic class
A class representing the diff between a recovery source and recovery target -
Field Summary
Modifier and TypeFieldDescriptionstatic String
This is an escape hatch for lucenes internal optimization that checks if the IndexInput is an instance of ByteBufferIndexInput and if that's the case doesn't load the term dictionary into ram but loads it off disk iff the fields is not an ID like field.static Setting<org.elasticsearch.core.TimeValue>
static org.apache.lucene.store.IOContext
SpecificIOContext
indicating that we will read only the Lucene file footer (containing the file checksum) SeeStore.MetadataSnapshot.checksumFromLuceneFile(org.apache.lucene.store.Directory, java.lang.String, java.util.Map<java.lang.String, org.elasticsearch.index.store.StoreFileMetadata>, org.apache.logging.log4j.Logger, java.lang.String, boolean)
.Fields inherited from class org.elasticsearch.index.shard.AbstractIndexShardComponent
indexSettings, logger, shardId
-
Constructor Summary
ConstructorDescriptionStore(ShardId shardId, IndexSettings indexSettings, org.apache.lucene.store.Directory directory, ShardLock shardLock)
Store(ShardId shardId, IndexSettings indexSettings, org.apache.lucene.store.Directory directory, ShardLock shardLock, Store.OnClose onClose)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
associateIndexWithNewTranslog(String translogUUID)
Force bakes the given translog generation as recovery information in the lucene index.void
void
Marks an existing lucene index with a new history uuid.void
bootstrapNewHistory(long localCheckpoint, long maxSeqNo)
Marks an existing lucene index with a new history uuid and sets the given local checkpoint as well as the maximum sequence number.org.apache.lucene.index.CheckIndex.Status
checkIndex(PrintStream out)
Checks and returns the status of the existing index in this store.static void
checkIntegrity(StoreFileMetadata md, org.apache.lucene.store.Directory directory)
boolean
static boolean
checkIntegrityNoException(StoreFileMetadata md, org.apache.lucene.store.Directory directory)
void
cleanupAndVerify(String reason, Store.MetadataSnapshot sourceMetadata)
This method deletes every file in this store that is not contained in the given source meta data or is a legacy checksum file.void
close()
void
creates an empty lucene index and a corresponding empty translog.org.apache.lucene.store.IndexOutput
createVerifyingOutput(String fileName, StoreFileMetadata metadata, org.apache.lucene.store.IOContext context)
The returned IndexOutput validates the files checksum.boolean
decRef()
Decreases the refCount of this Store instance.void
deleteQuiet(String... files)
static String
digestToString(long digest)
Produces a string representation of the given digest value.org.apache.lucene.store.Directory
void
Checks that the Lucene index contains a history uuid marker.void
findSafeIndexCommit(long globalCheckpoint)
Returns aSequenceNumbers.CommitInfo
of the safe commit if exists.getMetadata(org.apache.lucene.index.IndexCommit commit)
Returns a new MetadataSnapshot for the given commit.getMetadata(org.apache.lucene.index.IndexCommit commit, boolean lockDirectory)
Returns a new MetadataSnapshot for the given commit.void
incRef()
Increments the refCount of this Store instance.static boolean
isAutogenerated(String name)
Returns true if the file is auto-generated by the store and shouldn't be deleted during cleanup.boolean
boolean
void
markStoreCorrupted(IOException exception)
Marks this store as corrupted.org.apache.lucene.store.IndexInput
openVerifyingInput(String filename, org.apache.lucene.store.IOContext context, StoreFileMetadata metadata)
org.apache.lucene.index.SegmentInfos
Returns the last committed segments info for this storestatic Store.MetadataSnapshot
readMetadataSnapshot(Path indexLocation, ShardId shardId, NodeEnvironment.ShardLocker shardLocker, org.apache.logging.log4j.Logger logger)
Reads a MetadataSnapshot from the given index locations or returns an empty snapshot if it can't be read.int
refCount()
Returns the current reference count.void
Deletes all corruption markers from this store.void
renameTempFilesSafe(Map<String,String> tempFileMap)
Renames all the given files from the key of the map to the value of the map.stats(long reservedBytes, LongUnaryOperator localSizeFunction)
void
trimUnsafeCommits(long lastSyncedGlobalCheckpoint, long minRetainedTranslogGen, Version indexVersionCreated)
Keeping existing unsafe commits when opening an engine can be problematic because these commits are not safe at the recovering time but they can suddenly become safe in the future.boolean
Tries to increment the refCount of this Store instance.static void
tryOpenIndex(Path indexLocation, ShardId shardId, NodeEnvironment.ShardLocker shardLocker, org.apache.logging.log4j.Logger logger)
Tries to open an index for the given location.static void
verify(org.apache.lucene.store.IndexInput input)
static void
verify(org.apache.lucene.store.IndexOutput output)
Methods inherited from class org.elasticsearch.index.shard.AbstractIndexShardComponent
indexSettings, shardId
-
Field Details
-
FORCE_RAM_TERM_DICT
This is an escape hatch for lucenes internal optimization that checks if the IndexInput is an instance of ByteBufferIndexInput and if that's the case doesn't load the term dictionary into ram but loads it off disk iff the fields is not an ID like field. Since this optimization has been added very late in the release processes we add this setting to allow users to opt-out of this by exploiting lucene internals and wrapping the IndexInput in a simple delegate. -
CORRUPTED_MARKER_NAME_PREFIX
- See Also:
- Constant Field Values
-
INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING
public static final Setting<org.elasticsearch.core.TimeValue> INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING -
READONCE_CHECKSUM
public static final org.apache.lucene.store.IOContext READONCE_CHECKSUMSpecificIOContext
indicating that we will read only the Lucene file footer (containing the file checksum) SeeStore.MetadataSnapshot.checksumFromLuceneFile(org.apache.lucene.store.Directory, java.lang.String, java.util.Map<java.lang.String, org.elasticsearch.index.store.StoreFileMetadata>, org.apache.logging.log4j.Logger, java.lang.String, boolean)
.
-
-
Constructor Details
-
Store
public Store(ShardId shardId, IndexSettings indexSettings, org.apache.lucene.store.Directory directory, ShardLock shardLock) -
Store
public Store(ShardId shardId, IndexSettings indexSettings, org.apache.lucene.store.Directory directory, ShardLock shardLock, Store.OnClose onClose)
-
-
Method Details
-
directory
public org.apache.lucene.store.Directory directory() -
readLastCommittedSegmentsInfo
Returns the last committed segments info for this store- Throws:
IOException
- if the index is corrupted or the segments file is not present
-
getMetadata
public Store.MetadataSnapshot getMetadata(org.apache.lucene.index.IndexCommit commit) throws IOExceptionReturns a new MetadataSnapshot for the given commit. If the given commit isnull
the latest commit point is used. Note that this method requires the caller verify it has the right to access the store and no concurrent file changes are happening. If in doubt, you probably want to use one of the following:readMetadataSnapshot(Path, ShardId, NodeEnvironment.ShardLocker, Logger)
to read a meta data while lockingIndexShard.snapshotStoreMetadata()
to safely read from an existing shardIndexShard.acquireLastIndexCommit(boolean)
to get anIndexCommit
which is safe to use but has to be freed- Parameters:
commit
- the index commit to read the snapshot from ornull
if the latest snapshot should be read from the directory- Throws:
org.apache.lucene.index.CorruptIndexException
- if the lucene index is corrupted. This can be caused by a checksum mismatch or an unexpected exception when opening the index reading the segments file.org.apache.lucene.index.IndexFormatTooOldException
- if the lucene index is too old to be opened.org.apache.lucene.index.IndexFormatTooNewException
- if the lucene index is too new to be opened.FileNotFoundException
- if one or more files referenced by a commit are not present.NoSuchFileException
- if one or more files referenced by a commit are not present.org.apache.lucene.index.IndexNotFoundException
- if the commit point can't be found in this storeIOException
-
getMetadata
public Store.MetadataSnapshot getMetadata(org.apache.lucene.index.IndexCommit commit, boolean lockDirectory) throws IOExceptionReturns a new MetadataSnapshot for the given commit. If the given commit isnull
the latest commit point is used. Note that this method requires the caller verify it has the right to access the store and no concurrent file changes are happening. If in doubt, you probably want to use one of the following:readMetadataSnapshot(Path, ShardId, NodeEnvironment.ShardLocker, Logger)
to read a meta data while lockingIndexShard.snapshotStoreMetadata()
to safely read from an existing shardIndexShard.acquireLastIndexCommit(boolean)
to get anIndexCommit
which is safe to use but has to be freed- Parameters:
commit
- the index commit to read the snapshot from ornull
if the latest snapshot should be read from the directorylockDirectory
- iftrue
the index writer lock will be obtained before reading the snapshot. This should only be used if there is no started shard using this store.- Throws:
org.apache.lucene.index.CorruptIndexException
- if the lucene index is corrupted. This can be caused by a checksum mismatch or an unexpected exception when opening the index reading the segments file.org.apache.lucene.index.IndexFormatTooOldException
- if the lucene index is too old to be opened.org.apache.lucene.index.IndexFormatTooNewException
- if the lucene index is too new to be opened.FileNotFoundException
- if one or more files referenced by a commit are not present.NoSuchFileException
- if one or more files referenced by a commit are not present.org.apache.lucene.index.IndexNotFoundException
- if the commit point can't be found in this storeIOException
-
renameTempFilesSafe
Renames all the given files from the key of the map to the value of the map. All successfully renamed files are removed from the map in-place.- Throws:
IOException
-
checkIndex
Checks and returns the status of the existing index in this store.- Parameters:
out
- where infoStream messages should go. SeeCheckIndex.setInfoStream(PrintStream)
- Throws:
IOException
-
stats
- Parameters:
reservedBytes
- a prediction of how much larger the store is expected to grow, orStoreStats.UNKNOWN_RESERVED_BYTES
.localSizeFunction
- to calculate the local size of the shard based on the shard size.- Throws:
IOException
-
incRef
public final void incRef()Increments the refCount of this Store instance. RefCounts are used to determine when a Store can be closed safely, i.e. as soon as there are no more references. Be sure to always call a correspondingdecRef()
, in a finally clause; otherwise the store may never be closed. Note thatclose()
simply calls decRef(), which means that the Store will not really be closed untildecRef()
has been called for all outstanding references.Note: Close can safely be called multiple times.
- Specified by:
incRef
in interfaceorg.elasticsearch.core.RefCounted
- Throws:
org.apache.lucene.store.AlreadyClosedException
- iff the reference counter can not be incremented.- See Also:
decRef()
,tryIncRef()
-
tryIncRef
public final boolean tryIncRef()Tries to increment the refCount of this Store instance. This method will returntrue
iff the refCount was incremented successfully otherwisefalse
. RefCounts are used to determine when a Store can be closed safely, i.e. as soon as there are no more references. Be sure to always call a correspondingdecRef()
, in a finally clause; otherwise the store may never be closed. Note thatclose()
simply calls decRef(), which means that the Store will not really be closed untildecRef()
has been called for all outstanding references.Note: Close can safely be called multiple times.
-
decRef
public final boolean decRef()Decreases the refCount of this Store instance. If the refCount drops to 0, then this store is closed.- Specified by:
decRef
in interfaceorg.elasticsearch.core.RefCounted
- See Also:
incRef()
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
isClosing
public boolean isClosing()- Returns:
- true if the
close()
method has been called. This indicates that the current store is either closed or being closed waiting for all references to it to be released. You might prefer to useensureOpen()
instead.
-
readMetadataSnapshot
public static Store.MetadataSnapshot readMetadataSnapshot(Path indexLocation, ShardId shardId, NodeEnvironment.ShardLocker shardLocker, org.apache.logging.log4j.Logger logger) throws IOExceptionReads a MetadataSnapshot from the given index locations or returns an empty snapshot if it can't be read.- Throws:
IOException
- if the index we try to read is corrupted
-
tryOpenIndex
public static void tryOpenIndex(Path indexLocation, ShardId shardId, NodeEnvironment.ShardLocker shardLocker, org.apache.logging.log4j.Logger logger) throws IOException, ShardLockObtainFailedExceptionTries to open an index for the given location. This includes reading the segment infos and possible corruption markers. If the index can not be opened, an exception is thrown -
createVerifyingOutput
public org.apache.lucene.store.IndexOutput createVerifyingOutput(String fileName, StoreFileMetadata metadata, org.apache.lucene.store.IOContext context) throws IOExceptionThe returned IndexOutput validates the files checksum.Note: Checksums are calculated by default since version 4.8.0. This method only adds the verification against the checksum in the given metadata and does not add any significant overhead.
- Throws:
IOException
-
verify
- Throws:
IOException
-
openVerifyingInput
public org.apache.lucene.store.IndexInput openVerifyingInput(String filename, org.apache.lucene.store.IOContext context, StoreFileMetadata metadata) throws IOException- Throws:
IOException
-
verify
- Throws:
IOException
-
checkIntegrityNoException
-
checkIntegrityNoException
public static boolean checkIntegrityNoException(StoreFileMetadata md, org.apache.lucene.store.Directory directory) -
checkIntegrity
public static void checkIntegrity(StoreFileMetadata md, org.apache.lucene.store.Directory directory) throws IOException- Throws:
IOException
-
isMarkedCorrupted
- Throws:
IOException
-
removeCorruptionMarker
Deletes all corruption markers from this store.- Throws:
IOException
-
failIfCorrupted
- Throws:
IOException
-
cleanupAndVerify
public void cleanupAndVerify(String reason, Store.MetadataSnapshot sourceMetadata) throws IOExceptionThis method deletes every file in this store that is not contained in the given source meta data or is a legacy checksum file. After the delete it pulls the latest metadata snapshot from the store and compares it to the given snapshot. If the snapshots are inconsistent an illegal state exception is thrown.- Parameters:
reason
- the reason for this cleanup operation logged for each deleted filesourceMetadata
- the metadata used for cleanup. all files in this metadata should be kept around.- Throws:
IOException
- if an IOException occursIllegalStateException
- if the latest snapshot in this store differs from the given one after the cleanup.
-
refCount
public int refCount()Returns the current reference count. -
beforeClose
public void beforeClose() -
isAutogenerated
Returns true if the file is auto-generated by the store and shouldn't be deleted during cleanup. This includes write lock and checksum files -
digestToString
Produces a string representation of the given digest value. -
deleteQuiet
-
markStoreCorrupted
Marks this store as corrupted. This method writes acorrupted_${uuid}
file containing the given exception message. If a store contains acorrupted_${uuid}
fileisMarkedCorrupted()
will returntrue
.- Throws:
IOException
-
createEmpty
creates an empty lucene index and a corresponding empty translog. Any existing data will be deleted.- Throws:
IOException
-
bootstrapNewHistory
Marks an existing lucene index with a new history uuid. This is used to make sure no existing shard will recovery from this index using ops based recovery.- Throws:
IOException
-
bootstrapNewHistory
Marks an existing lucene index with a new history uuid and sets the given local checkpoint as well as the maximum sequence number. This is used to make sure no existing shard will recover from this index using ops based recovery.- Throws:
IOException
- See Also:
SequenceNumbers.LOCAL_CHECKPOINT_KEY
,SequenceNumbers.MAX_SEQ_NO
-
associateIndexWithNewTranslog
Force bakes the given translog generation as recovery information in the lucene index. This is used when recovering from a snapshot or peer file based recovery where a new empty translog is created and the existing lucene index needs should be changed to use it.- Throws:
IOException
-
ensureIndexHasHistoryUUID
Checks that the Lucene index contains a history uuid marker. If not, a new one is generated and committed.- Throws:
IOException
-
trimUnsafeCommits
public void trimUnsafeCommits(long lastSyncedGlobalCheckpoint, long minRetainedTranslogGen, Version indexVersionCreated) throws IOExceptionKeeping existing unsafe commits when opening an engine can be problematic because these commits are not safe at the recovering time but they can suddenly become safe in the future. The following issues can happen if unsafe commits are kept oninit.1. Replica can use unsafe commit in peer-recovery. This happens when a replica with a safe commit c1(max_seqno=1) and an unsafe commit c2(max_seqno=2) recovers from a primary with c1(max_seqno=1). If a new document(seqno=2) is added without flushing, the global checkpoint is advanced to 2; and the replica recovers again, it will use the unsafe commit c2(max_seqno=2 at most gcp=2) as the starting commit for sequenced-based recovery even the commit c2 contains a stale operation and the document(with seqno=2) will not be replicated to the replica.
2. Min translog gen for recovery can go backwards in peer-recovery. This happens when are replica with a safe commit c1(local_checkpoint=1, recovery_translog_gen=1) and an unsafe commit c2(local_checkpoint=2, recovery_translog_gen=2). The replica recovers from a primary, and keeps c2 as the last commit, then sets last_translog_gen to 2. Flushing a new commit on the replica will cause exception as the new last commit c3 will have recovery_translog_gen=1. The recovery translog generation of a commit is calculated based on the current local checkpoint. The local checkpoint of c3 is 1 while the local checkpoint of c2 is 2.
3. Commit without translog can be used in recovery. An old index, which was created before multiple-commits is introduced (v6.2), may not have a safe commit. If that index has a snapshotted commit without translog and an unsafe commit, the policy can consider the snapshotted commit as a safe commit for recovery even the commit does not have translog.
- Throws:
IOException
-
findSafeIndexCommit
public Optional<SequenceNumbers.CommitInfo> findSafeIndexCommit(long globalCheckpoint) throws IOExceptionReturns aSequenceNumbers.CommitInfo
of the safe commit if exists.- Throws:
IOException
-