Skip to content

Commit

Permalink
Cluster Connector Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
akageun committed Jul 3, 2024
1 parent ace69b0 commit d42a18d
Show file tree
Hide file tree
Showing 24 changed files with 519 additions and 152 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package kr.hakdang.cadio.core.domain.cluster;

import io.micrometer.common.util.StringUtils;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* ClusterInfo
* ClusterConnection
*
* @author akageun
* @since 2024-07-02
*/
@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterInfo {
public class ClusterConnection {
private String contactPoints;
private int port;
private String localDatacenter;
private String username;
private String password;

@Builder
public ClusterInfo(String contactPoints, int port, String localDatacenter, String username, String password) {
public ClusterConnection(String contactPoints, int port, String localDatacenter, String username, String password) {
//TODO : Validation

this.contactPoints = contactPoints;
Expand All @@ -32,4 +33,8 @@ public ClusterInfo(String contactPoints, int port, String localDatacenter, Strin
this.username = username;
this.password = password; //TODO : 암호화
}

public boolean isAuthCredentials() {
return StringUtils.isNotBlank(this.username) && StringUtils.isNotBlank(this.password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package kr.hakdang.cadio.core.domain.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfo;
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;
import java.util.ArrayList;
import java.util.List;

/**
* TempClusterConnector
* - 임시 목적으로 사용할 connector
*
* @author akageun
* @since 2024-07-03
*/
@Slf4j
@Service
public class TempClusterConnector {

private final ClusterInfoProvider clusterInfoProvider;

public TempClusterConnector(
ClusterInfoProvider clusterInfoProvider
) {
this.clusterInfoProvider = clusterInfoProvider;
}

public List<InetSocketAddress> makeContactPoint(String contactPoints, int port) {
String[] contactPointArr = StringUtils.split(contactPoints, ",");

List<InetSocketAddress> result = new ArrayList<>();
for (String contactPoint : contactPointArr) {
result.add(new InetSocketAddress(contactPoint, port));
}

return result;
}

public CqlSession makeSession(ClusterConnection clusterConnection) {
CqlSessionBuilder builder = CqlSession.builder()
.addContactPoints(makeContactPoint(clusterConnection.getContactPoints(), clusterConnection.getPort()))
.withLocalDatacenter(clusterConnection.getLocalDatacenter());

if (clusterConnection.isAuthCredentials()) {
builder.withAuthCredentials(clusterConnection.getUsername(), clusterConnection.getPassword());
}

return builder.build();
}

public CqlSession makeSession(String clusterId) {
ClusterInfo info = clusterInfoProvider.findClusterInfo(clusterId);
return makeSession(info.makeClusterConnector());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kr.hakdang.cadio.core.domain.cluster.info;

import com.fasterxml.jackson.core.type.TypeReference;
import kr.hakdang.cadio.core.domain.cluster.ClusterInfo;
import kr.hakdang.cadio.core.domain.cluster.ClusterConnection;

import java.io.File;
import java.util.List;
Expand All @@ -14,7 +14,7 @@
*/
public abstract class BaseClusterInfo {

protected final static TypeReference<List<ClusterInfo>> TYPED = new TypeReference<List<ClusterInfo>>() {
protected final static TypeReference<List<ClusterInfo>> TYPED = new TypeReference<>() {
};

protected String cadioBaseDir() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package kr.hakdang.cadio.core.domain.cluster.info;

import kr.hakdang.cadio.core.domain.cluster.ClusterConnection;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* ClusterInfo
*
* @author akageun
* @since 2024-07-03
*/
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterInfo {
private String clusterId;
private String clusterName;
private String contactPoints;
private int port;
private String localDatacenter;
private String username;
private String password;

@Builder(toBuilder = true)
public ClusterInfo(
String clusterId,
String clusterName,
String contactPoints,
int port,
String localDatacenter,
String username,
String password
) {
this.clusterId = clusterId;
this.clusterName = clusterName;
this.contactPoints = contactPoints;
this.port = port;
this.localDatacenter = localDatacenter;
this.username = username;
this.password = password;
}

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 @@ -3,13 +3,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import kr.hakdang.cadio.common.Jsons;
import kr.hakdang.cadio.core.domain.bootstrap.BootstrapProvider;
import kr.hakdang.cadio.core.domain.cluster.ClusterInfo;
import kr.hakdang.cadio.core.domain.cluster.ClusterConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* ClusterManager
Expand All @@ -33,22 +34,15 @@ public ClusterInfoManager(
this.clusterInfoProvider = clusterInfoProvider;
}

public void register(
String contactPoints,
int port,
String localDatacenter,
String username,
String password
) {
public void register(ClusterInfoRegisterArgs args) {
ObjectMapper om = Jsons.OBJECT_MAPPER;

try {
String cadioBaseDir = cadioBaseDir();

File baseDir = new File(cadioBaseDir);
if (!baseDir.exists()) {
boolean result = baseDir.mkdir();
log.info("make base directory : {}, path : {}", result, cadioBaseDir);
log.info("make base directory : {}, path : {}", baseDir.mkdir(), cadioBaseDir);
}

File clusterJsonFile = getClusterJsonFile();
Expand All @@ -57,16 +51,9 @@ public void register(
}

List<ClusterInfo> result = om.readValue(clusterJsonFile, TYPED);
result.add(ClusterInfo.builder()
.contactPoints(contactPoints)
.port(port)
.localDatacenter(localDatacenter)
.username(username)
.password(password)
.build());
result.add(args.makeClusterInfo(UUID.randomUUID().toString()));

om.writeValue(clusterJsonFile, result);
bootstrapProvider.updateMinClusterCountCheck(clusterInfoProvider.checkMinClusterCount());

} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package kr.hakdang.cadio.core.domain.cluster.info;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import kr.hakdang.cadio.common.Jsons;
import kr.hakdang.cadio.core.domain.cluster.ClusterInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* ClusterInfoProvider
Expand All @@ -25,23 +26,25 @@
@Service
public class ClusterInfoProvider extends BaseClusterInfo {

private static final ObjectMapper OM = Jsons.OBJECT_MAPPER;

public boolean checkMinClusterCount() {
long count = registeredCount();

return count > 0;
}

public long registeredCount() {
ObjectMapper om = Jsons.OBJECT_MAPPER;

try {
List<ClusterInfo> result = om.readValue(getClusterJsonFile(), TYPED);
List<ClusterInfo> result = OM.readValue(getClusterJsonFile(), TYPED);

if (CollectionUtils.isEmpty(result)) {
return 0;
}

return result.size();

} catch (FileNotFoundException e) {
log.warn(e.getMessage(), e); //첫 실행시, TODO : 에러메시지 등 고민 필요
return 0;
Expand All @@ -51,4 +54,23 @@ public long registeredCount() {
}
}

public List<ClusterInfo> getList() {
try {
return OM.readValue(getClusterJsonFile(), TYPED);
} catch (IOException e) {
log.error(e.getMessage(), e);
return Collections.emptyList();
}
}

/**
* @return key : clusterId
*/
public Map<String, ClusterInfo> getDictionary() {
return getList().stream().collect(Collectors.toMap(ClusterInfo::getClusterId, o -> o));
}

public ClusterInfo findClusterInfo(String clusterId) {
return getDictionary().get(clusterId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kr.hakdang.cadio.core.domain.cluster.info;

import kr.hakdang.cadio.core.domain.cluster.ClusterConnection;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* ClusterInfoRegisterArgs
*
* @author akageun
* @since 2024-07-03
*/
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterInfoRegisterArgs {
private String clusterName;
private String contactPoints;
private int port;
private String localDatacenter;
private String username;
private String password;

//Append

@Builder
public ClusterInfoRegisterArgs(String clusterName, String contactPoints, int port, String localDatacenter, String username, String password) {
this.clusterName = clusterName;
this.contactPoints = contactPoints;
this.port = port;
this.localDatacenter = localDatacenter;
this.username = username;
this.password = password;
}

public ClusterInfo makeClusterInfo(String clusterId) {
return ClusterInfo.builder()
.clusterId(clusterId)
.clusterName(clusterName)
.contactPoints(contactPoints)
.port(port)
.localDatacenter(localDatacenter)
.username(username)
.password(password)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.hakdang.cadio.core.domain.cluster.info;

/**
* ClusterInfoValidateArgs
*
* @author akageun
* @since 2024-07-03
*/
public class ClusterInfoValidateArgs {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public ClusterKeyspaceListResult keyspaceList(CqlSession session) {
boolean wasApplied = resultSet.wasApplied();
List<KeyspaceResult> keyspaceList = new ArrayList<>();
for (Row row : resultSet.all()) {
log.info("row :{}", row.getFormattedContents());
keyspaceList.add(
KeyspaceResult.builder()
.keyspaceName(row.getString("keyspace_name"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
class ClusterInfoManagerTest extends IntegrationTest {
class ClusterConnectionManagerTest extends IntegrationTest {

@Autowired
private ClusterInfoManager clusterInfoManager;

@Test
void registerTest() {
clusterInfoManager.register("127.0.0.1", 9042, "dc1", "", "");
// clusterInfoManager.register("127.0.0.1", 9042, "dc1", "", "");
}
}
Loading

0 comments on commit d42a18d

Please sign in to comment.