From d8d7e55b059ca41040e0328844a0a30a9855d1c6 Mon Sep 17 00:00:00 2001 From: Seungho Kang Date: Fri, 5 Jul 2024 10:52:40 +0900 Subject: [PATCH] Support Cassandra 3.x (#33) --- .github/workflows/ci-server.yml | 2 +- .../core/domain/cluster/TempClusterConnector.java | 10 ++++++++++ .../domain/cluster/keyspace/table/ClusterTable.java | 12 ++++++++---- .../cluster/keyspace/table/ClusterTableOption.java | 6 +++++- .../cadio/IntegrationTestDatabaseInitializer.java | 10 ++++------ 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci-server.yml b/.github/workflows/ci-server.yml index ee521e43..541ff24e 100644 --- a/.github/workflows/ci-server.yml +++ b/.github/workflows/ci-server.yml @@ -19,7 +19,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - cassandra-version: [ '4.0', '4.1', '5.0' ] # TODO: Add 3.11 + cassandra-version: [ '3.11', '4.0', '4.1', '5.0' ] steps: - name: checkout@v4 uses: actions/checkout@v4 diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java index c1eff145..fa8a7fdd 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java @@ -2,6 +2,8 @@ import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.CqlSessionBuilder; +import com.datastax.oss.driver.api.core.config.DefaultDriverOption; +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfo; import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoProvider; import lombok.extern.slf4j.Slf4j; @@ -9,6 +11,7 @@ import org.springframework.stereotype.Service; import java.net.InetSocketAddress; +import java.time.Duration; import java.util.ArrayList; import java.util.List; @@ -51,6 +54,13 @@ public CqlSession makeSession(ClusterConnection clusterConnection) { builder.withAuthCredentials(clusterConnection.getUsername(), clusterConnection.getPassword()); } + builder.withConfigLoader( + DriverConfigLoader.programmaticBuilder() + .withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(5)) + .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(5)) + .build() + ); + return builder.build(); } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTable.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTable.java index 84e37e29..872aeb5e 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTable.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTable.java @@ -7,11 +7,10 @@ import lombok.NoArgsConstructor; import lombok.ToString; -import java.util.Arrays; +import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.UUID; -import java.util.stream.Collectors; /** * ClusterTable @@ -38,8 +37,13 @@ private ClusterTable(UUID id, String tableName, String comment, Map options = Arrays.stream(ClusterTableOption.values()) - .collect(Collectors.toMap(option -> option.name().toLowerCase(Locale.ROOT), option -> option.extract(row))); + Map options = new HashMap<>(); + for (ClusterTableOption option : ClusterTableOption.values()) { + Object optionValue = option.extract(row); + if (optionValue != null) { + options.put(option.name().toLowerCase(Locale.ROOT), optionValue); + } + } return ClusterTable.builder() .id(row.getUuid("id")) diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableOption.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableOption.java index 96d4c7a9..234d2dac 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableOption.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableOption.java @@ -25,7 +25,11 @@ public enum ClusterTableOption { } public Object extract(Row row) { - return this.extracting.apply(row); + try { + return this.extracting.apply(row); + } catch (Exception exception) { + return null; + } } } diff --git a/cadio-core/src/test/java/kr/hakdang/cadio/IntegrationTestDatabaseInitializer.java b/cadio-core/src/test/java/kr/hakdang/cadio/IntegrationTestDatabaseInitializer.java index b86093c1..9228911f 100644 --- a/cadio-core/src/test/java/kr/hakdang/cadio/IntegrationTestDatabaseInitializer.java +++ b/cadio-core/src/test/java/kr/hakdang/cadio/IntegrationTestDatabaseInitializer.java @@ -39,7 +39,7 @@ public void initialize() { session.execute(createKeyspace.build()); - SimpleStatement createTable1 = SchemaBuilder.createTable("test_table_1") + SimpleStatement createTable1 = SchemaBuilder.createTable(keyspaceName, "test_table_1") .withPartitionKey("partition_key_1", DataTypes.TEXT) .withPartitionKey("partition_key_2", DataTypes.BIGINT) .withClusteringColumn("clustering_key_1", DataTypes.BIGINT) @@ -49,11 +49,10 @@ public void initialize() { .withClusteringOrder("clustering_key_2", ClusteringOrder.ASC) .withComment("test_table_one") .withBloomFilterFpChance(0.01) - .build() - .setKeyspace(keyspaceName); + .build(); session.execute(createTable1); - SimpleStatement createTable2 = SchemaBuilder.createTable("test_table_2") + SimpleStatement createTable2 = SchemaBuilder.createTable(keyspaceName, "test_table_2") .withPartitionKey("partition_key_11", DataTypes.TEXT) .withPartitionKey("partition_key_12", DataTypes.BIGINT) .withClusteringColumn("clustering_key_11", DataTypes.BIGINT) @@ -61,8 +60,7 @@ public void initialize() { .withColumn("column_11", DataTypes.TEXT) .withComment("test_table_two") .withBloomFilterFpChance(0.001) - .build() - .setKeyspace(keyspaceName); + .build(); session.execute(createTable2); }