This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git
The following commit(s) were added to refs/heads/master by this push:
new 3a98407 RATIS-1041. Change client to try the servers according to the
priority (#186)
3a98407 is described below
commit 3a98407f0039c5d45806c557037ea6af00c69608
Author: runzhiwang <[email protected]>
AuthorDate: Mon Aug 31 12:56:51 2020 +0800
RATIS-1041. Change client to try the servers according to the priority
(#186)
---
.../java/org/apache/ratis/client/RaftClient.java | 3 +++
.../apache/ratis/client/impl/RaftClientImpl.java | 24 ++++++++++++++++++++--
.../ratis/server/impl/GroupManagementBaseTest.java | 2 ++
3 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/ratis-client/src/main/java/org/apache/ratis/client/RaftClient.java
b/ratis-client/src/main/java/org/apache/ratis/client/RaftClient.java
index e70054c..37f889c 100644
--- a/ratis-client/src/main/java/org/apache/ratis/client/RaftClient.java
+++ b/ratis-client/src/main/java/org/apache/ratis/client/RaftClient.java
@@ -42,6 +42,9 @@ public interface RaftClient extends Closeable {
/** @return the id of this client. */
ClientId getId();
+ /** @return the cluster leaderId recorded by this client. */
+ RaftPeerId getLeaderId();
+
/** @return the client rpct. */
RaftClientRpc getClientRpc();
diff --git
a/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
b/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
index b69a53a..8f00e50 100644
---
a/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
+++
b/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
@@ -116,8 +116,7 @@ public final class RaftClientImpl implements RaftClient {
this.clientRpc = clientRpc;
this.peers = new ConcurrentLinkedQueue<>(group.getPeers());
this.groupId = group.getGroupId();
- this.leaderId = leaderId != null? leaderId
- : !peers.isEmpty()? peers.iterator().next().getId(): null;
+ this.leaderId = leaderId != null? leaderId : getHighestPriorityPeerId();
Preconditions.assertTrue(retryPolicy != null, "retry policy can't be
null");
this.retryPolicy = retryPolicy;
@@ -128,6 +127,27 @@ public final class RaftClientImpl implements RaftClient {
this.streamApi = JavaUtils.memoize(() -> StreamImpl.newInstance(this,
properties));
}
+ public RaftPeerId getLeaderId() {
+ return leaderId;
+ }
+
+ private RaftPeerId getHighestPriorityPeerId() {
+ if (peers == null) {
+ return null;
+ }
+
+ int maxPriority = Integer.MIN_VALUE;
+ RaftPeerId highestPriorityPeerId = null;
+ for (RaftPeer peer : peers) {
+ if (maxPriority < peer.getPriority()) {
+ maxPriority = peer.getPriority();
+ highestPriorityPeerId = peer.getId();
+ }
+ }
+
+ return highestPriorityPeerId;
+ }
+
@Override
public ClientId getId() {
return clientId;
diff --git
a/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
b/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
index fa9aca7..a8cc541 100644
---
a/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
+++
b/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
@@ -112,6 +112,8 @@ public abstract class GroupManagementBaseTest extends
BaseTest {
final RaftGroup newGroup = RaftGroup.valueOf(RaftGroupId.randomId(),
peersWithPriority);
LOG.info("add new group: " + newGroup);
try (final RaftClient client = cluster.createClient(newGroup)) {
+ // Before request, client try leader with the highest priority
+ Assert.assertTrue(client.getLeaderId() ==
peersWithPriority.get(suggestedLeaderIndex).getId());
for (RaftPeer p : newGroup.getPeers()) {
client.groupAdd(newGroup, p.getId());
}