2021-10-20 01:51:34 +02:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
|
|
|
import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
|
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
2022-10-02 03:09:50 +02:00
|
|
|
import io.netty5.buffer.Buffer;
|
|
|
|
import io.netty5.buffer.BufferAllocator;
|
|
|
|
import io.netty5.buffer.MemoryManager;
|
2022-07-15 02:44:50 +02:00
|
|
|
import io.netty5.util.Send;
|
2021-10-20 01:51:34 +02:00
|
|
|
import it.cavallium.dbengine.database.LLDelta;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import java.io.IOException;
|
2022-04-28 23:23:26 +02:00
|
|
|
import java.util.concurrent.locks.Lock;
|
2022-04-30 01:49:44 +02:00
|
|
|
import java.util.concurrent.locks.StampedLock;
|
2021-10-20 01:51:34 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.rocksdb.ColumnFamilyHandle;
|
|
|
|
import org.rocksdb.ReadOptions;
|
|
|
|
import org.rocksdb.RocksDBException;
|
|
|
|
import org.rocksdb.Transaction;
|
|
|
|
import org.rocksdb.TransactionDB;
|
|
|
|
import org.rocksdb.TransactionOptions;
|
|
|
|
import org.rocksdb.WriteOptions;
|
|
|
|
import reactor.core.scheduler.Schedulers;
|
|
|
|
|
|
|
|
public final class PessimisticRocksDBColumn extends AbstractRocksDBColumn<TransactionDB> {
|
|
|
|
|
|
|
|
private static final TransactionOptions DEFAULT_TX_OPTIONS = new TransactionOptions();
|
|
|
|
|
|
|
|
public PessimisticRocksDBColumn(TransactionDB db,
|
2022-04-06 02:41:32 +02:00
|
|
|
boolean nettyDirect,
|
2021-10-20 01:51:34 +02:00
|
|
|
BufferAllocator alloc,
|
2022-03-30 15:15:53 +02:00
|
|
|
String dbName,
|
2022-05-20 10:20:00 +02:00
|
|
|
ColumnFamilyHandle cfh,
|
2022-04-28 23:23:26 +02:00
|
|
|
MeterRegistry meterRegistry,
|
2022-04-30 01:49:44 +02:00
|
|
|
StampedLock closeLock) {
|
|
|
|
super(db, nettyDirect, alloc, dbName, cfh, meterRegistry, closeLock);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-05-20 10:20:00 +02:00
|
|
|
protected boolean commitOptimistically(Transaction tx) throws RocksDBException {
|
|
|
|
tx.commit();
|
2021-10-20 01:51:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-05-20 10:20:00 +02:00
|
|
|
protected Transaction beginTransaction(@NotNull WriteOptions writeOptions,
|
|
|
|
TransactionOptions txOpts) {
|
|
|
|
return getDb().beginTransaction(writeOptions, txOpts);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-05-20 10:20:00 +02:00
|
|
|
public @NotNull UpdateAtomicResult updateAtomicImpl(@NotNull ReadOptions readOptions,
|
|
|
|
@NotNull WriteOptions writeOptions,
|
2022-04-01 01:30:56 +02:00
|
|
|
Buffer key,
|
|
|
|
BinarySerializationFunction updater,
|
2022-03-30 15:15:53 +02:00
|
|
|
UpdateAtomicResultMode returnMode) throws IOException {
|
2022-04-01 01:30:56 +02:00
|
|
|
long initNanoTime = System.nanoTime();
|
|
|
|
try {
|
|
|
|
var cfh = getCfh();
|
|
|
|
var keyArray = LLUtils.toArray(key);
|
|
|
|
if (Schedulers.isInNonBlockingThread()) {
|
|
|
|
throw new UnsupportedOperationException("Called update in a nonblocking thread");
|
|
|
|
}
|
2022-05-20 10:20:00 +02:00
|
|
|
try (var txOpts = new TransactionOptions();
|
2022-05-12 19:14:27 +02:00
|
|
|
var tx = beginTransaction(writeOptions, txOpts)) {
|
2022-05-20 23:59:56 +02:00
|
|
|
Buffer prevData = null;
|
|
|
|
Buffer newData = null;
|
2022-04-01 01:30:56 +02:00
|
|
|
try {
|
2022-05-20 23:59:56 +02:00
|
|
|
boolean changed;
|
2021-12-29 00:31:35 +01:00
|
|
|
if (logger.isTraceEnabled()) {
|
2022-05-20 23:59:56 +02:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Reading {} (before update lock)", LLUtils.toStringSafe(key));
|
2022-04-01 01:30:56 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
var prevDataArray = tx.getForUpdate(readOptions, cfh, keyArray, true);
|
|
|
|
try {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Reading {}: {} (before update)",
|
|
|
|
LLUtils.toStringSafe(key),
|
|
|
|
LLUtils.toStringSafe(prevDataArray)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (prevDataArray != null) {
|
|
|
|
readValueFoundWithoutBloomBufferSize.record(prevDataArray.length);
|
|
|
|
prevData = MemoryManager.unsafeWrap(prevDataArray);
|
|
|
|
} else {
|
|
|
|
readValueNotFoundWithoutBloomBufferSize.record(0);
|
|
|
|
}
|
2022-04-01 01:30:56 +02:00
|
|
|
Buffer prevDataToSendToUpdater;
|
|
|
|
if (prevData != null) {
|
|
|
|
prevDataToSendToUpdater = prevData.copy().makeReadOnly();
|
2021-12-29 00:31:35 +01:00
|
|
|
} else {
|
2022-04-01 01:30:56 +02:00
|
|
|
prevDataToSendToUpdater = null;
|
2021-12-29 00:31:35 +01:00
|
|
|
}
|
2021-10-20 01:51:34 +02:00
|
|
|
|
2022-05-20 18:31:05 +02:00
|
|
|
try {
|
|
|
|
newData = updater.apply(prevDataToSendToUpdater);
|
|
|
|
} finally {
|
|
|
|
if (prevDataToSendToUpdater != null && prevDataToSendToUpdater.isAccessible()) {
|
|
|
|
prevDataToSendToUpdater.close();
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
var newDataArray = newData == null ? null : LLUtils.toArray(newData);
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Updating {}. previous data: {}, updated data: {}",
|
|
|
|
LLUtils.toStringSafe(key),
|
|
|
|
LLUtils.toStringSafe(prevDataArray),
|
|
|
|
LLUtils.toStringSafe(newDataArray)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (prevData != null && newData == null) {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Deleting {} (after update)", LLUtils.toStringSafe(key));
|
|
|
|
}
|
|
|
|
writeValueBufferSize.record(0);
|
|
|
|
tx.delete(cfh, keyArray, true);
|
|
|
|
changed = true;
|
|
|
|
tx.commit();
|
|
|
|
} else if (newData != null && (prevData == null || !LLUtils.equals(prevData, newData))) {
|
2022-04-01 01:30:56 +02:00
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
2022-05-20 23:59:56 +02:00
|
|
|
"Writing {}: {} (after update)",
|
2022-04-01 01:30:56 +02:00
|
|
|
LLUtils.toStringSafe(key),
|
2022-05-20 23:59:56 +02:00
|
|
|
LLUtils.toStringSafe(newData)
|
2022-04-01 01:30:56 +02:00
|
|
|
);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
writeValueBufferSize.record(newDataArray.length);
|
|
|
|
tx.put(cfh, keyArray, newDataArray);
|
|
|
|
changed = true;
|
|
|
|
tx.commit();
|
|
|
|
} else {
|
|
|
|
changed = false;
|
|
|
|
tx.rollback();
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
} finally {
|
|
|
|
tx.undoGetForUpdate(cfh, keyArray);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
recordAtomicUpdateTime(changed, prevData != null, newData != null, initNanoTime);
|
|
|
|
return switch (returnMode) {
|
|
|
|
case NOTHING -> {
|
|
|
|
if (prevData != null) {
|
|
|
|
prevData.close();
|
|
|
|
}
|
|
|
|
if (newData != null) {
|
|
|
|
newData.close();
|
|
|
|
}
|
|
|
|
yield RESULT_NOTHING;
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
case CURRENT -> {
|
|
|
|
if (prevData != null) {
|
|
|
|
prevData.close();
|
|
|
|
}
|
|
|
|
yield new UpdateAtomicResultCurrent(newData);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
case PREVIOUS -> {
|
|
|
|
if (newData != null) {
|
|
|
|
newData.close();
|
|
|
|
}
|
|
|
|
yield new UpdateAtomicResultPrevious(prevData);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
case BINARY_CHANGED -> {
|
|
|
|
if (prevData != null) {
|
|
|
|
prevData.close();
|
|
|
|
}
|
|
|
|
if (newData != null) {
|
|
|
|
newData.close();
|
|
|
|
}
|
|
|
|
yield new UpdateAtomicResultBinaryChanged(changed);
|
2022-04-01 01:30:56 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
case DELTA -> new UpdateAtomicResultDelta(LLDelta.of(prevData, newData));
|
|
|
|
};
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
if (prevData != null && prevData.isAccessible()) {
|
|
|
|
prevData.close();
|
2022-04-01 01:30:56 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
if (newData != null && newData.isAccessible()) {
|
|
|
|
newData.close();
|
2022-05-20 18:31:05 +02:00
|
|
|
}
|
2022-05-20 23:59:56 +02:00
|
|
|
throw ex;
|
|
|
|
}
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
2022-04-01 01:30:56 +02:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
throw new IOException("Failed to update key " + LLUtils.toStringSafe(key), ex);
|
2021-10-20 01:51:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsTransactions() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|