arjunashok commented on code in PR #139: URL: https://github.com/apache/cassandra-sidecar/pull/139#discussion_r1852989244
########## server/src/main/java/org/apache/cassandra/sidecar/job/OperationsJob.java: ########## @@ -0,0 +1,152 @@ +/* + * 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 java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.sidecar.common.utils.OperationsJobResult; +import org.apache.cassandra.sidecar.common.utils.OperationsJobResult.OperationsJobStatus; + +/** + * An abstract class representing a Operations job managed by the sidecar. + * + */ +public abstract class OperationsJob +{ + private static final Logger LOGGER = LoggerFactory.getLogger(OperationsJob.class); + + protected UUID jobId; + protected OperationsJobStatus status; + protected String failureReason; + final CountDownLatch latch = new CountDownLatch(1); + + protected OperationsJob() + { + + } + /** + * Constructs a job with a unique UUID, in Pending state + * @param jobId UUID representing the Job to be created + */ + protected OperationsJob(UUID jobId) + { + this.jobId = jobId; + this.status = OperationsJobStatus.Pending; + this.failureReason = ""; + } + + @VisibleForTesting + protected OperationsJob(UUID jobId, OperationsJobStatus status) + { + this.jobId = jobId; + this.status = status; + this.failureReason = ""; + } + + public void setStatus(OperationsJobStatus status) + { + this.status = status; + } + public OperationsJobStatus status() + { + return status; + } + public String failureReason() + { + return failureReason; + } + + public UUID jobId() + { + return jobId; + } + + /** + * Supplier specifying the functionality of the job to be triggered when the job is executed. Subclasses to + * provide operation-specific implementations + * @return a function with the operation implementation that returns a {@code JobResult} + * @throws Exception + */ + public abstract Supplier<OperationsJobResult> jobOperationSupplier() throws Exception; + + /** + * Provide a meaningful name of the operation executed by the concrete subclass. + * @return the name of the operation. eg. nodetool command name + */ + public abstract String operation(); + + /** + * Specifies the downstream operation to be performed to check if the job is running on the Cassandra node/cluster. + * This functionality is provided by the Job when it is asynchronously triggering the job via the {@code JobManager} + * For synchronous jobs this should always return false. + * @return true if the job is running downstream + */ + public abstract boolean checkInflightJob(); Review Comment: I had this renamed to `isRunningDownstream` in my earlier draft. Will update to that. ########## server/src/main/java/org/apache/cassandra/sidecar/job/OperationsJob.java: ########## @@ -0,0 +1,152 @@ +/* + * 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 java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.sidecar.common.utils.OperationsJobResult; +import org.apache.cassandra.sidecar.common.utils.OperationsJobResult.OperationsJobStatus; + +/** + * An abstract class representing a Operations job managed by the sidecar. + * + */ +public abstract class OperationsJob +{ + private static final Logger LOGGER = LoggerFactory.getLogger(OperationsJob.class); + + protected UUID jobId; + protected OperationsJobStatus status; + protected String failureReason; + final CountDownLatch latch = new CountDownLatch(1); + + protected OperationsJob() + { + + } + /** + * Constructs a job with a unique UUID, in Pending state + * @param jobId UUID representing the Job to be created + */ + protected OperationsJob(UUID jobId) + { + this.jobId = jobId; + this.status = OperationsJobStatus.Pending; + this.failureReason = ""; + } + + @VisibleForTesting + protected OperationsJob(UUID jobId, OperationsJobStatus status) + { + this.jobId = jobId; + this.status = status; + this.failureReason = ""; + } + + public void setStatus(OperationsJobStatus status) + { + this.status = status; + } + public OperationsJobStatus status() + { + return status; + } + public String failureReason() + { + return failureReason; + } + + public UUID jobId() + { + return jobId; + } + + /** + * Supplier specifying the functionality of the job to be triggered when the job is executed. Subclasses to + * provide operation-specific implementations + * @return a function with the operation implementation that returns a {@code JobResult} + * @throws Exception + */ + public abstract Supplier<OperationsJobResult> jobOperationSupplier() throws Exception; + + /** + * Provide a meaningful name of the operation executed by the concrete subclass. + * @return the name of the operation. eg. nodetool command name + */ + public abstract String operation(); + + /** + * Specifies the downstream operation to be performed to check if the job is running on the Cassandra node/cluster. + * This functionality is provided by the Job when it is asynchronously triggering the job via the {@code JobManager} + * For synchronous jobs this should always return false. + * @return true if the job is running downstream + */ + public abstract boolean checkInflightJob(); + + /** + * Execute the job behavior as specified in the operation supplier {@link #jobOperationSupplier()}, + * while tracking the status of the job's lifecycle. + */ + public void execute() Review Comment: Ack ########## server/src/main/java/org/apache/cassandra/sidecar/job/OperationsJob.java: ########## @@ -0,0 +1,152 @@ +/* + * 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 java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.sidecar.common.utils.OperationsJobResult; +import org.apache.cassandra.sidecar.common.utils.OperationsJobResult.OperationsJobStatus; + +/** + * An abstract class representing a Operations job managed by the sidecar. + * + */ +public abstract class OperationsJob +{ + private static final Logger LOGGER = LoggerFactory.getLogger(OperationsJob.class); + + protected UUID jobId; + protected OperationsJobStatus status; + protected String failureReason; + final CountDownLatch latch = new CountDownLatch(1); + + protected OperationsJob() + { + + } + /** + * Constructs a job with a unique UUID, in Pending state + * @param jobId UUID representing the Job to be created + */ + protected OperationsJob(UUID jobId) + { + this.jobId = jobId; + this.status = OperationsJobStatus.Pending; + this.failureReason = ""; + } + + @VisibleForTesting + protected OperationsJob(UUID jobId, OperationsJobStatus status) + { + this.jobId = jobId; + this.status = status; + this.failureReason = ""; + } + + public void setStatus(OperationsJobStatus status) + { + this.status = status; + } + public OperationsJobStatus status() + { + return status; + } + public String failureReason() + { + return failureReason; + } + + public UUID jobId() + { + return jobId; + } + + /** + * Supplier specifying the functionality of the job to be triggered when the job is executed. Subclasses to + * provide operation-specific implementations + * @return a function with the operation implementation that returns a {@code JobResult} + * @throws Exception + */ + public abstract Supplier<OperationsJobResult> jobOperationSupplier() throws Exception; + + /** + * Provide a meaningful name of the operation executed by the concrete subclass. + * @return the name of the operation. eg. nodetool command name + */ + public abstract String operation(); + + /** + * Specifies the downstream operation to be performed to check if the job is running on the Cassandra node/cluster. + * This functionality is provided by the Job when it is asynchronously triggering the job via the {@code JobManager} + * For synchronous jobs this should always return false. + * @return true if the job is running downstream + */ + public abstract boolean checkInflightJob(); + + /** + * Execute the job behavior as specified in the operation supplier {@link #jobOperationSupplier()}, + * while tracking the status of the job's lifecycle. + */ + public void execute() + { + try + { + LOGGER.info("Executing job with ID: {}", jobId); + OperationsJobResult result = jobOperationSupplier().get(); + status = result.status(); + failureReason = result.reason(); + LOGGER.debug("Job with ID: {} returned with status: {}", jobId, status); + } + catch (Exception e) + { + String reason = (e.getCause() != null) ? e.getCause().getMessage() : e.getMessage(); + LOGGER.error("Failed to execute job {} with reason: {}", jobId, reason); + status = OperationsJobStatus.Failed; + failureReason = reason; + } + finally + { + latch.countDown(); + } + } + + /** + * Returns if the job execution completes within the provided wait time (in seconds) + * @param waitSeconds no. of seconds to wait for the job execution to complete + * @return true if the job completed within the specified time + */ + public boolean isResultAvailable(long waitSeconds) + { + try + { + return (!latch.await(waitSeconds, TimeUnit.SECONDS)) ? false : true; Review Comment: Much better :) -- 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]

