aweisberg commented on code in PR #4696:
URL: https://github.com/apache/cassandra/pull/4696#discussion_r3040764522


##########
src/java/org/apache/cassandra/repair/MutationTrackingIncrementalRepairTask.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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 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.service.replication.migration.KeyspaceMigrationInfo;
+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;
+
+/** 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);
+
+        if (allRanges.isEmpty())
+        {
+            logger.info("No common ranges to repair for keyspace {}", 
keyspace);
+            return new 
AsyncPromise<CoordinatedRepairResult>().setSuccess(CoordinatedRepairResult.create(List.of(),
 List.of()));
+        }
+
+        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 (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 InterruptedException
+    {
+        long deadlineNanos = coordinator.ctx.clock().nanoTime() + 
TimeUnit.MILLISECONDS.toNanos(
+            
DatabaseDescriptor.getMutationTrackingSyncTimeout(TimeUnit.MILLISECONDS));
+        boolean allSucceeded = true;
+        Throwable error = null;
+        for (MutationTrackingSyncCoordinator syncCoordinator : 
syncCoordinators)
+        {
+            long remainingNanos = deadlineNanos - 
coordinator.ctx.clock().nanoTime();
+            try
+            {
+                if (!syncCoordinator.awaitCompletion(remainingNanos, 
TimeUnit.NANOSECONDS))
+                    allSucceeded = false;
+            }
+            catch (RuntimeException e)
+            {
+                allSucceeded = false;
+                error = Throwables.merge(error, e);
+            }
+            finally
+            {
+                if (!allSucceeded)
+                    syncCoordinator.cancel();
+            }
+        }
+
+        if (error != null)
+        {
+            logger.warn("Mutation tracking sync failed for keyspace {}", 
keyspace, error);
+            resultPromise.tryFailure(error);
+            return;
+        }
+
+        if (!allSucceeded)
+        {
+            logger.warn("Mutation tracking sync timed out for keyspace {}", 
keyspace);
+            resultPromise.tryFailure(new RuntimeException("Mutation tracking 
sync timed out for some ranges"));
+            return;
+        }
+
+        coordinator.notifyProgress("Mutation tracking sync completed for all 
ranges");
+
+        List<RepairSessionResult> results = new ArrayList<>();
+        for (int i = 0; i < rangeCollections.size(); i++)
+        {
+            Collection<Range<Token>> ranges = rangeCollections.get(i);
+            results.add(new RepairSessionResult(parentSession, keyspace, 
ranges, List.of(), false));
+        }
+        
resultPromise.trySuccess(CoordinatedRepairResult.create(rangeCollections, 
results));
+    }
+
+    /**
+     * Determines if this keyspace should use mutation tracking incremental 
repair.
+     * Returns true if:
+     * - Keyspace uses mutation tracking replication, OR
+     * - Keyspace is currently migrating (either direction)

Review Comment:
   I'll address that in the follow up where I am changing migration from 
tracked to untracked to be instant.



-- 
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]

Reply via email to