isaacreath commented on code in PR #339: URL: https://github.com/apache/cassandra-sidecar/pull/339#discussion_r3138430445
########## server/src/main/java/org/apache/cassandra/sidecar/db/schema/ActiveClusterOpsSchema.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.db.schema; + +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; +import org.apache.cassandra.sidecar.common.server.utils.SecondBoundConfiguration; +import org.apache.cassandra.sidecar.config.SchemaKeyspaceConfiguration; +import org.jetbrains.annotations.NotNull; + +/** + * Schema for the {@code active_cluster_ops} table, which tracks active operations + * and provides mutual exclusion via lightweight transactions (LWT). + */ +public class ActiveClusterOpsSchema extends TableSchema +{ + private static final String TABLE_NAME = "active_cluster_ops"; + + private final SchemaKeyspaceConfiguration keyspaceConfig; + private final SecondBoundConfiguration tableTtl; + + private PreparedStatement trySetActive; + private PreparedStatement getActive; + private PreparedStatement getActiveByType; + private PreparedStatement clearActive; + + public ActiveClusterOpsSchema(SchemaKeyspaceConfiguration keyspaceConfig, SecondBoundConfiguration tableTtl) + { + this.keyspaceConfig = keyspaceConfig; + this.tableTtl = tableTtl; + } + + @Override + protected String keyspaceName() + { + return keyspaceConfig.keyspace(); + } + + @Override + protected String tableName() + { + return TABLE_NAME; + } + + @Override + protected String createSchemaStatement() + { + return String.format("CREATE TABLE IF NOT EXISTS %s.%s (" + + " cluster_name text," + + " operation_type text," + + " operation_id uuid," + + " PRIMARY KEY ((cluster_name), operation_type)" + + ") WITH default_time_to_live = %s", + keyspaceConfig.keyspace(), TABLE_NAME, tableTtl.toSeconds()); + } + + @Override + protected void prepareStatements(@NotNull Session session) Review Comment: This class follows a different patten than the other schema classes that you implement below in that the below classes define a `CqlLiterals` inner class whereas this one just defines these as raw strings. Is there any reason why we aren't consistent across the schema classes in this PR? If not, I'd recommend consolidating on one of the two patterns. ########## server/src/main/java/org/apache/cassandra/sidecar/db/schema/ActiveClusterOpsSchema.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.db.schema; + +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; +import org.apache.cassandra.sidecar.common.server.utils.SecondBoundConfiguration; +import org.apache.cassandra.sidecar.config.SchemaKeyspaceConfiguration; +import org.jetbrains.annotations.NotNull; + +/** + * Schema for the {@code active_cluster_ops} table, which tracks active operations + * and provides mutual exclusion via lightweight transactions (LWT). + */ +public class ActiveClusterOpsSchema extends TableSchema +{ + private static final String TABLE_NAME = "active_cluster_ops"; + + private final SchemaKeyspaceConfiguration keyspaceConfig; + private final SecondBoundConfiguration tableTtl; + + private PreparedStatement trySetActive; + private PreparedStatement getActive; + private PreparedStatement getActiveByType; + private PreparedStatement clearActive; + + public ActiveClusterOpsSchema(SchemaKeyspaceConfiguration keyspaceConfig, SecondBoundConfiguration tableTtl) + { + this.keyspaceConfig = keyspaceConfig; + this.tableTtl = tableTtl; + } + + @Override + protected String keyspaceName() + { + return keyspaceConfig.keyspace(); + } + + @Override + protected String tableName() + { + return TABLE_NAME; + } + + @Override + protected String createSchemaStatement() + { + return String.format("CREATE TABLE IF NOT EXISTS %s.%s (" + Review Comment: Should we consider LCS for the compaction strategy on this table? May not be important given the (in-theory) low volume to this table. Just considering the challenges we face when using STCS with TTL tables and we can't use UCS as this should still work on Cassandra 4 clusters. ########## server/src/main/java/org/apache/cassandra/sidecar/db/schema/ClusterOpsNodeStateSchema.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.db.schema; + +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; +import org.apache.cassandra.sidecar.common.server.utils.SecondBoundConfiguration; +import org.apache.cassandra.sidecar.config.SchemaKeyspaceConfiguration; +import org.jetbrains.annotations.NotNull; + +/** + * Schema for the {@code cluster_ops_node_state} table, which tracks node-level status within an operation. Review Comment: Similarly, using the description from the CEP might be helpful: https://cwiki.apache.org/confluence/display/CASSANDRA/CEP-53%3A+Cassandra+Rolling+Restarts+via+Sidecar#CEP53:CassandraRollingRestartsviaSidecar-sidecar_internal.active_cluster_ops ########## server/src/main/java/org/apache/cassandra/sidecar/db/schema/ActiveClusterOpsSchema.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.db.schema; + +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; +import org.apache.cassandra.sidecar.common.server.utils.SecondBoundConfiguration; +import org.apache.cassandra.sidecar.config.SchemaKeyspaceConfiguration; +import org.jetbrains.annotations.NotNull; + +/** + * Schema for the {@code active_cluster_ops} table, which tracks active operations + * and provides mutual exclusion via lightweight transactions (LWT). Review Comment: Nit: We can improve this javadoc by explaining why this table exists and how it is used. The description from the CEP could be helpful here: https://cwiki.apache.org/confluence/display/CASSANDRA/CEP-53%3A+Cassandra+Rolling+Restarts+via+Sidecar#CEP53:CassandraRollingRestartsviaSidecar-sidecar_internal.active_cluster_ops ########## 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: This creates the implicit link between the cluster which is storing the operations and the cluster on which the operation is executing. I know that this PR has remote storage provider as out of scope, but making this something that we can set via constructor or via initialize would help make sure there is a pathway to store ops in a cluster separate from the one sidecar is connecting to. ########## server/src/main/java/org/apache/cassandra/sidecar/db/ClusterOpsDatabaseAccessor.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.db; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import com.google.common.reflect.TypeToken; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.cassandra.sidecar.common.data.OperationalJobStatus; +import org.apache.cassandra.sidecar.common.server.CQLSessionProvider; +import org.apache.cassandra.sidecar.db.schema.ClusterOpsSchema; +import org.apache.cassandra.sidecar.db.schema.SidecarSchema; +import org.apache.cassandra.sidecar.job.storage.OperationalJobRecord; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.VisibleForTesting; + +/** + * Database accessor for the {@code cluster_ops} table. + */ +@Singleton +public class ClusterOpsDatabaseAccessor extends DatabaseAccessor<ClusterOpsSchema> +{ + private static final Logger LOGGER = LoggerFactory.getLogger(ClusterOpsDatabaseAccessor.class); + @Inject + public ClusterOpsDatabaseAccessor(SidecarSchema sidecarSchema, CQLSessionProvider sessionProvider) + { + this(sidecarSchema.tableSchema(ClusterOpsSchema.class), sessionProvider); + } + + @VisibleForTesting + public ClusterOpsDatabaseAccessor(ClusterOpsSchema schema, CQLSessionProvider sessionProvider) + { + super(schema, sessionProvider); + } + + public void persistJob(String clusterName, OperationalJobRecord job) + { + BoundStatement statement = tableSchema.insertJob() + .bind(clusterName, + job.jobId(), + job.operationType(), + job.status().name(), + job.nodeExecutionOrder(), + job.operationMetadata()); + execute(statement); + } + + @Nullable + public OperationalJobRecord findJob(String clusterName, UUID jobId) + { + BoundStatement statement = tableSchema.selectJob().bind(clusterName, jobId); + ResultSet resultSet = execute(statement); + Row row = resultSet.one(); + if (row == null) + { + return null; + } + if (!resultSet.isExhausted()) + { + LOGGER.warn("Multiple rows found for operation_id={} in cluster={}. Using first match.", jobId, clusterName); + } + return recordFromRow(row); + } + + public void updateJobStatus(String clusterName, UUID jobId, String operationType, OperationalJobStatus status) + { + BoundStatement statement = tableSchema.updateStatus() + .bind(status.name(), clusterName, jobId, operationType); + execute(statement); + } + + @NotNull + public List<OperationalJobRecord> findAllJobs(String clusterName, int limit) + { + BoundStatement statement = tableSchema.findAllJobs().bind(clusterName, limit); + ResultSet resultSet = execute(statement); + List<OperationalJobRecord> records = new ArrayList<>(); + for (Row row : resultSet) + { + records.add(recordFromRow(row)); + } + return records; + } + + private static final TypeToken<List<List<String>>> NODES_ORDER_TYPE = new TypeToken<List<List<String>>>() {}; + + private OperationalJobRecord recordFromRow(Row row) + { + UUID operationId = row.getUUID("operation_id"); + String operationType = row.getString("operation_type"); + OperationalJobStatus status = OperationalJobStatus.valueOf(row.getString("status")); + List<List<String>> nodeExecutionOrder = row.get("node_execution_order", NODES_ORDER_TYPE); + Map<String, String> operationMetadata = row.getMap("operation_metadata", String.class, String.class); + return new OperationalJobRecord(operationId, operationType, status, + nodeExecutionOrder == null || nodeExecutionOrder.isEmpty() ? null : nodeExecutionOrder, + operationMetadata.isEmpty() ? null : operationMetadata); Review Comment: I'm not the biggest fan of the ternay operators used to provide values for the constructor of `OperationalJobRecord`. It subtly makes the reader of this code need to think of many different states when this object is returned. A few approaches we could consider here: 1) A builder for the `OperationalJobRecord`. 2) Move the logic into the constructor of the `OperationalJobRecord` 3) Extract this logic above. For example: ``` List<List<String>> nodeExecutionOrder = row.get("node_execution_order",NODES_ORDER_TYPE); if (nodeExecutionOrder != null && nodeExecutionOrder.isEmpty()) { nodeExecutironOrder = null; } ``` ########## server/src/test/java/org/apache/cassandra/sidecar/db/ClusterOpsDatabaseAccessorTest.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.db; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.Statement; +import com.datastax.driver.core.utils.UUIDs; +import org.apache.cassandra.sidecar.common.server.CQLSessionProvider; +import org.apache.cassandra.sidecar.db.schema.ClusterOpsSchema; +import org.apache.cassandra.sidecar.job.storage.OperationalJobRecord; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link ClusterOpsDatabaseAccessor} + */ +class ClusterOpsDatabaseAccessorTest Review Comment: Same as the `ActiveClusterOpsDatabaseAccessorTest`, I think an integration test would be more appropriate than this. ########## server/src/test/java/org/apache/cassandra/sidecar/db/ActiveClusterOpsDatabaseAccessorTest.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.db; + +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.Statement; +import org.apache.cassandra.sidecar.common.server.CQLSessionProvider; +import org.apache.cassandra.sidecar.db.schema.ActiveClusterOpsSchema; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link ActiveClusterOpsDatabaseAccessor} + */ +class ActiveClusterOpsDatabaseAccessorTest Review Comment: This test seems to mostly just be calling mocks. I'm not sure how much value it adds over writing an integration test for the database accessor. See similar testing strategy for `/api/v2/cassandra/settings`: https://github.com/apache/cassandra-sidecar/pull/264/changes#diff-a06cba1264721044cdd996db320d93d4297be64406f09bc3081ca67a1e52bf4f ########## server/src/main/java/org/apache/cassandra/sidecar/db/schema/ClusterOpsSchema.java: ########## @@ -0,0 +1,154 @@ +/* + * 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.db.schema; + +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; +import org.apache.cassandra.sidecar.common.server.utils.SecondBoundConfiguration; +import org.apache.cassandra.sidecar.config.SchemaKeyspaceConfiguration; +import org.jetbrains.annotations.NotNull; + +/** + * Schema for the {@code cluster_ops} table, which persists and tracks operational jobs. + */ +public class ClusterOpsSchema extends TableSchema +{ + private static final String TABLE_NAME = "cluster_ops"; + + private final SchemaKeyspaceConfiguration keyspaceConfig; + private final SecondBoundConfiguration tableTtl; + + private PreparedStatement insertJob; + private PreparedStatement selectJob; + private PreparedStatement updateStatus; + private PreparedStatement findAllJobs; + + public ClusterOpsSchema(SchemaKeyspaceConfiguration keyspaceConfig, SecondBoundConfiguration tableTtl) + { + this.keyspaceConfig = keyspaceConfig; + this.tableTtl = tableTtl; + } + + @Override + protected String keyspaceName() + { + return keyspaceConfig.keyspace(); + } + + @Override + protected String tableName() + { + return TABLE_NAME; + } + + @Override + protected String createSchemaStatement() + { + return String.format("CREATE TABLE IF NOT EXISTS %s.%s (" + + " cluster_name text," + + " operation_id timeuuid," + + " operation_type text," + + " status text," + + " node_execution_order frozen<list<frozen<list<text>>>>," + Review Comment: Shouldn't this be a `frozen<list<forozen<list<uuid>>>>`? Since it stores the order of node ids in which the execution will run? ########## server/src/test/java/org/apache/cassandra/sidecar/db/ClusterOpsNodeStateDatabaseAccessorTest.java: ########## @@ -0,0 +1,119 @@ +/* + * 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.db; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.datastax.driver.core.BatchStatement; +import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.Statement; +import org.apache.cassandra.sidecar.common.data.OperationalJobStatus; +import org.apache.cassandra.sidecar.common.server.CQLSessionProvider; +import org.apache.cassandra.sidecar.db.schema.ClusterOpsNodeStateSchema; +import org.mockito.ArgumentCaptor; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link ClusterOpsNodeStateDatabaseAccessor} + */ +class ClusterOpsNodeStateDatabaseAccessorTest Review Comment: Same as the `ActiveClusterOpsDatabaseAccessorTest`, I think an integration test would be more appropriate than this. ########## server/src/main/java/org/apache/cassandra/sidecar/db/ClusterOpsNodeStateDatabaseAccessor.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.db; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import com.datastax.driver.core.BatchStatement; +import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.cassandra.sidecar.common.data.OperationalJobStatus; +import org.apache.cassandra.sidecar.common.server.CQLSessionProvider; +import org.apache.cassandra.sidecar.db.schema.ClusterOpsNodeStateSchema; +import org.apache.cassandra.sidecar.db.schema.SidecarSchema; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.VisibleForTesting; + +/** + * Database accessor for the {@code cluster_ops_node_state} table. + */ +@Singleton +public class ClusterOpsNodeStateDatabaseAccessor extends DatabaseAccessor<ClusterOpsNodeStateSchema> +{ + @Inject + public ClusterOpsNodeStateDatabaseAccessor(SidecarSchema sidecarSchema, CQLSessionProvider sessionProvider) + { + this(sidecarSchema.tableSchema(ClusterOpsNodeStateSchema.class), sessionProvider); + } + + @VisibleForTesting + public ClusterOpsNodeStateDatabaseAccessor(ClusterOpsNodeStateSchema schema, CQLSessionProvider sessionProvider) + { + super(schema, sessionProvider); + } + + public void updateNodeStatus(String clusterName, UUID operationId, UUID nodeId, OperationalJobStatus nodeStatus) + { + BoundStatement statement = tableSchema.insertNodeStatus() + .bind(clusterName, operationId, nodeId, nodeStatus.name()); + execute(statement); + } + + static final int BATCH_CHUNK_SIZE = 100; Review Comment: Please put static properties above constructors (per Cassandra style guide: https://cassandra.apache.org/_/development/code_style.html) -- 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]

