From 44089fcf1613b976db04c7562677b2cd45b8adae Mon Sep 17 00:00:00 2001 From: Seungho Kang Date: Fri, 5 Jul 2024 01:51:45 +0900 Subject: [PATCH] =?UTF-8?q?System=20Keyspace=20=EC=A0=95=EB=A0=AC=EC=8B=9C?= =?UTF-8?q?=20=EC=95=9E=EC=AA=BD=EC=97=90=20=EC=98=A4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20system=20keyspace=20=EC=9D=B8?= =?UTF-8?q?=EC=A7=80=20=EA=B5=AC=EB=B6=84=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분 * System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분 * System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분 --- .../cluster/ClusterNodeListCommander.java | 11 +++----- .../domain/cluster/TempClusterConnector.java | 4 ++- .../keyspace/CassandraSystemKeyspace.java | 7 +++++ .../keyspace/ClusterKeyspaceCommander.java | 8 +++--- .../keyspace/ClusterKeyspaceDetailResult.java | 10 ------- .../keyspace/ClusterKeyspaceListResult.java | 8 +++++- .../cluster/keyspace/KeyspaceResult.java | 2 ++ .../table/ClusterTableGetCommander.java | 14 +++------- .../keyspace/CassandraSystemKeyspaceTest.java | 27 +++++++++++++++++++ 9 files changed, 58 insertions(+), 33 deletions(-) delete mode 100644 cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceDetailResult.java create mode 100644 cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspaceTest.java diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterNodeListCommander.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterNodeListCommander.java index e5cca6e3..371ec964 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterNodeListCommander.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterNodeListCommander.java @@ -3,6 +3,7 @@ import com.datastax.oss.driver.api.core.CqlSession; import org.springframework.stereotype.Service; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -18,14 +19,8 @@ public class ClusterNodeListCommander extends BaseClusterCommander { public List listNodes(CqlSession session) { return session.getMetadata().getNodes().values().stream() .map(ClusterNode::from) - .sorted((o1, o2) -> { - if (o1.getDatacenter().compareTo(o2.getDatacenter()) < 0) { - return -1; - } else if (o1.getDatacenter().compareTo(o2.getDatacenter()) > 0) { - return 1; - } - return o1.getRack().compareTo(o2.getRack()); - }) + .sorted(Comparator.comparing(ClusterNode::getDatacenter) + .thenComparing(ClusterNode::getRack)) .collect(Collectors.toList()); } 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 f6aef1bb..c1eff145 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 @@ -6,7 +6,6 @@ import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoProvider; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.net.InetSocketAddress; @@ -57,6 +56,9 @@ public CqlSession makeSession(ClusterConnection clusterConnection) { public CqlSession makeSession(String clusterId) { ClusterInfo info = clusterInfoProvider.findClusterInfo(clusterId); + if (info == null) { + throw new IllegalArgumentException(String.format("failed to load Cluster(%s)", clusterId)); + } return makeSession(info.makeClusterConnector()); } } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspace.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspace.java index b3dd07c6..f316ccf1 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspace.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspace.java @@ -2,6 +2,8 @@ import lombok.Getter; +import java.util.Arrays; + /** * CassandraSystemKeyspace * @@ -26,4 +28,9 @@ public enum CassandraSystemKeyspace { this.keyspaceName = keyspaceName; } + public static boolean isSystemKeyspace(String keyspaceName) { + return Arrays.stream(CassandraSystemKeyspace.values()) + .anyMatch(keyspace -> keyspace.getKeyspaceName().equals(keyspaceName)); + } + } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java index 0be79c0a..3ce96ed7 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java @@ -1,14 +1,13 @@ package kr.hakdang.cadio.core.domain.cluster.keyspace; -import com.datastax.oss.driver.api.core.CqlIdentifier; import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata; import com.datastax.oss.driver.api.querybuilder.QueryBuilder; -import com.datastax.oss.driver.shaded.guava.common.collect.Maps; import kr.hakdang.cadio.core.domain.cluster.BaseClusterCommander; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -49,7 +48,10 @@ public ClusterKeyspaceListResult keyspaceList(CqlSession session) { } public String describe(CqlSession session, ClusterKeyspaceDescribeArgs args) { - //TODO : system keyspace 는 접근 못함. + if (CassandraSystemKeyspace.isSystemKeyspace(args.getKeyspace())) { + return StringUtils.EMPTY; + } + KeyspaceMetadata keyspaceMetadata = session.getMetadata().getKeyspace(args.getKeyspace()) .orElseThrow(() -> new RuntimeException("not found keyspace")); diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceDetailResult.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceDetailResult.java deleted file mode 100644 index 12828e14..00000000 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceDetailResult.java +++ /dev/null @@ -1,10 +0,0 @@ -package kr.hakdang.cadio.core.domain.cluster.keyspace; - -/** - * ClusterKeyspaceDetailResult - * - * @author akageun - * @since 2024-07-02 - */ -public class ClusterKeyspaceDetailResult { -} diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceListResult.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceListResult.java index b5d6a8ea..d3e454dc 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceListResult.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceListResult.java @@ -5,7 +5,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.Comparator; import java.util.List; +import java.util.stream.Collectors; /** * ClusterKeyspaceListResult @@ -23,6 +25,10 @@ public class ClusterKeyspaceListResult { @Builder public ClusterKeyspaceListResult(boolean wasApplied, List keyspaceList) { this.wasApplied = wasApplied; - this.keyspaceList = keyspaceList; + this.keyspaceList = keyspaceList.stream() + .sorted(Comparator.comparing(KeyspaceResult::isSystemKeyspace).reversed() + .thenComparing(KeyspaceResult::getKeyspaceName)) + .collect(Collectors.toList()); } + } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/KeyspaceResult.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/KeyspaceResult.java index 2a30ac27..e05c5885 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/KeyspaceResult.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/KeyspaceResult.java @@ -20,11 +20,13 @@ public class KeyspaceResult { private String keyspaceName; private boolean durableWrites; private Map replication; + private boolean isSystemKeyspace; @Builder public KeyspaceResult(String keyspaceName, boolean durableWrites, Map replication) { this.keyspaceName = keyspaceName; this.durableWrites = durableWrites; this.replication = replication; + this.isSystemKeyspace = CassandraSystemKeyspace.isSystemKeyspace(keyspaceName); } } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableGetCommander.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableGetCommander.java index 14432731..c159e862 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableGetCommander.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/table/ClusterTableGetCommander.java @@ -3,7 +3,6 @@ import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.Row; import com.datastax.oss.driver.api.core.cql.SimpleStatement; -import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata; import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata; import com.datastax.oss.driver.api.querybuilder.QueryBuilder; import kr.hakdang.cadio.core.domain.cluster.BaseClusterCommander; @@ -14,6 +13,7 @@ import org.springframework.stereotype.Service; import java.time.Duration; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -46,7 +46,7 @@ public ClusterTableGetResult getTable(CqlSession session, ClusterTableGetArgs ar } String tableDescribe = ""; - if (args.isWithTableDescribe()) { + if (!CassandraSystemKeyspace.isSystemKeyspace(args.getKeyspace()) && args.isWithTableDescribe()) { TableMetadata tableMetadata = session.getMetadata().getKeyspace(args.getKeyspace()) .orElseThrow(() -> new RuntimeException("not found keyspace")) .getTable(args.getTable()) @@ -72,14 +72,8 @@ private List getColumnsInTable(CqlSession session, ClusterTableGetArgs a List columnRows = session.execute(statement).all(); return columnRows.stream() .map(Column::from) - .sorted((o1, o2) -> { - if (o1.getKind().getOrder() < o2.getKind().getOrder()) { - return -1; - } else if (o1.getKind().getOrder() > o2.getKind().getOrder()) { - return 1; - } - return o1.getPosition() - o2.getPosition(); - }) + .sorted(Comparator.comparing(o1 -> ((Column) o1).getKind().getOrder()) + .thenComparing(o1 -> ((Column) o1).getPosition())) .collect(Collectors.toList()); } diff --git a/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspaceTest.java b/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspaceTest.java new file mode 100644 index 00000000..910a6e5a --- /dev/null +++ b/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/keyspace/CassandraSystemKeyspaceTest.java @@ -0,0 +1,27 @@ +package kr.hakdang.cadio.core.domain.cluster.keyspace; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * CassandraSystemKeyspaceTest + * + * @author seungh0 + * @since 2024-07-05 + */ +class CassandraSystemKeyspaceTest { + + @Test + void system_keyspace() { + for (CassandraSystemKeyspace keyspace : CassandraSystemKeyspace.values()) { + assertThat(CassandraSystemKeyspace.isSystemKeyspace(keyspace.getKeyspaceName())).isTrue(); + } + } + + @Test + void not_system_keyspace() { + assertThat(CassandraSystemKeyspace.isSystemKeyspace("demo")).isFalse(); + } + +}