io.atomix.protocols.raft.protocol.RaftServerProtocol Java Examples

The following examples show how to use io.atomix.protocols.raft.protocol.RaftServerProtocol. 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
/**
 * Registers server handlers on the configured protocol.
 */
private void registerHandlers(RaftServerProtocol protocol) {
  protocol.registerOpenSessionHandler(request -> runOnContextIfReady(() -> role.onOpenSession(request), OpenSessionResponse::builder));
  protocol.registerCloseSessionHandler(request -> runOnContextIfReady(() -> role.onCloseSession(request), CloseSessionResponse::builder));
  protocol.registerKeepAliveHandler(request -> runOnContextIfReady(() -> role.onKeepAlive(request), KeepAliveResponse::builder));
  protocol.registerMetadataHandler(request -> runOnContextIfReady(() -> role.onMetadata(request), MetadataResponse::builder));
  protocol.registerConfigureHandler(request -> runOnContext(() -> role.onConfigure(request)));
  protocol.registerInstallHandler(request -> runOnContext(() -> role.onInstall(request)));
  protocol.registerJoinHandler(request -> runOnContext(() -> role.onJoin(request)));
  protocol.registerReconfigureHandler(request -> runOnContext(() -> role.onReconfigure(request)));
  protocol.registerLeaveHandler(request -> runOnContext(() -> role.onLeave(request)));
  protocol.registerTransferHandler(request -> runOnContext(() -> role.onTransfer(request)));
  protocol.registerAppendHandler(request -> runOnContext(() -> role.onAppend(request)));
  protocol.registerPollHandler(request -> runOnContext(() -> role.onPoll(request)));
  protocol.registerVoteHandler(request -> runOnContext(() -> role.onVote(request)));
  protocol.registerCommandHandler(request -> runOnContextIfReady(() -> role.onCommand(request), CommandResponse::builder));
  protocol.registerQueryHandler(request -> runOnContextIfReady(() -> role.onQuery(request), QueryResponse::builder));
}
 
Example #2
Source File: RaftContext.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Unregisters server handlers on the configured protocol.
 */
private void unregisterHandlers(RaftServerProtocol protocol) {
  protocol.unregisterOpenSessionHandler();
  protocol.unregisterCloseSessionHandler();
  protocol.unregisterKeepAliveHandler();
  protocol.unregisterMetadataHandler();
  protocol.unregisterConfigureHandler();
  protocol.unregisterInstallHandler();
  protocol.unregisterJoinHandler();
  protocol.unregisterReconfigureHandler();
  protocol.unregisterLeaveHandler();
  protocol.unregisterTransferHandler();
  protocol.unregisterAppendHandler();
  protocol.unregisterPollHandler();
  protocol.unregisterVoteHandler();
  protocol.unregisterCommandHandler();
  protocol.unregisterQueryHandler();
}
 
Example #3
Source File: RaftSessionRegistryTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
private RaftSession createSession(long sessionId) {
  RaftServiceContext context = mock(RaftServiceContext.class);
  when(context.serviceType()).thenReturn(TestPrimitiveType.instance());
  when(context.serviceName()).thenReturn("test");
  when(context.serviceId()).thenReturn(PrimitiveId.from(1));

  RaftContext server = mock(RaftContext.class);
  when(server.getProtocol()).thenReturn(mock(RaftServerProtocol.class));
  RaftServiceManager manager = mock(RaftServiceManager.class);
  when(manager.executor()).thenReturn(mock(ThreadContext.class));
  when(server.getServiceManager()).thenReturn(manager);

  return new RaftSession(
      SessionId.from(sessionId),
      MemberId.from("1"),
      "test",
      TestPrimitiveType.instance(),
      ReadConsistency.LINEARIZABLE,
      100,
      5000,
      System.currentTimeMillis(),
      Serializer.using(Namespaces.BASIC),
      context,
      server,
      mock(ThreadContextFactory.class));
}
 
Example #4
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Raft server.
 */
private RaftServer createServer(RaftMember member) {
  RaftServerProtocol protocol;
  if (USE_NETTY) {
    Address address = Address.from(++port);
    MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
    messagingServices.add(messagingManager);
    addressMap.put(member.memberId(), address);
    protocol = new RaftServerMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
  } else {
    protocol = protocolFactory.newServerProtocol(member.memberId());
  }

  RaftServer.Builder builder = RaftServer.builder(member.memberId())
      .withProtocol(protocol)
      .withStorage(RaftStorage.builder()
          .withStorageLevel(StorageLevel.DISK)
          .withDirectory(new File(String.format("target/fuzz-logs/%s", member.memberId())))
          .withNamespace(STORAGE_NAMESPACE)
          .withMaxSegmentSize(1024 * 1024)
          .build());

  RaftServer server = builder.build();
  servers.add(server);
  return server;
}
 
Example #5
Source File: RaftServiceManagerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Before
public void setupContext() throws IOException {
  deleteStorage();

  RaftStorage storage = RaftStorage.builder()
      .withPrefix("test")
      .withDirectory(PATH.toFile())
      .withNamespace(NAMESPACE)
      .build();
  PrimitiveTypeRegistry registry = new PrimitiveTypeRegistry() {
    @Override
    public Collection<PrimitiveType> getPrimitiveTypes() {
      return Collections.singleton(new TestType());
    }

    @Override
    public PrimitiveType getPrimitiveType(String typeName) {
      return new TestType();
    }
  };
  raft = new RaftContext(
      "test",
      MemberId.from("test-1"),
      mock(ClusterMembershipService.class),
      mock(RaftServerProtocol.class),
      storage,
      registry,
      ThreadModel.SHARED_THREAD_POOL.factory("raft-server-test-%d", 1, LoggerFactory.getLogger(RaftServer.class)),
      true);

  snapshotTaken = new AtomicBoolean();
  snapshotInstalled = new AtomicBoolean();
}
 
Example #6
Source File: RaftContext.java    From atomix with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public RaftContext(
    String name,
    MemberId localMemberId,
    ClusterMembershipService membershipService,
    RaftServerProtocol protocol,
    RaftStorage storage,
    PrimitiveTypeRegistry primitiveTypes,
    ThreadContextFactory threadContextFactory,
    boolean closeOnStop) {
  this.name = checkNotNull(name, "name cannot be null");
  this.membershipService = checkNotNull(membershipService, "membershipService cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.storage = checkNotNull(storage, "storage cannot be null");
  this.primitiveTypes = checkNotNull(primitiveTypes, "registry cannot be null");
  this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(RaftServer.class)
      .addValue(name)
      .build());

  // Lock the storage directory.
  if (!storage.lock(localMemberId.id())) {
    throw new StorageException("Failed to acquire storage lock; ensure each Raft server is configured with a distinct storage directory");
  }

  String baseThreadName = String.format("raft-server-%s", name);
  this.threadContext = new SingleThreadContext(namedThreads(baseThreadName, log));
  this.loadContext = new SingleThreadContext(namedThreads(baseThreadName + "-load", log));
  this.stateContext = new SingleThreadContext(namedThreads(baseThreadName + "-state", log));

  this.threadContextFactory = checkNotNull(threadContextFactory, "threadContextFactory cannot be null");
  this.closeOnStop = closeOnStop;

  this.loadMonitor = new LoadMonitor(LOAD_WINDOW_SIZE, HIGH_LOAD_THRESHOLD, loadContext);

  // Open the metadata store.
  this.meta = storage.openMetaStore();

  // Load the current term and last vote from disk.
  this.term = meta.loadTerm();
  this.lastVotedFor = meta.loadVote();

  // Construct the core log, reader, writer, and compactor.
  this.raftLog = storage.openLog();
  this.logWriter = raftLog.writer();
  this.logReader = raftLog.openReader(1, RaftLogReader.Mode.ALL);

  // Open the snapshot store.
  this.snapshotStore = storage.openSnapshotStore();

  // Create a new internal server state machine.
  this.stateMachine = new RaftServiceManager(this, stateContext, threadContextFactory);

  this.cluster = new RaftClusterContext(localMemberId, this);

  // Register protocol listeners.
  registerHandlers(protocol);
}
 
Example #7
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a Raft server.
 */
private RaftServer createServer(Member member, List<Node> members) {
  RaftServerProtocol protocol;
  ManagedMessagingService messagingService;
  if (USE_NETTY) {
    messagingService = (ManagedMessagingService) new NettyMessagingService("test", member.address(), new MessagingConfig())
        .start()
        .join();
    messagingServices.add(messagingService);
    protocol = new RaftServerMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
  } else {
    protocol = protocolFactory.newServerProtocol(member.id());
  }

  BootstrapService bootstrapService = new BootstrapService() {
    @Override
    public MessagingService getMessagingService() {
      return messagingService;
    }

    @Override
    public UnicastService getUnicastService() {
      return new UnicastServiceAdapter();
    }

    @Override
    public BroadcastService getBroadcastService() {
      return new BroadcastServiceAdapter();
    }
  };

  RaftServer.Builder builder = RaftServer.builder(member.id())
      .withProtocol(protocol)
      .withThreadModel(ThreadModel.SHARED_THREAD_POOL)
      .withMembershipService(new DefaultClusterMembershipService(
          member,
          Version.from("1.0.0"),
          new DefaultNodeDiscoveryService(bootstrapService, member, new BootstrapDiscoveryProvider(members)),
          bootstrapService,
          new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig())))
      .withStorage(RaftStorage.builder()
          .withStorageLevel(StorageLevel.DISK)
          .withDirectory(new File(String.format("target/perf-logs/%s", member.id())))
          .withNamespace(STORAGE_NAMESPACE)
          .withMaxSegmentSize(1024 * 1024 * 64)
          .withDynamicCompaction()
          .withFlushOnCommit(false)
          .build());

  RaftServer server = builder.build();
  servers.add(server);
  return server;
}
 
Example #8
Source File: LocalRaftProtocolFactory.java    From submarine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new test server protocol.
 *
 * @param memberId the server member identifier
 * @return a new test server protocol
 */
public RaftServerProtocol newServerProtocol(MemberId memberId) {
  return new LocalRaftServerProtocol(memberId, serializer, servers, clients);
}
 
Example #9
Source File: LocalRaftProtocolFactory.java    From zeppelin with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new test server protocol.
 *
 * @param memberId the server member identifier
 * @return a new test server protocol
 */
public RaftServerProtocol newServerProtocol(MemberId memberId) {
  return new LocalRaftServerProtocol(memberId, serializer, servers, clients);
}
 
Example #10
Source File: RaftContext.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the server protocol.
 *
 * @return The server protocol.
 */
public RaftServerProtocol getProtocol() {
  return protocol;
}
 
Example #11
Source File: RaftServer.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the server protocol.
 *
 * @param protocol The server protocol.
 * @return The server builder.
 */
public Builder withProtocol(RaftServerProtocol protocol) {
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  return this;
}
 
Example #12
Source File: LocalRaftProtocolFactory.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new test server protocol.
 *
 * @param memberId the server member identifier
 * @return a new test server protocol
 */
public RaftServerProtocol newServerProtocol(MemberId memberId) {
  return new LocalRaftServerProtocol(memberId, serializer, servers, clients);
}