Search in sources :

Example 1 with ScanningSnapshotReader

use of io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader in project datarouter by hotpads.

the class DatarouterSnapshotEntriesHandler method buildTable.

private DomContent buildTable(SnapshotKey snapshotKey, long offset, long limit) {
    SnapshotGroup group = groups.getGroup(snapshotKey.groupId);
    var reader = new ScanningSnapshotReader(snapshotKey, exec, 2, groups, 1);
    SnapshotRecordStringDecoder decoder = ReflectionTool.create(group.getSnapshotEntryDecoderClass());
    List<SnapshotRecordStrings> rows = reader.scan(0).skip(offset).limit(limit).map(decoder::decode).list();
    var table = new J2HtmlTable<SnapshotRecordStrings>().withClasses("sortable table table-sm table-striped my-4 border").withColumn("id", row -> row.id).withColumn(decoder.keyName(), row -> row.key).withColumn(decoder.valueName(), row -> {
        if (row.value == null) {
            return "";
        } else if (row.value.length() < 64) {
            return row.value;
        } else {
            return row.value.subSequence(0, 64) + "...";
        }
    }).withHtmlColumn("details", row -> {
        String href = new URIBuilder().setPath(request.getContextPath() + snapshotPaths.datarouter.snapshot.individual.entry.toSlashedString()).addParameter(DatarouterSnapshotEntryHandler.P_groupId, snapshotKey.groupId).addParameter(DatarouterSnapshotEntryHandler.P_snapshotId, snapshotKey.snapshotId).addParameter(DatarouterSnapshotEntryHandler.P_id, Long.toString(row.id)).toString();
        return td(a("view").withHref(href));
    }).build(rows);
    return table;
}
Also used : ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) J2HtmlTable(io.datarouter.web.html.j2html.J2HtmlTable) URIBuilder(org.apache.http.client.utils.URIBuilder) Mav(io.datarouter.web.handler.mav.Mav) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) ReflectionTool(io.datarouter.util.lang.ReflectionTool) TagCreator.a(j2html.TagCreator.a) DatarouterSnapshotWebExecutor(io.datarouter.filesystem.snapshot.web.DatarouterSnapshotExecutors.DatarouterSnapshotWebExecutor) OptionalLong(io.datarouter.web.handler.types.optional.OptionalLong) Inject(javax.inject.Inject) DomContent(j2html.tags.DomContent) List(java.util.List) DatarouterWebRequireJsV2(io.datarouter.web.requirejs.DatarouterWebRequireJsV2) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) SnapshotGroups(io.datarouter.filesystem.snapshot.group.SnapshotGroups) TagCreator.td(j2html.TagCreator.td) BaseHandler(io.datarouter.web.handler.BaseHandler) Bootstrap4PageFactory(io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) Param(io.datarouter.web.handler.types.Param) TagCreator.div(j2html.TagCreator.div) J2HtmlTable(io.datarouter.web.html.j2html.J2HtmlTable) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with ScanningSnapshotReader

use of io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader 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 ScanningSnapshotReader

use of io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader in project datarouter by hotpads.

the class BaseSnapshotTests method testScan.

@Test
public void testScan() {
    if (!ENABLED_TESTS.contains(TestId.SCAN)) {
        return;
    }
    BlockLoader blockLoader = makeBlockLoader(useMemoryCache(), shareMemoryCache());
    var reader = new ScanningSnapshotReader(snapshotKey, exec, getNumThreads(), blockLoader, SCAN_NUM_BLOCKS);
    List<SnapshotRecord> outputs = reader.scan(0).list();
    Assert.assertEquals(outputs.size(), sortedInputs.size());
    for (int i = 0; i < sortedInputs.size(); ++i) {
        Input input = sortedInputs.get(i);
        SnapshotRecord output = outputs.get(i);
        Assert.assertEquals(i, output.id);
        for (int column = 0; column < input.entry.columnValues.length; ++column) {
            if (!SnapshotEntry.equalColumnValue(input.entry, output.entry(), column)) {
                String message = String.format("%s, actual=%s, expected=%s", i, utf8(output.columnValues[column]), utf8(input.entry.columnValues[column]));
                throw new RuntimeException(message);
            }
        }
    }
}
Also used : ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) SnapshotRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord) Test(org.testng.annotations.Test)

Example 4 with ScanningSnapshotReader

use of io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader in project datarouter by hotpads.

the class BaseSnapshotTests method testSearches.

@Test
public void testSearches() {
    if (!ENABLED_TESTS.contains(TestId.SEARCHES)) {
        return;
    }
    BlockLoader blockLoader = makeBlockLoader(useMemoryCache(), shareMemoryCache());
    var reader = new ScanningSnapshotReader(snapshotKey, exec, getNumThreads(), blockLoader, SCAN_NUM_BLOCKS);
    int step = 1000;
    int limit = 1000;
    Scanner.iterate(0, fromId -> fromId += step).advanceWhile(fromId -> fromId < sortedInputs.size() - limit).parallel(new ParallelScannerContext(scanExec, getNumThreads(), true)).forEach(fromId -> {
        var idReader = new SnapshotIdReader(snapshotKey, blockLoader);
        // known first key inclusive
        byte[] searchKey = idReader.getRecord(fromId).key;
        List<SnapshotLeafRecord> outputsInclusive = reader.scanLeafRecords(searchKey, true).limit(limit).list();
        for (int i = 0; i < limit; ++i) {
            Input input = sortedInputs.get(fromId + i);
            SnapshotLeafRecord output = outputsInclusive.get(i);
            Assert.assertEquals(fromId + i, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
        }
        // known first key exclusive
        List<SnapshotLeafRecord> outputsExclusive = reader.scanLeafRecords(searchKey, false).limit(limit).list();
        for (int i = 0; i < limit; ++i) {
            // plus one because exclusive
            Input input = sortedInputs.get(fromId + i + 1);
            SnapshotLeafRecord output = outputsExclusive.get(i);
            Assert.assertEquals(input.id, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
        }
        // fake first key (should act like exclusive)
        byte[] nonExistentKey = ByteTool.concat(searchKey, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 });
        List<SnapshotLeafRecord> outputsNonExistentKey = reader.scanLeafRecords(nonExistentKey, true).limit(limit).list();
        for (int i = 0; i < limit; ++i) {
            // plus one because the first key didn't exist
            Input input = sortedInputs.get(fromId + i + 1);
            SnapshotLeafRecord output = outputsNonExistentKey.get(i);
            Assert.assertEquals(input.id, output.id);
            Assert.assertEquals(new Bytes(input.entry.key()), new Bytes(output.key));
        }
    });
}
Also used : ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) IntStream(java.util.stream.IntStream) SnapshotRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord) Scanner(io.datarouter.scanner.Scanner) Arrays(java.util.Arrays) SnapshotKeyReader(io.datarouter.filesystem.snapshot.reader.SnapshotKeyReader) ByteTool(io.datarouter.bytes.ByteTool) BlockKey(io.datarouter.filesystem.snapshot.block.BlockKey) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) LoggerFactory(org.slf4j.LoggerFactory) Test(org.testng.annotations.Test) Bytes(io.datarouter.bytes.Bytes) GzipBlockCompressor(io.datarouter.filesystem.snapshot.compress.GzipBlockCompressor) SnapshotWriterConfig(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig) NumberFormatter(io.datarouter.util.number.NumberFormatter) SnapshotWriteResult(io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult) Assert(org.testng.Assert) ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) DatarouterFilesystemModuleFactory(io.datarouter.filesystem.DatarouterFilesystemModuleFactory) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) SnapshotWriterConfigBuilder(io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfigBuilder) ExecutorService(java.util.concurrent.ExecutorService) PhaseTimer(io.datarouter.util.timer.PhaseTimer) SnapshotIdReader(io.datarouter.filesystem.snapshot.reader.SnapshotIdReader) AfterClass(org.testng.annotations.AfterClass) Logger(org.slf4j.Logger) BeforeClass(org.testng.annotations.BeforeClass) SnapshotGroup(io.datarouter.filesystem.snapshot.group.SnapshotGroup) Set(java.util.Set) MemoryBlockCache(io.datarouter.filesystem.snapshot.cache.MemoryBlockCache) SnapshotEntry(io.datarouter.filesystem.snapshot.entry.SnapshotEntry) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Guice(org.testng.annotations.Guice) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Optional(java.util.Optional) SnapshotKey(io.datarouter.filesystem.snapshot.key.SnapshotKey) ListTool(io.datarouter.util.collection.ListTool) Require(io.datarouter.util.Require) Bytes(io.datarouter.bytes.Bytes) SnapshotLeafRecord(io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) SnapshotIdReader(io.datarouter.filesystem.snapshot.reader.SnapshotIdReader) Test(org.testng.annotations.Test)

Example 5 with ScanningSnapshotReader

use of io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader in project datarouter by hotpads.

the class BaseSnapshotTests method testScanValues.

@Test
public void testScanValues() {
    if (!ENABLED_TESTS.contains(TestId.SCAN_VALUES)) {
        return;
    }
    BlockLoader blockLoader = makeBlockLoader(useMemoryCache(), shareMemoryCache());
    var reader = new ScanningSnapshotReader(snapshotKey, exec, getNumThreads(), blockLoader, SCAN_NUM_BLOCKS);
    List<byte[]> actuals = reader.scanValues().list();
    Assert.assertEquals(actuals.size(), sortedInputs.size());
    for (int i = 0; i < sortedInputs.size(); ++i) {
        Input input = sortedInputs.get(i);
        Assert.assertEquals(input.entry.value(), actuals.get(i));
    }
}
Also used : ScanningSnapshotReader(io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader) BlockLoader(io.datarouter.filesystem.snapshot.reader.block.BlockLoader) Test(org.testng.annotations.Test)

Aggregations

ScanningSnapshotReader (io.datarouter.filesystem.snapshot.reader.ScanningSnapshotReader)11 Test (org.testng.annotations.Test)9 BlockLoader (io.datarouter.filesystem.snapshot.reader.block.BlockLoader)8 SnapshotGroup (io.datarouter.filesystem.snapshot.group.SnapshotGroup)5 SnapshotKey (io.datarouter.filesystem.snapshot.key.SnapshotKey)5 List (java.util.List)5 SnapshotWriteResult (io.datarouter.filesystem.snapshot.group.dto.SnapshotWriteResult)4 SnapshotLeafRecord (io.datarouter.filesystem.snapshot.reader.record.SnapshotLeafRecord)4 SnapshotRecord (io.datarouter.filesystem.snapshot.reader.record.SnapshotRecord)4 SnapshotWriterConfig (io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfig)4 Scanner (io.datarouter.scanner.Scanner)4 ExecutorService (java.util.concurrent.ExecutorService)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ByteTool (io.datarouter.bytes.ByteTool)3 DatarouterFilesystemModuleFactory (io.datarouter.filesystem.DatarouterFilesystemModuleFactory)3 BlockKey (io.datarouter.filesystem.snapshot.block.BlockKey)3 SnapshotEntry (io.datarouter.filesystem.snapshot.entry.SnapshotEntry)3 SnapshotWriterConfigBuilder (io.datarouter.filesystem.snapshot.writer.SnapshotWriterConfigBuilder)3 ParallelScannerContext (io.datarouter.scanner.ParallelScannerContext)3