Package org.elasticsearch.index.translog
Class TranslogWriter
- java.lang.Object
-
- org.elasticsearch.index.translog.BaseTranslogReader
-
- org.elasticsearch.index.translog.TranslogWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Comparable<BaseTranslogReader>
public class TranslogWriter extends BaseTranslogReader implements java.io.Closeable
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.atomic.AtomicBooleanclosed-
Fields inherited from class org.elasticsearch.index.translog.BaseTranslogReader
channel, generation, header, path
-
-
Method Summary
Modifier and Type Method Description Translog.Locationadd(BytesReference data, long seqNo)Add the given bytes to the translog with the specified sequence number; returns the location the bytes were written to.voidclose()TranslogReadercloseIntoReader()Closes this writer and transfers its underlying file channel to a new immutableTranslogReaderstatic TranslogWritercreate(ShardId shardId, java.lang.String translogUUID, long fileGeneration, java.nio.file.Path file, ChannelFactory channelFactory, ByteSizeValue bufferSize, long initialMinTranslogGen, long initialGlobalCheckpoint, java.util.function.LongSupplier globalCheckpointSupplier, java.util.function.LongSupplier minTranslogGenerationSupplier, long primaryTerm, TragicExceptionHolder tragedy)protected voidensureOpen()protected booleanisClosed()org.elasticsearch.index.translog.TranslogSnapshotnewSnapshot()protected voidreadBytes(java.nio.ByteBuffer targetBuffer, long position)reads bytes at position into the given buffer, filling it.longsizeInBytes()voidsync()write all buffered ops to disk and fsync file.booleansyncNeeded()Returnstrueif there are buffered operations that have not been flushed and fsynced to disk or if the latest global checkpoint has not yet been fsyncedbooleansyncUpTo(long offset)Syncs the translog up to at least the given offset unless already syncedinttotalOperations()-
Methods inherited from class org.elasticsearch.index.translog.BaseTranslogReader
checksummedStream, compareTo, getFirstOperationOffset, getGeneration, getLastModifiedTime, getPrimaryTerm, path, read, readSize, toString
-
-
-
-
Method Detail
-
create
public static TranslogWriter create(ShardId shardId, java.lang.String translogUUID, long fileGeneration, java.nio.file.Path file, ChannelFactory channelFactory, ByteSizeValue bufferSize, long initialMinTranslogGen, long initialGlobalCheckpoint, java.util.function.LongSupplier globalCheckpointSupplier, java.util.function.LongSupplier minTranslogGenerationSupplier, long primaryTerm, TragicExceptionHolder tragedy) throws java.io.IOException
- Throws:
java.io.IOException
-
add
public Translog.Location add(BytesReference data, long seqNo) throws java.io.IOException
Add the given bytes to the translog with the specified sequence number; returns the location the bytes were written to.- Parameters:
data- the bytes to writeseqNo- the sequence number associated with the operation- Returns:
- the location the bytes were written to
- Throws:
java.io.IOException- if writing to the translog resulted in an I/O exception
-
sync
public void sync() throws java.io.IOExceptionwrite all buffered ops to disk and fsync file. Note: any exception during the sync process will be interpreted as a tragic exception and the writer will be closed before raising the exception.- Throws:
java.io.IOException
-
syncNeeded
public boolean syncNeeded()
Returnstrueif there are buffered operations that have not been flushed and fsynced to disk or if the latest global checkpoint has not yet been fsynced
-
totalOperations
public int totalOperations()
- Specified by:
totalOperationsin classBaseTranslogReader
-
sizeInBytes
public long sizeInBytes()
- Specified by:
sizeInBytesin classBaseTranslogReader
-
closeIntoReader
public TranslogReader closeIntoReader() throws java.io.IOException
Closes this writer and transfers its underlying file channel to a new immutableTranslogReader- Returns:
- a new
TranslogReader - Throws:
java.io.IOException- if any of the file operations resulted in an I/O exception
-
newSnapshot
public org.elasticsearch.index.translog.TranslogSnapshot newSnapshot()
- Overrides:
newSnapshotin classBaseTranslogReader
-
syncUpTo
public boolean syncUpTo(long offset) throws java.io.IOExceptionSyncs the translog up to at least the given offset unless already synced- Returns:
trueif this call caused an actual sync operation- Throws:
java.io.IOException
-
readBytes
protected void readBytes(java.nio.ByteBuffer targetBuffer, long position) throws java.io.IOExceptionDescription copied from class:BaseTranslogReaderreads bytes at position into the given buffer, filling it.- Specified by:
readBytesin classBaseTranslogReader- Throws:
java.io.IOException
-
ensureOpen
protected final void ensureOpen()
-
close
public final void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
isClosed
protected final boolean isClosed()
-
-