nvharikrishna commented on code in PR #231:
URL: https://github.com/apache/cassandra-sidecar/pull/231#discussion_r2662539078


##########
server/src/main/java/org/apache/cassandra/sidecar/job/RepairJob.java:
##########
@@ -0,0 +1,380 @@
+/*
+ * 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.sidecar.job;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.vertx.core.Future;
+import io.vertx.core.Promise;
+import org.apache.cassandra.sidecar.adapters.base.RepairOptions;
+import org.apache.cassandra.sidecar.common.data.OperationalJobStatus;
+import org.apache.cassandra.sidecar.common.request.data.RepairPayload;
+import org.apache.cassandra.sidecar.common.server.StorageOperations;
+import org.apache.cassandra.sidecar.concurrent.TaskExecutorPool;
+import org.apache.cassandra.sidecar.config.RepairJobsConfiguration;
+import org.apache.cassandra.sidecar.handlers.data.RepairRequestParam;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Implementation of {@link OperationalJob} to perform repair operation.
+ * <p>
+ * Cassandra repair operations are asynchronous and can run for extended 
periods. When a repair is
+ * detected to be IN_PROGRESS, this job sets its status to RUNNING and 
completes its promise.
+ * <p>
+ * The job's status can be checked at any time using the {@link #status()} 
method, which directly
+ * queries the current repair status from Cassandra, regardless of whether the 
promise has been completed.
+ * <p>
+ * This design ensures timely responses to clients while allowing accurate 
status reporting for
+ * long-running repair operations.
+ */
+public class RepairJob extends OperationalJob
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(RepairJob.class);
+    private static final String OPERATION = "repair";
+    private static final String PREVIEW_KIND_REPAIRED = "REPAIRED";
+
+    private final RepairRequestParam repairParams;
+    private final RepairJobsConfiguration config;
+    private final TaskExecutorPool internalPool;
+    protected StorageOperations storageOperations;
+    private volatile OperationalJobStatus currentStatus;
+    private volatile int commandId = -1; // Store the command ID for status 
checks
+
+    /**
+     * Enum representing the status of a parent repair session
+     */
+    public enum ParentRepairStatus
+    {
+        IN_PROGRESS, COMPLETED, FAILED
+    }
+
+    /**
+     * Constructor for creation of RepairJob
+     *
+     * @param taskExecutorPool    TaskExecutorPool instance (for testing)
+     * @param config              Repair job configuration
+     * @param jobId               UUID representing the Job to be created
+     * @param storageOps          Reference to the storage operations interface
+     * @param repairParams        Repair request parameters
+     */
+    public RepairJob(TaskExecutorPool taskExecutorPool,
+                     RepairJobsConfiguration config,
+                     UUID jobId,
+                     StorageOperations storageOps,
+                     RepairRequestParam repairParams)
+    {
+        super(jobId);
+        this.internalPool = taskExecutorPool;
+        this.config = config;
+        this.storageOperations = storageOps;
+        this.repairParams = repairParams;
+        this.currentStatus = OperationalJobStatus.CREATED;
+    }
+
+    /**
+     * {@inheritDoc}
+     * RepairJob allows parallel executions since multiple repairs can run 
concurrently
+     * on different tables or with different parameters.
+     */
+    @Override
+    public boolean hasConflict(@NotNull List<OperationalJob> sameOperationJobs)
+    {
+        // For now, we simply allow all repair jobs to run in parallel
+        // A more sophisticated implementation could check the repair 
parameters
+        // against currently running repairs to detect potential conflicts
+        return false;
+    }
+
+    @Override
+    protected Future<Void> executeInternal()
+    {
+        try
+        {
+            Map<String, String> options = 
generateRepairOptions(repairParams.requestPayload());
+            String keyspace = repairParams.keyspace().name();
+
+            LOGGER.info("Executing repair operation for keyspace {} jobId={}", 
keyspace, this.jobId());
+
+            try
+            {
+                commandId = storageOperations.repair(keyspace, options);
+            }
+            catch (Exception e)
+            {
+                LOGGER.error("Failed to initiate repair for keyspace {} 
jobId={}", keyspace, this.jobId(), e);
+                currentStatus = OperationalJobStatus.FAILED;
+                return Future.failedFuture(e);
+            }
+
+            if (commandId <= 0)
+            {
+                // When there are no relevant token ranges for the keyspace or 
RF is 1, the repair is inapplicable
+                LOGGER.info("Repair is not applicable for the provided options 
and keyspace '{}' jobId '{}'", keyspace, this.jobId());
+                currentStatus = OperationalJobStatus.SUCCEEDED;
+                return Future.succeededFuture();
+            }
+
+            // Create promise only when we need it for periodic status checking
+            final Promise<Void> repairJobPromise = Promise.promise();
+            int maxAttempts = config.repairStatusMaxAttempts();
+            final AtomicInteger attemptCounter = new AtomicInteger(0);
+
+            // Periodic timer that checks for a valid repair status for a 
specified no. attempts to make a best-effort attempt
+            // to validate that the repair has been kicked-off before 
returning.
+            internalPool.setPeriodic(0, 
config.repairPollInterval().toIntMillis(), id -> {
+                try
+                {
+                    int currentAttempt = attemptCounter.incrementAndGet();
+                    if (currentAttempt > maxAttempts)
+                    {
+                        internalPool.cancelTimer(id);
+                        String msg = String.format("Failed to obtain repair 
status after %d attempts.", maxAttempts);
+                        LOGGER.warn(msg);
+                        // Set status to RUNNING and complete the promise to 
ensure the job is properly handled

Review Comment:
   ^ response deserves to go to the comment :)



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