当前位置: 首页>>代码示例>>Java>>正文


Java Replica类代码示例

本文整理汇总了Java中org.robotninjas.barge.Replica的典型用法代码示例。如果您正苦于以下问题:Java Replica类的具体用法?Java Replica怎么用?Java Replica使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Replica类属于org.robotninjas.barge包,在下文中一共展示了Replica类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: requestVote

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Nonnull
public ListenableFuture<RequestVoteResponse> requestVote(@Nonnull
final Replica replica, @Nonnull
final RequestVote request) {
    checkNotNull(replica);
    checkNotNull(request);

    // Put a (possibly) blocking connect onto a different thread
    ListenableFuture<ListenableFuture<RequestVoteResponse>> response = networkCallExecutor.submit(new Callable<ListenableFuture<RequestVoteResponse>>() {
        @Override
        public ListenableFuture<RequestVoteResponse> call() throws Exception {
            RaftClient client = clientProvider.get(replica);
            return client.requestVote(request);
        }
    });

    // Transfer the response back onto the raft thread
    return transform(response, Identity.<RequestVoteResponse> identity(), raftExecutor);

}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:21,代码来源:Client.java

示例2: appendEntries

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Nonnull
public ListenableFuture<AppendEntriesResponse> appendEntries(@Nonnull
final Replica replica, @Nonnull
final AppendEntries request) {
    checkNotNull(replica);
    checkNotNull(request);

    // Put a (possibly) blocking connect onto a different thread
    ListenableFuture<ListenableFuture<AppendEntriesResponse>> response = networkCallExecutor.submit(new Callable<ListenableFuture<AppendEntriesResponse>>() {
        @Override
        public ListenableFuture<AppendEntriesResponse> call() throws Exception {
            RaftClient client = clientProvider.get(replica);
            return client.appendEntries(request);
        }
    });

    // Transfer the response back onto the raft thread
    return transform(response, Identity.<AppendEntriesResponse> identity(), raftExecutor);

}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:21,代码来源:Client.java

示例3: 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();

}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:23,代码来源:Candidate.java

示例4: sendRequests

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@VisibleForTesting
List<ListenableFuture<RequestVoteResponse>> sendRequests(final RaftStateContext ctx) {

    RequestVote request = RequestVote.newBuilder().setTerm(log.currentTerm()).setCandidateId(log.self().toString()).setLastLogIndex(log.lastLogIndex()).setLastLogTerm(log.lastLogTerm()).build();

    List<ListenableFuture<RequestVoteResponse>> responses = Lists.newArrayList();
    for (Replica replica : log.members()) {
        ListenableFuture<RequestVoteResponse> response = client.requestVote(replica, request);
        Futures.addCallback(response, new FutureCallback<RequestVoteResponse>() {
            @Override
            public void onSuccess(@Nullable RequestVoteResponse result) {
                checkTermOnResponse(ctx, result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });
        responses.add(response);
    }

    return responses;
}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:24,代码来源:Candidate.java

示例5: updateVotedFor

import org.robotninjas.barge.Replica; //导入依赖的package包/类
public void updateVotedFor(@Nonnull Optional<Replica> vote) {

        LOGGER.debug("Voting for {}", vote.orNull());

        setLastVotedFor(vote);

        try {
            LogProto.Vote.Builder voteBuilder = LogProto.Vote.newBuilder();

            if (vote.isPresent()) {
                voteBuilder.setVotedFor(vote.get().toString());
            }

            LogProto.JournalEntry entry = LogProto.JournalEntry.newBuilder().setVote(voteBuilder).build();

            journal.write(entry.toByteArray(), WriteType.SYNC);
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:21,代码来源:DefaultRaftLog.java

示例6: main

import org.robotninjas.barge.Replica; //导入依赖的package包/类
public static void main(String... args) throws Exception {
    PropertyConfigurator.configure("D:/log4j.properties");

    try {
        List<Replica> members = Lists.newArrayList();
        members.add(Replica.fromString("localhost:10000"));
        members.add(Replica.fromString("localhost:10002"));
        File logDir = new File("D:/raft1");
        logDir.mkdir();

        // configure the service
        RaftService raft = RaftService.newBuilder().local(Replica.fromString("localhost:10001")).members(members).logDir(logDir).timeout(300).build(new Test2());

        // start this replica
        raft.startAsync().awaitRunning();

    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:22,代码来源:Test2.java

示例7: main

import org.robotninjas.barge.Replica; //导入依赖的package包/类
public static void main(String... args) throws Exception {
    PropertyConfigurator.configure("D:/log4j.properties");

    try {
        List<Replica> members = Lists.newArrayList();
        members.add(Replica.fromString("localhost:10000"));
        members.add(Replica.fromString("localhost:10001"));
        File logDir = new File("D:/raft2");
        logDir.mkdir();

        // configure the service
        RaftService raft = RaftService.newBuilder().local(Replica.fromString("localhost:10002")).members(members).logDir(logDir).timeout(300).build(new Test3());

        // start this replica
        raft.startAsync().awaitRunning();

        // let's commit some things
        //            for (int i = 0; i < 10; i++) {
        //                raft.commit(new byte[] { 'O', '_', 'o' });
        //            }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:26,代码来源:Test3.java

示例8: main

import org.robotninjas.barge.Replica; //导入依赖的package包/类
public static void main(String... args) throws Exception {
    PropertyConfigurator.configure("D:/log4j.properties");

    try {
        List<Replica> members = Lists.newArrayList();
        members.add(Replica.fromString("localhost:10001"));
        members.add(Replica.fromString("localhost:10002"));
        File logDir = new File("D:/raft");
        logDir.mkdir();

        // configure the service
        RaftService raft = RaftService.newBuilder().local(Replica.fromString("localhost:10000")).members(members).logDir(logDir).timeout(300).build(new Test());

        // start this replica
        Service guavaservice = raft.startAsync();
        guavaservice.awaitRunning();

        // let's commit some things
        //            for (int i = 0; i < 10; i++) {
        //                raft.commit(new byte[] { 'O', '_', 'o' });
        //            }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:26,代码来源:Test.java

示例9: sendVoteRequest

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@VisibleForTesting
ListenableFuture<RequestVoteResponse> sendVoteRequest(RaftStateContext ctx, Replica replica) {

  RaftLog log = getLog();
  RequestVote request =
    RequestVote.newBuilder()
      .setTerm(log.currentTerm())
      .setCandidateId(log.self().toString())
      .setLastLogIndex(log.lastLogIndex())
      .setLastLogTerm(log.lastLogTerm())
      .build();

  ListenableFuture<RequestVoteResponse> response = client.requestVote(replica, request);
  Futures.addCallback(response, checkTerm(ctx));

  return response;
}
 
开发者ID:mgodave,项目名称:barge,代码行数:18,代码来源:Candidate.java

示例10: initMocks

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Before
public void initMocks() {

  MockitoAnnotations.initMocks(this);

  when(mockRaftLog.currentTerm()).thenReturn(2l);
  when(mockRaftLog.lastLogIndex()).thenReturn(2l);
  when(mockRaftLog.lastLogTerm()).thenReturn(2l);
  when(mockRaftLog.self()).thenReturn(self);
  when(mockRaftLog.config()).thenReturn(config);
  when(mockRaftLog.getReplica(anyString())).thenAnswer(new Answer<Replica>() {
    @Override
    public Replica answer(InvocationOnMock invocation) throws Throwable {
      String arg = (String) invocation.getArguments()[0];
      return config.getReplica(arg);
    }
  });

}
 
开发者ID:mgodave,项目名称:barge,代码行数:20,代码来源:BaseStateTest.java

示例11: testHaventVoted

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Test
public void testHaventVoted() {

  BaseState state = new EmptyState(mockRaftLog);

  RequestVote requestVote = RequestVote.newBuilder()
    .setCandidateId(candidate.toString())
    .setLastLogIndex(2)
    .setLastLogTerm(2)
    .setTerm(2)
    .build();

  when(mockRaftLog.votedFor()).thenReturn(Optional.<Replica>absent());
  boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);

  assertTrue(shouldVote);
}
 
开发者ID:mgodave,项目名称:barge,代码行数:18,代码来源:BaseStateTest.java

示例12: testCandidateWithGreaterTerm

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Test
@Ignore
public void testCandidateWithGreaterTerm() {

  BaseState state = new EmptyState(mockRaftLog);

  RequestVote requestVote = RequestVote.newBuilder()
    .setCandidateId(candidate.toString())
    .setLastLogIndex(2)
    .setLastLogTerm(3)
    .setTerm(2)
    .build();

  Replica otherCandidate = config.getReplica("other");
  when(mockRaftLog.votedFor()).thenReturn(Optional.of(otherCandidate));
  boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);

  assertTrue(shouldVote);
}
 
开发者ID:mgodave,项目名称:barge,代码行数:20,代码来源:BaseStateTest.java

示例13: testCandidateWithLesserTerm

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Test
public void testCandidateWithLesserTerm() {

  BaseState state = new EmptyState(mockRaftLog);

  RequestVote requestVote = RequestVote.newBuilder()
    .setCandidateId(candidate.toString())
    .setLastLogIndex(2)
    .setLastLogTerm(1)
    .setTerm(2)
    .build();

  Replica otherCandidate = config.getReplica("other");
  when(mockRaftLog.votedFor()).thenReturn(Optional.of(otherCandidate));
  boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);

  assertFalse(shouldVote);
}
 
开发者ID:mgodave,项目名称:barge,代码行数:19,代码来源:BaseStateTest.java

示例14: testCandidateWithLesserIndex

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Test
public void testCandidateWithLesserIndex() {

  BaseState state = new EmptyState(mockRaftLog);

  RequestVote requestVote = RequestVote.newBuilder()
    .setCandidateId(candidate.toString())
    .setLastLogIndex(1)
    .setLastLogTerm(2)
    .setTerm(2)
    .build();

  Replica otherCandidate = config.getReplica("other");
  when(mockRaftLog.votedFor()).thenReturn(Optional.of(otherCandidate));
  boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);

  assertFalse(shouldVote);
}
 
开发者ID:mgodave,项目名称:barge,代码行数:19,代码来源:BaseStateTest.java

示例15: testCandidateWithGreaterIndex

import org.robotninjas.barge.Replica; //导入依赖的package包/类
@Test
@Ignore
public void testCandidateWithGreaterIndex() {

  BaseState state = new EmptyState(mockRaftLog);

  RequestVote requestVote = RequestVote.newBuilder()
    .setCandidateId(candidate.toString())
    .setLastLogIndex(3)
    .setLastLogTerm(2)
    .setTerm(2)
    .build();

  Replica otherCandidate = config.getReplica("other");
  when(mockRaftLog.votedFor()).thenReturn(Optional.of(otherCandidate));
  boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote);

  assertTrue(shouldVote);
}
 
开发者ID:mgodave,项目名称:barge,代码行数:20,代码来源:BaseStateTest.java


注:本文中的org.robotninjas.barge.Replica类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。