You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2017/07/11 20:39:12 UTC
incubator-ratis git commit: RATIS-93. Filter peers from the iteration
before random selection.
Repository: incubator-ratis
Updated Branches:
refs/heads/master 237be5d10 -> d45275123
RATIS-93. Filter peers from the iteration before random selection.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/d4527512
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/d4527512
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/d4527512
Branch: refs/heads/master
Commit: d45275123fd0bc9dd88a965067408882a21c5367
Parents: 237be5d
Author: Tsz-Wo Nicholas Sze <sz...@hortonworks.com>
Authored: Tue Jul 11 13:38:19 2017 -0700
Committer: Tsz-Wo Nicholas Sze <sz...@hortonworks.com>
Committed: Tue Jul 11 13:38:19 2017 -0700
----------------------------------------------------------------------
.../ratis/client/impl/RaftClientImpl.java | 2 +-
.../org/apache/ratis/util/CollectionUtils.java | 28 +++++++++++---------
.../ratis/grpc/client/AppendStreamer.java | 3 +--
.../apache/ratis/statemachine/StateMachine.java | 3 +--
4 files changed, 18 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/d4527512/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java
----------------------------------------------------------------------
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 fbecb90..1082f38 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
@@ -180,7 +180,7 @@ final class RaftClientImpl implements RaftClient {
final RaftPeerId oldLeader = request.getServerId();
if (newLeader == null && oldLeader.equals(leaderId)) {
newLeader = CollectionUtils.random(oldLeader,
- peers.stream().map(RaftPeer::getId).collect(Collectors.toList()));
+ CollectionUtils.as(peers, RaftPeer::getId));
}
if (newLeader != null && oldLeader.equals(leaderId)) {
LOG.debug("{}: change Leader from {} to {}", clientId, oldLeader, newLeader);
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/d4527512/ratis-common/src/main/java/org/apache/ratis/util/CollectionUtils.java
----------------------------------------------------------------------
diff --git a/ratis-common/src/main/java/org/apache/ratis/util/CollectionUtils.java b/ratis-common/src/main/java/org/apache/ratis/util/CollectionUtils.java
index d50d6ce..2fdb502 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/CollectionUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/CollectionUtils.java
@@ -20,12 +20,14 @@
package org.apache.ratis.util;
+import org.apache.ratis.shaded.io.netty.util.internal.ThreadLocalRandom;
+
import java.util.*;
import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
public interface CollectionUtils {
- Random random = new Random();
-
/**
* @return the next element in the iteration right after the given element;
* if the given element is not in the iteration, return the first one
@@ -49,20 +51,20 @@ public interface CollectionUtils {
/**
* @return a randomly picked element which is not the given element.
*/
- static <T> T random(final T given, List<T> list) {
+ static <T> T random(final T given, Iterable<T> iteration) {
Objects.requireNonNull(given, "given == null");
- Preconditions.assertTrue(list != null && !list.isEmpty(), "c is null or empty");
+ Objects.requireNonNull(iteration, "iteration == null");
+ Preconditions.assertTrue(iteration.iterator().hasNext(), "iteration is empty");
- if (list.size() == 1) {
- return list.get(0);
+ final List<T> list = StreamSupport.stream(iteration.spliterator(), false)
+ .filter(e -> !given.equals(e))
+ .collect(Collectors.toList());
+ final int size = list.size();
+ if (size == 0) {
+ throw new IllegalArgumentException(
+ "All elements in the iteration equals to the given element.");
}
-
- T selected;
- do {
- selected = list.get(random.nextInt(list.size()));
- } while (selected == given);
-
- return selected;
+ return list.get(ThreadLocalRandom.current().nextInt(size));
}
static <INPUT, OUTPUT> Iterable<OUTPUT> as(
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/d4527512/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
----------------------------------------------------------------------
diff --git a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
index 75d1dd7..5d01235 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
@@ -117,8 +117,7 @@ public class AppendStreamer implements Closeable {
if (oldLeader == null) {
leaderId = peers.keySet().iterator().next();
} else {
- leaderId = CollectionUtils.random(oldLeader,
- new ArrayList<>(peers.keySet()));
+ leaderId = CollectionUtils.random(oldLeader, peers.keySet());
}
}
LOG.debug("{} switches leader from {} to {}. suggested leader: {}", this,
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/d4527512/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java b/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
index 4dcac72..17990cd 100644
--- a/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
+++ b/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
@@ -75,8 +75,7 @@ public interface StateMachine extends Closeable {
*
* In the meanwhile, when the size of raft log outside of the latest snapshot
* exceeds certain threshold, the RaftServer may choose to trigger a snapshot
- * if {@link RaftServerConfigKeys#RAFT_SERVER_AUTO_SNAPSHOT_ENABLED_KEY} is
- * enabled.
+ * if {@link RaftServerConfigKeys.Snapshot#AUTO_TRIGGER_ENABLED_KEY} is enabled.
*
* The snapshot should include the latest raft configuration.
*