code: private void registerSession(long index, long timestamp, ServerSessionContext session, ServerSessionContext oldSession, CompletableFuture<Long> future, ThreadContext context) {
      if (!log.isOpen()) {
        context.executor().execute(() -> future.completeExceptionally(new IllegalStateException("log closed")));
        return;
      }
  
      // Trigger scheduled callbacks in the state machine.
      executor.tick(index, timestamp);
  
      // Update the state machine context with the register entry's index. This ensures that events published
      // within the register method will be properly associated with the unregister entry's index. All events
      // published during registration of a session are linearizable to ensure that clients receive related events
      // before the registration is completed.
      executor.init(index, Instant.ofEpochMilli(timestamp), ServerStateMachineContext.Type.COMMAND);
  
      // If the session overrides a previous session, expire the old session.
      if (oldSession != null) {
        oldSession.expire(0);
      }
  
      // Register the session and then open it. This ensures that state machines cannot publish events to this
      // session before the client has learned of the session ID.
      for (SessionListener listener : executor.context().sessions().listeners) {
        if (oldSession != null) {
          listener.expire(oldSession);
          listener.close(oldSession);
        }
        listener.register(session);
      }
      session.open();
  
      // Calculate the last completed index.
      long lastCompleted = calculateLastCompleted(index);
  
      // Once register callbacks have been completed, ensure that events published during the callbacks are
      // received by clients. The state machine context will generate an event future for all published events
      // to all sessions.
      executor.commit();
      context.executor().execute(() -> {
        setLastCompleted(lastCompleted);
        future.complete(index);
      });
    }
docs: Registers a session.