nvharikrishna commented on code in PR #231: URL: https://github.com/apache/cassandra-sidecar/pull/231#discussion_r2661398151
########## server/src/main/java/org/apache/cassandra/sidecar/handlers/validations/ValidateKeyspaceExistenceHandler.java: ########## @@ -0,0 +1,97 @@ +/* + * 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.handlers.validations; + +import com.datastax.driver.core.KeyspaceMetadata; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.net.SocketAddress; +import io.vertx.ext.web.RoutingContext; +import org.apache.cassandra.sidecar.common.server.data.Name; +import org.apache.cassandra.sidecar.concurrent.ExecutorPools; +import org.apache.cassandra.sidecar.handlers.AbstractHandler; +import org.apache.cassandra.sidecar.routes.RoutingContextUtils; +import org.apache.cassandra.sidecar.utils.CassandraInputValidator; +import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher; +import org.jetbrains.annotations.NotNull; + +import static org.apache.cassandra.sidecar.utils.HttpExceptions.wrapHttpException; + +/** + * Validate the request keyspace should exist in Cassandra, when the endpoint Review Comment: ```suggestion * Validates that the requested keyspace exists in Cassandra when the endpoint ``` Does it sounds better? ########## server/src/main/java/org/apache/cassandra/sidecar/handlers/data/RepairRequestParam.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.handlers.data; + + +import org.apache.cassandra.sidecar.common.request.data.RepairPayload; +import org.apache.cassandra.sidecar.common.server.data.Name; + +/** + * Holder class for the {@link org.apache.cassandra.sidecar.handlers.RepairHandler} + * request parameters + */ +public class RepairRequestParam +{ + private final Name keyspace; + private final RepairPayload repairRequestPayload; + + private RepairRequestParam(Name keyspace, RepairPayload requestpayload) + { + + this.keyspace = keyspace; + this.repairRequestPayload = requestpayload; + } + + public static RepairRequestParam from(Name keyspace, RepairPayload payload) + { + return new RepairRequestParam(keyspace, payload); + } + + /** + * @return the keyspace in Cassandra + */ + public Name keyspace() + { + return keyspace; + } + + /** + * @return the Repair request payload + */ + public RepairPayload requestPayload() + { + return repairRequestPayload; Review Comment: Three different names used for same member (repairRequestPayload, repairPayload and payload). Maybe we can stick to one variable name? ########## server/src/main/java/org/apache/cassandra/sidecar/handlers/validations/ValidateTableExistenceHandler.java: ########## @@ -76,45 +75,52 @@ protected void handleInternal(RoutingContext context, return; } - getKeyspaceMetadata(host, input.maybeQuotedKeyspace()) - .onFailure(context::fail) // fail the request with the internal server error thrown from getKeyspaceMetadata - .onSuccess(keyspaceMetadata -> { - if (keyspaceMetadata == null) + ValidationUtils.requireKeyspaceExists(metadataFetcher, executorPools, host, input.keyspace()) + .onComplete(ar -> { + if (ar.failed()) { - context.fail(wrapHttpException(HttpResponseStatus.NOT_FOUND, - "Keyspace " + input.keyspace() + " was not found")); + // Handle failure + if (ar.cause().getMessage().contains("not found")) + { + context.fail(wrapHttpException(HttpResponseStatus.NOT_FOUND, ar.cause().getMessage())); + } + else + { + context.fail(ar.cause()); + } return; } - + + // Store metadata in context + KeyspaceMetadata keyspaceMetadata = ar.result(); RoutingContextUtils.put(context, RoutingContextUtils.SC_KEYSPACE_METADATA, keyspaceMetadata); - String table = input.maybeQuotedTableName(); Review Comment: Wondering why it is removed... ########## server/src/main/java/org/apache/cassandra/sidecar/handlers/data/RepairRequestParam.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.handlers.data; + + +import org.apache.cassandra.sidecar.common.request.data.RepairPayload; +import org.apache.cassandra.sidecar.common.server.data.Name; + +/** + * Holder class for the {@link org.apache.cassandra.sidecar.handlers.RepairHandler} + * request parameters + */ +public class RepairRequestParam +{ + private final Name keyspace; + private final RepairPayload repairRequestPayload; + + private RepairRequestParam(Name keyspace, RepairPayload requestpayload) Review Comment: ```suggestion private RepairRequestParam(Name keyspace, RepairPayload requestPayload) ``` ########## server/src/test/java/org/apache/cassandra/sidecar/handlers/RepairHandlerTest.java: ########## @@ -0,0 +1,306 @@ +/* + * 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.handlers; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.TableMetadata; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.util.Modules; +import io.vertx.core.Vertx; +import io.vertx.ext.web.client.WebClient; +import io.vertx.junit5.VertxExtension; +import io.vertx.junit5.VertxTestContext; +import org.apache.cassandra.sidecar.TestModule; +import org.apache.cassandra.sidecar.cluster.CassandraAdapterDelegate; +import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata; +import org.apache.cassandra.sidecar.common.request.data.RepairPayload; +import org.apache.cassandra.sidecar.common.response.OperationalJobResponse; +import org.apache.cassandra.sidecar.common.server.StorageOperations; +import org.apache.cassandra.sidecar.modules.SidecarModules; +import org.apache.cassandra.sidecar.server.Server; +import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher; +import org.mockito.AdditionalAnswers; +import org.mockito.ArgumentCaptor; + +import static io.netty.handler.codec.http.HttpResponseStatus.ACCEPTED; +import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static org.apache.cassandra.sidecar.common.data.OperationalJobStatus.CREATED; +import static org.apache.cassandra.sidecar.common.data.OperationalJobStatus.FAILED; +import static org.apache.cassandra.sidecar.common.data.OperationalJobStatus.SUCCEEDED; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for the {@link RepairHandler} + */ +@ExtendWith(VertxExtension.class) +public class RepairHandlerTest +{ + static final Logger LOGGER = LoggerFactory.getLogger(RepairHandlerTest.class); + private static final String REPAIR_ROUTE = "/api/v1/cassandra/keyspaces/testkeyspace/repair"; + Vertx vertx; + Server server; + StorageOperations mockStorageOperations = mock(StorageOperations.class); + InstanceMetadataFetcher mockMetadataFetcher = mock(InstanceMetadataFetcher.class); + + @BeforeEach + void before() throws InterruptedException + { + // Set up the mock metadata chain + InstanceMetadata mockInstanceMetadata = mock(InstanceMetadata.class); + CassandraAdapterDelegate mockDelegate = mock(CassandraAdapterDelegate.class); + Metadata mockMetadata = mock(Metadata.class); + KeyspaceMetadata mockKeyspaceMetadata = mock(KeyspaceMetadata.class); + TableMetadata mockTableMetadata = mock(TableMetadata.class); + + // Configure the mock chain Review Comment: Deep stubbing may help to avoid wiring each and every mock. Worth giving a try... ########## 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: Shouldn't it be a failed/completed state? Can you elaborate on this comment? Intended to say that the Cassandra instance will keep running the repair, but Sidecar won't pool the status after maxAttempts? ########## server/src/main/java/org/apache/cassandra/sidecar/job/RepairJob.java: ########## @@ -0,0 +1,371 @@ +/* + * 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.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, NEW_STATUS + } + + /** + * 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() + { + final Promise<Void> repairJobPromise = Promise.promise(); + 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(); + } + + 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 + currentStatus = OperationalJobStatus.RUNNING; + repairJobPromise.tryComplete(); + return; + } + + // Check the repair status + List<String> status; + try + { + status = storageOperations.getParentRepairStatus(commandId); + } + catch (Exception e) + { + LOGGER.warn("Failed to get repair status for cmd: {} (attempt {}/{})", + commandId, currentAttempt, maxAttempts, e); + // Continue polling on exception + return; + } + + // If status is empty, continue polling + if (status == null || status.isEmpty()) + { + LOGGER.debug("No parent repair session status found for cmd: {} - repair may be initializing (attempt {}/{})", + commandId, currentAttempt, maxAttempts); + return; + } + + internalPool.cancelTimer(id); + updateRepairJobStatus(repairJobPromise, status); + } + catch (Exception e) + { + LOGGER.error("Unexpected error in repair status check", e); + internalPool.cancelTimer(id); + currentStatus = OperationalJobStatus.FAILED; + repairJobPromise.tryFail(e); + } + }); + + return repairJobPromise.future(); + } + catch (Exception e) + { + // Catch any exceptions in the overall method + LOGGER.error("Failed to execute repair job", e); + currentStatus = OperationalJobStatus.FAILED; + return Future.failedFuture(e); + } + } + + + @Override + public OperationalJobStatus status() + { + // If we have a valid command ID, check the actual repair status + if (commandId > 0) + { + List<String> status = storageOperations.getParentRepairStatus(commandId); + if (status != null && !status.isEmpty()) + { + try + { + ParentRepairStatus parentRepairStatus = ParentRepairStatus.valueOf(status.get(0)); + switch (parentRepairStatus) + { + case COMPLETED: + return OperationalJobStatus.SUCCEEDED; + case FAILED: + return OperationalJobStatus.FAILED; + case IN_PROGRESS: + return OperationalJobStatus.RUNNING; + default: + LOGGER.warn("Encountered unexpected repair status: {}", parentRepairStatus); + // Don't update currentStatus here, fall back to parent implementation + } + } + catch (IllegalArgumentException e) + { + LOGGER.warn("Invalid parent repair status: {}", status.get(0), e); + // Don't update currentStatus here, fall back to parent implementation + } + } + } + + // If we have a current status, return it + if (currentStatus != null) + { + return currentStatus; + } + + // Otherwise, fall back to the parent implementation + return super.status(); + } + + /** + * Updates the repair job status based on the parent repair status from Cassandra. + * <p> + * When the parent repair status is IN_PROGRESS, this method sets the currentStatus + * to RUNNING and completes the promise + * <p> + * This approach ensures that resources are properly managed while still providing + * accurate status reporting through the {@link #status()} method. + * + * @param repairJobPromise the promise to complete + * @param status the parent repair status from Cassandra + */ + private void updateRepairJobStatus(Promise<Void> repairJobPromise, List<String> status) + { + ParentRepairStatus parentRepairStatus = ParentRepairStatus.valueOf(status.get(0)); + List<String> messages = status.subList(1, status.size()); + + LOGGER.info("Parent repair session {} has {} status. Messages: {}", + parentRepairStatus.name().toLowerCase(), + parentRepairStatus, + String.join("\n", messages)); + switch (parentRepairStatus) + { + case COMPLETED: + currentStatus = OperationalJobStatus.SUCCEEDED; + repairJobPromise.tryComplete(); + break; + case FAILED: + currentStatus = OperationalJobStatus.FAILED; + String reason = !messages.isEmpty() ? messages.get(0) : + "Repair failed with no error message"; + repairJobPromise.tryFail(new IOException(reason)); + break; + case IN_PROGRESS: + currentStatus = OperationalJobStatus.RUNNING; + repairJobPromise.tryComplete(); + break; + default: + String message = String.format("Encountered unexpected repair status: %s Messages: %s", + parentRepairStatus, String.join("\n", messages)); + LOGGER.error(message); + currentStatus = OperationalJobStatus.FAILED; + repairJobPromise.tryFail(message); + break; + } + } + + /** + * {@inheritDoc} + */ + @Override + public String name() + { + return OPERATION; + } + + private Map<String, String> generateRepairOptions(RepairPayload repairPayload) + { + Map<String, String> options = new HashMap<>(); + + List<String> tables = repairPayload.tables(); + if (tables != null && !tables.isEmpty()) + { + options.put(RepairOptions.COLUMNFAMILIES.optionName(), String.join(",", tables)); + } + + Boolean isPrimaryRange = repairPayload.isPrimaryRange(); + if (isPrimaryRange != null) + { + options.put(RepairOptions.PRIMARY_RANGE.optionName(), String.valueOf(isPrimaryRange)); + } + // TODO: Verify use-cases involving multiple DCs + + String dc = repairPayload.datacenter(); + if (dc != null) + { + options.put(RepairOptions.DATACENTERS.optionName(), dc); + } + + List<String> hosts = repairPayload.hosts(); + if (hosts != null && !hosts.isEmpty()) + { + options.put(RepairOptions.HOSTS.optionName(), String.join(",", hosts)); + } + + if (repairPayload.startToken() != null && repairPayload.endToken() != null) Review Comment: > Curently, it expects both tokens, with the intention to not have silent defaults, since repair is an expensive operation. Sounds good! Do we validate it anywhere (RepairHandler or any other place)? ########## 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 + currentStatus = OperationalJobStatus.RUNNING; + repairJobPromise.tryComplete(); + return; + } + + // Check the repair status + List<String> status; + try + { + status = storageOperations.getParentRepairStatus(commandId); + } + catch (Exception e) + { + LOGGER.warn("Failed to get repair status for cmd: {} (attempt {}/{})", + commandId, currentAttempt, maxAttempts, e); + // Continue polling on exception + return; + } + + // If status is empty, continue polling + if (status == null || status.isEmpty()) + { + LOGGER.debug("No parent repair session status found for cmd: {} - repair may be initializing (attempt {}/{})", + commandId, currentAttempt, maxAttempts); + return; + } + + internalPool.cancelTimer(id); + updateRepairJobStatus(repairJobPromise, status); + } + catch (Exception e) + { + LOGGER.error("Unexpected error in repair status check", e); + internalPool.cancelTimer(id); + currentStatus = OperationalJobStatus.FAILED; + repairJobPromise.tryFail(e); + } + }); + + return repairJobPromise.future(); + } + catch (Exception e) + { + // Catch any exceptions in the overall method + LOGGER.error("Failed to execute repair job", e); + currentStatus = OperationalJobStatus.FAILED; + return Future.failedFuture(e); + } + } + + + @Override + public OperationalJobStatus status() + { + // If we have a valid command ID, check the actual repair status + if (commandId > 0) + { + List<String> status = storageOperations.getParentRepairStatus(commandId); + if (status != null && !status.isEmpty()) + { + try + { + ParentRepairStatus parentRepairStatus = ParentRepairStatus.valueOf(status.get(0)); + switch (parentRepairStatus) + { + case COMPLETED: + return OperationalJobStatus.SUCCEEDED; + case FAILED: + return OperationalJobStatus.FAILED; + case IN_PROGRESS: + return OperationalJobStatus.RUNNING; + default: + LOGGER.warn("Encountered unexpected repair status: {}", parentRepairStatus); + // Don't update currentStatus here, fall back to parent implementation + } + } + catch (IllegalArgumentException e) + { + LOGGER.warn("Invalid parent repair status: {}", status.get(0), e); + // Don't update currentStatus here, fall back to parent implementation + } + } + } + + // If we have a current status, return it + if (currentStatus != null) + { + return currentStatus; + } + + // Otherwise, fall back to the parent implementation + return super.status(); + } + + /** + * Updates the repair job status based on the parent repair status from Cassandra. + * <p> + * When the parent repair status is IN_PROGRESS, this method sets the currentStatus + * to RUNNING and completes the promise + * <p> + * This approach ensures that resources are properly managed while still providing + * accurate status reporting through the {@link #status()} method. + * + * @param repairJobPromise the promise to complete + * @param status the parent repair status from Cassandra + */ + private void updateRepairJobStatus(Promise<Void> repairJobPromise, List<String> status) + { + ParentRepairStatus parentRepairStatus = ParentRepairStatus.valueOf(status.get(0)); + List<String> messages = status.subList(1, status.size()); + + LOGGER.info("Parent repair session {} has {} status. Messages: {}", + parentRepairStatus.name().toLowerCase(), + parentRepairStatus, + String.join("\n", messages)); + switch (parentRepairStatus) + { + case COMPLETED: + currentStatus = OperationalJobStatus.SUCCEEDED; + repairJobPromise.tryComplete(); + break; + case FAILED: + currentStatus = OperationalJobStatus.FAILED; + String reason = !messages.isEmpty() ? messages.get(0) : + "Repair failed with no error message"; + repairJobPromise.tryFail(new IOException(reason)); + break; + case IN_PROGRESS: + currentStatus = OperationalJobStatus.RUNNING; + repairJobPromise.tryComplete(); Review Comment: RUNNING is not a final state, is it? Wondering why the promise is completed. -- 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]

