Class Store

  • All Implemented Interfaces:
    Closeable, AutoCloseable, RefCounted, CloseableIndexComponent, IndexShardComponent


    public class Store
    extends AbstractIndexShardComponent
    implements CloseableIndexComponent, Closeable, RefCounted
    A Store provides plain access to files written by an elasticsearch index shard. Each shard has a dedicated store that is uses to access Lucene's Directory which represents the lowest level of file abstraction in Lucene used to read and write Lucene indices. This class also provides access to metadata information like checksums for committed files. A committed file is a file that belongs to a segment written by a Lucene commit. Files that have not been committed ie. created during a merge or a shard refresh / NRT reopen are not considered in the MetadataSnapshot.

    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();
          }
     
    • Method Detail

      • directory

        public org.apache.lucene.store.Directory directory()
      • readLastCommittedSegmentsInfo

        public org.apache.lucene.index.SegmentInfos readLastCommittedSegmentsInfo()
                                                                           throws IOException
        Returns the last committed segments info for this store
        Throws:
        IOException - if the index is corrupted or the segments file is not present
      • getMetadataOrEmpty

        public Store.MetadataSnapshot getMetadataOrEmpty()
                                                  throws IOException
        Returns a new MetadataSnapshot for the latest commit in this store or an empty snapshot if no index exists or can not be opened.
        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.
        IOException
      • getMetadata

        public Store.MetadataSnapshot getMetadata()
                                           throws IOException
        Returns a new MetadataSnapshot for the latest commit in 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.
        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 no index / valid commit-point can be found in this store
        IOException
      • getMetadata

        public Store.MetadataSnapshot getMetadata(org.apache.lucene.index.IndexCommit commit)
                                           throws IOException
        Returns a new MetadataSnapshot for the given commit. If the given commit is null the latest commit point is used.
        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.
        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 store
        IOException
      • renameTempFilesSafe

        public void renameTempFilesSafe(Map<String,String> tempFileMap)
                                 throws IOException
        Renames all the given files form the key of the map to the value of the map. All successfully renamed files are removed from the map in-place.
        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 corresponding decRef(), in a finally clause; otherwise the store may never be closed. Note that close() simply calls decRef(), which means that the Store will not really be closed until decRef() has been called for all outstanding references.

        Note: Close can safely be called multiple times.
        Specified by:
        incRef in interface  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 return true iff the refCount was incremented successfully otherwise false. 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 corresponding decRef(), in a finally clause; otherwise the store may never be closed. Note that close() simply calls decRef(), which means that the Store will not really be closed until decRef() has been called for all outstanding references.

        Note: Close can safely be called multiple times.
        Specified by:
        tryIncRef in interface  RefCounted
        See Also:
        decRef(), incRef()
      • decRef

        public final void decRef()
        Decreases the refCount of this Store instance.If the refCount drops to 0, then this store is closed.
        Specified by:
        decRef in interface  RefCounted
        See Also:
        incRef()
      • close

        public void close()
        Description copied from interface: CloseableIndexComponent
        Closes the index component. A boolean indicating if its part of an actual index deletion or not is passed.
      • readMetadataSnapshot

        public static Store.MetadataSnapshot readMetadataSnapshot(File[] indexLocations,
                                                                  ESLogger logger)
                                                           throws IOException
        Reads 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
      • canOpenIndex

        public static boolean canOpenIndex(ESLogger logger,
                                           File... indexLocations)
                                    throws IOException
        Returns true iff the given location contains an index an the index can be successfully opened. This includes reading the segment infos and possible corruption markers.
        Throws:
        IOException
      • createVerifyingOutput

        public org.apache.lucene.store.IndexOutput createVerifyingOutput(String fileName,
                                                                         StoreFileMetaData metadata,
                                                                         org.apache.lucene.store.IOContext context)
                                                                  throws IOException
        The returned IndexOutput might validate the files checksum if the file has been written with a newer lucene version and the metadata holds the necessary information to detect that it was been written by Lucene 4.8 or newer. If it has only a legacy checksum, returned IndexOutput will not verify the checksum.

        Note: Checksums are calculated nevertheless since lucene does it 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

        public static void verify(org.apache.lucene.store.IndexOutput output)
                           throws IOException
        Throws:
        IOException
      • verify

        public static void verify(org.apache.lucene.store.IndexInput input)
                           throws IOException
        Throws:
        IOException
      • checkIntegrity

        public static boolean checkIntegrity(StoreFileMetaData md,
                                             org.apache.lucene.store.Directory directory)
      • removeCorruptionMarker

        public void removeCorruptionMarker()
                                    throws IOException
        Deletes all corruption markers from this store.
        Throws:
        IOException
      • cleanup

        public void cleanup(String reason,
                            Set<String> filesToClean)
                     throws IOException
        This method deletes every file in this store that is not contained in the given source meta data or is a legacy checksum file.
        Parameters:
        reason - the reason for this cleanup operation logged for each deleted file
        filesToClean - the file names used for cleanup. all files names in this metadata should be kept around.
        Throws:
        IOException - if an IOException occurs
      • cleanupAndVerify

        public void cleanupAndVerify(String reason,
                                     Store.MetadataSnapshot sourceMetaData)
                              throws IOException
        This 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 file
        sourceMetaData - the metadata used for cleanup. all files in this metadata should be kept around.
        Throws:
        IOException - if an IOException occurs
        ElasticsearchIllegalStateException - if the latest snapshot in this store differs from the given one after the cleanup.
      • refCount

        public int refCount()
        Returns the current reference count.
      • logDeleteFile

        public void logDeleteFile(String message,
                                  String fileName)
        Log that we are about to delete this file, to the index.store.deletes component.
      • logDeleteFile

        public static void logDeleteFile(org.apache.lucene.store.Directory dir,
                                         String message,
                                         String fileName)
        Log that we are about to delete this file, to the index.store.deletes component.
      • isChecksum

        public static final boolean isChecksum(String name)
      • digestToString

        public static String digestToString(long digest)
        Produces a string representation of the given digest value.
      • deleteQuiet

        public void deleteQuiet(String... files)
      • markStoreCorrupted

        public void markStoreCorrupted(IOException exception)
                                throws IOException
        Marks this store as corrupted. This method writes a corrupted_${uuid} file containing the given exception message. If a store contains a corrupted_${uuid} file isMarkedCorrupted() will return true.
        Throws:
        IOException