andresbeckruiz commented on code in PR #359: URL: https://github.com/apache/cassandra-sidecar/pull/359#discussion_r3508813769
########## server/src/main/java/org/apache/cassandra/sidecar/job/DurableOperationalJobTracker.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.Collections; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.cassandra.sidecar.common.data.OperationalJobStatus; +import org.apache.cassandra.sidecar.concurrent.TaskExecutorPool; +import org.apache.cassandra.sidecar.config.ServiceConfiguration; +import org.apache.cassandra.sidecar.job.storage.OperationalJobRecord; +import org.apache.cassandra.sidecar.job.storage.StorageProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A durable implementation of {@link OperationalJobTracker} that persists job state + * via a {@link StorageProvider}. A local {@link ConcurrentHashMap} caches live + * {@link OperationalJob} references for the current process, since executing jobs + * with Vert.x promises cannot be reconstituted from storage. Once a job completes, + * it is removed from the local map, and subsequent lookups are served from storage. + */ +@Singleton +public class DurableOperationalJobTracker implements OperationalJobTracker +{ + private static final Logger LOGGER = LoggerFactory.getLogger(DurableOperationalJobTracker.class); + private static final int MAX_STATUS_UPDATE_ATTEMPTS = 2; + private static final long RETRY_DELAY_MS = 100; + + private final ConcurrentHashMap<UUID, OperationalJob> liveJobs; + private final StorageProvider storageProvider; + private final TaskExecutorPool executor; + + @Inject + public DurableOperationalJobTracker(ServiceConfiguration serviceConfiguration, + StorageProvider storageProvider, + TaskExecutorPool executor) + { + this.liveJobs = new ConcurrentHashMap<>(serviceConfiguration.operationalJobTrackerSize()); + this.storageProvider = storageProvider; + this.executor = executor; + } + + /** + * {@inheritDoc} + */ + @Override + public OperationalJob computeIfAbsent(UUID jobId, Function<UUID, OperationalJob> mappingFunction) + { + return liveJobs.computeIfAbsent(jobId, id -> { + OperationalJob job = mappingFunction.apply(id); + + storageProvider.persistJob(OperationalJobRecord.fromOperationalJob(job)); Review Comment: Addressed in [464f03e](https://github.com/apache/cassandra-sidecar/pull/359/commits/464f03e82124a4a74bc835b995f25f06856bbc6b). Moved the job persistence to outside of the CHM lock, and used `executeBlocking` for the I/O operation. There is a possible issue where we return to the client a successful job submission but the job persistence later fails. This should be addressed in a follow up patch to change the return type of this method to `Future<OperationalJob> `and chain the return to the client on the job persistence. The other option is to move job persistence to `OperationalJob.execute,` but this couples jobs with the storage layer. I have left this out of this patch to limit scope creep. -- 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]

