Skip to content

Commit

Permalink
Improve code readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveTeng0 committed Jun 11, 2024
1 parent d4cd161 commit b943e2a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
71 changes: 34 additions & 37 deletions ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.UUID;

Expand Down Expand Up @@ -104,21 +105,21 @@ public static RaftClient createClient(RaftGroup raftGroup) {
.build();
}


/**
* Execute a given function with input parameter from the members of a list.
* Apply the given function to the given parameter a list.
*
* @param list the input parameters
* @param function the function to be executed
* @param <T> parameter type
* @param <K> return value type
* @param <E> the exception type thrown by the given function.
* @return the value returned by the given function.
* @param list the input parameter list
* @param function the function to be applied
* @param <PARAMETER> parameter type
* @param <RETURN> return value type
* @param <EXCEPTION> the exception type thrown by the given function.
* @return the first non-null value returned by the given function applied to the given list.
*/
public static <T, K, E extends Throwable> K runFunction(Collection<T> list, CheckedFunction<T, K, E> function) {
for (T t : list) {
private static <PARAMETER, RETURN, EXCEPTION extends Throwable> RETURN applyFunctionReturnFirstNonNull(
Collection<PARAMETER> list, CheckedFunction<PARAMETER, RETURN, EXCEPTION> function) {
for (PARAMETER parameter : list) {
try {
K ret = function.apply(t);
RETURN ret = function.apply(parameter);
if (ret != null) {
return ret;
}
Expand All @@ -129,7 +130,6 @@ public static <T, K, E extends Throwable> K runFunction(Collection<T> list, Chec
return null;
}


public static List<RaftPeer> buildRaftPeersFromStr(String peers) {
List<InetSocketAddress> addresses = new ArrayList<>();
String[] peersArray = peers.split(",");
Expand All @@ -146,54 +146,51 @@ public static List<RaftPeer> buildRaftPeersFromStr(String peers) {
}

public static RaftGroupId buildRaftGroupIdFromStr(String groupId) {
return (groupId != null && !groupId.equals("")) ? RaftGroupId.valueOf(UUID.fromString(groupId))
return groupId.isEmpty() ? RaftGroupId.valueOf(UUID.fromString(groupId))
: DEFAULT_RAFT_GROUP_ID;
}

public static RaftGroupId retrieveRemoteGroupId(RaftGroupId raftGroupIdFromConfig,
List<RaftPeer> peers,
RaftClient client, PrintStream printStream) throws IOException {
RaftGroupId remoteGroupId;
if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) {
if (!DEFAULT_RAFT_GROUP_ID .equals(raftGroupIdFromConfig)) {
return raftGroupIdFromConfig;
} else {
final List<RaftGroupId> groupIds = runFunction(peers,
p -> client.getGroupManagementApi((p.getId())).list().getGroupIds());

if (groupIds == null) {
printStream.println("Failed to get group ID from " + peers);
throw new IOException("Failed to get group ID from " + peers);
} else if (groupIds.size() == 1) {
remoteGroupId = groupIds.get(0);
} else {
printStream.println("There are more than one groups, you should specific one. " + groupIds);
throw new IOException("There are more than one groups, you should specific one. " + groupIds);
}
}

final RaftGroupId remoteGroupId;
final List<RaftGroupId> groupIds = applyFunctionReturnFirstNonNull(peers,
p -> client.getGroupManagementApi((p.getId())).list().getGroupIds());

if (groupIds == null) {
printStream.println("Failed to get group ID from " + peers);
throw new IOException("Failed to get group ID from " + peers);
} else if (groupIds.size() == 1) {
remoteGroupId = groupIds.get(0);
} else {
String message = "Unexpected multiple group IDs " + groupIds
+ ". In such case, the target group ID must be specified.";
printStream.println(message);
throw new IOException(message);
}
return remoteGroupId;
}

public static GroupInfoReply retrieveGroupInfoByGroupId(RaftGroupId remoteGroupId, List<RaftPeer> peers,
RaftClient client, PrintStream printStream)
throws IOException {
GroupInfoReply groupInfoReply = runFunction(peers,
GroupInfoReply groupInfoReply = applyFunctionReturnFirstNonNull(peers,
p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId));
processReply(groupInfoReply,
printStream::println, "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers);
processReply(groupInfoReply, printStream::println,
() -> "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers);
return groupInfoReply;
}

public static void processReply(RaftClientReply reply, Consumer<String> printer, String message) throws IOException {
processReplyInternal(reply, () -> printer.accept(message));
}

private static void processReplyInternal(RaftClientReply reply, Runnable printer) throws IOException {
public static void processReply(RaftClientReply reply, Consumer<String> printer, Supplier<String> message) throws IOException {
if (reply == null || !reply.isSuccess()) {
final RaftException e = Optional.ofNullable(reply)
.map(RaftClientReply::getException)
.orElseGet(() -> new RaftException("Reply: " + reply));
printer.run();
printer.accept(message.get());
throw new IOException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ protected RaftPeerProto getLeader(RoleInfoProto roleInfo) {
}

protected void processReply(RaftClientReply reply, Supplier<String> messageSupplier) throws IOException {
RaftUtils.processReply(reply,
getPrintStream()::println, messageSupplier.get());
RaftUtils.processReply(reply, getPrintStream()::println, messageSupplier);
}

protected List<RaftPeerId> getIds(String[] optionValues, BiConsumer<RaftPeerId, InetSocketAddress> consumer) {
Expand Down

0 comments on commit b943e2a

Please sign in to comment.