arjunashok commented on code in PR #139:
URL: https://github.com/apache/cassandra-sidecar/pull/139#discussion_r1852990474


##########
server/src/main/java/org/apache/cassandra/sidecar/job/OperationsJobManager.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.List;
+import java.util.UUID;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import javax.inject.Inject;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Singleton;
+import 
org.apache.cassandra.sidecar.common.utils.OperationsJobResult.OperationsJobStatus;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+
+/**
+ * An abstraction of the management and tracking of long-running jobs running 
on the sidecar.
+ */
+@Singleton
+public class OperationsJobManager
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(OperationsJobManager.class);
+
+    private final OperationsJobTracker jobTracker;
+    private final ExecutorPools executorPools;
+
+    /**
+     * Creates a manager instance with a default sized job-tracker.
+     * @param executorPools
+     */
+    @Inject
+    public OperationsJobManager(ExecutorPools executorPools, 
OperationsJobTracker jobTracker)
+    {
+        this.executorPools = executorPools;
+        this.jobTracker = jobTracker;
+    }
+
+    /**
+     * Fetches the inflight jobs being tracked on the sidecar
+     * @return instances of the jobs that are in pending or running states
+     */
+    public List<OperationsJob> allInflightJobs()
+    {
+        return jobTracker.getJobsView().values()
+                         .stream()
+                         .filter(j -> j.status() == 
OperationsJobStatus.Pending || j.status() == OperationsJobStatus.Running)
+                         .collect(Collectors.toList());
+    }
+
+    /**
+     * Fetch the job using its UUID
+     * @param jobId identifier of the job
+     * @return instance of the job or null
+     */
+    public OperationsJob getJobIfExists(UUID jobId)
+    {
+        return jobTracker.get(jobId);
+    }
+
+    /**
+     * Asynchronously submit (and lazily create, via the supplier) the job, if 
it is not currently being
+     * tracked and is not running downstream. The job is triggered on a 
separate internal thread-pool.
+     * The job execution failure behavior is tracked within the {@link 
OperationsJob}.
+     * @param jobId job identifier
+     * @param jobSupplier supplier used to create an instance of the job
+     * @return the instance of the job that is either being tracked or was 
just submitted
+     */
+    public OperationsJob trySubmitJob(UUID jobId, Supplier<OperationsJob> 
jobSupplier)

Review Comment:
   I changed this to use a creator function (`Function<UUID, OperationsJob> 
creator`) that takes UUID argument, to improve clarity on which ID is used for 
the job.  This should address the correctness issue as the returned job always 
corresponds to the headerId passed in (or a new UUID when absent).
   
   As for the return-type, we need to job status and id to send the appropriate 
response.



##########
server/src/main/java/org/apache/cassandra/sidecar/routes/OperationsJobsHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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 javax.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.response.OperationsJobsResponse;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.job.OperationsJob;
+import org.apache.cassandra.sidecar.job.OperationsJobManager;
+import org.apache.cassandra.sidecar.utils.CassandraInputValidator;
+import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher;
+
+import static 
org.apache.cassandra.sidecar.common.ApiEndpointsV1.OPERATIONS_JOB_ID_PATH_PARAM;
+import static 
org.apache.cassandra.sidecar.common.http.SidecarHttpHeaderNames.OPERATIONS_JOB_HEADER_NAME;
+import static 
org.apache.cassandra.sidecar.utils.HttpExceptions.wrapHttpException;
+
+/**
+ * Handler for retrieving the status of async operations jobs running on the 
sidecar
+ */
+public class OperationsJobsHandler extends AbstractHandler<Void>
+{
+    private final OperationsJobManager jobManager;
+    @Inject
+    public OperationsJobsHandler(InstanceMetadataFetcher metadataFetcher,
+                                 ExecutorPools executorPools,
+                                 CassandraInputValidator validator,
+                                 OperationsJobManager jobManager)
+    {
+        super(metadataFetcher, executorPools, validator);
+        this.jobManager = jobManager;
+    }
+
+    protected Void extractParamsOrThrow(RoutingContext context)
+    {
+        return null;
+    }
+
+    @Override
+    public void handleInternal(RoutingContext context, HttpServerRequest 
httpRequest, String host, SocketAddress remoteAddress, Void request)
+    {
+        UUID jobUUID = validateJobIdParam(context);

Review Comment:
   Ack



##########
server/src/main/java/org/apache/cassandra/sidecar/routes/OperationsJobsHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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 javax.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.response.OperationsJobsResponse;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.job.OperationsJob;
+import org.apache.cassandra.sidecar.job.OperationsJobManager;
+import org.apache.cassandra.sidecar.utils.CassandraInputValidator;
+import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher;
+
+import static 
org.apache.cassandra.sidecar.common.ApiEndpointsV1.OPERATIONS_JOB_ID_PATH_PARAM;
+import static 
org.apache.cassandra.sidecar.common.http.SidecarHttpHeaderNames.OPERATIONS_JOB_HEADER_NAME;
+import static 
org.apache.cassandra.sidecar.utils.HttpExceptions.wrapHttpException;
+
+/**
+ * Handler for retrieving the status of async operations jobs running on the 
sidecar
+ */
+public class OperationsJobsHandler extends AbstractHandler<Void>
+{
+    private final OperationsJobManager jobManager;
+    @Inject
+    public OperationsJobsHandler(InstanceMetadataFetcher metadataFetcher,
+                                 ExecutorPools executorPools,
+                                 CassandraInputValidator validator,
+                                 OperationsJobManager jobManager)
+    {
+        super(metadataFetcher, executorPools, validator);
+        this.jobManager = jobManager;
+    }
+
+    protected Void extractParamsOrThrow(RoutingContext context)

Review Comment:
   Ack



##########
server/src/main/java/org/apache/cassandra/sidecar/routes/OperationsJobsHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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 javax.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.response.OperationsJobsResponse;
+import org.apache.cassandra.sidecar.concurrent.ExecutorPools;
+import org.apache.cassandra.sidecar.job.OperationsJob;
+import org.apache.cassandra.sidecar.job.OperationsJobManager;
+import org.apache.cassandra.sidecar.utils.CassandraInputValidator;
+import org.apache.cassandra.sidecar.utils.InstanceMetadataFetcher;
+
+import static 
org.apache.cassandra.sidecar.common.ApiEndpointsV1.OPERATIONS_JOB_ID_PATH_PARAM;
+import static 
org.apache.cassandra.sidecar.common.http.SidecarHttpHeaderNames.OPERATIONS_JOB_HEADER_NAME;
+import static 
org.apache.cassandra.sidecar.utils.HttpExceptions.wrapHttpException;
+
+/**
+ * Handler for retrieving the status of async operations jobs running on the 
sidecar
+ */
+public class OperationsJobsHandler extends AbstractHandler<Void>
+{
+    private final OperationsJobManager jobManager;
+    @Inject
+    public OperationsJobsHandler(InstanceMetadataFetcher metadataFetcher,
+                                 ExecutorPools executorPools,
+                                 CassandraInputValidator validator,
+                                 OperationsJobManager jobManager)
+    {
+        super(metadataFetcher, executorPools, validator);
+        this.jobManager = jobManager;
+    }
+
+    protected Void extractParamsOrThrow(RoutingContext context)
+    {
+        return null;
+    }
+
+    @Override
+    public void handleInternal(RoutingContext context, HttpServerRequest 
httpRequest, String host, SocketAddress remoteAddress, Void request)
+    {
+        UUID jobUUID = validateJobIdParam(context);
+
+        executorPools.service().executeBlocking(() -> {
+                         OperationsJob job = 
jobManager.getJobIfExists(jobUUID);
+                         if (job == null)
+                         {
+                             String response = String.format("Unknown job with 
ID:%s. Please retry the operation.", jobUUID);
+                             logger.info(response);
+                             
context.fail(wrapHttpException(HttpResponseStatus.NOT_FOUND, response));

Review Comment:
   Ack



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