예제 #1
0
  @SuppressWarnings("unchecked")
  public void testAppendTermAndLeaderUpdated() throws Throwable {
    runOnServer(
        () -> {
          int leader =
              serverContext
                  .getClusterState()
                  .getActiveMemberStates()
                  .iterator()
                  .next()
                  .getMember()
                  .id();
          serverContext.setTerm(1);
          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(2)
                  .withLeader(leader)
                  .withEntries(Collections.EMPTY_LIST)
                  .withCommitIndex(0)
                  .withGlobalIndex(0)
                  .build();

          AppendResponse response = state.append(request).get();

          assertEquals(response.status(), Status.OK);
          assertTrue(response.succeeded());
          assertEquals(serverContext.getTerm(), 2L);
          assertEquals(serverContext.getLeader().hashCode(), leader);
          assertEquals(response.term(), 2L);
        });
  }
예제 #2
0
  public void testAppendOnNonEmptyLog() throws Throwable {
    runOnServer(
        () -> {
          serverContext.setTerm(1);
          append(1, 1);

          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(1)
                  .withLeader(
                      serverContext
                          .getClusterState()
                          .getActiveMemberStates()
                          .iterator()
                          .next()
                          .getMember()
                          .id())
                  .withLogIndex(0)
                  .withLogTerm(0)
                  .withCommitIndex(2)
                  .withGlobalIndex(0)
                  .withEntries(new TestEntry().setIndex(2).setTerm(1))
                  .build();

          AppendResponse response = state.append(request).get();

          assertEquals(response.status(), Status.OK);
          assertTrue(response.succeeded());
          assertEquals(response.term(), 1L);
          assertEquals(response.logIndex(), 2L);

          assertEquals(serverContext.getLog().length(), 2L);
          assertNotNull(get(2));
        });
  }
예제 #3
0
  @SuppressWarnings("unchecked")
  public void testAppendUpdatesLeaderAndTerm() throws Throwable {
    runOnServer(
        () -> {
          serverContext.setTerm(1);
          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(2)
                  .withLeader(members.get(1).hashCode())
                  .withEntries(Collections.EMPTY_LIST)
                  .withLogIndex(0)
                  .withLogTerm(0)
                  .withCommitIndex(0)
                  .withGlobalIndex(0)
                  .build();

          AppendResponse response = state.append(request).get();

          threadAssertEquals(serverContext.getTerm(), 2L);
          threadAssertEquals(
              serverContext.getLeader().serverAddress(), members.get(1).serverAddress());
          threadAssertEquals(serverContext.getLastVotedFor(), 0);
          threadAssertEquals(response.term(), 2L);
          threadAssertTrue(response.succeeded());
        });
  }
예제 #4
0
  @SuppressWarnings("unchecked")
  public void testRejectAppendOnTerm() throws Throwable {
    runOnServer(
        () -> {
          serverContext.setTerm(2);
          append(2, 2);

          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(1)
                  .withLeader(
                      serverContext
                          .getClusterState()
                          .getActiveMemberStates()
                          .iterator()
                          .next()
                          .getMember()
                          .id())
                  .withEntries(Collections.EMPTY_LIST)
                  .withLogIndex(2)
                  .withLogTerm(2)
                  .withCommitIndex(0)
                  .withGlobalIndex(0)
                  .build();

          AppendResponse response = state.append(request).get();

          assertEquals(response.status(), Status.OK);
          assertFalse(response.succeeded());
          assertEquals(response.term(), 2L);
          assertEquals(response.logIndex(), 2L);
        });
  }