nvharikrishna commented on code in PR #259: URL: https://github.com/apache/cassandra-sidecar/pull/259#discussion_r2461739393
########## server/src/main/java/org/apache/cassandra/sidecar/handlers/livemigration/LiveMigrationStatusGetHandler.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.livemigration; + +import java.util.Set; + +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.json.Json; +import io.vertx.core.net.SocketAddress; +import io.vertx.ext.auth.authorization.Authorization; +import io.vertx.ext.web.RoutingContext; +import org.apache.cassandra.sidecar.acl.authorization.BasicPermissions; +import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata; +import org.apache.cassandra.sidecar.concurrent.ExecutorPools; +import org.apache.cassandra.sidecar.handlers.AbstractHandler; +import org.apache.cassandra.sidecar.handlers.AccessProtected; +import org.apache.cassandra.sidecar.livemigration.LiveMigrationStatusTracker; +import org.apache.cassandra.sidecar.utils.CassandraInputValidator; +import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher; +import org.jetbrains.annotations.NotNull; + +/** + * Handler to retrieve the current live migration status for an instance. + * <p> + * This endpoint allows clients to query the migration state of a specific instance, + * returning a {@link org.apache.cassandra.sidecar.common.response.LiveMigrationStatus} + * object containing the state (COMPLETED or NOT_COMPLETED) and timestamp. + */ +@Singleton +public class LiveMigrationStatusGetHandler extends AbstractHandler<Void> implements AccessProtected +{ + + private final LiveMigrationStatusTracker statusTracker; + + @Inject + public LiveMigrationStatusGetHandler(InstanceMetadataFetcher metadataFetcher, + ExecutorPools executorPools, + CassandraInputValidator validator, + LiveMigrationStatusTracker statusTracker) + { + super(metadataFetcher, executorPools, validator); + this.statusTracker = statusTracker; + } + + @Override + protected Void extractParamsOrThrow(RoutingContext routingContext) + { + return null; + } + + @Override + protected void handleInternal(RoutingContext routingContext, HttpServerRequest httpServerRequest, + @NotNull String host, SocketAddress socketAddress, Void unused) + { + InstanceMetadata instance = metadataFetcher.instance(host); + statusTracker.getMigrationStatus(instance) + .compose(status -> routingContext.response().send(Json.encode(status))) Review Comment: done ########## server/src/main/java/org/apache/cassandra/sidecar/handlers/livemigration/LiveMigrationStatusClearHandler.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.livemigration; + +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.inject.Inject; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.net.SocketAddress; +import io.vertx.ext.auth.authorization.Authorization; +import io.vertx.ext.web.RoutingContext; +import org.apache.cassandra.sidecar.acl.authorization.BasicPermissions; +import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata; +import org.apache.cassandra.sidecar.concurrent.ExecutorPools; +import org.apache.cassandra.sidecar.handlers.AbstractHandler; +import org.apache.cassandra.sidecar.handlers.AccessProtected; +import org.apache.cassandra.sidecar.livemigration.LiveMigrationStatusTracker; +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; + +/** + * Handler to clear the live migration status for an instance. + * <p> + * This endpoint removes the migration completion status, allowing the instance to be + * migrated again in the future. Clearing the status is necessary to prevent blocking + * future migrations of the same destination instance. + * + * <h3>Usage Safety:</h3> + * IMPORTANT: This endpoint should ONLY be called after completing the live migration + * process and the instance entry has been removed from the Live Migration map + * Calling this endpoint prematurely may lead to inconsistent migration state. + * + * <h3>Use Cases:</h3> + * <ul> + * <li>After successful completion of a live migration to allow future migrations</li> + * <li>To recover from erroneous COMPLETED status markings</li> + * </ul> + */ +public class LiveMigrationStatusClearHandler extends AbstractHandler<Void> implements AccessProtected +{ + private static final Logger LOGGER = LoggerFactory.getLogger(LiveMigrationStatusClearHandler.class); + + private final LiveMigrationStatusTracker statusTracker; + + @Inject + public LiveMigrationStatusClearHandler(InstanceMetadataFetcher metadataFetcher, + ExecutorPools executorPools, + CassandraInputValidator validator, + LiveMigrationStatusTracker statusTracker) + { + super(metadataFetcher, executorPools, validator); + this.statusTracker = statusTracker; + } + + @Override + protected Void extractParamsOrThrow(RoutingContext context) + { + return null; + } + + @Override + protected void handleInternal(RoutingContext context, HttpServerRequest httpRequest, @NotNull String host, SocketAddress remoteAddress, Void request) + { + InstanceMetadata instanceMetadata = metadataFetcher.instance(host); + statusTracker.clearMigrationStatus(instanceMetadata) + .onSuccess(v -> { + LOGGER.info("Successfully cleared live migration status for instance {}", host); + context.response().setStatusCode(HttpResponseStatus.OK.code()).end(); + }) + .onFailure(e -> { + LOGGER.error("Error while clearing live migration status for instance {}", host, e); + if (e instanceof IllegalStateException) + { + context.fail(wrapHttpException(HttpResponseStatus.BAD_REQUEST, e.getMessage(), e)); + } + else + { + context.fail(wrapHttpException(HttpResponseStatus.SERVICE_UNAVAILABLE, e.getMessage(), e)); + } + }); + } + + @Override + public Set<Authorization> requiredAuthorizations() + { + return Set.of(BasicPermissions.STREAM_FILES.toAuthorization()); Review Comment: Good question! I am having second thoughts too. What if this permission is renamed as DATA_COPY? It is generic enough to cover the whole data-copy process of LiveMigration. What do you say? ########## server/src/main/java/org/apache/cassandra/sidecar/livemigration/DataCopyTaskManager.java: ########## @@ -157,7 +141,7 @@ LiveMigrationTask createTask(LiveMigrationDataCopyRequest request, int port, InstanceMetadata localInstanceMetadata) { - return liveMigrationTaskFactory.create(request, source, port, localInstanceMetadata); + return liveMigrationTaskFactory.create(UUID.randomUUID().toString(), request, source, port, localInstanceMetadata); Review Comment: Thought that a random UUID is good enough instead of using a class from a non-JDK library to generate one. But I see that Sidecar is already using `com.datastax.driver.core.utils.UUIDs`. Made the changes. ########## server/src/main/java/org/apache/cassandra/sidecar/handlers/livemigration/LiveMigrationStatusClearHandler.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.livemigration; + +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.inject.Inject; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.net.SocketAddress; +import io.vertx.ext.auth.authorization.Authorization; +import io.vertx.ext.web.RoutingContext; +import org.apache.cassandra.sidecar.acl.authorization.BasicPermissions; +import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata; +import org.apache.cassandra.sidecar.concurrent.ExecutorPools; +import org.apache.cassandra.sidecar.handlers.AbstractHandler; +import org.apache.cassandra.sidecar.handlers.AccessProtected; +import org.apache.cassandra.sidecar.livemigration.LiveMigrationStatusTracker; +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; + +/** + * Handler to clear the live migration status for an instance. + * <p> + * This endpoint removes the migration completion status, allowing the instance to be + * migrated again in the future. Clearing the status is necessary to prevent blocking + * future migrations of the same destination instance. + * + * <h3>Usage Safety:</h3> + * IMPORTANT: This endpoint should ONLY be called after completing the live migration + * process and the instance entry has been removed from the Live Migration map + * Calling this endpoint prematurely may lead to inconsistent migration state. + * + * <h3>Use Cases:</h3> + * <ul> + * <li>After successful completion of a live migration to allow future migrations</li> + * <li>To recover from erroneous COMPLETED status markings</li> + * </ul> + */ +public class LiveMigrationStatusClearHandler extends AbstractHandler<Void> implements AccessProtected +{ + private static final Logger LOGGER = LoggerFactory.getLogger(LiveMigrationStatusClearHandler.class); + + private final LiveMigrationStatusTracker statusTracker; + + @Inject + public LiveMigrationStatusClearHandler(InstanceMetadataFetcher metadataFetcher, + ExecutorPools executorPools, + CassandraInputValidator validator, + LiveMigrationStatusTracker statusTracker) + { + super(metadataFetcher, executorPools, validator); + this.statusTracker = statusTracker; + } + + @Override + protected Void extractParamsOrThrow(RoutingContext context) + { + return null; + } + + @Override + protected void handleInternal(RoutingContext context, HttpServerRequest httpRequest, @NotNull String host, SocketAddress remoteAddress, Void request) + { + InstanceMetadata instanceMetadata = metadataFetcher.instance(host); + statusTracker.clearMigrationStatus(instanceMetadata) + .onSuccess(v -> { + LOGGER.info("Successfully cleared live migration status for instance {}", host); + context.response().setStatusCode(HttpResponseStatus.OK.code()).end(); Review Comment: done, thanks for sharing the tip 👍 -- 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]

