本文整理汇总了Java中org.robotninjas.barge.Replica.fromString方法的典型用法代码示例。如果您正苦于以下问题:Java Replica.fromString方法的具体用法?Java Replica.fromString怎么用?Java Replica.fromString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.robotninjas.barge.Replica
的用法示例。
在下文中一共展示了Replica.fromString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestVote
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Nonnull
@Override
public RequestVoteResponse requestVote(@Nonnull RaftStateContext ctx, @Nonnull RequestVote request) {
LOGGER.debug("RequestVote received for term {}", request.getTerm());
Replica candidate = Replica.fromString(request.getCandidateId());
boolean voteGranted = false;
if (request.getTerm() > log.currentTerm()) {
log.updateCurrentTerm(request.getTerm());
stepDown(ctx);
voteGranted = shouldVoteFor(log, request);
if (voteGranted) {
log.updateVotedFor(Optional.of(candidate));
}
}
return RequestVoteResponse.newBuilder().setTerm(log.currentTerm()).setVoteGranted(voteGranted).build();
}
示例2: testCandidateWithGreaterTerm
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Test
@Ignore
public void testCandidateWithGreaterTerm() {
BaseState state = new EmptyState();
RequestVote requestVote = RequestVote.newBuilder().setCandidateId(candidate.toString()).setLastLogIndex(2).setLastLogTerm(3).setTerm(2).build();
Replica otherCandidate = Replica.fromString("localhost:8002");
when(mockRaftLog.lastVotedFor()).thenReturn(Optional.of(otherCandidate));
boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);
assertTrue(shouldVote);
}
示例3: testCandidateWithGreaterIndex
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Test
@Ignore
public void testCandidateWithGreaterIndex() {
BaseState state = new EmptyState();
RequestVote requestVote = RequestVote.newBuilder().setCandidateId(candidate.toString()).setLastLogIndex(3).setLastLogTerm(2).setTerm(2).build();
Replica otherCandidate = Replica.fromString("localhost:8002");
when(mockRaftLog.lastVotedFor()).thenReturn(Optional.of(otherCandidate));
boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);
assertTrue(shouldVote);
}
示例4: main
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
final int port = Integer.parseInt(args[0]);
Replica local = Replica.fromString("localhost:" + (10000 + port));
AppendLogConfig config = new AppendLogConfig();
File baseDir = new File(args[0]);
config.httpPort = (9990 + port);
SnapshotStorage snapshotStore = new LocalSnapshotStorage(new File(baseDir, "snapshots"));
final AppendLogServer server = new AppendLogServer(baseDir, local, config, snapshotStore);
server.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
server.stop();
} catch (Exception e) {
log.error("Error stopping server", e);
}
}
});
}
示例5: startServers
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@BeforeClass
public static void startServers() throws Exception {
TEMPDIR = Files.createTempDir();
SERVERS = new AppendLogServer[3];
for (int i = 0; i < SERVERS.length; i++) {
Replica self = Replica.fromString("localhost:" + (10000 + i));
List<Replica> allMembers = Lists.newArrayList();
for (int j = 0; j < SERVERS.length; j++) {
allMembers.add(Replica.fromString("localhost:" + (10000 + j)));
}
int httpPort = 9990 + i;
File baseDir = new File(TEMPDIR, "" + i);
AppendLogConfig config = new AppendLogConfig();
config.httpPort = httpPort;
config.seedConfig = new ClusterConfig(self, allMembers, ClusterConfig.buildDefaultTimeouts());
SnapshotStorage snapshotStorage = new LocalSnapshotStorage(new File(TEMPDIR, "snapshots"));
SERVERS[i] = new AppendLogServer(baseDir, self, config, snapshotStorage);
SERVERS[i].start();
}
Thread.sleep(2000);
}
示例6: main
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
final int port = Integer.parseInt(args[0]);
Replica local = Replica.fromString("localhost:" + (10000 + port));
KeyValueConfig config = new KeyValueConfig();
File baseDir = new File(args[0]);
config.httpPort = (9990 + port);
int redisPort = 6379 + port;
int protobufPort = 2000 + port;
config.redisEndpoint = HostAndPort.fromParts("", redisPort);
config.protobufEndpoint = HostAndPort.fromParts("", protobufPort);
SnapshotStorage snapshotStore = new LocalSnapshotStorage(new File(baseDir, "snapshots"));
final KeyValueServer server = new KeyValueServer(baseDir, local, config, snapshotStore);
server.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
server.stop();
} catch (Exception e) {
log.error("Error stopping server", e);
}
}
});
}
示例7: buildServer
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
protected static KeyValueServer buildServer(int i, Iterable<KeyValueServer> seedMembers) throws IOException {
Replica local = Replica.fromString("localhost:" + (10000 + i));
File baseDir = new File(TEMPDIR, "" + i);
KeyValueConfig config = new KeyValueConfig();
config.redisEndpoint = HostAndPort.fromParts("", 6379 + i + 1);
config.protobufEndpoint = HostAndPort.fromParts("", 2000 + i + 1);
config.httpPort = 9990 + i;
/*
config.gossip = new GossipConfig();
config.gossip.serviceId = "keyvalue";
config.gossip.nodeId = "node" + i;
// config.gossip.broadcastAddress = HostAndPort.fromParts("225.1.1.1", 2100 + i + 1);
config.gossip.protobufEndpoint = HostAndPort.fromParts("", 2200 + i + 1);
*/
List<Replica> allMembers = Lists.newArrayList();
for (KeyValueServer seedMember : seedMembers) {
if (seedMember == null) continue;
allMembers.add(seedMember.getReplica());
}
ConfigTimeouts timeouts = ClusterConfig.buildDefaultTimeouts();
ClusterConfig seedConfig = new ClusterConfig(local, allMembers, timeouts);
config.seedConfig = seedConfig;
SnapshotStorage snapshotStore = new LocalSnapshotStorage(new File(TEMPDIR, "snapshots"));
KeyValueServer server = new KeyValueServer(baseDir, local, config, snapshotStore);
return server;
}
示例8: main
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
final int port = Integer.parseInt(args[0]);
Replica local = Replica.fromString("localhost:" + (10000 + port));
List<Replica> members = Lists.newArrayList(Replica.fromString("localhost:10001"),
Replica.fromString("localhost:10002"), Replica.fromString("localhost:10003"));
members.remove(local);
File baseDir = new File(args[0]);
int httpPort = (9990 + port);
int protobufPort = 2100 + port;
HostAndPort protobufSocketAddress = HostAndPort.fromParts("", protobufPort);
final StructuredServer server = new StructuredServer(baseDir, local, members, httpPort, protobufSocketAddress);
server.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
server.stop();
} catch (Exception e) {
log.warn("Error shutting down HTTP server", e);
}
}
});
}
示例9: startServers
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@BeforeClass
public static void startServers() throws Exception {
TEMPDIR = Files.createTempDir();
SERVERS = new StructuredServer[3];
for (int i = 0; i < SERVERS.length; i++) {
Replica local = Replica.fromString("localhost:" + (10000 + i));
List<Replica> members = Lists.newArrayList();
for (int j = 0; j < SERVERS.length; j++) {
members.add(Replica.fromString("localhost:" + (10000 + j)));
}
members.remove(local);
int httpPort = 9990 + i;
int protobufPort = 2010 + i;
File baseDir = new File(TEMPDIR, "" + i);
SERVERS[i] = new StructuredServer(baseDir, local, members, httpPort,
HostAndPort.fromParts("", protobufPort));
SERVERS[i].start();
}
// TODO: Remove the need for a sleep here
Thread.sleep(1000);
}
示例10: requestVote
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Nonnull
@Override
public RequestVoteResponse requestVote(@Nonnull RaftStateContext ctx, @Nonnull RequestVote request) {
LOGGER.debug("RequestVote received for term {}", request.getTerm());
boolean voteGranted = false;
if (request.getTerm() > log.currentTerm()) {
log.updateCurrentTerm(request.getTerm());
stepDown(ctx);
Replica candidate = Replica.fromString(request.getCandidateId());
voteGranted = shouldVoteFor(log, request);
if (voteGranted) {
log.updateVotedFor(Optional.of(candidate));
}
}
return RequestVoteResponse.newBuilder().setTerm(log.currentTerm()).setVoteGranted(voteGranted).build();
}
示例11: requestVote
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Nonnull
@Override
public RequestVoteResponse requestVote(@Nonnull RaftStateContext ctx, @Nonnull RequestVote request) {
LOGGER.debug("RequestVote received for term {}", request.getTerm());
boolean voteGranted = false;
if (request.getTerm() >= log.currentTerm()) {
if (request.getTerm() > log.currentTerm()) {
log.updateCurrentTerm(request.getTerm());
}
Replica candidate = Replica.fromString(request.getCandidateId());
voteGranted = shouldVoteFor(log, request);
if (voteGranted) {
log.updateVotedFor(Optional.of(candidate));
}
}
return RequestVoteResponse.newBuilder().setTerm(log.currentTerm()).setVoteGranted(voteGranted).build();
}
示例12: shouldVoteFor
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@VisibleForTesting
boolean shouldVoteFor(@Nonnull RaftLog log, @Nonnull RequestVote request) {
Optional<Replica> lastVotedFor = log.lastVotedFor();
Replica candidate = Replica.fromString(request.getCandidateId());
boolean hasAtLeastTerm = request.getLastLogTerm() >= log.lastLogTerm();
boolean hasAtLeastIndex = request.getLastLogIndex() >= log.lastLogIndex();
boolean logAsComplete = (hasAtLeastTerm && hasAtLeastIndex);
boolean alreadyVotedForCandidate = lastVotedFor.equals(Optional.of(candidate));
boolean notYetVoted = !lastVotedFor.isPresent();
return (alreadyVotedForCandidate && logAsComplete) || (notYetVoted && logAsComplete) || (request.getLastLogTerm() > log.lastLogTerm()) || (hasAtLeastTerm && (request.getLastLogIndex() > log.lastLogIndex())) || logAsComplete;
}
示例13: testRequestVoteWithOlderTerm
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Test
public void testRequestVoteWithOlderTerm() throws Exception {
Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 150, mockRaftClient);
candidate.init(mockRaftStateContext);
Replica mockCandidate = Replica.fromString("localhost:10001");
RequestVote request = RequestVote.newBuilder().setCandidateId(mockCandidate.toString()).setLastLogIndex(1L).setLastLogTerm(1L).setTerm(1L).build();
candidate.requestVote(mockRaftStateContext, request);
verify(mockRaftLog).updateCurrentTerm(3L);
verify(mockRaftLog, times(1)).updateCurrentTerm(anyLong());
verify(mockRaftLog).updateVotedFor(Optional.of(self));
verify(mockRaftLog, never()).updateVotedFor(Optional.of(mockCandidate));
verify(mockRaftLog, times(1)).updateVotedFor(any(Optional.class));
verify(mockRaftLog, never()).updateCommitIndex(anyLong());
verifyZeroInteractions(mockRaftStateContext);
verifyZeroInteractions(mockRaftClient);
}
示例14: testRequestVoteWithSameTerm
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Test
public void testRequestVoteWithSameTerm() throws Exception {
Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 150, mockRaftClient);
candidate.init(mockRaftStateContext);
Replica mockCandidate = Replica.fromString("localhost:10001");
RequestVote request = RequestVote.newBuilder().setCandidateId(mockCandidate.toString()).setLastLogIndex(1L).setLastLogTerm(1L).setTerm(2L).build();
candidate.requestVote(mockRaftStateContext, request);
verify(mockRaftLog).updateCurrentTerm(3L);
verify(mockRaftLog, times(1)).updateCurrentTerm(anyLong());
verify(mockRaftLog).updateVotedFor(Optional.of(self));
verify(mockRaftLog, never()).updateVotedFor(Optional.of(mockCandidate));
verify(mockRaftLog, times(1)).updateVotedFor(any(Optional.class));
verify(mockRaftLog, never()).updateCommitIndex(anyLong());
verify(mockRaftStateContext, never()).setState(any(RaftStateContext.StateType.class));
verifyZeroInteractions(mockRaftStateContext);
verifyZeroInteractions(mockRaftClient);
}
示例15: testAppendEntriesWithOlderTerm
import org.robotninjas.barge.Replica; //导入方法依赖的package包/类
@Test
public void testAppendEntriesWithOlderTerm() throws Exception {
Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 1, mockRaftClient);
candidate.init(mockRaftStateContext);
Replica mockLeader = Replica.fromString("localhost:10001");
AppendEntries request = RaftProto.AppendEntries.newBuilder().setTerm(1L).setLeaderId(mockLeader.toString()).setPrevLogIndex(1L).setPrevLogTerm(1L).setCommitIndex(1L).build();
candidate.appendEntries(mockRaftStateContext, request);
verify(mockRaftLog).updateCurrentTerm(3L);
verify(mockRaftLog, times(1)).updateCurrentTerm(anyLong());
verify(mockRaftLog).updateVotedFor(Optional.of(self));
verify(mockRaftLog, times(1)).updateVotedFor(any(Optional.class));
verify(mockRaftLog, never()).updateCommitIndex(anyLong());
verifyZeroInteractions(mockRaftStateContext);
verifyZeroInteractions(mockRaftClient);
}