clohfink commented on code in PR #3367:
URL: https://github.com/apache/cassandra/pull/3367#discussion_r1683363500


##########
src/java/org/apache/cassandra/repair/autorepair/AutoRepair.java:
##########
@@ -0,0 +1,401 @@
+/*
+ * 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.autorepair;
+
+import java.util.ArrayList;
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableList;
+
+import org.apache.cassandra.repair.RepairRunnable;
+import org.apache.cassandra.utils.Pair;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.concurrent.NamedThreadFactory;
+import org.apache.cassandra.concurrent.ScheduledExecutorPlus;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.db.ColumnFamilyStore;
+import org.apache.cassandra.db.Keyspace;
+import org.apache.cassandra.dht.Range;
+import org.apache.cassandra.dht.Token;
+import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.gms.Gossiper;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Tables;
+import org.apache.cassandra.service.AutoRepairService;
+import org.apache.cassandra.utils.FBUtilities;
+import org.apache.cassandra.repair.autorepair.AutoRepairUtils.RepairTurn;
+
+import static 
org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory;
+import static 
org.apache.cassandra.repair.autorepair.AutoRepairUtils.RepairTurn.MY_TURN;
+import static 
org.apache.cassandra.repair.autorepair.AutoRepairUtils.RepairTurn.MY_TURN_DUE_TO_PRIORITY;
+import static 
org.apache.cassandra.repair.autorepair.AutoRepairUtils.RepairTurn.MY_TURN_FORCE_REPAIR;
+
+public class AutoRepair
+{
+    // Initial delay for repair session to start after setup
+    final static long INITIAL_REPAIR_DELAY_SEC = 30;
+
+    private static final Logger logger = 
LoggerFactory.getLogger(AutoRepair.class);
+
+    @VisibleForTesting
+    protected static Supplier<Long> timeFunc = System::currentTimeMillis;
+
+    public static AutoRepair instance = new AutoRepair();
+
+    @VisibleForTesting
+    protected final Map<AutoRepairConfig.RepairType, ScheduledExecutorPlus> 
repairExecutors;
+    @VisibleForTesting
+    protected final Map<AutoRepairConfig.RepairType, AutoRepairState> 
repairStates;
+
+    protected final Map<AutoRepairConfig.RepairType, 
IAutoRepairTokenRangeSplitter> tokenRangeSplitters = new 
EnumMap<>(AutoRepairConfig.RepairType.class);
+
+
+    @VisibleForTesting
+    protected AutoRepair()
+    {
+        AutoRepairConfig config = DatabaseDescriptor.getAutoRepairConfig();
+        repairExecutors = new EnumMap<>(AutoRepairConfig.RepairType.class);
+        repairStates = new EnumMap<>(AutoRepairConfig.RepairType.class);
+        for (AutoRepairConfig.RepairType repairType : 
AutoRepairConfig.RepairType.values())
+        {
+            repairExecutors.put(repairType, executorFactory().scheduled(false, 
"AutoRepair-Repair-" + repairType, Thread.NORM_PRIORITY));
+            repairStates.put(repairType, 
AutoRepairConfig.RepairType.getAutoRepairState(repairType));
+            tokenRangeSplitters.put(repairType, 
FBUtilities.newAutoRepairTokenRangeSplitter(config.getTokenRangeSplitter(repairType)));
+        }
+    }
+
+    public void setup()
+    {
+        verifyIsSafeToEnable();
+
+        AutoRepairConfig config = DatabaseDescriptor.getAutoRepairConfig();
+        AutoRepairService.setup();
+        AutoRepairUtils.setup();
+
+        for (AutoRepairConfig.RepairType repairType : 
AutoRepairConfig.RepairType.values())
+        {
+            repairExecutors.get(repairType).scheduleWithFixedDelay(
+            () -> repair(repairType, 60000),
+            INITIAL_REPAIR_DELAY_SEC,
+            config.getRepairCheckInterval().toSeconds(),
+            TimeUnit.SECONDS);
+        }
+    }
+
+    @VisibleForTesting
+    protected void verifyIsSafeToEnable()
+    {
+        AutoRepairConfig config = DatabaseDescriptor.getAutoRepairConfig();
+        if 
(config.isAutoRepairEnabled(AutoRepairConfig.RepairType.incremental) &&
+            (DatabaseDescriptor.getMaterializedViewsEnabled() || 
DatabaseDescriptor.isCDCEnabled()))
+            throw new ConfigurationException("Cannot enable incremental repair 
with materialized views or CDC enabled");
+    }
+
+    // repairAsync runs a repair session of the given type asynchronously.
+    public void repairAsync(AutoRepairConfig.RepairType repairType, long 
millisToWait)
+    {
+        repairExecutors.get(repairType).submit(() -> repair(repairType, 
millisToWait));
+    }
+
+    // repair runs a repair session of the given type synchronously.
+    public void repair(AutoRepairConfig.RepairType repairType, long 
millisToWait)

Review Comment:
   NP: maybe simplify this method a little breaking it up, its very long and 
deep



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