|
| 1 | +/* |
| 2 | + * Copyright 2024 Signal Messenger, LLC |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package org.whispersystems.textsecuregcm.workers; |
| 7 | + |
| 8 | +import io.dropwizard.core.Application; |
| 9 | +import io.dropwizard.core.setup.Environment; |
| 10 | +import io.micrometer.core.instrument.Counter; |
| 11 | +import io.micrometer.core.instrument.Metrics; |
| 12 | +import net.sourceforge.argparse4j.inf.Namespace; |
| 13 | +import net.sourceforge.argparse4j.inf.Subparser; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | +import org.whispersystems.textsecuregcm.WhisperServerConfiguration; |
| 17 | +import org.whispersystems.textsecuregcm.metrics.MetricsUtil; |
| 18 | +import org.whispersystems.textsecuregcm.storage.IssuedReceiptsManager; |
| 19 | +import reactor.core.publisher.Flux; |
| 20 | +import reactor.core.publisher.Mono; |
| 21 | +import reactor.core.scheduler.Schedulers; |
| 22 | + |
| 23 | +import java.time.Clock; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.function.Function; |
| 29 | + |
| 30 | +public class IssuedReceiptMigrationCommand extends AbstractCommandWithDependencies { |
| 31 | + |
| 32 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 33 | + |
| 34 | + private static final String SEGMENT_COUNT_ARGUMENT = "segments"; |
| 35 | + private static final String DRY_RUN_ARGUMENT = "dry-run"; |
| 36 | + private static final String MAX_CONCURRENCY_ARGUMENT = "max-concurrency"; |
| 37 | + private static final String BUFFER_ARGUMENT = "buffer"; |
| 38 | + |
| 39 | + private static final String INSPECTED_ISSUED_RECEIPTS = MetricsUtil.name(IssuedReceiptMigrationCommand.class, |
| 40 | + "inspectedIssuedReceipts"); |
| 41 | + private static final String MIGRATED_ISSUED_RECEIPTS = MetricsUtil.name(IssuedReceiptMigrationCommand.class, |
| 42 | + "migratedIssuedReceipts"); |
| 43 | + |
| 44 | + private final Clock clock; |
| 45 | + |
| 46 | + public IssuedReceiptMigrationCommand(final Clock clock) { |
| 47 | + super(new Application<>() { |
| 48 | + @Override |
| 49 | + public void run(final WhisperServerConfiguration configuration, final Environment environment) { |
| 50 | + } |
| 51 | + }, "migrate-issued-receipts", "Migrates columns in the issued receipts table"); |
| 52 | + this.clock = clock; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void configure(final Subparser subparser) { |
| 57 | + super.configure(subparser); |
| 58 | + |
| 59 | + subparser.addArgument("--segments") |
| 60 | + .type(Integer.class) |
| 61 | + .dest(SEGMENT_COUNT_ARGUMENT) |
| 62 | + .required(false) |
| 63 | + .setDefault(1) |
| 64 | + .help("The total number of segments for a DynamoDB scan"); |
| 65 | + |
| 66 | + subparser.addArgument("--max-concurrency") |
| 67 | + .type(Integer.class) |
| 68 | + .dest(MAX_CONCURRENCY_ARGUMENT) |
| 69 | + .required(false) |
| 70 | + .setDefault(16) |
| 71 | + .help("Max concurrency for migration operations"); |
| 72 | + |
| 73 | + subparser.addArgument("--dry-run") |
| 74 | + .type(Boolean.class) |
| 75 | + .dest(DRY_RUN_ARGUMENT) |
| 76 | + .required(false) |
| 77 | + .setDefault(true) |
| 78 | + .help("If true, don’t actually perform migration"); |
| 79 | + |
| 80 | + subparser.addArgument("--buffer") |
| 81 | + .type(Integer.class) |
| 82 | + .dest(BUFFER_ARGUMENT) |
| 83 | + .setDefault(16_384) |
| 84 | + .help("Records to buffer"); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void run(final Environment environment, final Namespace namespace, |
| 89 | + final WhisperServerConfiguration configuration, final CommandDependencies commandDependencies) throws Exception { |
| 90 | + final int bufferSize = namespace.getInt(BUFFER_ARGUMENT); |
| 91 | + final int segments = Objects.requireNonNull(namespace.getInt(SEGMENT_COUNT_ARGUMENT)); |
| 92 | + final int concurrency = Objects.requireNonNull(namespace.getInt(MAX_CONCURRENCY_ARGUMENT)); |
| 93 | + final boolean dryRun = namespace.getBoolean(DRY_RUN_ARGUMENT); |
| 94 | + |
| 95 | + logger.info("Crawling issuedReceipts with {} segments and {} processors", |
| 96 | + segments, |
| 97 | + Runtime.getRuntime().availableProcessors()); |
| 98 | + |
| 99 | + final Counter inspected = Metrics.counter(INSPECTED_ISSUED_RECEIPTS, |
| 100 | + "dryRun", Boolean.toString(dryRun)); |
| 101 | + final Counter migrated = Metrics.counter(MIGRATED_ISSUED_RECEIPTS, |
| 102 | + "dryRun", Boolean.toString(dryRun)); |
| 103 | + |
| 104 | + final IssuedReceiptsManager issuedReceiptsManager = commandDependencies.issuedReceiptsManager(); |
| 105 | + final Flux<IssuedReceiptsManager.IssuedReceipt> receipts = |
| 106 | + issuedReceiptsManager.receiptsWithoutTagSet(segments, Schedulers.parallel()); |
| 107 | + final long count = bufferShuffle(receipts, bufferSize) |
| 108 | + .doOnNext(issuedReceipt -> inspected.increment()) |
| 109 | + .flatMap(issuedReceipt -> Mono |
| 110 | + .fromCompletionStage(() -> dryRun |
| 111 | + ? CompletableFuture.completedFuture(null) |
| 112 | + : issuedReceiptsManager.migrateToTagSet(issuedReceipt)) |
| 113 | + .thenReturn(true) |
| 114 | + .retry(3) |
| 115 | + .onErrorResume(throwable -> { |
| 116 | + logger.error("Failed to migrate {} after 3 attempts, giving up", issuedReceipt.itemId(), throwable); |
| 117 | + return Mono.just(false); |
| 118 | + }), |
| 119 | + concurrency) |
| 120 | + .doOnNext(success -> |
| 121 | + Metrics.counter(MIGRATED_ISSUED_RECEIPTS, |
| 122 | + "dryRun", Boolean.toString(dryRun), |
| 123 | + "success", Boolean.toString(success))) |
| 124 | + .count() |
| 125 | + .block(); |
| 126 | + logger.info("Attempted to migrate {} issued receipts", count); |
| 127 | + } |
| 128 | + |
| 129 | + private static <T> Flux<T> bufferShuffle(Flux<T> f, int bufferSize) { |
| 130 | + return f.buffer(bufferSize) |
| 131 | + .map(source -> { |
| 132 | + final ArrayList<T> shuffled = new ArrayList<>(source); |
| 133 | + Collections.shuffle(shuffled); |
| 134 | + return shuffled; |
| 135 | + }) |
| 136 | + .limitRate(2) |
| 137 | + .flatMapIterable(Function.identity()); |
| 138 | + } |
| 139 | +} |
0 commit comments