2020-12-07 22:15:18 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-05-03 21:41:51 +02:00
|
|
|
import io.netty.buffer.ByteBufAllocator;
|
2021-01-30 00:24:55 +01:00
|
|
|
import it.cavallium.dbengine.database.Column;
|
|
|
|
import it.cavallium.dbengine.database.LLKeyValueDatabase;
|
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
2021-02-13 01:31:24 +01:00
|
|
|
import it.cavallium.dbengine.database.UpdateMode;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2021-03-21 13:06:54 +01:00
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2021-03-21 13:06:54 +01:00
|
|
|
import org.apache.commons.lang3.time.StopWatch;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.rocksdb.BlockBasedTableConfig;
|
|
|
|
import org.rocksdb.BloomFilter;
|
|
|
|
import org.rocksdb.ColumnFamilyDescriptor;
|
|
|
|
import org.rocksdb.ColumnFamilyHandle;
|
2021-03-19 20:55:38 +01:00
|
|
|
import org.rocksdb.CompactRangeOptions;
|
2021-05-04 01:21:29 +02:00
|
|
|
import org.rocksdb.CompactionPriority;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.rocksdb.CompactionStyle;
|
|
|
|
import org.rocksdb.CompressionType;
|
|
|
|
import org.rocksdb.DBOptions;
|
|
|
|
import org.rocksdb.DbPath;
|
|
|
|
import org.rocksdb.FlushOptions;
|
2021-03-20 12:41:11 +01:00
|
|
|
import org.rocksdb.LRUCache;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.rocksdb.Options;
|
2021-05-11 21:59:05 +02:00
|
|
|
import org.rocksdb.RateLimiter;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.rocksdb.RocksDB;
|
|
|
|
import org.rocksdb.RocksDBException;
|
|
|
|
import org.rocksdb.Snapshot;
|
2021-05-04 01:21:29 +02:00
|
|
|
import org.rocksdb.TableFormatConfig;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.rocksdb.WALRecoveryMode;
|
2021-03-20 12:41:11 +01:00
|
|
|
import org.rocksdb.WriteBufferManager;
|
2021-03-19 20:55:38 +01:00
|
|
|
import org.warp.commonutils.log.Logger;
|
|
|
|
import org.warp.commonutils.log.LoggerFactory;
|
2021-01-30 01:42:37 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
2021-02-01 02:21:53 +01:00
|
|
|
import reactor.core.scheduler.Scheduler;
|
2021-01-30 01:42:37 +01:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
|
|
|
|
|
|
|
|
static {
|
|
|
|
RocksDB.loadLibrary();
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:55:38 +01:00
|
|
|
protected static final Logger logger = LoggerFactory.getLogger(LLLocalKeyValueDatabase.class);
|
2020-12-07 22:15:18 +01:00
|
|
|
private static final ColumnFamilyDescriptor DEFAULT_COLUMN_FAMILY = new ColumnFamilyDescriptor(
|
|
|
|
RocksDB.DEFAULT_COLUMN_FAMILY);
|
|
|
|
|
2021-05-03 21:41:51 +02:00
|
|
|
private final ByteBufAllocator allocator;
|
2021-02-01 02:21:53 +01:00
|
|
|
private final Scheduler dbScheduler;
|
2020-12-07 22:15:18 +01:00
|
|
|
private final Path dbPath;
|
2021-04-30 19:15:04 +02:00
|
|
|
private final boolean inMemory;
|
2020-12-07 22:15:18 +01:00
|
|
|
private final String name;
|
|
|
|
private RocksDB db;
|
|
|
|
private final Map<Column, ColumnFamilyHandle> handles;
|
|
|
|
private final ConcurrentHashMap<Long, Snapshot> snapshotsHandles = new ConcurrentHashMap<>();
|
|
|
|
private final AtomicLong nextSnapshotNumbers = new AtomicLong(1);
|
|
|
|
|
2021-05-03 21:41:51 +02:00
|
|
|
public LLLocalKeyValueDatabase(ByteBufAllocator allocator,
|
|
|
|
String name,
|
|
|
|
Path path,
|
|
|
|
List<Column> columns,
|
|
|
|
List<ColumnFamilyHandle> handles,
|
|
|
|
boolean crashIfWalError,
|
|
|
|
boolean lowMemory,
|
|
|
|
boolean inMemory) throws IOException {
|
|
|
|
this.allocator = allocator;
|
2020-12-07 22:15:18 +01:00
|
|
|
Options options = openRocksDb(path, crashIfWalError, lowMemory);
|
|
|
|
try {
|
|
|
|
List<ColumnFamilyDescriptor> descriptors = new LinkedList<>();
|
2021-01-31 12:43:28 +01:00
|
|
|
descriptors
|
|
|
|
.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
|
2020-12-07 22:15:18 +01:00
|
|
|
for (Column column : columns) {
|
|
|
|
descriptors
|
|
|
|
.add(new ColumnFamilyDescriptor(column.getName().getBytes(StandardCharsets.US_ASCII)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get databases directory path
|
|
|
|
Path databasesDirPath = path.toAbsolutePath().getParent();
|
|
|
|
|
|
|
|
String dbPathString = databasesDirPath.toString() + File.separatorChar + path.getFileName();
|
|
|
|
Path dbPath = Paths.get(dbPathString);
|
|
|
|
this.dbPath = dbPath;
|
2021-04-30 19:15:04 +02:00
|
|
|
this.inMemory = inMemory;
|
2020-12-07 22:15:18 +01:00
|
|
|
this.name = name;
|
2021-04-15 00:00:42 +02:00
|
|
|
this.dbScheduler = Schedulers.newBoundedElastic(lowMemory ? Runtime.getRuntime().availableProcessors()
|
|
|
|
: Math.max(8, Runtime.getRuntime().availableProcessors()),
|
|
|
|
Schedulers.DEFAULT_BOUNDED_ELASTIC_QUEUESIZE,
|
|
|
|
"db-" + name,
|
|
|
|
60,
|
|
|
|
true
|
|
|
|
);
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
createIfNotExists(descriptors, options, inMemory, this.dbPath, dbPathString);
|
2020-12-07 22:15:18 +01:00
|
|
|
// Create all column families that don't exist
|
2021-04-30 19:15:04 +02:00
|
|
|
createAllColumns(descriptors, options, inMemory, dbPathString);
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
// a factory method that returns a RocksDB instance
|
2021-04-30 19:15:04 +02:00
|
|
|
this.db = RocksDB.open(new DBOptions(options),
|
|
|
|
dbPathString,
|
|
|
|
inMemory ? List.of(DEFAULT_COLUMN_FAMILY) : descriptors,
|
|
|
|
handles
|
|
|
|
);
|
|
|
|
createInMemoryColumns(descriptors, inMemory, handles);
|
2020-12-07 22:15:18 +01:00
|
|
|
this.handles = new HashMap<>();
|
|
|
|
for (int i = 0; i < columns.size(); i++) {
|
|
|
|
this.handles.put(columns.get(i), handles.get(i));
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:06:54 +01:00
|
|
|
// compactDb(db, handles);
|
2020-12-07 22:15:18 +01:00
|
|
|
flushDb(db, handles);
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
throw new IOException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDatabaseName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void flushAndCloseDb(RocksDB db, List<ColumnFamilyHandle> handles)
|
|
|
|
throws RocksDBException {
|
|
|
|
flushDb(db, handles);
|
|
|
|
|
|
|
|
for (ColumnFamilyHandle handle : handles) {
|
|
|
|
handle.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
db.closeE();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void flushDb(RocksDB db, List<ColumnFamilyHandle> handles) throws RocksDBException {
|
|
|
|
// force flush the database
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
db.flush(new FlushOptions().setWaitForFlush(true).setAllowWriteStall(true), handles);
|
|
|
|
db.flushWal(true);
|
|
|
|
db.syncWal();
|
|
|
|
}
|
|
|
|
// end force flush
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:09:06 +02:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private void compactDb(RocksDB db, List<ColumnFamilyHandle> handles) {
|
2021-03-19 20:55:38 +01:00
|
|
|
// force compact the database
|
|
|
|
for (ColumnFamilyHandle cfh : handles) {
|
|
|
|
var t = new Thread(() -> {
|
2021-03-21 13:06:54 +01:00
|
|
|
int r = ThreadLocalRandom.current().nextInt();
|
|
|
|
var s = StopWatch.createStarted();
|
2021-03-19 20:55:38 +01:00
|
|
|
try {
|
|
|
|
// Range rangeToCompact = db.suggestCompactRange(cfh);
|
2021-03-21 13:06:54 +01:00
|
|
|
logger.info("Compacting range {}", r);
|
|
|
|
db.compactRange(cfh, null, null, new CompactRangeOptions()
|
|
|
|
.setAllowWriteStall(true)
|
|
|
|
.setExclusiveManualCompaction(true)
|
|
|
|
.setChangeLevel(false));
|
2021-03-19 20:55:38 +01:00
|
|
|
} catch (RocksDBException e) {
|
|
|
|
if ("Database shutdown".equalsIgnoreCase(e.getMessage())) {
|
|
|
|
logger.warn("Compaction cancelled: database shutdown");
|
|
|
|
} else {
|
|
|
|
logger.warn("Failed to compact range", e);
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 13:06:54 +01:00
|
|
|
logger.info("Compacted range {} in {} milliseconds", r, s.getTime(TimeUnit.MILLISECONDS));
|
2021-03-19 20:55:38 +01:00
|
|
|
}, "Compaction");
|
|
|
|
t.setDaemon(true);
|
|
|
|
t.start();
|
|
|
|
}
|
|
|
|
// end force compact
|
|
|
|
}
|
|
|
|
|
2021-05-04 01:21:29 +02:00
|
|
|
@SuppressWarnings({"CommentedOutCode", "PointlessArithmeticExpression"})
|
2020-12-07 22:15:18 +01:00
|
|
|
private static Options openRocksDb(Path path, boolean crashIfWalError, boolean lowMemory)
|
|
|
|
throws IOException {
|
|
|
|
// Get databases directory path
|
|
|
|
Path databasesDirPath = path.toAbsolutePath().getParent();
|
|
|
|
// Create base directories
|
|
|
|
if (Files.notExists(databasesDirPath)) {
|
|
|
|
Files.createDirectories(databasesDirPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the Options class contains a set of configurable DB options
|
|
|
|
// that determines the behaviour of the database.
|
|
|
|
var options = new Options();
|
|
|
|
options.setCreateIfMissing(true);
|
|
|
|
options.setCompactionStyle(CompactionStyle.LEVEL);
|
|
|
|
options.setLevelCompactionDynamicLevelBytes(true);
|
|
|
|
options.setTargetFileSizeBase(64 * 1024 * 1024); // 64MiB sst file
|
2021-05-11 21:59:05 +02:00
|
|
|
options.setTargetFileSizeMultiplier(2); // Each level is 2 times the previous level
|
|
|
|
options.setCompressionPerLevel(List.of(CompressionType.NO_COMPRESSION,
|
|
|
|
CompressionType.SNAPPY_COMPRESSION,
|
|
|
|
CompressionType.SNAPPY_COMPRESSION
|
|
|
|
));
|
|
|
|
//options.setMaxBytesForLevelBase(4 * 256 * 1024 * 1024); // 4 times the sst file
|
2020-12-07 22:15:18 +01:00
|
|
|
options.setManualWalFlush(false);
|
|
|
|
options.setMinWriteBufferNumberToMerge(3);
|
|
|
|
options.setMaxWriteBufferNumber(4);
|
|
|
|
options.setWalTtlSeconds(30); // flush wal after 30 seconds
|
|
|
|
options.setAvoidFlushDuringShutdown(false); // Flush all WALs during shutdown
|
|
|
|
options.setAvoidFlushDuringRecovery(false); // Flush all WALs during startup
|
2021-02-01 11:00:27 +01:00
|
|
|
options.setWalRecoveryMode(crashIfWalError
|
|
|
|
? WALRecoveryMode.AbsoluteConsistency
|
|
|
|
: WALRecoveryMode.PointInTimeRecovery); // Crash if the WALs are corrupted.Default: TolerateCorruptedTailRecords
|
2020-12-07 22:15:18 +01:00
|
|
|
options.setDeleteObsoleteFilesPeriodMicros(20 * 1000000); // 20 seconds
|
|
|
|
options.setPreserveDeletes(false);
|
|
|
|
options.setKeepLogFileNum(10);
|
2021-03-20 12:41:11 +01:00
|
|
|
options.setAllowMmapReads(true);
|
|
|
|
options.setAllowMmapWrites(true);
|
|
|
|
options.setAllowFAllocate(true);
|
2021-05-11 21:59:05 +02:00
|
|
|
options.setRateLimiter(new RateLimiter(10L * 1024L * 1024L)); // 10MiB/s max compaction write speed
|
|
|
|
options.setDbPaths(List.of(new DbPath(databasesDirPath.resolve(path.getFileName() + "_hot"),
|
|
|
|
50L * 1024L * 1024L * 1024L), // 50GiB
|
|
|
|
new DbPath(databasesDirPath.resolve(path.getFileName() + "_cold"),
|
|
|
|
400L * 1024L * 1024L * 1024L), // 400GiB
|
|
|
|
new DbPath(databasesDirPath.resolve(path.getFileName() + "_colder"),
|
|
|
|
600L * 1024L * 1024L * 1024L))); // 600GiB
|
2020-12-07 22:15:18 +01:00
|
|
|
// Direct I/O parameters. Removed because they use too much disk.
|
|
|
|
//options.setUseDirectReads(true);
|
|
|
|
//options.setUseDirectIoForFlushAndCompaction(true);
|
|
|
|
//options.setWritableFileMaxBufferSize(1024 * 1024); // 1MB by default
|
2021-03-22 20:02:19 +01:00
|
|
|
//options.setCompactionReadaheadSize(2 * 1024 * 1024); // recommend at least 2MB
|
2021-03-20 12:41:11 +01:00
|
|
|
final BlockBasedTableConfig tableOptions = new BlockBasedTableConfig();
|
2020-12-07 22:15:18 +01:00
|
|
|
if (lowMemory) {
|
|
|
|
// LOW MEMORY
|
|
|
|
options
|
2021-05-04 01:21:29 +02:00
|
|
|
.setBytesPerSync(512 * 1024) // 512KiB
|
2020-12-07 22:15:18 +01:00
|
|
|
.setWalBytesPerSync(1024 * 1024)
|
|
|
|
.setIncreaseParallelism(1)
|
2021-05-05 15:16:32 +02:00
|
|
|
.setMaxOpenFiles(15)
|
2020-12-07 22:15:18 +01:00
|
|
|
.optimizeLevelStyleCompaction(1024 * 1024) // 1MiB of ram will be used for level style compaction
|
|
|
|
.setWriteBufferSize(1024 * 1024) // 1MB
|
|
|
|
.setWalSizeLimitMB(16) // 16MB
|
|
|
|
.setMaxTotalWalSize(1024L * 1024L * 1024L) // 1GiB max wal directory size
|
|
|
|
;
|
2021-03-20 12:41:11 +01:00
|
|
|
tableOptions.setBlockCache(new LRUCache(8L * 1024L * 1024L)); // 8MiB
|
|
|
|
options.setWriteBufferManager(new WriteBufferManager(8L * 1024L * 1024L, new LRUCache(8L * 1024L * 1024L))); // 8MiB
|
2020-12-07 22:15:18 +01:00
|
|
|
} else {
|
|
|
|
// HIGH MEMORY
|
|
|
|
options
|
|
|
|
.setAllowConcurrentMemtableWrite(true)
|
|
|
|
.setEnableWriteThreadAdaptiveYield(true)
|
|
|
|
.setIncreaseParallelism(Runtime.getRuntime().availableProcessors())
|
2021-05-04 01:21:29 +02:00
|
|
|
.setBytesPerSync(1 * 1024 * 1024) // 1MiB
|
2020-12-07 22:15:18 +01:00
|
|
|
.setWalBytesPerSync(10 * 1024 * 1024)
|
2021-05-05 15:16:32 +02:00
|
|
|
.setMaxOpenFiles(150)
|
2020-12-07 22:15:18 +01:00
|
|
|
.optimizeLevelStyleCompaction(
|
|
|
|
128 * 1024 * 1024) // 128MiB of ram will be used for level style compaction
|
2021-03-20 12:41:11 +01:00
|
|
|
.setWriteBufferSize(64 * 1024 * 1024) // 64MB
|
2020-12-07 22:15:18 +01:00
|
|
|
.setWalSizeLimitMB(1024) // 1024MB
|
2021-03-20 12:41:11 +01:00
|
|
|
.setMaxTotalWalSize(2L * 1024L * 1024L * 1024L) // 2GiB max wal directory size
|
2020-12-07 22:15:18 +01:00
|
|
|
;
|
2021-05-04 01:21:29 +02:00
|
|
|
tableOptions.setBlockCache(new LRUCache(128L * 1024L * 1024L)); // 128MiB
|
|
|
|
options.setWriteBufferManager(new WriteBufferManager(256L * 1024L * 1024L, new LRUCache(128L * 1024L * 1024L))); // 128MiB
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 10:57:16 +01:00
|
|
|
final BloomFilter bloomFilter = new BloomFilter(10, false);
|
2021-05-04 01:21:29 +02:00
|
|
|
tableOptions.setOptimizeFiltersForMemory(true);
|
2020-12-07 22:15:18 +01:00
|
|
|
tableOptions.setFilterPolicy(bloomFilter);
|
2021-05-04 01:21:29 +02:00
|
|
|
tableOptions.setBlockSize(16 * 1024); // 16MiB
|
|
|
|
tableOptions.setCacheIndexAndFilterBlocks(true);
|
|
|
|
tableOptions.setPinL0FilterAndIndexBlocksInCache(true);
|
2020-12-07 22:15:18 +01:00
|
|
|
options.setTableFormatConfig(tableOptions);
|
2021-05-04 01:21:29 +02:00
|
|
|
options.setCompactionPriority(CompactionPriority.MinOverlappingRatio);
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
private void createAllColumns(List<ColumnFamilyDescriptor> totalDescriptors, Options options, boolean inMemory, String dbPathString) throws RocksDBException {
|
|
|
|
if (inMemory) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-07 22:15:18 +01:00
|
|
|
List<byte[]> columnFamiliesToCreate = new LinkedList<>();
|
|
|
|
|
|
|
|
for (ColumnFamilyDescriptor descriptor : totalDescriptors) {
|
|
|
|
columnFamiliesToCreate.add(descriptor.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<byte[]> existingColumnFamilies = RocksDB.listColumnFamilies(options, dbPathString);
|
|
|
|
|
|
|
|
columnFamiliesToCreate.removeIf((columnFamilyName) -> {
|
|
|
|
for (byte[] cfn : existingColumnFamilies) {
|
|
|
|
if (Arrays.equals(cfn, columnFamilyName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> descriptors = new LinkedList<>();
|
2021-01-31 12:43:28 +01:00
|
|
|
descriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
|
2020-12-07 22:15:18 +01:00
|
|
|
for (byte[] existingColumnFamily : existingColumnFamilies) {
|
|
|
|
descriptors.add(new ColumnFamilyDescriptor(existingColumnFamily));
|
|
|
|
}
|
|
|
|
|
|
|
|
var handles = new LinkedList<ColumnFamilyHandle>();
|
|
|
|
|
2021-01-30 00:24:55 +01:00
|
|
|
/*
|
|
|
|
SkipStatsUpdateOnDbOpen = true because this RocksDB.open session is used only to add just some columns
|
2020-12-07 22:15:18 +01:00
|
|
|
*/
|
|
|
|
//var dbOptionsFastLoadSlowEdit = new DBOptions(options.setSkipStatsUpdateOnDbOpen(true));
|
|
|
|
|
|
|
|
this.db = RocksDB.open(new DBOptions(options), dbPathString, descriptors, handles);
|
|
|
|
|
|
|
|
for (byte[] name : columnFamiliesToCreate) {
|
|
|
|
db.createColumnFamily(new ColumnFamilyDescriptor(name)).close();
|
|
|
|
}
|
|
|
|
|
|
|
|
flushAndCloseDb(db, handles);
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
private void createInMemoryColumns(List<ColumnFamilyDescriptor> totalDescriptors,
|
|
|
|
boolean inMemory,
|
|
|
|
List<ColumnFamilyHandle> handles)
|
|
|
|
throws RocksDBException {
|
|
|
|
if (!inMemory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<byte[]> columnFamiliesToCreate = new LinkedList<>();
|
|
|
|
|
|
|
|
for (ColumnFamilyDescriptor descriptor : totalDescriptors) {
|
|
|
|
columnFamiliesToCreate.add(descriptor.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (byte[] name : columnFamiliesToCreate) {
|
|
|
|
if (!Arrays.equals(name, DEFAULT_COLUMN_FAMILY.getName())) {
|
|
|
|
var descriptor = new ColumnFamilyDescriptor(name);
|
|
|
|
handles.add(db.createColumnFamily(descriptor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createIfNotExists(List<ColumnFamilyDescriptor> descriptors,
|
|
|
|
Options options,
|
|
|
|
boolean inMemory,
|
|
|
|
Path dbPath,
|
|
|
|
String dbPathString) throws RocksDBException {
|
|
|
|
if (inMemory) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-07 22:15:18 +01:00
|
|
|
if (Files.notExists(dbPath)) {
|
|
|
|
// Check if handles are all different
|
|
|
|
var descriptorsSet = new HashSet<>(descriptors);
|
|
|
|
if (descriptorsSet.size() != descriptors.size()) {
|
|
|
|
throw new IllegalArgumentException("Descriptors must be unique!");
|
|
|
|
}
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> descriptorsToCreate = new LinkedList<>(descriptors);
|
|
|
|
descriptorsToCreate
|
|
|
|
.removeIf((cf) -> Arrays.equals(cf.getName(), DEFAULT_COLUMN_FAMILY.getName()));
|
|
|
|
|
2021-01-30 00:24:55 +01:00
|
|
|
/*
|
|
|
|
SkipStatsUpdateOnDbOpen = true because this RocksDB.open session is used only to add just some columns
|
2020-12-07 22:15:18 +01:00
|
|
|
*/
|
|
|
|
//var dbOptionsFastLoadSlowEdit = options.setSkipStatsUpdateOnDbOpen(true);
|
|
|
|
|
|
|
|
LinkedList<ColumnFamilyHandle> handles = new LinkedList<>();
|
|
|
|
|
|
|
|
this.db = RocksDB.open(options, dbPathString);
|
|
|
|
for (ColumnFamilyDescriptor columnFamilyDescriptor : descriptorsToCreate) {
|
|
|
|
handles.add(db.createColumnFamily(columnFamilyDescriptor));
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
if (!inMemory) {
|
|
|
|
flushAndCloseDb(db, handles);
|
|
|
|
}
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 15:47:48 +01:00
|
|
|
public Mono<LLLocalSingleton> getSingleton(byte[] singletonListColumnName, byte[] name, byte[] defaultValue) {
|
|
|
|
return Mono
|
|
|
|
.fromCallable(() -> new LLLocalSingleton(db,
|
|
|
|
handles.get(Column.special(Column.toString(singletonListColumnName))),
|
|
|
|
(snapshot) -> snapshotsHandles.get(snapshot.getSequenceNumber()),
|
|
|
|
LLLocalKeyValueDatabase.this.name,
|
|
|
|
name,
|
2021-02-11 22:27:43 +01:00
|
|
|
dbScheduler,
|
2021-01-31 15:47:48 +01:00
|
|
|
defaultValue
|
|
|
|
))
|
2021-03-04 22:01:50 +01:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to read " + Arrays.toString(name), cause))
|
2021-02-01 02:21:53 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-13 01:31:24 +01:00
|
|
|
public Mono<LLLocalDictionary> getDictionary(byte[] columnName, UpdateMode updateMode) {
|
2021-01-31 15:47:48 +01:00
|
|
|
return Mono
|
2021-05-03 21:41:51 +02:00
|
|
|
.fromCallable(() -> new LLLocalDictionary(
|
|
|
|
allocator,
|
|
|
|
db,
|
2021-01-31 15:47:48 +01:00
|
|
|
handles.get(Column.special(Column.toString(columnName))),
|
|
|
|
name,
|
2021-02-01 02:21:53 +01:00
|
|
|
dbScheduler,
|
2021-02-13 01:31:24 +01:00
|
|
|
(snapshot) -> snapshotsHandles.get(snapshot.getSequenceNumber()),
|
|
|
|
updateMode
|
2021-01-31 15:47:48 +01:00
|
|
|
))
|
2021-02-01 02:21:53 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 15:47:48 +01:00
|
|
|
public Mono<Long> getProperty(String propertyName) {
|
|
|
|
return Mono.fromCallable(() -> db.getAggregatedLongProperty(propertyName))
|
2021-03-04 22:01:50 +01:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to read " + propertyName, cause))
|
2021-02-01 02:21:53 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:41:51 +02:00
|
|
|
@Override
|
|
|
|
public ByteBufAllocator getAllocator() {
|
|
|
|
return allocator;
|
|
|
|
}
|
|
|
|
|
2020-12-07 22:15:18 +01:00
|
|
|
@Override
|
2021-01-30 01:42:37 +01:00
|
|
|
public Mono<LLSnapshot> takeSnapshot() {
|
|
|
|
return Mono
|
|
|
|
.fromCallable(() -> {
|
|
|
|
var snapshot = db.getSnapshot();
|
|
|
|
long currentSnapshotSequenceNumber = nextSnapshotNumbers.getAndIncrement();
|
|
|
|
this.snapshotsHandles.put(currentSnapshotSequenceNumber, snapshot);
|
|
|
|
return new LLSnapshot(currentSnapshotSequenceNumber);
|
|
|
|
})
|
2021-02-01 02:21:53 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-30 01:42:37 +01:00
|
|
|
public Mono<Void> releaseSnapshot(LLSnapshot snapshot) {
|
|
|
|
return Mono
|
|
|
|
.<Void>fromCallable(() -> {
|
|
|
|
Snapshot dbSnapshot = this.snapshotsHandles.remove(snapshot.getSequenceNumber());
|
|
|
|
if (dbSnapshot == null) {
|
|
|
|
throw new IOException("Snapshot " + snapshot.getSequenceNumber() + " not found!");
|
|
|
|
}
|
|
|
|
db.releaseSnapshot(dbSnapshot);
|
|
|
|
return null;
|
|
|
|
})
|
2021-02-01 02:21:53 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<Void> close() {
|
|
|
|
return Mono
|
|
|
|
.<Void>fromCallable(() -> {
|
|
|
|
try {
|
|
|
|
flushAndCloseDb(db, new ArrayList<>(handles.values()));
|
|
|
|
deleteUnusedOldLogFiles();
|
|
|
|
} catch (RocksDBException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
2021-03-04 22:01:50 +01:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to close", cause))
|
2021-02-01 02:21:53 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this method ONLY AFTER flushing completely a db and closing it!
|
|
|
|
*/
|
2021-01-30 00:24:55 +01:00
|
|
|
@SuppressWarnings("unused")
|
2020-12-07 22:15:18 +01:00
|
|
|
private void deleteUnusedOldLogFiles() {
|
|
|
|
Path basePath = dbPath;
|
|
|
|
try {
|
|
|
|
Files
|
|
|
|
.walk(basePath, 1)
|
|
|
|
.filter(p -> !p.equals(basePath))
|
|
|
|
.filter(p -> {
|
|
|
|
var fileName = p.getFileName().toString();
|
|
|
|
if (fileName.startsWith("LOG.old.")) {
|
|
|
|
var parts = fileName.split("\\.");
|
|
|
|
if (parts.length == 3) {
|
|
|
|
try {
|
|
|
|
long nameSuffix = Long.parseUnsignedLong(parts[2]);
|
|
|
|
return true;
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fileName.endsWith(".log")) {
|
|
|
|
var parts = fileName.split("\\.");
|
|
|
|
if (parts.length == 2) {
|
|
|
|
try {
|
|
|
|
int name = Integer.parseUnsignedInt(parts[0]);
|
|
|
|
return true;
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.filter(p -> {
|
|
|
|
try {
|
|
|
|
BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class);
|
|
|
|
if (attrs.isRegularFile() && !attrs.isSymbolicLink() && !attrs.isDirectory()) {
|
|
|
|
long ctime = attrs.creationTime().toMillis();
|
|
|
|
long atime = attrs.lastAccessTime().toMillis();
|
|
|
|
long mtime = attrs.lastModifiedTime().toMillis();
|
|
|
|
long lastTime = Math.max(Math.max(ctime, atime), mtime);
|
|
|
|
long safeTime;
|
|
|
|
if (p.getFileName().toString().startsWith("LOG.old.")) {
|
|
|
|
safeTime = System.currentTimeMillis() - Duration.ofHours(24).toMillis();
|
|
|
|
} else {
|
|
|
|
safeTime = System.currentTimeMillis() - Duration.ofHours(12).toMillis();
|
|
|
|
}
|
|
|
|
if (lastTime < safeTime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException ex) {
|
2021-03-19 20:55:38 +01:00
|
|
|
logger.error("Error when deleting unused log files", ex);
|
2020-12-07 22:15:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.forEach(path -> {
|
|
|
|
try {
|
|
|
|
Files.deleteIfExists(path);
|
|
|
|
System.out.println("Deleted log file \"" + path + "\"");
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|