aweisberg commented on code in PR #4696: URL: https://github.com/apache/cassandra/pull/4696#discussion_r3074835019
########## src/java/org/apache/cassandra/repair/MutationTrackingIncrementalRepairTask.java: ########## @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.repair; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.cassandra.concurrent.ExecutorPlus; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Token; +import org.apache.cassandra.replication.MutationTrackingSyncCoordinator; +import org.apache.cassandra.schema.KeyspaceMetadata; +import org.apache.cassandra.tcm.ClusterMetadata; +import org.apache.cassandra.utils.Throwables; +import org.apache.cassandra.utils.TimeUUID; +import org.apache.cassandra.utils.concurrent.AsyncPromise; +import org.apache.cassandra.utils.concurrent.Future; + +import static com.google.common.base.Preconditions.checkState; + +/** Repair task that syncs mutation tracking offsets across replicas */ +public class MutationTrackingIncrementalRepairTask extends AbstractRepairTask +{ + + private final TimeUUID parentSession; + private final RepairCoordinator.NeighborsAndRanges neighborsAndRanges; + private final String[] cfnames; + private final ClusterMetadata metadata; + + protected MutationTrackingIncrementalRepairTask(RepairCoordinator coordinator, + TimeUUID parentSession, + RepairCoordinator.NeighborsAndRanges neighborsAndRanges, + String[] cfnames) + { + super(coordinator); + this.parentSession = parentSession; + this.neighborsAndRanges = neighborsAndRanges; + this.cfnames = cfnames; + this.metadata = coordinator.metadata; + } + + @Override + public String name() + { + return "MutationTrackingRepair"; + } + + @Override + public Future<CoordinatedRepairResult> performUnsafe(ExecutorPlus executor, Scheduler validationScheduler) + { + List<CommonRange> allRanges = neighborsAndRanges.filterCommonRanges(keyspace, cfnames); + checkState(!allRanges.isEmpty(), "No ranges to repair"); + + List<MutationTrackingSyncCoordinator> syncCoordinators = new ArrayList<>(); + List<Collection<Range<Token>>> rangeCollections = new ArrayList<>(); + + for (CommonRange commonRange : allRanges) + { + for (Range<Token> range : commonRange.ranges) + { + RepairJobDesc desc = new RepairJobDesc(parentSession, TimeUUID.Generator.nextTimeUUID(), + keyspace, "Mutation Tracking Sync", List.of(range)); + MutationTrackingSyncCoordinator syncCoordinator = + new MutationTrackingSyncCoordinator(coordinator.ctx, desc, commonRange.endpoints, metadata); + syncCoordinator.start(); + syncCoordinators.add(syncCoordinator); + rangeCollections.add(List.of(range)); + + logger.info("Started mutation tracking sync for range {}", range); + } + } + + coordinator.notifyProgress("Started mutation tracking sync for " + syncCoordinators.size() + " ranges"); + + AsyncPromise<CoordinatedRepairResult> resultPromise = new AsyncPromise<>(); + + executor.execute(() -> { + try + { + waitForSyncCompletion(syncCoordinators, rangeCollections, resultPromise); + } + catch (InterruptedException e) + { + try + { + resultPromise.tryFailure(new RuntimeException("Interrupted waiting for Mutation Tracking sync coordinators to finish", e)); + } + finally + { + Thread.currentThread().interrupt(); + } + } + catch (Exception e) + { + logger.error("Error during mutation tracking repair", e); + resultPromise.tryFailure(e); + } + }); + + return resultPromise; + } + + private void waitForSyncCompletion(List<MutationTrackingSyncCoordinator> syncCoordinators, + List<Collection<Range<Token>>> rangeCollections, + AsyncPromise<CoordinatedRepairResult> resultPromise) throws Exception + { + long deadlineNanos = coordinator.ctx.clock().nanoTime() + TimeUnit.MILLISECONDS.toNanos( + DatabaseDescriptor.getMutationTrackingSyncTimeout(TimeUnit.MILLISECONDS)); + Exception error = null; + for (MutationTrackingSyncCoordinator syncCoordinator : syncCoordinators) + { + long remainingNanos = deadlineNanos - coordinator.ctx.clock().nanoTime(); + try + { + syncCoordinator.awaitCompletion(remainingNanos, TimeUnit.NANOSECONDS); + } + catch (InterruptedException e) + { + try + { + syncCoordinators.forEach(MutationTrackingSyncCoordinator::cancel); + } + finally + { + throw e; + } + } + catch (TimeoutException e) + { + error = Throwables.merge(error, new RuntimeException("Mutation tracking sync timed out", e)); + } + catch (Exception e) + { + error = Throwables.merge(error, e); Review Comment: I don't think we need to/should? ExecutionException is part of the chain of exceptions? It's the caller that catches the exceptions that needs to analyze the chain and decide what to do. Reality here is that we don't care what errors happened and we aren't going to handle them individually we are just going to mark it as failed and log them. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

