Skip to content

Commit

Permalink
move ratis client with TlsConf to a different PR
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveTeng0 committed May 28, 2024
1 parent a3b4463 commit d4cd161
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@

import org.apache.ratis.client.RaftClient;
import org.apache.ratis.client.RaftClientConfigKeys;
import org.apache.ratis.conf.Parameters;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.grpc.GrpcConfigKeys;
import org.apache.ratis.grpc.GrpcTlsConfig;
import org.apache.ratis.netty.NettyConfigKeys;
import org.apache.ratis.protocol.GroupInfoReply;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftGroup;
Expand All @@ -32,8 +28,6 @@
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.protocol.exceptions.RaftException;
import org.apache.ratis.retry.ExponentialBackoffRetry;
import org.apache.ratis.rpc.RpcType;
import org.apache.ratis.rpc.SupportedRpcType;
import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.util.function.CheckedFunction;

Expand Down Expand Up @@ -88,18 +82,6 @@ public static RaftPeerId getPeerId(String host, int port) {
* @return return a raft client
*/
public static RaftClient createClient(RaftGroup raftGroup) {
return createClient(raftGroup, null, null);
}


/**
* Create a raft client to communicate to ratis server.
* @param raftGroup the raft group
* @param rpcType the rpcType
* @param tlsConfig the tlsConfig
* @return return a raft client
*/
public static RaftClient createClient(RaftGroup raftGroup, RpcType rpcType, GrpcTlsConfig tlsConfig) {
RaftProperties properties = new RaftProperties();
RaftClientConfigKeys.Rpc.setRequestTimeout(properties,
TimeDuration.valueOf(15, TimeUnit.SECONDS));
Expand All @@ -119,10 +101,10 @@ public static RaftClient createClient(RaftGroup raftGroup, RpcType rpcType, Grpc
.setRaftGroup(raftGroup)
.setProperties(properties)
.setRetryPolicy(retryPolicy)
.setParameters(setClientTlsConf(rpcType, tlsConfig))
.build();
}


/**
* Execute a given function with input parameter from the members of a list.
*
Expand Down Expand Up @@ -228,32 +210,4 @@ public static InetSocketAddress parseInetSocketAddress(String address) {
}
}

public static Parameters setClientTlsConf(RpcType rpcType,
GrpcTlsConfig tlsConfig) {
// TODO: GRPC TLS only for now, netty/hadoop RPC TLS support later.
if (tlsConfig != null && rpcType == SupportedRpcType.GRPC) {
Parameters parameters = new Parameters();
setAdminTlsConf(parameters, tlsConfig);
setClientTlsConf(parameters, tlsConfig);
return parameters;
}
return null;
}

private static void setAdminTlsConf(Parameters parameters,
GrpcTlsConfig tlsConfig) {
if (tlsConfig != null) {
GrpcConfigKeys.Admin.setTlsConf(parameters, tlsConfig);
}
}

private static void setClientTlsConf(Parameters parameters,
GrpcTlsConfig tlsConfig) {
if (tlsConfig != null) {
GrpcConfigKeys.Client.setTlsConf(parameters, tlsConfig);
NettyConfigKeys.DataStream.Client.setTlsConf(parameters, tlsConfig);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public int run(CommandLine cl) throws IOException {
raftGroup = RaftGroup.valueOf(raftGroupIdFromConfig, peers);
PrintStream printStream = getPrintStream();
try (final RaftClient client = RaftUtils.createClient(raftGroup)) {
RaftGroupId remoteGroupId = retrieveRemoteGroupId(raftGroupIdFromConfig, peers, client, printStream);
final RaftGroupId remoteGroupId = retrieveRemoteGroupId(raftGroupIdFromConfig, peers, client, printStream);
groupInfoReply = retrieveGroupInfoByGroupId(remoteGroupId, peers, client, printStream);
raftGroup = groupInfoReply.getGroup();
}
Expand Down Expand Up @@ -181,5 +181,4 @@ protected Stream<RaftPeer> getPeerStream(RaftPeerRole role) {
.stream()
.filter(targets::contains);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public int run(CommandLine cl) throws IOException {
try(final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
GroupListReply reply = raftClient.getGroupManagementApi(peerId).list();
processReply(reply, () -> String.format("Failed to get group information of peerId %s (server %s)",
peerId.toString(), address));
peerId, address));
printf(String.format("The peerId %s (server %s) is in %d groups, and the groupIds is: %s",
peerId, address, reply.getGroupIds().size(), reply.getGroupIds()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public int run(CommandLine cl) throws IOException {
peerId = null;
}
RaftClientReply reply = raftClient.getSnapshotManagementApi(peerId).create(timeout);
processReply(reply, () -> String.format("Failed to take snapshot of peerId %s",
peerId != null ? peerId.toString() : null));
processReply(reply, () -> String.format("Failed to take snapshot of peerId %s", peerId));
printf(String.format("Successful take snapshot on peerId %s, the latest snapshot index is %d",
peerId, reply.getLogIndex()));
}
Expand Down

0 comments on commit d4cd161

Please sign in to comment.