Skip to content

Commit

Permalink
driver bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
akageun committed Jul 30, 2024
1 parent 88e4522 commit 5d7cca0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.hakdang.cassdio.core.domain.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
import kr.hakdang.cassdio.common.utils.IdGenerator;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfo;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfoArgs;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterManager;
Expand All @@ -10,6 +12,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
* ClusterProvider
Expand All @@ -22,9 +25,11 @@
public class ClusterProvider {

private final ClusterManager clusterManager;
private final ClusterConnector clusterConnector;

public ClusterProvider(ClusterManager clusterManager) {
public ClusterProvider(ClusterManager clusterManager, ClusterConnector clusterConnector) {
this.clusterManager = clusterManager;
this.clusterConnector = clusterConnector;
}

public List<ClusterInfo> findAll() {
Expand All @@ -45,12 +50,24 @@ public ClusterInfo findByIdWithoutCache(String clusterId) {
}

public void register(ClusterInfoArgs args) {
clusterManager.register(args);
try (CqlSession session = clusterConnector.makeSession(args.makeClusterConnector())) {
String clusterName = session.getMetadata().getClusterName()
.orElse(UUID.randomUUID().toString());

String clusterId = IdGenerator.makeId();

clusterManager.register(args.makeClusterInfo(clusterId, clusterName));
}
}

@CacheEvict(cacheNames = CacheType.CacheTypeNames.CLUSTER_DETAIL, key = "#clusterId")
public void updateById(String clusterId, ClusterInfoArgs args) {
clusterManager.update(clusterId, args);
try (CqlSession session = clusterConnector.makeSession(args.makeClusterConnector())) {
String clusterName = session.getMetadata().getClusterName()
.orElse(UUID.randomUUID().toString());

clusterManager.update(clusterId, args.makeClusterInfo(clusterId, clusterName));
}
}

@CacheEvict(cacheNames = CacheType.CacheTypeNames.CLUSTER_DETAIL, key = "#clusterId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterInfoArgs {
private String clusterName;
private String contactPoints;
private int port;
private String localDatacenter;
Expand All @@ -28,11 +27,11 @@ public class ClusterInfoArgs {

@Builder
public ClusterInfoArgs(
String clusterName, String contactPoints, int port, String localDatacenter, String username, String password, String memo
String contactPoints, int port, String localDatacenter, String username, String password, String memo
) {
if (StringUtils.isBlank(clusterName)) {
throw new IllegalArgumentException("Cluster name is blank");
}
// if (StringUtils.isBlank(clusterName)) {
// throw new IllegalArgumentException("Cluster name is blank");
// }

if (StringUtils.isBlank(contactPoints)) {
throw new IllegalArgumentException("Contact points is blank");
Expand All @@ -46,7 +45,6 @@ public ClusterInfoArgs(
throw new IllegalArgumentException("Local datacenter is blank");
}

this.clusterName = clusterName;
this.contactPoints = contactPoints;
this.port = port;
this.localDatacenter = localDatacenter;
Expand All @@ -55,7 +53,7 @@ public ClusterInfoArgs(
this.memo = memo;
}

public ClusterInfo makeClusterInfo(String clusterId) {
public ClusterInfo makeClusterInfo(String clusterId, String clusterName) {
return ClusterInfo.builder()
.clusterId(clusterId)
.clusterName(clusterName)
Expand All @@ -67,4 +65,14 @@ public ClusterInfo makeClusterInfo(String clusterId) {
.memo(memo)
.build();
}

public ClusterConnection makeClusterConnector() {
return ClusterConnection.builder()
.contactPoints(contactPoints)
.port(port)
.localDatacenter(localDatacenter)
.username(username)
.password(password)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,22 @@ public class ClusterManager implements InitializingBean, DisposableBean {

private static DB mapDb;

public void register(ClusterInfoArgs args) {
public void register(ClusterInfo info) {
ConcurrentMap<String, String> map = mapDb
.hashMap("cluster", Serializer.STRING, Serializer.STRING)
.createOrOpen();

String clusterId = IdGenerator.makeId();

map.put(clusterId, Jsons.toJson(args.makeClusterInfo(clusterId)));
map.put(info.getClusterId(), Jsons.toJson(info));

mapDb.commit();
}

public void update(String clusterId, ClusterInfoArgs args) {
public void update(String clusterId, ClusterInfo info) {
ConcurrentMap<String, String> map = mapDb
.hashMap("cluster", Serializer.STRING, Serializer.STRING)
.createOrOpen();

map.put(clusterId, Jsons.toJson(args.makeClusterInfo(clusterId)));
map.put(clusterId, Jsons.toJson(info));

mapDb.commit();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package kr.hakdang.cassdio.web.route.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
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.ClusterProvider;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfo;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfoArgs;
import kr.hakdang.cassdio.web.common.dto.response.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -22,7 +20,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* ClusterApi
Expand Down Expand Up @@ -78,15 +75,7 @@ public ApiResponse<Map<String, Object>> clusterDetail(
public ApiResponse<Void> clusterRegister(
@Valid @RequestBody ClusterRegisterRequest request
) {
try (CqlSession session = clusterConnector.makeSession(request.makeClusterConnector())) {
String clusterName = session.getMetadata().getClusterName()
.orElse(UUID.randomUUID().toString());

ClusterInfoArgs args = request.makeArgs(clusterName);
//실행 안되면 exception

clusterProvider.register(args);
}
clusterProvider.register(request.makeArgs());

return ApiResponse.ok();
}
Expand All @@ -96,14 +85,8 @@ public ApiResponse<Void> clusterUpdate(
@PathVariable(name = CassdioConstants.CLUSTER_ID_PATH) String clusterId,
@Valid @RequestBody ClusterRegisterRequest request
) {
try (CqlSession session = clusterConnector.makeSession(request.makeClusterConnector())) {
String clusterName = session.getMetadata().getClusterName()
.orElse(UUID.randomUUID().toString());

ClusterInfoArgs args = request.makeArgs(clusterName);

clusterProvider.updateById(clusterId, args);
}
clusterProvider.updateById(clusterId, request.makeArgs());

return ApiResponse.ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public ClusterRegisterRequest(String contactPoints, int port, String localDatace
this.memo = memo;
}

public ClusterInfoArgs makeArgs(String clusterName) {
public ClusterInfoArgs makeArgs() {
return ClusterInfoArgs.builder()
.clusterName(clusterName)
.contactPoints(contactPoints)
.port(port)
.localDatacenter(localDatacenter)
Expand All @@ -44,14 +43,4 @@ public ClusterInfoArgs makeArgs(String clusterName) {
.memo(memo)
.build();
}

public ClusterConnection makeClusterConnector() {
return ClusterConnection.builder()
.contactPoints(contactPoints)
.port(port)
.localDatacenter(localDatacenter)
.username(username)
.password(password)
.build();
}
}

0 comments on commit 5d7cca0

Please sign in to comment.