private static SpecificTableSweeper initializeSweepEndpoint( Consumer<Object> env, KeyValueService kvs, TransactionManager transactionManager, SweepTaskRunner sweepRunner, BackgroundSweeperPerformanceLogger sweepPerfLogger, LegacySweepMetrics sweepMetrics, boolean initializeAsync, AdjustableSweepBatchConfigSource sweepBatchConfigSource) { SpecificTableSweeper specificTableSweeper = SpecificTableSweeper.create( transactionManager, kvs, sweepRunner, SweepTableFactory.of(), sweepPerfLogger, sweepMetrics, initializeAsync); env.accept(new SweeperServiceImpl(specificTableSweeper, sweepBatchConfigSource)); return specificTableSweeper; }
public static BackgroundSweeperImpl create( MetricsManager metricsManager, AdjustableSweepBatchConfigSource sweepBatchConfigSource, Supplier<Boolean> isSweepEnabled, Supplier<Integer> sweepThreads, Supplier<Long> sweepPauseMillis, Supplier<SweepPriorityOverrideConfig> sweepPriorityOverrideConfig, PersistentLockManager persistentLockManager, SpecificTableSweeper specificTableSweeper) { NextTableToSweepProvider nextTableToSweepProvider = NextTableToSweepProvider .create(specificTableSweeper.getKvs(), specificTableSweeper.getTxManager().getLockService(), specificTableSweeper.getSweepPriorityStore()); return new BackgroundSweeperImpl( metricsManager, specificTableSweeper.getTxManager().getLockService(), nextTableToSweepProvider, sweepBatchConfigSource, isSweepEnabled, sweepThreads, sweepPauseMillis, sweepPriorityOverrideConfig, persistentLockManager, specificTableSweeper); }
public static SpecificTableSweeper create( TransactionManager txManager, KeyValueService kvs, SweepTaskRunner sweepRunner, SweepTableFactory tableFactory, BackgroundSweeperPerformanceLogger sweepPerfLogger, LegacySweepMetrics sweepMetrics, boolean initializeAsync) { SweepProgressStore sweepProgressStore = SweepProgressStoreImpl.create(kvs, initializeAsync); SweepPriorityStore sweepPriorityStore = SweepPriorityStoreImpl.create(kvs, tableFactory, initializeAsync); return new SpecificTableSweeper(txManager, kvs, sweepRunner, sweepPriorityStore, sweepProgressStore, sweepPerfLogger, sweepMetrics, System::currentTimeMillis); }
SweepResults runOneIteration(TableReference tableRef, byte[] startRow, SweepBatchConfig batchConfig) { try { SweepResults results = sweepRunner.run(tableRef, batchConfig, startRow); logSweepPerformance(tableRef, startRow, results); return results; } catch (RuntimeException e) { // This error may be logged on some paths above, but I prefer to log defensively. logSweepError(tableRef, startRow, batchConfig, e); throw e; } }
private void processSweepResults(TableToSweep tableToSweep, SweepResults currentIteration) { updateTimeMetricsOneIteration(currentIteration.getTimeInMillis(), currentIteration.getTimeElapsedSinceStartedSweeping()); SweepResults cumulativeResults = getCumulativeSweepResults(tableToSweep, currentIteration); if (currentIteration.getNextStartRow().isPresent()) { saveIntermediateSweepResults(tableToSweep, cumulativeResults); } else { processFinishedSweep(tableToSweep, cumulativeResults); } }
SweepTaskRunner sweepRunner = new SweepTaskRunner(kvs, TS_SUPPLIER, TS_SUPPLIER, txService, ssm, cellsSweeper); LegacySweepMetrics sweepMetrics = new LegacySweepMetrics(metricsManager.getRegistry()); specificTableSweeper = SpecificTableSweeper.create( txManager, kvs, NextTableToSweepProvider.create(kvs, txManager.getLockService(), specificTableSweeper.getSweepPriorityStore()), sweepBatchConfigSource, () -> true, // sweepEnabled
private Optional<TableToSweep> getTableToSweep() { return specificTableSweeper.getTxManager().runTaskWithRetry( tx -> { Optional<SweepProgress> progress = currentTable.flatMap( tableToSweep -> specificTableSweeper.getSweepProgressStore().loadProgress( tableToSweep.getTableRef())); SweepPriorityOverrideConfig overrideConfig = sweepPriorityOverrideConfig.get(); if (progress.map( realProgress -> shouldContinueSweepingCurrentTable(realProgress, overrideConfig)) .orElse(false)) { try { // If we're here, currentTable exists and we're going to sweep it again this iteration updateProgressAndRefreshLock(progress.get()); return currentTable; } catch (InterruptedException ex) { log.info("Sweep lost the lock for table {}", LoggingArgs.tableRef(progress.get().tableRef())); closeTableLockIfHeld(); currentTable = Optional.empty(); // We'll fall through and choose a new table } } log.info("Sweep is choosing a new table to sweep."); closeTableLockIfHeld(); return getNextTableToSweep(tx, overrideConfig); }); }
private void clearSweepProgress(TableReference tableRef) { specificTableSweeper.getSweepProgressStore().clearProgress(tableRef); }
private void checkTableExists(String tableName, TableReference tableRef) { WebPreconditions.checkArgument(specificTableSweeper.getKvs().getAllTableNames().contains(tableRef), "Table requested to sweep %s does not exist", tableName); }
private Optional<TableToSweep> getNextTableToSweep(Transaction tx, SweepPriorityOverrideConfig overrideConfig) { Optional<TableToSweep> nextTableToSweep = nextTableToSweepProvider.getNextTableToSweep( tx, specificTableSweeper.getSweepRunner().getConservativeSweepTimestamp(), overrideConfig); if (nextTableToSweep.isPresent()) { // Check if we're resuming this table after a previous sweep nextTableToSweep = augmentWithProgress(nextTableToSweep.get()); currentTable = nextTableToSweep; } return nextTableToSweep; }
private void waitUntilSpecificTableSweeperIsInitialized() throws InterruptedException { while (!specificTableSweeper.isInitialized()) { log.info("Sweep Priority Table and Sweep Progress Table are not initialized yet. If you have enabled " + "asynchronous initialization, these tables are being initialized asynchronously. Background " + "sweeper will start once the initialization is complete."); sleepFor(getBackoffTimeWhenSweepHasNotRun()); } }
private void processSweepResults(TableToSweep tableToSweep, SweepResults currentIteration) { updateTimeMetricsOneIteration(currentIteration.getTimeInMillis(), currentIteration.getTimeElapsedSinceStartedSweeping()); SweepResults cumulativeResults = getCumulativeSweepResults(tableToSweep, currentIteration); if (currentIteration.getNextStartRow().isPresent()) { saveIntermediateSweepResults(tableToSweep, cumulativeResults); } else { processFinishedSweep(tableToSweep, cumulativeResults); } }
SweepTaskRunner sweepRunner = new SweepTaskRunner(kvs, TS_SUPPLIER, TS_SUPPLIER, txService, ssm, cellsSweeper); LegacySweepMetrics sweepMetrics = new LegacySweepMetrics(metricsManager.getRegistry()); specificTableSweeper = SpecificTableSweeper.create( txManager, kvs, NextTableToSweepProvider.create(kvs, txManager.getLockService(), specificTableSweeper.getSweepPriorityStore()), sweepBatchConfigSource, () -> true, // sweepEnabled
SweepResults runOneIteration(TableReference tableRef, byte[] startRow, SweepBatchConfig batchConfig) { try { SweepResults results = sweepRunner.run(tableRef, batchConfig, startRow); logSweepPerformance(tableRef, startRow, results); return results; } catch (RuntimeException e) { // This error may be logged on some paths above, but I prefer to log defensively. logSweepError(tableRef, startRow, batchConfig, e); throw e; } }
private Optional<TableToSweep> getTableToSweep() { return specificTableSweeper.getTxManager().runTaskWithRetry( tx -> { Optional<SweepProgress> progress = currentTable.flatMap( tableToSweep -> specificTableSweeper.getSweepProgressStore().loadProgress( tableToSweep.getTableRef())); SweepPriorityOverrideConfig overrideConfig = sweepPriorityOverrideConfig.get(); if (progress.map( realProgress -> shouldContinueSweepingCurrentTable(realProgress, overrideConfig)) .orElse(false)) { try { // If we're here, currentTable exists and we're going to sweep it again this iteration updateProgressAndRefreshLock(progress.get()); return currentTable; } catch (InterruptedException ex) { log.info("Sweep lost the lock for table {}", LoggingArgs.tableRef(progress.get().tableRef())); closeTableLockIfHeld(); currentTable = Optional.empty(); // We'll fall through and choose a new table } } log.info("Sweep is choosing a new table to sweep."); closeTableLockIfHeld(); return getNextTableToSweep(tx, overrideConfig); }); }
private Optional<TableToSweep> augmentWithProgress(TableToSweep nextTableWithoutProgress) { Optional<SweepProgress> sweepProgress = specificTableSweeper.getSweepProgressStore().loadProgress( nextTableWithoutProgress.getTableRef()); if (sweepProgress.isPresent()) { TableToSweep nextTableWithProgress = TableToSweep.continueSweeping(nextTableWithoutProgress.getTableRef(), nextTableWithoutProgress.getSweepLock(), sweepProgress.get()); return Optional.of(nextTableWithProgress); } return Optional.of(nextTableWithoutProgress); }
private SweepOutcome determineCauseOfFailure(Exception originalException, TableToSweep tableToSweep) { try { Set<TableReference> tables = specificTableSweeper.getKvs().getAllTableNames(); if (!tables.contains(tableToSweep.getTableRef())) { clearSweepProgress(tableToSweep.getTableRef()); log.info("The table being swept by the background sweeper was dropped, moving on..."); tableToSweep.getSweepLock().close(); return SweepOutcome.TABLE_DROPPED_WHILE_SWEEPING; } log.info("The background sweep job failed unexpectedly; will retry with a lower batch size...", originalException); return SweepOutcome.ERROR; } catch (RuntimeException newE) { log.warn("Sweep failed", originalException); log.warn("Failed to check whether the table being swept was dropped. Retrying...", newE); return SweepOutcome.ERROR; } }
private Optional<TableToSweep> getNextTableToSweep(Transaction tx, SweepPriorityOverrideConfig overrideConfig) { Optional<TableToSweep> nextTableToSweep = nextTableToSweepProvider.getNextTableToSweep( tx, specificTableSweeper.getSweepRunner().getConservativeSweepTimestamp(), overrideConfig); if (nextTableToSweep.isPresent()) { // Check if we're resuming this table after a previous sweep nextTableToSweep = augmentWithProgress(nextTableToSweep.get()); currentTable = nextTableToSweep; } return nextTableToSweep; }
private void waitUntilSpecificTableSweeperIsInitialized() throws InterruptedException { while (!specificTableSweeper.isInitialized()) { log.info("Sweep Priority Table and Sweep Progress Table are not initialized yet. If you have enabled " + "asynchronous initialization, these tables are being initialized asynchronously. Background " + "sweeper will start once the initialization is complete."); sleepFor(getBackoffTimeWhenSweepHasNotRun()); } }
public static BackgroundSweeperImpl create( MetricsManager metricsManager, AdjustableSweepBatchConfigSource sweepBatchConfigSource, Supplier<Boolean> isSweepEnabled, Supplier<Integer> sweepThreads, Supplier<Long> sweepPauseMillis, Supplier<SweepPriorityOverrideConfig> sweepPriorityOverrideConfig, PersistentLockManager persistentLockManager, SpecificTableSweeper specificTableSweeper) { NextTableToSweepProvider nextTableToSweepProvider = NextTableToSweepProvider .create(specificTableSweeper.getKvs(), specificTableSweeper.getTxManager().getLockService(), specificTableSweeper.getSweepPriorityStore()); return new BackgroundSweeperImpl( metricsManager, specificTableSweeper.getTxManager().getLockService(), nextTableToSweepProvider, sweepBatchConfigSource, isSweepEnabled, sweepThreads, sweepPauseMillis, sweepPriorityOverrideConfig, persistentLockManager, specificTableSweeper); }