Search in sources :

Example 6 with PrimaryKey

use of io.datarouter.model.key.primary.PrimaryKey in project datarouter by hotpads.

the class CopyTableService method copyTableSpan.

public <PK extends PrimaryKey<PK>, D extends Databean<PK, D>> CopyTableSpanResult copyTableSpan(String sourceNodeName, String targetNodeName, String fromKeyExclusiveString, String toKeyInclusiveString, int numThreads, int batchSize, long batchId, long numBatches) {
    @SuppressWarnings("unchecked") SortedMapStorageNode<PK, D, ?> sourceNode = (SortedMapStorageNode<PK, D, ?>) nodes.getNode(sourceNodeName);
    Objects.requireNonNull(sourceNode, sourceNodeName + " not found");
    @SuppressWarnings("unchecked") SortedMapStorageNode<PK, D, ?> targetNode = (SortedMapStorageNode<PK, D, ?>) nodes.getNode(targetNodeName);
    Objects.requireNonNull(targetNode, targetNodeName + " not found");
    PK fromKeyExclusive = PrimaryKeyPercentCodecTool.decode(sourceNode.getFieldInfo().getPrimaryKeySupplier(), fromKeyExclusiveString);
    PK toKeyInclusive = PrimaryKeyPercentCodecTool.decode(sourceNode.getFieldInfo().getPrimaryKeySupplier(), toKeyInclusiveString);
    // hbase and bigtable put config
    Config putConfig = new Config();
    // .setPersistentPut(persistentPut)
    // .setIgnoreNullFields(true);//could be configurable
    var numSkipped = new AtomicLong();
    Range<PK> range = new Range<>(fromKeyExclusive, false, toKeyInclusive, true);
    var numScanned = new AtomicLong();
    var numCopied = new AtomicLong();
    AtomicReference<PK> lastKey = new AtomicReference<>();
    try {
        sourceNode.scan(range, SCAN_CONFIG).each($ -> numScanned.incrementAndGet()).each($ -> Counters.inc("copyTable " + sourceNodeName + " read")).batch(batchSize).parallel(putMultiScannerContext.get(numThreads)).each(batch -> targetNode.putMulti(batch, putConfig)).each($ -> Counters.inc("copyTable " + sourceNodeName + " write")).each(batch -> numCopied.addAndGet(batch.size())).each(batch -> lastKey.set(ListTool.getLast(batch).getKey())).sample(10, true).forEach(batch -> logProgress(false, numSkipped.get(), numScanned.get(), numCopied.get(), batchId, numBatches, sourceNodeName, targetNodeName, lastKey.get(), null));
        logProgress(true, numSkipped.get(), numScanned.get(), numCopied.get(), batchId, numBatches, sourceNodeName, targetNodeName, lastKey.get(), null);
        return new CopyTableSpanResult(true, null, numCopied.get(), null);
    } catch (Throwable e) {
        PK pk = lastKey.get();
        logProgress(false, numSkipped.get(), numScanned.get(), numCopied.get(), batchId, numBatches, sourceNodeName, targetNodeName, pk, e);
        String resumeFromKeyString = pk == null ? null : PrimaryKeyPercentCodecTool.encode(pk);
        return new CopyTableSpanResult(false, e, numCopied.get(), resumeFromKeyString);
    }
}
Also used : Logger(org.slf4j.Logger) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) LoggerFactory(org.slf4j.LoggerFactory) Counters(io.datarouter.instrumentation.count.Counters) PrimaryKeyPercentCodecTool(io.datarouter.storage.util.PrimaryKeyPercentCodecTool) Singleton(javax.inject.Singleton) AtomicReference(java.util.concurrent.atomic.AtomicReference) DatarouterCopyTablePutMultiExecutor(io.datarouter.plugin.copytable.config.DatarouterCopyTableExecutors.DatarouterCopyTablePutMultiExecutor) PrimaryKey(io.datarouter.model.key.primary.PrimaryKey) Objects(java.util.Objects) Inject(javax.inject.Inject) NumberFormatter(io.datarouter.util.number.NumberFormatter) Range(io.datarouter.util.tuple.Range) AtomicLong(java.util.concurrent.atomic.AtomicLong) Databean(io.datarouter.model.databean.Databean) Config(io.datarouter.storage.config.Config) DatarouterNodes(io.datarouter.storage.node.DatarouterNodes) ListTool(io.datarouter.util.collection.ListTool) SortedMapStorageNode(io.datarouter.storage.node.op.combo.SortedMapStorage.SortedMapStorageNode) Config(io.datarouter.storage.config.Config) AtomicReference(java.util.concurrent.atomic.AtomicReference) Range(io.datarouter.util.tuple.Range) SortedMapStorageNode(io.datarouter.storage.node.op.combo.SortedMapStorage.SortedMapStorageNode) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

Databean (io.datarouter.model.databean.Databean)6 PrimaryKey (io.datarouter.model.key.primary.PrimaryKey)6 Inject (javax.inject.Inject)6 TableSamplerService (io.datarouter.nodewatch.service.TableSamplerService)4 Config (io.datarouter.storage.config.Config)4 DatarouterNodes (io.datarouter.storage.node.DatarouterNodes)4 PrimaryKeyPercentCodecTool (io.datarouter.storage.util.PrimaryKeyPercentCodecTool)4 NumberFormatter (io.datarouter.util.number.NumberFormatter)4 StringTool (io.datarouter.util.string.StringTool)4 BaseHandler (io.datarouter.web.handler.BaseHandler)4 Mav (io.datarouter.web.handler.mav.Mav)4 Param (io.datarouter.web.handler.types.Param)4 OptionalString (io.datarouter.web.handler.types.optional.OptionalString)4 HtmlForm (io.datarouter.web.html.form.HtmlForm)4 Bootstrap4FormHtml (io.datarouter.web.html.j2html.bootstrap4.Bootstrap4FormHtml)4 Bootstrap4PageFactory (io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory)4 TagCreator.br (j2html.TagCreator.br)4 TagCreator.div (j2html.TagCreator.div)4 TagCreator.h2 (j2html.TagCreator.h2)4 ContainerTag (j2html.tags.ContainerTag)4