Java Code Examples for io.atomix.protocols.raft.cluster.RaftMember#Type

The following examples show how to use io.atomix.protocols.raft.cluster.RaftMember#Type . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RaftContext.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Transitions the server to the base state for the given member type.
 */
public void transition(RaftMember.Type type) {
  switch (type) {
    case ACTIVE:
      if (!(role instanceof ActiveRole)) {
        transition(RaftServer.Role.FOLLOWER);
      }
      break;
    case PROMOTABLE:
      if (this.role.role() != RaftServer.Role.PROMOTABLE) {
        transition(RaftServer.Role.PROMOTABLE);
      }
      break;
    case PASSIVE:
      if (this.role.role() != RaftServer.Role.PASSIVE) {
        transition(RaftServer.Role.PASSIVE);
      }
      break;
    default:
      if (this.role.role() != RaftServer.Role.INACTIVE) {
        transition(RaftServer.Role.INACTIVE);
      }
      break;
  }
}
 
Example 2
Source File: RaftTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests joining a server after many entries have been committed.
 */
private void testServerJoinLate(RaftMember.Type type, RaftServer.Role role) throws Throwable {
  createServers(3);
  RaftClient client = createClient();
  TestPrimitive primitive = createPrimitive(client);
  submit(primitive, 0, 100);
  await(15000);
  RaftServer joiner = createServer(nextNodeId());
  joiner.addRoleChangeListener(s -> {
    if (s == role) {
      resume();
    }
  });
  if (type == RaftMember.Type.ACTIVE) {
    joiner.join(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).thenRun(this::resume);
  } else {
    joiner.listen(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).thenRun(this::resume);
  }
  await(15000, 2);
  submit(primitive, 0, 10);
  await(15000);
  Thread.sleep(5000);
}
 
Example 3
Source File: RaftTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a member join event.
 */
private void testJoinEvent(RaftMember.Type type) throws Throwable {
  List<RaftServer> servers = createServers(3);

  RaftMember member = nextMember(type);

  RaftServer server = servers.get(0);
  server.cluster().addListener(event -> {
    if (event.type() == RaftClusterEvent.Type.JOIN) {
      threadAssertEquals(event.subject().memberId(), member.memberId());
      resume();
    }
  });

  RaftServer joiner = createServer(member.memberId());
  if (type == RaftMember.Type.ACTIVE) {
    joiner.join(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).thenRun(this::resume);
  } else {
    joiner.listen(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).thenRun(this::resume);
  }
  await(15000, 2);
}
 
Example 4
Source File: DefaultRaftMember.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the member type.
 *
 * @param type The member type.
 * @return The member.
 */
public DefaultRaftMember update(RaftMember.Type type, Instant time) {
  if (this.type != type) {
    this.type = checkNotNull(type, "type cannot be null");
    if (time.isAfter(updated)) {
      this.updated = checkNotNull(time, "time cannot be null");
    }
    if (typeChangeListeners != null) {
      typeChangeListeners.forEach(l -> l.accept(type));
    }
  }
  return this;
}
 
Example 5
Source File: DefaultRaftMember.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Demotes the server to the given type.
 */
private CompletableFuture<Void> configure(RaftMember.Type type) {
  if (type == this.type) {
    return CompletableFuture.completedFuture(null);
  }
  CompletableFuture<Void> future = new CompletableFuture<>();
  cluster.getContext().getThreadContext().execute(() -> configure(type, future));
  return future;
}
 
Example 6
Source File: DefaultRaftMember.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively reconfigures the cluster.
 */
private void configure(RaftMember.Type type, CompletableFuture<Void> future) {
  // Set a timer to retry the attempt to leave the cluster.
  configureTimeout = cluster.getContext().getThreadContext().schedule(cluster.getContext().getElectionTimeout(), () -> {
    configure(type, future);
  });

  // Attempt to leave the cluster by submitting a LeaveRequest directly to the server state.
  // Non-leader states should forward the request to the leader if there is one. Leader states
  // will log, replicate, and commit the reconfiguration.
  cluster.getContext().getRaftRole().onReconfigure(ReconfigureRequest.builder()
      .withIndex(cluster.getConfiguration().index())
      .withTerm(cluster.getConfiguration().term())
      .withMember(new DefaultRaftMember(id, type, updated))
      .build()).whenComplete((response, error) -> {
        if (error == null) {
          if (response.status() == RaftResponse.Status.OK) {
            cancelConfigureTimer();
            cluster.configure(new Configuration(response.index(), response.term(), response.timestamp(), response.members()));
            future.complete(null);
          } else if (response.error() == null
              || response.error().type() == RaftError.Type.UNAVAILABLE
              || response.error().type() == RaftError.Type.PROTOCOL_ERROR
              || response.error().type() == RaftError.Type.NO_LEADER) {
            cancelConfigureTimer();
            configureTimeout = cluster.getContext().getThreadContext().schedule(cluster.getContext().getElectionTimeout().multipliedBy(2), () -> configure(type, future));
          } else {
            cancelConfigureTimer();
            future.completeExceptionally(response.error().createException());
          }
        } else {
          future.completeExceptionally(error);
        }
      });
}
 
Example 7
Source File: RaftTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests a server joining the cluster.
 */
private void testServerJoin(RaftMember.Type type) throws Throwable {
  createServers(3);
  RaftServer server = createServer(nextNodeId());
  if (type == RaftMember.Type.ACTIVE) {
    server.join(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).thenRun(this::resume);
  } else {
    server.listen(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).thenRun(this::resume);
  }
  await(15000);
}
 
Example 8
Source File: DefaultRaftMember.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public RaftMember.Type getType() {
  return type;
}
 
Example 9
Source File: RaftClusterContext.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a list of member states for the given type.
 *
 * @param type The member type.
 * @return A list of member states for the given type.
 */
public List<RaftMemberContext> getRemoteMemberStates(RaftMember.Type type) {
  List<RaftMemberContext> members = memberTypes.get(type);
  return members != null ? members : Collections.EMPTY_LIST;
}
 
Example 10
Source File: RaftTest.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the next server address.
 *
 * @param type The startup member type.
 * @return The next server address.
 */
private RaftMember nextMember(RaftMember.Type type) {
  return new TestMember(nextNodeId(), type);
}
 
Example 11
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the next server address.
 *
 * @param type The startup member type.
 * @return The next server address.
 */
private RaftMember nextMember(RaftMember.Type type) {
  return new TestMember(nextNodeId(), type);
}