Class AbstractLogStorage
java.lang.Object
org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.AbstractLogStorage
- All Implemented Interfaces:
AutoCloseable,TinkerStorage
- Direct Known Subclasses:
GraphBinaryStorage
Log-structured durable storage machinery shared by
TinkerStorage engines, independent of how an element is
encoded. It persists a TinkerStorageGraph as an append-only commit log (log.gbin) plus an optional
folded snapshot.gbin; on open the snapshot is read followed by the log, last-write-wins per element id.
This base owns everything that is not the element codec: the on-disk file layout, the single-source-of-truth
VERSION marker, the store id and compaction generation that tie a log to its snapshot, length+CRC frame
framing (which tells an interrupted trailing append apart from genuine corruption, with log frames also checksumming
their length), the replay fold loop, durability via SyncMode, and crash-safe atomic compaction with a size
threshold. Concrete engines supply only the codec through encodeCommit(long, java.util.Collection<org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.TinkerStorageMutation<org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex>>, java.util.Collection<org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.TinkerStorageMutation<org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge>>), decodeFrame(byte[], java.util.Map<java.lang.Object, org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex>, java.util.Map<java.lang.Object, org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge>),
writeSnapshot(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph, java.io.DataOutputStream), and (optionally) beginReplay() for per-replay decode state.
The in-memory graph remains authoritative (write-through); this machinery does not support graphs larger than memory.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidReset any per-replay decode state (e.g. a dictionary) before a fold begins.voidclear(TinkerStorageGraph graph) Replace everything the engine holds with the graph's current committed state, which the caller has just emptied, so that the store holds nothing and a reopen finds an empty graph.voidclose()voidcompact(TinkerStorageGraph graph) Fold accumulated changes into a compact representation of the current committed state, reclaiming space.protected voidcompact(TinkerStorageGraph graph, boolean force) Every compaction, explicit, automatic or forclear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph), runs through here.protected voidconfigureCodec(org.apache.commons.configuration2.Configuration config) Read any codec-specific configuration.protected abstract voiddecodeFrame(byte[] record, Map<Object, DetachedVertex> vertices, Map<Object, DetachedEdge> edges) Decode one record payload, folding its puts and deletes into the supplied maps (last-write-wins per id).protected abstract byte[]encodeCommit(long txVersion, Collection<TinkerStorageMutation<TinkerVertex>> changedVertices, Collection<TinkerStorageMutation<TinkerEdge>> changedEdges) Encode a committing transaction's changeset into a single record payload (the framing is added by the caller).voidStop accepting writes, as after a storage failure, until the graph is reopened.voidflush()Force any buffered writes to durable storage.booleanisFailed()Whether this engine has stopped accepting writes after a storage failure, such as a failed log write or the store being removed while the graph was open.booleanWhether this engine accepts no writes at all, such as when a damaged store was opened for recovery.voidmaybeCompact(TinkerStorageGraph graph) Compact if the engine's accumulated data has grown past its own threshold, otherwise do nothing.voidopen(TinkerStorageGraph graph, org.apache.commons.configuration2.Configuration config) Prepare the storage engine for use, resolving its backing location from the supplied configuration.protected FileOutputStreamopenEmptyLog(File tmp) Open the temporary file an empty log is written to before it replaces the log.protected FileOutputStreamopenLogForAppend(File file) Open the log for appending.voidpersist(long txVersion, Collection<TinkerStorageMutation<TinkerVertex>> changedVertices, Collection<TinkerStorageMutation<TinkerEdge>> changedEdges) Durably record the changeset of a committing transaction.voidreplay(TinkerStorageGraph graph) Rebuild the in-memory state of the graph from previously persisted data.protected static voidwriteFrame(DataOutputStream out, byte[] payload) Write a snapshot frame: a 4-byte big-endian payload length, a 4-byte CRC32 of the payload, then the payload.protected abstract voidwriteSnapshot(TinkerStorageGraph graph, DataOutputStream out) Write the entire current committed state of the graph tooutas framed records (viawriteFrame(java.io.DataOutputStream, byte[])), for compaction.
-
Constructor Details
-
AbstractLogStorage
public AbstractLogStorage()
-
-
Method Details
-
encodeCommit
protected abstract byte[] encodeCommit(long txVersion, Collection<TinkerStorageMutation<TinkerVertex>> changedVertices, Collection<TinkerStorageMutation<TinkerEdge>> changedEdges) throws IOException Encode a committing transaction's changeset into a single record payload (the framing is added by the caller). A failed encode fails only its own transaction and later commits are still accepted, so on failure an implementation must undo any codec state it changed while encoding, such as dictionary entries.- Throws:
IOException
-
decodeFrame
protected abstract void decodeFrame(byte[] record, Map<Object, DetachedVertex> vertices, Map<Object, throws IOExceptionDetachedEdge> edges) Decode one record payload, folding its puts and deletes into the supplied maps (last-write-wins per id).- Throws:
IOException
-
writeSnapshot
protected abstract void writeSnapshot(TinkerStorageGraph graph, DataOutputStream out) throws IOException Write the entire current committed state of the graph tooutas framed records (viawriteFrame(java.io.DataOutputStream, byte[])), for compaction. The file header has already been written toout.- Throws:
IOException
-
beginReplay
protected void beginReplay()Reset any per-replay decode state (e.g. a dictionary) before a fold begins. Default is a no-op. -
open
Description copied from interface:TinkerStoragePrepare the storage engine for use, resolving its backing location from the supplied configuration. Called once during graph construction beforeTinkerStorage.replay(TinkerStorageGraph).- Specified by:
openin interfaceTinkerStorage- Parameters:
graph- the graph that owns this engineconfig- the graph configuration, includinggremlin.tinkergraph.storage.directory
-
configureCodec
protected void configureCodec(org.apache.commons.configuration2.Configuration config) Read any codec-specific configuration. Called once duringopen(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph, org.apache.commons.configuration2.Configuration). Default is a no-op. -
replay
Description copied from interface:TinkerStorageRebuild the in-memory state of the graph from previously persisted data. Called once during graph construction. Implementations should re-apply persisted elements through the graph's own mutation API; the graph sets itsloadingguard for the duration so this does not re-persist.- Specified by:
replayin interfaceTinkerStorage- Parameters:
graph- the graph to populate
-
persist
public void persist(long txVersion, Collection<TinkerStorageMutation<TinkerVertex>> changedVertices, Collection<TinkerStorageMutation<TinkerEdge>> changedEdges) Description copied from interface:TinkerStorageDurably record the changeset of a committing transaction. Called from within the transaction commit, while the changed elements are locked, before the in-memory commit is applied. Each mutation is either a put (added or modified element) or a delete (seeTinkerStorageMutation).- Specified by:
persistin interfaceTinkerStorage- Parameters:
txVersion- the version number of the committing transactionchangedVertices- the vertex mutations in this transactionchangedEdges- the edge mutations in this transaction
-
flush
public void flush()Description copied from interface:TinkerStorageForce any buffered writes to durable storage. Called afterTinkerStorage.persist(long, Collection, Collection)as the commit's durability point.- Specified by:
flushin interfaceTinkerStorage
-
compact
Description copied from interface:TinkerStorageFold accumulated changes into a compact representation of the current committed state, reclaiming space. Safe to call at any time; typically invoked onTinkerStorage.close()or on a size/commit-count threshold.- Specified by:
compactin interfaceTinkerStorage- Parameters:
graph- the graph whose current committed state should be snapshotted
-
compact
Every compaction, explicit, automatic or forclear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph), runs through here.- Parameters:
force- rewrite the store even when nothing was logged since the last compaction, asclear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph)must, since the graph it writes then no longer matches the snapshot
-
maybeCompact
Description copied from interface:TinkerStorageCompact if the engine's accumulated data has grown past its own threshold, otherwise do nothing. Called on the commit path afterTinkerStorage.flush()(while the graph's commit lock is held) so a long-running graph that is never explicitly closed does not grow its backing storage without bound. The default is a no-op, leaving compaction entirely under the control ofTinkerStorage.compact(TinkerStorageGraph)andTinkerStorage.close().- Specified by:
maybeCompactin interfaceTinkerStorage- Parameters:
graph- the graph whose current committed state would be snapshotted
-
clear
Description copied from interface:TinkerStorageReplace everything the engine holds with the graph's current committed state, which the caller has just emptied, so that the store holds nothing and a reopen finds an empty graph. Called byTinkerGraph.clear()under the graph's commit lock, and never on a read-only engine. If the store cannot be emptied, the engine must refuse further commits until the graph is reopened: appending them to the old data would bring the cleared elements back on reopen, mixed with the new ones. The graph fail-stops the engine throughTinkerStorage.failStop(Throwable)when this throws. Rewriting the store from the emptied graph, as a compaction does, is one way to empty it, but it must then run even when nothing was written since the last compaction, since the store still holds the data the graph no longer has.- Specified by:
clearin interfaceTinkerStorage- Parameters:
graph- the graph, already emptied, whose committed state the store should now hold
-
isReadOnly
public boolean isReadOnly()Description copied from interface:TinkerStorageWhether this engine accepts no writes at all, such as when a damaged store was opened for recovery. The graph then also leaves its other files in the storage directory, such as index definitions, unchanged.- Specified by:
isReadOnlyin interfaceTinkerStorage
-
isFailed
public boolean isFailed()Description copied from interface:TinkerStorageWhether this engine has stopped accepting writes after a storage failure, such as a failed log write or the store being removed while the graph was open. It stays so until the graph is reopened. The graph's committed data remains readable in memory for export, so operations that would discard or rewrite it are refused.- Specified by:
isFailedin interfaceTinkerStorage
-
failStop
Description copied from interface:TinkerStorageStop accepting writes, as after a storage failure, until the graph is reopened. The graph calls this when its memory no longer matches the store, so that nothing written from that memory, by a commit or a compaction, may reach the store: when a transaction's changes were persisted and flushed but could not then be applied in memory, so the transaction is durable and returns on the next open, and whenTinkerStorage.clear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph)fails after the graph was emptied in memory.TinkerStorage.isFailed()reportstrueafterwards.- Specified by:
failStopin interfaceTinkerStorage- Parameters:
cause- why the graph's memory no longer matches the store
-
close
public void close()Description copied from interface:TinkerStorageFlush and release resources. Does not delete persisted data.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceTinkerStorage
-
openLogForAppend
Open the log for appending. Exists so tests can substitute a stream that fails on demand.- Throws:
IOException
-
openEmptyLog
Open the temporary file an empty log is written to before it replaces the log. A seam for tests that fail installing a log, as foropenLogForAppend(java.io.File).- Throws:
IOException
-
writeFrame
Write a snapshot frame: a 4-byte big-endian payload length, a 4-byte CRC32 of the payload, then the payload. The checksum lets a reader tell a bit-flip inside a complete frame from an incomplete one. Available to codec subclasses writing per-element snapshot frames; log frames are written bywriteLogFrame(java.io.DataOutputStream, int, byte[]).- Throws:
IOException
-