io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry Java Examples

The following examples show how to use io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry. 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: RaftServiceManager.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Applies an open session entry to the state machine.
 */
private long applyOpenSession(Indexed<OpenSessionEntry> entry) {
  PrimitiveType primitiveType = raft.getPrimitiveTypes().getPrimitiveType(entry.entry().serviceType());

  // Get the state machine executor or create one if it doesn't already exist.
  RaftServiceContext service = getOrInitializeService(
      PrimitiveId.from(entry.index()),
      primitiveType,
      entry.entry().serviceName(),
      entry.entry().serviceConfig());

  if (service == null) {
    throw new RaftException.UnknownService("Unknown service type " + entry.entry().serviceType());
  }

  SessionId sessionId = SessionId.from(entry.index());
  RaftSession session = raft.getSessions().addSession(new RaftSession(
      sessionId,
      MemberId.from(entry.entry().memberId()),
      entry.entry().serviceName(),
      primitiveType,
      entry.entry().readConsistency(),
      entry.entry().minTimeout(),
      entry.entry().maxTimeout(),
      entry.entry().timestamp(),
      service.serializer(),
      service,
      raft,
      threadContextFactory));
  return service.openSession(entry.index(), entry.entry().timestamp(), session);
}
 
Example #2
Source File: RaftServiceManagerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSnapshotTakeInstall() throws Exception {
  RaftLogWriter writer = raft.getLogWriter();
  writer.append(new InitializeEntry(1, System.currentTimeMillis()));
  writer.append(new OpenSessionEntry(
      1,
      System.currentTimeMillis(),
      "test-1",
      "test",
      "test",
      null,
      ReadConsistency.LINEARIZABLE,
      100,
      1000));
  writer.commit(2);

  RaftServiceManager manager = raft.getServiceManager();

  manager.apply(2).join();

  Snapshot snapshot = manager.snapshot();
  assertEquals(2, snapshot.index());
  assertTrue(snapshotTaken.get());

  snapshot = snapshot.complete();

  assertEquals(2, raft.getSnapshotStore().getCurrentSnapshot().index());

  manager.install(snapshot);
  assertTrue(snapshotInstalled.get());
}
 
Example #3
Source File: RaftServiceManagerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstallSnapshotOnApply() throws Exception {
  RaftLogWriter writer = raft.getLogWriter();
  writer.append(new InitializeEntry(1, System.currentTimeMillis()));
  writer.append(new OpenSessionEntry(
      1,
      System.currentTimeMillis(),
      "test-1",
      "test",
      "test",
      null,
      ReadConsistency.LINEARIZABLE,
      100,
      1000));
  writer.commit(2);

  RaftServiceManager manager = raft.getServiceManager();

  manager.apply(2).join();

  Snapshot snapshot = manager.snapshot();
  assertEquals(2, snapshot.index());
  assertTrue(snapshotTaken.get());

  snapshot.complete();

  assertEquals(2, raft.getSnapshotStore().getCurrentSnapshot().index());

  writer.append(new CommandEntry(1, System.currentTimeMillis(), 2, 1, new PrimitiveOperation(RUN, new byte[0])));
  writer.commit(3);

  manager.apply(3).join();
  assertTrue(snapshotInstalled.get());
}
 
Example #4
Source File: LeaderRole.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> onOpenSession(OpenSessionRequest request) {
  final long term = raft.getTerm();
  final long timestamp = System.currentTimeMillis();
  final long minTimeout = request.minTimeout();

  // If the client submitted a session timeout, use the client's timeout, otherwise use the configured
  // default server session timeout.
  final long maxTimeout;
  if (request.maxTimeout() != 0) {
    maxTimeout = request.maxTimeout();
  } else {
    maxTimeout = raft.getSessionTimeout().toMillis();
  }

  raft.checkThread();
  logRequest(request);

  CompletableFuture<OpenSessionResponse> future = new CompletableFuture<>();
  appendAndCompact(new OpenSessionEntry(
      term,
      timestamp,
      request.node(),
      request.serviceName(),
      request.serviceType(),
      request.serviceConfig(),
      request.readConsistency(),
      minTimeout,
      maxTimeout))
      .whenCompleteAsync((entry, error) -> {
        if (error != null) {
          future.complete(logResponse(OpenSessionResponse.builder()
              .withStatus(RaftResponse.Status.ERROR)
              .withError(RaftError.Type.PROTOCOL_ERROR)
              .build()));
          return;
        }

        appender.appendEntries(entry.index()).whenComplete((commitIndex, commitError) -> {
          raft.checkThread();
          if (isRunning()) {
            if (commitError == null) {
              raft.getServiceManager().<Long>apply(entry.index()).whenComplete((sessionId, sessionError) -> {
                if (sessionError == null) {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.OK)
                      .withSession(sessionId)
                      .withTimeout(maxTimeout)
                      .build()));
                } else if (sessionError instanceof CompletionException && sessionError.getCause() instanceof RaftException) {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(((RaftException) sessionError.getCause()).getType(), sessionError.getMessage())
                      .build()));
                } else if (sessionError instanceof RaftException) {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(((RaftException) sessionError).getType(), sessionError.getMessage())
                      .build()));
                } else {
                  future.complete(logResponse(OpenSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(RaftError.Type.PROTOCOL_ERROR, sessionError.getMessage())
                      .build()));
                }
              });
            } else {
              future.complete(logResponse(OpenSessionResponse.builder()
                  .withStatus(RaftResponse.Status.ERROR)
                  .withError(RaftError.Type.PROTOCOL_ERROR)
                  .build()));
            }
          } else {
            future.complete(logResponse(OpenSessionResponse.builder()
                .withStatus(RaftResponse.Status.ERROR)
                .withError(RaftError.Type.ILLEGAL_MEMBER_STATE)
                .build()));
          }
        });
      }, raft.getThreadContext());

  return future;
}