Package org.elasticsearch.gateway
Class MetadataStateFormat<T>
java.lang.Object
org.elasticsearch.gateway.MetadataStateFormat<T>
- Type Parameters:
T
- the type of the XContent base data-structure
public abstract class MetadataStateFormat<T>
extends java.lang.Object
MetadataStateFormat is a base class to write checksummed
XContent based files to one or more directories in a standardized directory structure.
-
Field Summary
Fields Modifier and Type Field Description static org.elasticsearch.common.xcontent.XContentType
FORMAT
static java.lang.String
STATE_DIR_NAME
static java.lang.String
STATE_FILE_EXTENSION
-
Constructor Summary
Constructors Modifier Constructor Description protected
MetadataStateFormat(java.lang.String prefix)
Creates a newMetadataStateFormat
instance -
Method Summary
Modifier and Type Method Description void
cleanupOldFiles(long currentGeneration, java.nio.file.Path[] locations)
Clean ups all state files not matching passed generation.static void
deleteMetaState(java.nio.file.Path... dataLocations)
Deletes all meta state directories recursively for the given data locationsabstract T
fromXContent(org.elasticsearch.common.xcontent.XContentParser parser)
Reads a new instance of the state from the given XContentParser Subclasses need to implement this class for theirs specific state.java.lang.String
getPrefix()
java.lang.String
getStateFileName(long generation)
T
loadGeneration(org.apache.logging.log4j.Logger logger, org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, long generation, java.nio.file.Path... dataLocations)
Tries to load the state of particular generation from the given data-locations.T
loadLatestState(org.apache.logging.log4j.Logger logger, org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, java.nio.file.Path... dataLocations)
Tries to load the latest state from the given data-locations.org.elasticsearch.common.collect.Tuple<T,java.lang.Long>
loadLatestStateWithGeneration(org.apache.logging.log4j.Logger logger, org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, java.nio.file.Path... dataLocations)
Tries to load the latest state from the given data-locations.protected org.apache.lucene.store.Directory
newDirectory(java.nio.file.Path dir)
protected org.elasticsearch.common.xcontent.XContentBuilder
newXContentBuilder(org.elasticsearch.common.xcontent.XContentType type, java.io.OutputStream stream)
T
read(org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, java.nio.file.Path file)
Reads the state from a given file and compares the expected version against the actual version of the state.abstract void
toXContent(org.elasticsearch.common.xcontent.XContentBuilder builder, T state)
Writes the given state to the given XContentBuilder Subclasses need to implement this class for theirs specific state.long
write(T state, java.nio.file.Path... locations)
Writes the given state to the given directories.long
writeAndCleanup(T state, java.nio.file.Path... locations)
Writes the given state to the given directories and performs cleanup of old state files if the write succeeds or newly created state file if write fails.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
FORMAT
public static final org.elasticsearch.common.xcontent.XContentType FORMAT -
STATE_DIR_NAME
public static final java.lang.String STATE_DIR_NAME- See Also:
- Constant Field Values
-
STATE_FILE_EXTENSION
public static final java.lang.String STATE_FILE_EXTENSION- See Also:
- Constant Field Values
-
-
Constructor Details
-
MetadataStateFormat
protected MetadataStateFormat(java.lang.String prefix)Creates a newMetadataStateFormat
instance
-
-
Method Details
-
writeAndCleanup
public final long writeAndCleanup(T state, java.nio.file.Path... locations) throws WriteStateExceptionWrites the given state to the given directories and performs cleanup of old state files if the write succeeds or newly created state file if write fails. See alsowrite(Object, Path...)
andcleanupOldFiles(long, Path[])
.- Throws:
WriteStateException
-
write
Writes the given state to the given directories. The state is written to a state directory ("_state") underneath each of the given file locations and is created if it doesn't exist. The state is serialized to a temporary file in that directory and is then atomically moved to it's target filename of the pattern{prefix}{version}.st
. If this method returns without exception there is a guarantee that state is persisted to the disk and loadLatestState will return it.
This method always performs cleanup of temporary files regardless whether it succeeds or fails. Cleanup logic for state files is more involved. If this method fails with an exception, it performs cleanup of newly created state file. But if this method succeeds, it does not perform cleanup of old state files. If this write succeeds, but some further write fails, you may want to rollback the transaction and keep old file around. After transaction is finished usecleanupOldFiles(long, Path[])
for the clean-up. If this write is not a part of bigger transaction, consider usingwriteAndCleanup(Object, Path...)
method instead.- Parameters:
state
- the state object to writelocations
- the locations where the state should be written to.- Returns:
- generation of newly written state.
- Throws:
WriteStateException
- if some exception during writing state occurs. See alsoWriteStateException.isDirty()
.
-
newXContentBuilder
protected org.elasticsearch.common.xcontent.XContentBuilder newXContentBuilder(org.elasticsearch.common.xcontent.XContentType type, java.io.OutputStream stream) throws java.io.IOException- Throws:
java.io.IOException
-
toXContent
public abstract void toXContent(org.elasticsearch.common.xcontent.XContentBuilder builder, T state) throws java.io.IOExceptionWrites the given state to the given XContentBuilder Subclasses need to implement this class for theirs specific state.- Throws:
java.io.IOException
-
fromXContent
public abstract T fromXContent(org.elasticsearch.common.xcontent.XContentParser parser) throws java.io.IOExceptionReads a new instance of the state from the given XContentParser Subclasses need to implement this class for theirs specific state.- Throws:
java.io.IOException
-
read
public final T read(org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, java.nio.file.Path file) throws java.io.IOExceptionReads the state from a given file and compares the expected version against the actual version of the state.- Throws:
java.io.IOException
-
newDirectory
protected org.apache.lucene.store.Directory newDirectory(java.nio.file.Path dir) throws java.io.IOException- Throws:
java.io.IOException
-
cleanupOldFiles
public void cleanupOldFiles(long currentGeneration, java.nio.file.Path[] locations)Clean ups all state files not matching passed generation.- Parameters:
currentGeneration
- state generation to keep.locations
- state paths.
-
getStateFileName
public java.lang.String getStateFileName(long generation) -
loadGeneration
public T loadGeneration(org.apache.logging.log4j.Logger logger, org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, long generation, java.nio.file.Path... dataLocations)Tries to load the state of particular generation from the given data-locations. If any of data locations contain state files with given generation, state will be loaded from these state files.- Parameters:
logger
- a logger instance.generation
- the generation to be loaded.dataLocations
- the data-locations to try.- Returns:
- the state of asked generation or
null
if no state was found.
-
loadLatestStateWithGeneration
public org.elasticsearch.common.collect.Tuple<T,java.lang.Long> loadLatestStateWithGeneration(org.apache.logging.log4j.Logger logger, org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, java.nio.file.Path... dataLocations) throws java.io.IOExceptionTries to load the latest state from the given data-locations.- Parameters:
logger
- a logger instance.dataLocations
- the data-locations to try.- Returns:
- tuple of the latest state and generation. (null, -1) if no state is found.
- Throws:
java.io.IOException
-
loadLatestState
public T loadLatestState(org.apache.logging.log4j.Logger logger, org.elasticsearch.common.xcontent.NamedXContentRegistry namedXContentRegistry, java.nio.file.Path... dataLocations) throws java.io.IOExceptionTries to load the latest state from the given data-locations.- Parameters:
logger
- a logger instance.dataLocations
- the data-locations to try.- Returns:
- the latest state or
null
if no state was found. - Throws:
java.io.IOException
-
deleteMetaState
public static void deleteMetaState(java.nio.file.Path... dataLocations) throws java.io.IOExceptionDeletes all meta state directories recursively for the given data locations- Parameters:
dataLocations
- the data location to delete- Throws:
java.io.IOException
-
getPrefix
public java.lang.String getPrefix()
-