andresbeckruiz commented on code in PR #339: URL: https://github.com/apache/cassandra-sidecar/pull/339#discussion_r3157413932
########## server/src/main/java/org/apache/cassandra/sidecar/job/storage/CassandraStorageProvider.java: ########## @@ -0,0 +1,200 @@ +/* + * 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.storage; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.function.Supplier; + +import com.datastax.driver.core.exceptions.DriverException; +import org.apache.cassandra.sidecar.common.data.OperationalJobStatus; +import org.apache.cassandra.sidecar.common.server.CQLSessionProvider; +import org.apache.cassandra.sidecar.db.ActiveClusterOpsDatabaseAccessor; +import org.apache.cassandra.sidecar.db.ClusterOpsDatabaseAccessor; +import org.apache.cassandra.sidecar.db.ClusterOpsNodeStateDatabaseAccessor; +import org.apache.cassandra.sidecar.exceptions.CassandraUnavailableException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A {@link StorageProvider} implementation backed by Cassandra system tables. + * Delegates to three database accessors for cluster operations, node state, and active operation coordination. + * Each instance is scoped to a single cluster identified by {@code clusterName}. + */ +public class CassandraStorageProvider implements StorageProvider +{ + private final CQLSessionProvider sessionProvider; + private final ClusterOpsDatabaseAccessor clusterOpsAccessor; + private final ClusterOpsNodeStateDatabaseAccessor nodeStateAccessor; + private final ActiveClusterOpsDatabaseAccessor activeOpsAccessor; + private volatile String clusterName; + + public CassandraStorageProvider(CQLSessionProvider sessionProvider, + ClusterOpsDatabaseAccessor clusterOpsAccessor, + ClusterOpsNodeStateDatabaseAccessor nodeStateAccessor, + ActiveClusterOpsDatabaseAccessor activeOpsAccessor) + { + this.sessionProvider = sessionProvider; + this.clusterOpsAccessor = clusterOpsAccessor; + this.nodeStateAccessor = nodeStateAccessor; + this.activeOpsAccessor = activeOpsAccessor; + } + + @Override + public void persistJob(OperationalJobRecord job) + { + execute("persistJob", () -> { + clusterOpsAccessor.persistJob(clusterName, job); + return null; + }); + } + + @Override + @Nullable + public OperationalJobRecord findJob(UUID jobId) + { + return execute("findJob", () -> clusterOpsAccessor.findJob(clusterName, jobId)); + } + + @Override + public void updateJobStatus(UUID jobId, String operationType, OperationalJobStatus status) + { + execute("updateJobStatus", () -> { + clusterOpsAccessor.updateJobStatus(clusterName, jobId, operationType, status); + return null; + }); + } + + @Override + @NotNull + public List<OperationalJobRecord> findAllJobs(int limit) + { + return execute("findAllJobs", () -> clusterOpsAccessor.findAllJobs(clusterName, limit)); + } + + @Override + public boolean trySetActiveOperation(String operationType, UUID operationId) + { + return execute("trySetActiveOperation", + () -> activeOpsAccessor.trySetActiveOperation(clusterName, operationType, operationId)); + } + + @Override + @Nullable + public UUID getActiveOperation(String operationType) + { + return execute("getActiveOperation", + () -> activeOpsAccessor.getActiveOperation(clusterName, operationType)); + } + + @Override + @NotNull + public Map<String, UUID> getActiveOperations() + { + return execute("getActiveOperations", + () -> activeOpsAccessor.getActiveOperations(clusterName)); + } + + @Override + public boolean clearActiveOperation(String operationType, UUID operationId) + { + return execute("clearActiveOperation", + () -> activeOpsAccessor.clearActiveOperation(clusterName, operationType, operationId)); + } + + @Override + public void updateNodeStatuses(UUID operationId, List<UUID> nodeIds, OperationalJobStatus nodeStatus) + { + execute("updateNodeStatuses", () -> { + nodeStateAccessor.updateNodeStatuses(clusterName, operationId, nodeIds, nodeStatus); + return null; + }); + } + + @Override + public void updateNodeStatus(UUID operationId, UUID nodeId, OperationalJobStatus nodeStatus) + { + execute("updateNodeStatus", () -> { + nodeStateAccessor.updateNodeStatus(clusterName, operationId, nodeId, nodeStatus); + return null; + }); + } + + @Override + @Nullable + public OperationalJobStatus getNodeStatus(UUID operationId, UUID nodeId) + { + return execute("getNodeStatus", + () -> nodeStateAccessor.getNodeStatus(clusterName, operationId, nodeId)); + } + + @Override + @NotNull + public Map<UUID, OperationalJobStatus> getNodeStatusesForOperation(UUID operationId) + { + return execute("getNodeStatusesForOperation", + () -> nodeStateAccessor.getNodeStatusesForOperation(clusterName, operationId)); + } + + @Override + public void initialize() + { + // Schema initialization is handled by SidecarSchemaInitializer. + // TTL on all tables handles record pruning. + try + { + clusterName = sessionProvider.get().getCluster().getMetadata().getClusterName(); Review Comment: Added `clusterName` argument to constructor -- 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]

