use of org.elasticsearch.common.util.concurrent.ReleasableLock in project crate by crate.
the class Translog method getMaxSeqNo.
/**
* Returns the max seq_no of translog operations found in this translog. Since this value is calculated based on the current
* existing readers, this value is not necessary to be the max seq_no of all operations have been stored in this translog.
*/
public long getMaxSeqNo() {
try (ReleasableLock ignored = readLock.acquire()) {
ensureOpen();
final OptionalLong maxSeqNo = Stream.concat(readers.stream(), Stream.of(current)).mapToLong(reader -> reader.getCheckpoint().maxSeqNo).max();
assert maxSeqNo.isPresent() : "must have at least one translog generation";
return maxSeqNo.getAsLong();
}
}
use of org.elasticsearch.common.util.concurrent.ReleasableLock in project crate by crate.
the class Translog method add.
/**
* Adds an operation to the transaction log.
*
* @param operation the operation to add
* @return the location of the operation in the translog
* @throws IOException if adding the operation to the translog resulted in an I/O exception
*/
public Location add(final Operation operation) throws IOException {
final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(bigArrays);
boolean successfullySerialized = false;
try {
final long start = out.position();
out.skip(Integer.BYTES);
writeOperationNoSize(new BufferedChecksumStreamOutput(out), operation);
final long end = out.position();
final int operationSize = (int) (end - Integer.BYTES - start);
out.seek(start);
out.writeInt(operationSize);
out.seek(end);
successfullySerialized = true;
try (ReleasableBytesReference bytes = new ReleasableBytesReference(out.bytes(), out);
ReleasableLock ignored = readLock.acquire()) {
ensureOpen();
if (operation.primaryTerm() > current.getPrimaryTerm()) {
assert false : "Operation term is newer than the current term; " + "current term[" + current.getPrimaryTerm() + "], operation term[" + operation + "]";
throw new IllegalArgumentException("Operation term is newer than the current term; " + "current term[" + current.getPrimaryTerm() + "], operation term[" + operation + "]");
}
return current.add(bytes, operation.seqNo());
}
} catch (final AlreadyClosedException | IOException ex) {
closeOnTragicEvent(ex);
throw ex;
} catch (final Exception ex) {
closeOnTragicEvent(ex);
throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", ex);
} finally {
if (successfullySerialized == false) {
Releasables.close(out);
}
}
}
use of org.elasticsearch.common.util.concurrent.ReleasableLock in project crate by crate.
the class TranslogWriter method syncUpTo.
/**
* Syncs the translog up to at least the given offset unless already synced
*
* @return <code>true</code> if this call caused an actual sync operation
*/
public boolean syncUpTo(long offset) throws IOException {
if (lastSyncedCheckpoint.offset < offset && syncNeeded()) {
synchronized (syncLock) {
// only one sync/checkpoint should happen concurrently but we wait
if (lastSyncedCheckpoint.offset < offset && syncNeeded()) {
// double checked locking - we don't want to fsync unless we have to and now that we have
// the lock we should check again since if this code is busy we might have fsynced enough already
final Checkpoint checkpointToSync;
final LongArrayList flushedSequenceNumbers;
final ArrayDeque<ReleasableBytesReference> toWrite;
try (ReleasableLock toClose = writeLock.acquire()) {
synchronized (this) {
ensureOpen();
checkpointToSync = getCheckpoint();
toWrite = pollOpsToWrite();
flushedSequenceNumbers = nonFsyncedSequenceNumbers;
nonFsyncedSequenceNumbers = new LongArrayList(64);
}
try {
// Write ops will release operations.
writeAndReleaseOps(toWrite);
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
}
// we can continue writing to the buffer etc.
try {
channel.force(false);
writeCheckpoint(checkpointChannel, checkpointPath, checkpointToSync);
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
flushedSequenceNumbers.forEach((LongProcedure) persistedSequenceNumberConsumer::accept);
assert lastSyncedCheckpoint.offset <= checkpointToSync.offset : "illegal state: " + lastSyncedCheckpoint.offset + " <= " + checkpointToSync.offset;
// write protected by syncLock
lastSyncedCheckpoint = checkpointToSync;
return true;
}
}
}
return false;
}
Aggregations