Skip to content

Commit

Permalink
Prevent registration of unsupported Cassandra versions in the cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
seungh0 committed Jul 31, 2024
1 parent fe50fe9 commit fe174b4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kr.hakdang.cassdio.core.domain.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.Version;
import kr.hakdang.cassdio.common.error.NotSupportedCassandraVersionException;
import org.springframework.stereotype.Service;

/**
* CassdioVersionChecker
*
* @author seungh0
* @since 2024-07-31
*/
@Service
public class CassdioVersionChecker {

private static final Version MIN_SUPPORT_VERSION = Version.V3_0_0;

private final ClusterVersionEvaluator clusterVersionEvaluator;
private final ClusterConnector clusterConnector;

public CassdioVersionChecker(ClusterVersionEvaluator clusterVersionEvaluator, ClusterConnector clusterConnector) {
this.clusterVersionEvaluator = clusterVersionEvaluator;
this.clusterConnector = clusterConnector;
}

public void verifyCompatibilityCassandraVersion(ClusterConnection connection) {
try (CqlSession session = clusterConnector.makeSession(connection)) {
if (clusterVersionEvaluator.isLessThan(session, MIN_SUPPORT_VERSION)) {
throw new NotSupportedCassandraVersionException("Not supported cassandra with cluster version");
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kr.hakdang.cassdio.core.domain.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.Version;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;

/**
* ClusterVersionEvaluator
*
Expand All @@ -19,23 +22,63 @@ public ClusterVersionEvaluator(ClusterVersionGetCommander clusterVersionGetComma
}

public boolean isLessThan(String clusterId, Version version) {
return clusterVersionGetCommander.getCassandraVersion(clusterId).compareTo(version) < 0;
return isLessThan(() -> clusterVersionGetCommander.getCassandraVersion(clusterId), version);
}

public boolean isLessThanOrEqual(String clusterId, Version version) {
return clusterVersionGetCommander.getCassandraVersion(clusterId).compareTo(version) <= 0;
return isLessThanOrEqual(() -> clusterVersionGetCommander.getCassandraVersion(clusterId), version);
}

public boolean isGreaterThan(String clusterId, Version version) {
return clusterVersionGetCommander.getCassandraVersion(clusterId).compareTo(version) > 0;
return isGreaterThan(() -> clusterVersionGetCommander.getCassandraVersion(clusterId), version);
}

public boolean isGreaterThanOrEqual(String clusterId, Version version) {
return clusterVersionGetCommander.getCassandraVersion(clusterId).compareTo(version) >= 0;
return isGreaterThanOrEqual(() -> clusterVersionGetCommander.getCassandraVersion(clusterId), version);
}

public boolean isEqual(String clusterId, Version version) {
return clusterVersionGetCommander.getCassandraVersion(clusterId).compareTo(version) == 0;
return isEqual(() -> clusterVersionGetCommander.getCassandraVersion(clusterId), version);
}

public boolean isLessThan(CqlSession session, Version version) {
return isLessThan(() -> clusterVersionGetCommander.getCassandraVersionWithSession(session), version);
}

public boolean isLessThanOrEqual(CqlSession session, Version version) {
return isLessThanOrEqual(() -> clusterVersionGetCommander.getCassandraVersionWithSession(session), version);
}

public boolean isGreaterThan(CqlSession session, Version version) {
return isGreaterThan(() -> clusterVersionGetCommander.getCassandraVersionWithSession(session), version);
}

public boolean isGreaterThanOrEqual(CqlSession session, Version version) {
return isGreaterThanOrEqual(() -> clusterVersionGetCommander.getCassandraVersionWithSession(session), version);
}

public boolean isEqual(CqlSession session, Version version) {
return isEqual(() -> clusterVersionGetCommander.getCassandraVersionWithSession(session), version);
}

private boolean isLessThan(Supplier<Version> versionConsumer, Version target) {
return versionConsumer.get().compareTo(target) < 0;
}

private boolean isLessThanOrEqual(Supplier<Version> versionConsumer, Version target) {
return versionConsumer.get().compareTo(target) <= 0;
}

private boolean isGreaterThan(Supplier<Version> versionConsumer, Version target) {
return versionConsumer.get().compareTo(target) > 0;
}

private boolean isGreaterThanOrEqual(Supplier<Version> versionConsumer, Version target) {
return versionConsumer.get().compareTo(target) >= 0;
}

private boolean isEqual(Supplier<Version> versionConsumer, Version target) {
return versionConsumer.get().compareTo(target) == 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.micrometer.common.util.StringUtils;
import kr.hakdang.cassdio.common.error.NotSupportedCassandraVersionException;
import kr.hakdang.cassdio.core.domain.cluster.BaseClusterCommander;
import kr.hakdang.cassdio.core.domain.cluster.ClusterVersionGetCommander;
import kr.hakdang.cassdio.core.domain.cluster.ClusterVersionEvaluator;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.CassdioColumnDefinition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -32,21 +32,20 @@
@Service
public class ClusterQueryCommander extends BaseClusterCommander {

private final ClusterVersionGetCommander clusterVersionGetCommander;
private final ClusterVersionEvaluator clusterVersionEvaluator;

public ClusterQueryCommander(
ClusterVersionGetCommander clusterVersionGetCommander
ClusterVersionEvaluator clusterVersionEvaluator
) {
this.clusterVersionGetCommander = clusterVersionGetCommander;
this.clusterVersionEvaluator = clusterVersionEvaluator;
}

public boolean useKeyspaceQueryCommandNotSupport(String clusterId) {
CqlSession session = cqlSessionFactory.get(clusterId);
return useKeyspaceQueryCommandNotSupportWithSession(session);
return clusterVersionEvaluator.isLessThan(clusterId, Version.V4_0_0);
}

public boolean useKeyspaceQueryCommandNotSupportWithSession(CqlSession session) {
return clusterVersionGetCommander.getCassandraVersionWithSession(session).compareTo(Version.V4_0_0) < 0;
return clusterVersionEvaluator.isLessThan(session, Version.V4_0_0);
}

public QueryDTO.ClusterQueryCommanderResult execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import jakarta.validation.Valid;
import kr.hakdang.cassdio.common.CassdioConstants;
import kr.hakdang.cassdio.core.domain.cluster.ClusterConnector;
import kr.hakdang.cassdio.core.domain.cluster.CassdioVersionChecker;
import kr.hakdang.cassdio.core.domain.cluster.ClusterProvider;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfo;
import kr.hakdang.cassdio.web.common.dto.response.ApiResponse;
Expand Down Expand Up @@ -32,14 +32,14 @@
@RequestMapping("/api/cassandra/cluster")
public class ClusterApi {

private final ClusterConnector clusterConnector;
private final CassdioVersionChecker cassdioVersionChecker;
private final ClusterProvider clusterProvider;

public ClusterApi(
ClusterConnector clusterConnector,
CassdioVersionChecker cassdioVersionChecker,
ClusterProvider clusterProvider
) {
this.clusterConnector = clusterConnector;
this.cassdioVersionChecker = cassdioVersionChecker;
this.clusterProvider = clusterProvider;
}

Expand Down Expand Up @@ -75,6 +75,8 @@ public ApiResponse<Map<String, Object>> clusterDetail(
public ApiResponse<Void> clusterRegister(
@Valid @RequestBody ClusterRegisterRequest request
) {
cassdioVersionChecker.verifyCompatibilityCassandraVersion(request.makeArgs().makeClusterConnector());

clusterProvider.register(request.makeArgs());

return ApiResponse.ok();
Expand Down

0 comments on commit fe174b4

Please sign in to comment.