Skip to content

Commit

Permalink
make peerId optional
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveTeng0 committed Apr 4, 2024
1 parent 6621086 commit 15cf83d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
import org.apache.ratis.protocol.RaftPeerId;
//import org.apache.ratis.shell.cli.RaftUtils;
import org.apache.ratis.shell.cli.RaftUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
Expand All @@ -37,7 +38,9 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Command for generate a new raft-meta.conf file based on original raft-meta.conf and new peers,
Expand Down Expand Up @@ -70,19 +73,33 @@ public int run(CommandLine cl) throws IOException {
printf("peers or path can't be empty.");
return -1;
}
Set<String> addresses = new HashSet<>();
List<RaftPeerProto> raftPeerProtos = new ArrayList<>();
for (String idWithAddress : peersStr.split(",")) {
String[] tmp = idWithAddress.split("\\|");
String message = "Please make sure to provide list of peers" +
" in format <P0_Id:P0_HOST:P0_PORT,P1_Id:P1_HOST:P1_PORT,P2_Id:P2_HOST:P2_PORT>";
if (tmp.length < 2) {
" in format <P0_Id|P0_HOST:P0_PORT,P1_Id|P1_HOST:P1_PORT,P2_Id|P2_HOST:P2_PORT> or " +
"<P0_HOST:P0_PORT,P1_HOST:P1_PORT,P2_HOST:P2_PORT>";
if (tmp.length < 1 || tmp.length > 2) {
printf(message);
return -1;
}
String address = parseInetSocketAddress(tmp[tmp.length - 1]).toString();
if (addresses.contains(address)) {
printf("Please make sure the addresses of peers have no duplicated value.");
return -1;
}
addresses.add(address);
// String peerId = RaftUtils.getPeerId(parseInetSocketAddress(idWithAddress)).toString();
String peerId = RaftPeerId.getRaftPeerId(tmp[0]).toString();
String peerId;
if (tmp.length == 2) {
// Peer ID is provided
peerId = RaftPeerId.getRaftPeerId(tmp[0]).toString();
} else {
// Peer ID is not provided
peerId = RaftUtils.getPeerId(parseInetSocketAddress(address)).toString(); // use host address as default value
}

String address = parseInetSocketAddress(tmp[1]).toString();
raftPeerProtos.add(RaftPeerProto.newBuilder()
.setId(ByteString.copyFrom(peerId.getBytes(StandardCharsets.UTF_8))).setAddress(address)
.setStartupRole(RaftPeerRole.FOLLOWER).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
Expand All @@ -49,6 +48,8 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -81,12 +82,16 @@ public void setup() throws IOException {
}


void getRaftConf(Path path, int index) throws IOException {
void getRaftConf(String peerStr, Path path, int index) throws IOException {
Map<String, String> map = new HashMap<>();
map.put("peer1_Id", "host1:9872");
map.put("peer2_Id", "host2:9872");
map.put("peer3_Id", "host3:9872");
map.put("peer4_Id", "host4:9872");
// if (containsPeerId(peerStr)) {
//
// } else {
map.put("peer1_Id", "host1:9872");
map.put("peer2_Id", "host2:9872");
map.put("peer3_Id", "host3:9872");
map.put("peer4_Id", "host4:9872");
// }
List<RaftProtos.RaftPeerProto> raftPeerProtos = new ArrayList<>();
for (Map.Entry<String, String> en : map.entrySet()) {
raftPeerProtos.add(RaftProtos.RaftPeerProto.newBuilder()
Expand All @@ -102,46 +107,77 @@ void getRaftConf(Path path, int index) throws IOException {
generateLogEntryProto.writeTo(out);
}
}

private static List<String> data() {
return Arrays.asList(
"peer1_Id|host1:9872,peer2_id|host2:9872,peer3_id|host3:9872",
"host1:9872,host2:9872,host3:9872");
}

// @ParameterizedTest
// @MethodSource("data")
@Test
public void testRunMethod(@TempDir Path tempDir) throws Exception {
Path output = tempDir
.resolve(RAFT_META_CONF);
int index = 1;
getRaftConf(output, index);
String[] testPeersList = {"peer1_Id|host1:9872,peer2_id|host2:9872,peer3_id|host3:9872",
"host1:9872,host2:9872,host3:9872"};

// Options options = new Options();
// options.addOption(Option.builder("p").longOpt(RaftMetaConfCommand.PEER_OPTION_NAME).hasArg().build());
// options.addOption(Option.builder("d").longOpt(RaftMetaConfCommand.PATH_OPTION_NAME).hasArg().build());
//
// String[] args = {"--peers=peer1|host1:port1,peer2|host2:port2", "--path=" + tempDir.toAbsolutePath().toString()};
// CommandLine commandLine = new DefaultParser().parse(options, args);
for (String peersStr : testPeersList) {
getRaftConf(peersStr, output, index);

final StringPrintStream out = new StringPrintStream();
StringPrintStream out = new StringPrintStream();
RatisShell shell = new RatisShell(out.getPrintStream());
String peersUpdated = "peer1_Id|host1:9872,peer2_id|host2:9872,peer3_id|host3:9872";
int ret = shell.run("local", "raftMetaConf", "-peers", peersUpdated, "-path", tempDir.toString());
// String peersUpdated = "peer1_Id|host1:9872,peer2_id|host2:9872,peer3_id|host3:9872";
int ret = shell.run("local", "raftMetaConf", "-peers", peersStr, "-path", tempDir.toString());
Assertions.assertEquals(0, ret);

long indexFromNewConf;
List<RaftPeerProto> peers;
// Add additional assertions to verify the contents of the new-raft-meta.conf file
try (InputStream in = Files.newInputStream(tempDir.resolve(NEW_RAFT_META_CONF))) {
LogEntryProto logEntry = LogEntryProto.newBuilder().mergeFrom(in).build();
indexFromNewConf = logEntry.getIndex();
peers = logEntry.getConfigurationEntry().getPeersList();
}
long indexFromNewConf;
List<RaftPeerProto> peers;
// Add additional assertions to verify the contents of the new-raft-meta.conf file
try (InputStream in = Files.newInputStream(tempDir.resolve(NEW_RAFT_META_CONF))) {
LogEntryProto logEntry = LogEntryProto.newBuilder().mergeFrom(in).build();
indexFromNewConf = logEntry.getIndex();
peers = logEntry.getConfigurationEntry().getPeersList();
}

Assertions.assertEquals(index + 1, indexFromNewConf);

// List<String> list;
// StringBuilder tmp = new StringBuilder();
String tmp = "";
boolean bb = containsPeerId(peersStr);
System.out.println("**_____ "+ bb + " => containsPeerId(" + peersStr + ")");
if (containsPeerId(peersStr)) {
tmp = peers.stream()
.map(peer -> peer.getId().toStringUtf8() + "|" + peer.getAddress())
.collect(Collectors.joining(","));
// tmp =
// .forEach(
// peer ->
// tmp.append(peer.getId().toStringUtf8()).append("|").append(peer.getAddress()).append(","));
// peers.stream().forEach(peer ->
// tmp.append(peer.getId().toStringUtf8()).append("|").append(peer.getAddress()).append(","));
} else {
tmp = peers.stream().map(RaftPeerProto::getAddress)
.collect(Collectors.joining(","));
}
// tmp.deleteCharAt(tmp.length() - 1); // delete last comma

Assertions.assertEquals(peersStr, tmp.toString());

Assertions.assertEquals(index + 1, indexFromNewConf);
List<String> peersStr = peers.stream()
.map(peer -> peer.getId().toStringUtf8() + "|" + peer.getAddress())
.collect(Collectors.toList());
}

StringBuilder tmp = new StringBuilder();
peers.stream().forEach(peer ->
tmp.append(peer.getId().toStringUtf8()).append("|").append(peer.getAddress()).append(","));
tmp.deleteCharAt(tmp.length() - 1); // delete last comma
}

Assertions.assertEquals(peersUpdated, tmp.toString());
private boolean containsPeerId(String str) {
Pattern p = Pattern.compile("(?:\\w+\\|\\w+:\\d+,?)+");
Matcher m = p.matcher(str);
// while(m.find()) {
// System.out.println(m.group());
// }
return m.find();
}


Expand Down

0 comments on commit 15cf83d

Please sign in to comment.