yifan-c commented on code in PR #144:
URL: https://github.com/apache/cassandra-sidecar/pull/144#discussion_r1915513644
##########
server/src/test/java/org/apache/cassandra/sidecar/routes/OperationalJobHandlerTest.java:
##########
@@ -124,6 +124,8 @@ void testGetJobStatusRunningJob(VertxTestContext context)
.expect(ResponsePredicate.SC_ACCEPTED)
.send(context.succeeding(response -> {
assertThat(response.statusCode()).isEqualTo(ACCEPTED.code());
+ LOGGER.info("Response Status:" + response.statusCode());
+ LOGGER.info("Response Body:" + response.bodyAsString());
Review Comment:
nit: use `{}` to eval log message... I know that it is just test code. But
for the sake of consistency.
##########
server/src/test/integration/org/apache/cassandra/sidecar/routes/NodeDecommissionIntegrationTest.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.routes;
+
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.buffer.Buffer;
+import io.vertx.ext.web.client.HttpResponse;
+import io.vertx.junit5.VertxExtension;
+import io.vertx.junit5.VertxTestContext;
+import org.apache.cassandra.sidecar.common.data.OperationalJobStatus;
+import org.apache.cassandra.sidecar.common.response.OperationalJobResponse;
+import org.apache.cassandra.sidecar.testing.IntegrationTestBase;
+import org.apache.cassandra.testing.CassandraIntegrationTest;
+
+import static org.apache.cassandra.sidecar.AssertionUtils.loopAssert;
+import static
org.apache.cassandra.sidecar.common.data.OperationalJobStatus.FAILED;
+import static
org.apache.cassandra.sidecar.common.data.OperationalJobStatus.RUNNING;
+import static
org.apache.cassandra.sidecar.common.data.OperationalJobStatus.SUCCEEDED;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+/**
+ * Test the node decommission endpoint with cassandra container.
+ */
+@ExtendWith(VertxExtension.class)
+public class NodeDecommissionIntegrationTest extends IntegrationTestBase
+{
+ @CassandraIntegrationTest(nodesPerDc = 2)
+ void decommissionNodeDefault(VertxTestContext context) throws
InterruptedException
+ {
+ final String[] jobId = new String[1];
+ String testRoute =
"/api/v1/cassandra/operations/decommission?force=true";
+ testWithClient(client -> client.put(server.actualPort(), "127.0.0.1",
testRoute)
+ .send(context.succeeding(response -> {
+ logger.info("Response Status:" +
response.statusCode());
+ OperationalJobResponse
decommissionResponse = response.bodyAsJson(OperationalJobResponse.class);
+
assertThat(decommissionResponse.status()).isEqualTo(RUNNING);
+ jobId[0] =
String.valueOf(decommissionResponse.jobId());
+ })));
+ Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS);
Review Comment:
I see. I thought it could speed up the test a little bit w/o waiting for the
complete 10 seconds by using a count down latch. But it does not show a big
difference. Feel free to either update or keep the current as-is.
```java
AtomicReference<String> jobId = new AtomicReference<>();
CountDownLatch jobIdRetrieved = new CountDownLatch(1);
String testRoute =
"/api/v1/cassandra/operations/decommission?force=true";
testWithClient(client -> client.put(server.actualPort(),
"127.0.0.1", testRoute)
.send(context.succeeding(response -> {
logger.info("Response Status:" +
response.statusCode());
logger.info("Response Body:" +
response.bodyAsString());
OperationalJobResponse
decommissionResponse = response.bodyAsJson(OperationalJobResponse.class);
assertThat(decommissionResponse.status()).isEqualTo(RUNNING);
jobId.set(String.valueOf(decommissionResponse.jobId()));
jobIdRetrieved.countDown();
})));
awaitLatchOrThrow(jobIdRetrieved, 10, TimeUnit.SECONDS, "Wait for
the conflict jobId");
assertThat(jobId.get()).isNotNull();
pollStatusForState(jobId.get(), SUCCEEDED, null);
context.completeNow();
```
##########
adapters/cassandra41/src/main/java/org/apache/cassandra/sidecar/adapters/cassandra41/Cassandra41StorageOperations.java:
##########
@@ -34,6 +34,7 @@
*/
public class Cassandra41StorageOperations extends CassandraStorageOperations
{
+
Review Comment:
nit: empty line..
##########
server/src/main/java/org/apache/cassandra/sidecar/routes/NodeDecommissionHandler.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.routes;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+
+import com.datastax.driver.core.utils.UUIDs;
+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.web.RoutingContext;
+import org.apache.cassandra.sidecar.common.data.OperationalJobStatus;
+import org.apache.cassandra.sidecar.common.response.OperationalJobResponse;
+import org.apache.cassandra.sidecar.common.server.StorageOperations;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.config.ServiceConfiguration;
+import org.apache.cassandra.sidecar.exceptions.OperationalJobConflictException;
+import org.apache.cassandra.sidecar.job.NodeDecommissionJob;
+import org.apache.cassandra.sidecar.job.OperationalJobManager;
+import org.apache.cassandra.sidecar.utils.CassandraInputValidator;
+import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher;
+
+import static
org.apache.cassandra.sidecar.utils.RequestUtils.parseBooleanQueryParam;
+
+/**
+ * Provides REST API for asynchronously decommissioning the corresponding
Cassandra node
+ */
+public class NodeDecommissionHandler extends OperationalJobHandler
+{
+ private final OperationalJobManager jobManager;
+ private final ServiceConfiguration config;
+
+ /**
+ * Constructs a handler with the provided {@code metadataFetcher}
+ *
+ * @param metadataFetcher the interface to retrieve instance metadata
+ * @param executorPools the executor pools for blocking executions
+ * @param validator a validator instance to validate
Cassandra-specific input
+ */
+ @Inject
+ protected NodeDecommissionHandler(InstanceMetadataFetcher metadataFetcher,
+ ExecutorPools executorPools,
+ ServiceConfiguration
serviceConfiguration,
+ CassandraInputValidator validator,
+ OperationalJobManager jobManager)
+ {
+ super(metadataFetcher, executorPools, validator, jobManager);
+ this.jobManager = jobManager;
+ this.config = serviceConfiguration;
+ }
+
+ @Override
+ public void handleInternal(RoutingContext context, HttpServerRequest
httpRequest, String host, SocketAddress remoteAddress, Void request)
+ {
+ StorageOperations operations =
metadataFetcher.delegate(host).storageOperations();
+ boolean isForce = parseBooleanQueryParam(context.request(), "force",
false);
+
+ NodeDecommissionJob job = new NodeDecommissionJob(UUIDs.timeBased(),
operations, isForce);
+ try
+ {
+ jobManager.trySubmitJob(job);
+ }
+ catch (OperationalJobConflictException oje)
+ {
+ String reason = oje.getMessage();
+ logger.error("Conflicting job encountered. reason={}", reason);
+
context.response().setStatusCode(HttpResponseStatus.CONFLICT.code());
+ context.json(new OperationalJobResponse(job.jobId(),
OperationalJobStatus.FAILED, job.name(), reason));
Review Comment:
It can/should return early from the catch block.
##########
server/src/main/java/org/apache/cassandra/sidecar/job/NodeDecommissionJob.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.util.UUID;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.sidecar.common.data.OperationalJobStatus;
+import org.apache.cassandra.sidecar.common.server.StorageOperations;
+
+/**
+ * Implementation of {@link OperationalJob} to perform node decommission
operation.
+ */
+public class NodeDecommissionJob extends OperationalJob
+{
+ private static final Logger LOGGER =
LoggerFactory.getLogger(NodeDecommissionJob.class);
+ private static final String operation = "decommission";
+ private final boolean isForce;
+ protected StorageOperations storageOperations;
+
+ public NodeDecommissionJob(UUID jobId, StorageOperations storageOps,
boolean isForce)
+ {
+ super(jobId);
+ this.storageOperations = storageOps;
+ this.isForce = isForce;
+ }
+
+ @Override
+ public boolean isRunningOnCassandra()
+ {
+ String operationMode = storageOperations.getOperationMode();
+ return operationMode.equals("LEAVING") ||
operationMode.equals("DECOMMISSIONED");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public OperationalJobStatus status()
+ {
+ String operationMode = storageOperations.getOperationMode();
+
+ if (operationMode.equals("LEAVING"))
Review Comment:
yep. no worries. as said, it is a nit.
##########
server/src/main/java/org/apache/cassandra/sidecar/routes/NodeDecommissionHandler.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.routes;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+
+import com.datastax.driver.core.utils.UUIDs;
+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.web.RoutingContext;
+import org.apache.cassandra.sidecar.common.data.OperationalJobStatus;
+import org.apache.cassandra.sidecar.common.response.OperationalJobResponse;
+import org.apache.cassandra.sidecar.common.server.StorageOperations;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.config.ServiceConfiguration;
+import org.apache.cassandra.sidecar.exceptions.OperationalJobConflictException;
+import org.apache.cassandra.sidecar.job.NodeDecommissionJob;
+import org.apache.cassandra.sidecar.job.OperationalJobManager;
+import org.apache.cassandra.sidecar.utils.CassandraInputValidator;
+import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher;
+
+import static
org.apache.cassandra.sidecar.utils.RequestUtils.parseBooleanQueryParam;
+
+/**
+ * Provides REST API for asynchronously decommissioning the corresponding
Cassandra node
+ */
+public class NodeDecommissionHandler extends OperationalJobHandler
+{
+ private final OperationalJobManager jobManager;
+ private final ServiceConfiguration config;
+
+ /**
+ * Constructs a handler with the provided {@code metadataFetcher}
+ *
+ * @param metadataFetcher the interface to retrieve instance metadata
+ * @param executorPools the executor pools for blocking executions
+ * @param validator a validator instance to validate
Cassandra-specific input
+ */
+ @Inject
+ protected NodeDecommissionHandler(InstanceMetadataFetcher metadataFetcher,
+ ExecutorPools executorPools,
+ ServiceConfiguration
serviceConfiguration,
+ CassandraInputValidator validator,
+ OperationalJobManager jobManager)
+ {
+ super(metadataFetcher, executorPools, validator, jobManager);
+ this.jobManager = jobManager;
+ this.config = serviceConfiguration;
+ }
+
+ @Override
+ public void handleInternal(RoutingContext context, HttpServerRequest
httpRequest, String host, SocketAddress remoteAddress, Void request)
+ {
+ StorageOperations operations =
metadataFetcher.delegate(host).storageOperations();
+ boolean isForce = parseBooleanQueryParam(context.request(), "force",
false);
+
+ NodeDecommissionJob job = new NodeDecommissionJob(UUIDs.timeBased(),
operations, isForce);
+ try
+ {
+ jobManager.trySubmitJob(job);
+ }
+ catch (OperationalJobConflictException oje)
+ {
+ String reason = oje.getMessage();
+ logger.error("Conflicting job encountered. reason={}", reason);
+
context.response().setStatusCode(HttpResponseStatus.CONFLICT.code());
+ context.json(new OperationalJobResponse(job.jobId(),
OperationalJobStatus.FAILED, job.name(), reason));
+ }
+
+ // Get the result, waiting for the specified wait time for result
+ job.asyncResult(executorPools.service(),
+
Duration.of(config.operationalJobExecutionMaxWaitTimeInMillis(),
ChronoUnit.MILLIS))
+ .onComplete(v -> sendStatusBasedResponse(context, job));
Review Comment:
I think it is still off by 1 space? Is it?
--
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]