Skip to content

Commit

Permalink
chore(ct): reformat code style based on code review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
HJ-Young committed Sep 25, 2024
1 parent 48cfc6e commit 80835a3
Show file tree
Hide file tree
Showing 20 changed files with 426 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ arthas.disabled_commands=jad
#auth.user_tokens=[]

# rpc server configs for multi graph-servers or raft-servers
rpc.server_host=$RPC_HOST$
rpc.server_host=127.0.0.1
rpc.server_port=$RPC_PORT$
#rpc.server_timeout=30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ management:

grpc:
# grpc 的服务地址
host: $GRPC_HOST$
host: 127.0.0.1
port: $GRPC_PORT$
netty-server:
max-inbound-message-size: 1000MB
Expand All @@ -48,8 +48,8 @@ server:

app:
# 存储路径,支持多个路径,逗号分割
data-path: $STORE_DATA_PATH$
#raft-path: $STORE_DATA_PATH$
data-path: ./storage
#raft-path: ./storage

spring:
application:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
package org.apache.hugegraph.ct.base;

public enum EnvType {
Simple,

SingleNode,
MultiNode;

public static EnvType getSystemEnvType() {
String envType = System.getProperty("TestEnv", Simple.name());
String envType = System.getProperty("test_env", SingleNode.toString());
return EnvType.valueOf(envType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,49 @@

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;

public class EnvUtil {

private static final Logger LOG = HGTestLogger.LOG;
private static final List<Integer> ports = new ArrayList<>();
private static final Set<Integer> ports = new HashSet<>();

public static int getAvailablePort() {
try {
ServerSocket socket = new ServerSocket(0);
int port = socket.getLocalPort();
while (ports.contains(port)) {
socket.close();
socket = new ServerSocket(0);
int port = -1;
while (port < 0 || ports.contains(port)) {
ServerSocket socket = new ServerSocket(0);
port = socket.getLocalPort();
socket.close();
}
ports.add(port);
socket.close();
return port;
} catch (IOException e) {
LOG.error("fail to get available ports", e);
LOG.error("Failed to get available ports", e);
return -1;
}
}

public static void copyFileToDestination(Path source, Path destination) {
try {
ensureParentDirectoryExists(destination);
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioException) {
LOG.error("Failed to copy files to destination dir", ioException);
throw new RuntimeException(ioException);
}
}

private static void ensureParentDirectoryExists(Path destination) throws IOException {
Path parentDir = destination.getParent();
if (parentDir != null && Files.notExists(parentDir)) {
Files.createDirectories(parentDir);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ protected void readTemplate(Path filePath) {
protected void updateConfigs() {
for (Map.Entry<String, String> entry : properties.entrySet()) {
String placeholder = "$" + entry.getKey() + "$";
config = config.replace(placeholder, entry.getValue());
this.config = this.config.replace(placeholder, entry.getValue());
}
}

public void writeConfig(String filePath) {
updateConfigs();
Path destPath = Paths.get(filePath + File.separator + fileName);
Path destPath = Paths.get(filePath + File.separator + this.fileName);
try {
if (Files.notExists(destPath.getParent())) {
Files.createDirectories(destPath.getParent());
}
} catch (IOException e) {
e.printStackTrace();
LOG.error("Failed to create dir", e);
}
try (FileWriter writer = new FileWriter(String.valueOf(destPath))) {
writer.write(config);
writer.write(this.config);
} catch (IOException e) {
e.printStackTrace();
LOG.error("Failed to write in file", e);
}
}

Expand All @@ -80,5 +80,3 @@ protected void setProperty(String propertyName, String value) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.hugegraph.ct.base.HGTestLogger;
import org.slf4j.Logger;

public class ClusterConf {
public class ClusterConfig {

protected static final Logger LOG = HGTestLogger.LOG;
protected List<PDConfig> pdConfigs;
Expand All @@ -33,7 +33,7 @@ public class ClusterConf {

protected List<String> pdGrpcList, pdRaftList, storeGrpcList;

public ClusterConf(int pdCnt, int storeCnt, int serverCnt) {
public ClusterConfig(int pdCnt, int storeCnt, int serverCnt) {
pdConfigs = new ArrayList<>();
storeConfigs = new ArrayList<>();
serverConfigs = new ArrayList<>();
Expand Down Expand Up @@ -96,6 +96,14 @@ public List<String> getPDRestAddrs() {
return addrs;
}

public List<String> getPDGrpcAddrs() {
List<String> addrs = new ArrayList<>();
for (PDConfig pdConfig : pdConfigs) {
addrs.add(pdConfig.getGrpcAddress());
}
return addrs;
}

public List<String> getStoreRestAddrs() {
List<String> addrs = new ArrayList<>();
for (StoreConfig storeConfig : storeConfigs) {
Expand All @@ -105,6 +113,15 @@ public List<String> getStoreRestAddrs() {
return addrs;
}

public List<String> getStoreGrpcAddrs() {
List<String> addrs = new ArrayList<>();
for (StoreConfig storeConfig : storeConfigs) {
addrs.add("127.0.0.1" + ":" +
storeConfig.getGrpcPort());
}
return addrs;
}

public List<String> getServerRestAddrs() {
List<String> addrs = new ArrayList<>();
for (ServerConfig serverConfig : serverConfigs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ public void setPDPeersList(List<String> pdPeersList) {
String pdPeers = pdPeersList.stream().collect(Collectors.joining(","));
setProperty("PD_PEERS_LIST", pdPeers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
public class PDConfig extends AbstractConfig {

@Getter
private final int raftPort, grpcPort, restPort;
private final int raftPort;
@Getter
private final int grpcPort;
@Getter
private final int restPort;

public PDConfig() {
readTemplate(
Expand All @@ -40,14 +44,9 @@ public PDConfig() {
this.raftPort = getAvailablePort();
this.grpcPort = getAvailablePort();
this.restPort = getAvailablePort();
properties.put("GRPC_PORT", String.valueOf(grpcPort));
properties.put("REST_PORT", String.valueOf(restPort));
properties.put("RAFT_ADDRESS", "127.0.0.1:"
+ raftPort);
}

public void setRaftHost(String raftHost) {
setProperty("RAFT_ADDRESS", "127.0.0.1:" + raftPort);
properties.put("GRPC_PORT", String.valueOf(this.grpcPort));
properties.put("REST_PORT", String.valueOf(this.restPort));
properties.put("RAFT_ADDRESS", "127.0.0.1:" + this.raftPort);
}

public void setRaftPeerList(List<String> raftPeerList) {
Expand All @@ -67,10 +66,10 @@ public void setStoreGrpcList(List<String> storeGrpcList) {
}

public String getRaftAddress() {
return "127.0.0.1:" + raftPort;
return "127.0.0.1:" + this.raftPort;
}

public String getGrpcAddress() {
return "127.0.0.1:" + grpcPort;
return "127.0.0.1:" + this.grpcPort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,17 @@
public class ServerConfig extends AbstractConfig {

@Getter
private int rpcPort, restPort;
private int rpcPort;
@Getter
private String restHost, rpcHost;
private int restPort;

public ServerConfig() {
readTemplate(
Paths.get(CT_PACKAGE_PATH + SERVER_TEMPLATE_FILE));
readTemplate(Paths.get(CT_PACKAGE_PATH + SERVER_TEMPLATE_FILE));
this.fileName = SERVER_PROPERTIES;
this.restHost = "127.0.0.1";
this.rpcHost = "127.0.0.1";
this.rpcPort = getAvailablePort();
this.restPort = getAvailablePort();
properties.put("REST_SERVER_ADDRESS", restHost + ":" + restPort);
properties.put("RPC_PORT", String.valueOf(rpcPort));
properties.put("RPC_HOST", rpcHost);
}

public void setRestHost(String restHost) {
this.restHost = restHost;
setProperty("REST_SERVER_ADDRESS", restHost + ":" + restPort);
properties.put("REST_SERVER_ADDRESS", "127.0.0.1:" + this.restPort);
properties.put("RPC_PORT", String.valueOf(this.rpcPort));
}

public void setServerID(String serverID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,22 @@
public class StoreConfig extends AbstractConfig {

@Getter
private int raftPort, grpcPort, restPort;
private int raftPort;
@Getter
private String grpcHost, dataPath, raftHost;
private int grpcPort;
@Getter
private int restPort;

public StoreConfig() {
readTemplate(
Paths.get(CT_PACKAGE_PATH + STORE_TEMPLATE_FILE));
this.fileName = APPLICATION_FILE;
this.grpcHost = "127.0.0.1";
this.raftHost = "127.0.0.1";
this.dataPath = "./storage";
this.raftPort = getAvailablePort();
this.grpcPort = getAvailablePort();
this.restPort = getAvailablePort();
properties.put("GRPC_HOST", grpcHost);
properties.put("GRPC_PORT", String.valueOf(grpcPort));
properties.put("REST_PORT", String.valueOf(restPort));
properties.put("STORE_DATA_PATH", dataPath);
properties.put("RAFT_ADDRESS", raftHost + ":"
+ raftPort);
}

public void setGrpcHost(String grpcHost) {
this.grpcHost = grpcHost;
setProperty("GRPC_HOST", grpcHost);
}

public void setDataPath(String dataPath) {
this.dataPath = dataPath;
setProperty("PD_DATA_PATH", dataPath);
}

public void setRaftHost(String raftHost) {
this.raftHost = raftHost;
setProperty("RAFT_ADDRESS", raftHost + ":" + raftPort);
properties.put("GRPC_PORT", String.valueOf(this.grpcPort));
properties.put("REST_PORT", String.valueOf(this.restPort));
properties.put("RAFT_ADDRESS", "127.0.0.1:" + this.raftPort);
}

public void setPDServerList(List<String> pdServerList) {
Expand All @@ -75,6 +56,6 @@ public void setPDServerList(List<String> pdServerList) {
}

public String getGrpcAddress() {
return grpcHost + ":" + grpcPort;
return "127.0.0.1:" + this.grpcPort;
}
}
Loading

0 comments on commit 80835a3

Please sign in to comment.