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

The following examples show how to use io.atomix.protocols.raft.storage.log.entry.CloseSessionEntry. 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 6 votes vote down vote up
/**
 * Applies a close session entry to the state machine.
 */
private void applyCloseSession(Indexed<CloseSessionEntry> entry) {
  RaftSession session = raft.getSessions().getSession(entry.entry().session());

  // If the server session is null, the session either never existed or already expired.
  if (session == null) {
    throw new RaftException.UnknownSession("Unknown session: " + entry.entry().session());
  }

  RaftServiceContext service = session.getService();
  service.closeSession(entry.index(), entry.entry().timestamp(), session, entry.entry().expired());

  // If this is a delete, unregister the service.
  if (entry.entry().delete()) {
    raft.getServices().unregisterService(service);
    service.close();
  }
}
 
Example #2
Source File: LeaderRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Expires the given session.
 */
private void expireSession(RaftSession session) {
  if (expiring.add(session.sessionId())) {
    log.debug("Expiring session due to heartbeat failure: {}", session);
    appendAndCompact(new CloseSessionEntry(raft.getTerm(), System.currentTimeMillis(), session.sessionId().id(), true, false))
        .whenCompleteAsync((entry, error) -> {
          if (error != null) {
            expiring.remove(session.sessionId());
            return;
          }

          appender.appendEntries(entry.index()).whenComplete((commitIndex, commitError) -> {
            raft.checkThread();
            if (isRunning()) {
              if (commitError == null) {
                raft.getServiceManager().<Long>apply(entry.index())
                    .whenCompleteAsync((r, e) -> expiring.remove(session.sessionId()), raft.getThreadContext());
              } else {
                expiring.remove(session.sessionId());
              }
            }
          });
        }, raft.getThreadContext());
  }
}
 
Example #3
Source File: LeaderRole.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<CloseSessionResponse> onCloseSession(CloseSessionRequest request) {
  final long term = raft.getTerm();
  final long timestamp = System.currentTimeMillis();

  raft.checkThread();
  logRequest(request);

  CompletableFuture<CloseSessionResponse> future = new CompletableFuture<>();
  appendAndCompact(new CloseSessionEntry(term, timestamp, request.session(), false, request.delete()))
      .whenCompleteAsync((entry, error) -> {
        if (error != null) {
          future.complete(logResponse(CloseSessionResponse.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((closeResult, closeError) -> {
                if (closeError == null) {
                  future.complete(logResponse(CloseSessionResponse.builder()
                      .withStatus(RaftResponse.Status.OK)
                      .build()));
                } else if (closeError instanceof CompletionException && closeError.getCause() instanceof RaftException) {
                  future.complete(logResponse(CloseSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(((RaftException) closeError.getCause()).getType(), closeError.getMessage())
                      .build()));
                } else if (closeError instanceof RaftException) {
                  future.complete(logResponse(CloseSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(((RaftException) closeError).getType(), closeError.getMessage())
                      .build()));
                } else {
                  future.complete(logResponse(CloseSessionResponse.builder()
                      .withStatus(RaftResponse.Status.ERROR)
                      .withError(RaftError.Type.PROTOCOL_ERROR, closeError.getMessage())
                      .build()));
                }
              });
            } else {
              future.complete(logResponse(CloseSessionResponse.builder()
                  .withStatus(RaftResponse.Status.ERROR)
                  .withError(RaftError.Type.PROTOCOL_ERROR)
                  .build()));
            }
          } else {
            future.complete(logResponse(CloseSessionResponse.builder()
                .withStatus(RaftResponse.Status.ERROR)
                .withError(RaftError.Type.ILLEGAL_MEMBER_STATE)
                .build()));
          }
        });
      }, raft.getThreadContext());

  return future;
}