Search in sources :

Example 1 with SnapshotWriteResult

use of io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult in project datarouter by hotpads.

the class SnapshotMerger method merge.

public void merge() {
    Map<SnapshotKey, SnapshotKeyAndNumRecords> summaryByKey = mergeGroup.keyReadOps(false).scanSnapshotKeysAndRootBlocks(readExec, 10).map(SnapshotKeyAndNumRecords::new).toMap(summary -> summary.key);
    while (summaryByKey.size() > 1) {
        SnapshotGroup outputGroup = summaryByKey.size() <= mergeFactor ? destinationGroup : mergeGroup;
        Scanner.of(summaryByKey.values()).minN(SnapshotKeyAndNumRecords.BY_NUM_RECORDS, mergeFactor).map(summary -> summary.key).flush(keys -> {
            SnapshotWriteResult result = combineSnapshots(keys, outputGroup);
            var newSummary = new SnapshotKeyAndNumRecords(result.toSnapshotKeyAndRoot());
            summaryByKey.put(result.key, newSummary);
        }).forEach(summaryByKey::remove);
    }
}
Also used : Scanner(io.datarouter.scanner.Scanner) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) Supplier(java.util.function.Supplier) SnapshotKeyAndNumRecords(io.datarouter.filesystem.snapshot.group.dto.SnapshotKeyAndNumRecords) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) List(java.util.List) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) Map(java.util.Map) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) ExecutorService(java.util.concurrent.ExecutorService) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) SnapshotKeyAndNumRecords(io.datarouter.filesystem.snapshot.group.dto.SnapshotKeyAndNumRecords) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup)

Example 2 with SnapshotWriteResult

use of io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult in project datarouter by hotpads.

the class SnapshotMerger method combineSnapshots.

private SnapshotWriteResult combineSnapshots(List<SnapshotKey> keys, SnapshotGroup outputGroup) {
    SnapshotWriteResult result = Scanner.of(keys).map(key -> new ScanningSnapshotReader(key, readExec, 10, mergeGroup, scanNumBlocks)).collate(reader -> reader.scanLeafRecords(0), SnapshotLeafRecord.KEY_COMPARATOR).deduplicateConsecutiveBy(leafRecord -> leafRecord.key, Arrays::equals).map(SnapshotLeafRecord::entry).batch(10_000).apply(batches -> outputGroup.writeOps().write(writerConfig, batches, writeExec, shouldStop));
    keys.forEach(key -> mergeGroup.deleteOps().deleteSnapshot(key, writeExec, 10));
    logger.warn("combined {}, {}", keys.size(), keys);
    return result;
}
Also used : Scanner(io.datarouter.scanner.Scanner) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) Supplier(java.util.function.Supplier) SnapshotKeyAndNumRecords(io.datarouter.filesystem.snapshot.group.dto.SnapshotKeyAndNumRecords) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) List(java.util.List) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) Map(java.util.Map) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) ExecutorService(java.util.concurrent.ExecutorService) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult)

Example 3 with SnapshotWriteResult

use of io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult in project datarouter by hotpads.

the class SnapshotGroupWriteOps method writeWithId.

// could be public if something like back-dating snapshotIds is necessary
private SnapshotWriteResult writeWithId(SnapshotWriterConfig config, Scanner<List<SnapshotEntry>> entries, String snapshotId, ExecutorService exec, Supplier<Boolean> shouldStop) {
    var snapshotKey = new SnapshotKey(groupId, snapshotId);
    SnapshotFileStorage snapshotFileStorage = group.makeSnapshotFileStorage(snapshotKey.snapshotId);
    SnapshotBlockStorage snapshotBlockStorage = cacheStorage == null ? null : group.makeCacheStorage(snapshotKey.snapshotId);
    try (var writer = new SnapshotWriter(snapshotKey, snapshotFileStorage, snapshotBlockStorage, config, exec)) {
        entries.advanceUntil($ -> shouldStop.get()).forEach(writer::addBatch);
        if (shouldStop.get()) {
            return SnapshotWriteResult.failure(snapshotKey);
        }
        return writer.complete().map(rootBlock -> {
            writeIdFile(snapshotKey.snapshotId);
            decodingBlockLoaderBySnapshotKey.put(snapshotKey, decodingBlockLoaderFactory.create(rootBlock, group.makeStorageReader(snapshotKey.snapshotId)));
            return SnapshotWriteResult.success(snapshotKey, rootBlock);
        }).orElseGet(() -> {
            logger.warn("snapshot {} had no entries and was not written", snapshotKey);
            return SnapshotWriteResult.empty(snapshotKey);
        });
    }
}
Also used : SnapshotFileStorage(io.datarouter.filesystem.snapshot.storage.file.SnapshotFileStorage) Scanner(io.datarouter.scanner.Scanner) Logger(org.slf4j.Logger) UlidTool(io.datarouter.util.UlidTool) LoggerFactory(org.slf4j.LoggerFactory) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) SnapshotEntry(io.datarouter.filesystem.snapshot.entry.SnapshotEntry) Supplier(java.util.function.Supplier) Directory(io.datarouter.storage.file.Directory) SnapshotWriter(io.datarouter.filesystem.snapshot.writer.SnapshotWriter) DecodingBlockLoaderFactory(io.datarouter.filesystem.snapshot.reader.block.DecodingBlockLoaderFactory) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) EmptyArray(io.datarouter.bytes.EmptyArray) DecodingBlockLoader(io.datarouter.filesystem.snapshot.reader.block.DecodingBlockLoader) List(java.util.List) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) SnapshotFileStorage(io.datarouter.filesystem.snapshot.storage.file.SnapshotFileStorage) Map(java.util.Map) PathbeanKey(io.datarouter.storage.file.PathbeanKey) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) SnapshotBlockStorage(io.datarouter.filesystem.snapshot.storage.block.SnapshotBlockStorage) ExecutorService(java.util.concurrent.ExecutorService) SnapshotBlockStorage(io.datarouter.filesystem.snapshot.storage.block.SnapshotBlockStorage) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) SnapshotWriter(io.datarouter.filesystem.snapshot.writer.SnapshotWriter)

Example 4 with SnapshotWriteResult

use of io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult in project datarouter by hotpads.

the class SnapshotBenchmark method execute.

public RootBlock execute() {
    var timer = new PhaseTimer("writeSnapshot");
    SnapshotWriteResult result = makeEntryScanner(scannerExec, numInputThreads).apply(entries -> group.writeOps().write(makeSnapshotWriterConfig(), entries, writerExec, () -> false));
    snapshotKey = result.key;
    timer.add("wrote " + NumberFormatter.addCommas(result.optRoot.get().numItems()));
    logger.warn("{} @{}/s", timer, NumberFormatter.addCommas(timer.getItemsPerSecond(numEntries)));
    return result.optRoot.get();
}
Also used : PhaseTimer(io.datarouter.util.timer.PhaseTimer) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult)

Example 5 with SnapshotWriteResult

use of io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult in project datarouter by hotpads.

the class FilesystemSnapshotSortingTests method writeOutputSnapshot.

private RootBlock writeOutputSnapshot(Scanner<SnapshotRecord> records) {
    var timer = new PhaseTimer("writeOutputSnapshot");
    SnapshotWriterConfig config = makeSnapshotWriterConfig(true);
    SnapshotWriteResult result = records.map(SnapshotRecord::entry).batch(1000).apply(entries -> outputGroup.writeOps().write(config, entries, exec, () -> false));
    outputSnapshotKey = result.key;
    timer.add("wrote " + NumberFormatter.addCommas(result.optRoot.get().numItems()));
    logger.warn("{}", timer);
    return result.optRoot.get();
}
Also used : PhaseTimer(io.datarouter.util.timer.PhaseTimer) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) SnapshotRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord)

Aggregations

SnapshotWriteResult (io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult)8 SnapshotWriterConfig (io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig)6 PhaseTimer (io.datarouter.util.timer.PhaseTimer)5 SnapshotGroup (io.datarouter.filesystem.snapshot.group.SnapshotGroup)4 SnapshotKey (io.datarouter.filesystem.snapshot.key.SnapshotKey)4 Scanner (io.datarouter.scanner.Scanner)4 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ScanningSnapshotReader (io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader)3 SnapshotRecord (io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord)3 Map (java.util.Map)3 Supplier (java.util.function.Supplier)3 EmptyArray (io.datarouter.bytes.EmptyArray)2 SnapshotEntry (io.datarouter.filesystem.snapshot.entry.SnapshotEntry)2 SnapshotKeyAndNumRecords (io.datarouter.filesystem.snapshot.group.dto.SnapshotKeyAndNumRecords)2 SnapshotLeafRecord (io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord)2 Arrays (java.util.Arrays)2 ByteTool (io.datarouter.bytes.ByteTool)1