diff --git a/tarantool-pooling/src/main/java/io/tarantool/pool/entry/EntryEvent.java b/tarantool-pooling/src/main/java/io/tarantool/pool/entry/EntryEvent.java new file mode 100644 index 00000000..125aeb93 --- /dev/null +++ b/tarantool-pooling/src/main/java/io/tarantool/pool/entry/EntryEvent.java @@ -0,0 +1,403 @@ +/* + * Copyright (c) 2026 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY + * All Rights Reserved. + */ + +package io.tarantool.pool.entry; + +import java.util.concurrent.CompletableFuture; + +import io.tarantool.core.IProtoClient; +import io.tarantool.core.state.Event; + +/** + * Events of the one-shot {@link PoolEntryStateMachine}. + * + *
The events are split into two groups: + * + *
A state handles only the events that make sense for it. Every other event is ignored, and this + * is how stale events (for example, a result of {@code policy.start} that arrives after the session + * has started closing) are dropped. + */ +abstract class EntryEvent implements Event { + + private final String name; + + EntryEvent(String name) { + this.name = name; + } + + @Override + public String toString() { + return this.name; + } + + /** + * A command to start the connection process. It is the first event of every session. + * + *
Sent by: {@code ConnectionSession.connect()}, that is, by the pool entry when it + * creates a session (the first connect or a reconnect). + * + *
Handled by: + * + *
The future carried by the event is completed with the client when the connection is
+ * established (see {@link ConnectedEvent}) and fails if the session finishes before that.
+ */
+ static final class ConnectEvent extends EntryEvent {
+
+ private final CompletableFuture Sent by: the connect chain that {@code ConnectingState} starts when it is entered.
+ *
+ * Handled by: {@code ConnectingState}: increments the success metric, emits {@code
+ * onConnectionOpened}, completes the connect future (without waiting for the availability policy)
+ * and moves to {@code POLICY_STARTING}. Other states ignore it: for example, if a close request
+ * has already moved the session to {@code CLOSING}, a late {@code ConnectedEvent} is dropped.
+ *
+ * It has no payload, so a single shared instance is used.
+ */
+ static final class ConnectedEvent extends EntryEvent {
+
+ static final EntryEvent INSTANCE = new ConnectedEvent();
+
+ private ConnectedEvent() {
+ super("CONNECTED_EVT");
+ }
+ }
+
+ /**
+ * The connection process has failed: the connect itself, the authorization or the first ping.
+ *
+ * Sent by: the connect chain of {@code ConnectingState} (also when the chain could not
+ * be started because of a synchronous exception). {@code ConnectingState} also creates it from a
+ * {@link RemoteCloseEvent} that arrives while connecting, because the client reports a close both
+ * through the close listener and through the failed connect future.
+ *
+ * Handled by: {@code ConnectingState}: increments the error metric, logs the error and
+ * starts closing the session with a failed outcome, so the pool entry will reconnect.
+ * Other states ignore it.
+ */
+ static final class ConnectErrorEvent extends EntryEvent {
+
+ private final Throwable cause;
+
+ ConnectErrorEvent(Throwable cause) {
+ super("CONNECT_ERROR_EVT");
+ this.cause = cause;
+ }
+
+ /**
+ * The reason of the failure.
+ *
+ * @return never null.
+ */
+ Throwable cause() {
+ return this.cause;
+ }
+ }
+
+ /**
+ * {@code AvailabilityPolicy.start} has completed successfully: the policy is running and is now
+ * responsible for the availability state of the entry.
+ *
+ * Sent by: {@code PolicyStartingState}, when the stage returned by the policy completes
+ * normally.
+ *
+ * Handled by: {@code PolicyStartingState}: moves to {@code ACTIVE}. Other states ignore
+ * it. This is also the case when a close request has arrived while the policy was starting.
+ *
+ * It has no payload, so a single shared instance is used.
+ */
+ static final class PolicyStartedEvent extends EntryEvent {
+
+ static final EntryEvent INSTANCE = new PolicyStartedEvent();
+
+ private PolicyStartedEvent() {
+ super("POLICY_STARTED_EVT");
+ }
+ }
+
+ /**
+ * {@code AvailabilityPolicy.start} has failed: the returned stage completed exceptionally or the
+ * call itself threw an exception.
+ *
+ * Sent by: {@code PolicyStartingState}.
+ *
+ * Handled by: {@code PolicyStartingState}: remembers {@link #cause()} (it is passed to
+ * {@code policy.stop}) and moves to {@code POLICY_STOPPING}. Per the policy contract the library
+ * then stops and restarts the policy while the connection stays up. Other states ignore it.
+ */
+ static final class PolicyStartErrorEvent extends EntryEvent {
+
+ private final Throwable cause;
+
+ PolicyStartErrorEvent(Throwable cause) {
+ super("POLICY_START_ERROR_EVT");
+ this.cause = cause;
+ }
+
+ /**
+ * The reason why the policy has not started.
+ *
+ * @return never null.
+ */
+ Throwable cause() {
+ return this.cause;
+ }
+ }
+
+ /**
+ * {@code AvailabilityPolicy.stop} has finished in {@code POLICY_STOPPING}. The state is entered
+ * after a failed policy start and when the session is being finished, so this is the only place
+ * where the policy is stopped.
+ *
+ * Sent by: {@code PolicyStoppingState}, when the stage returned by the policy completes
+ * (normally or not) or when the call throws.
+ *
+ * Handled by: {@code PolicyStoppingState}. The result of the stop does not change the
+ * flow, a failed stop is only logged:
+ *
+ * Sent by: the timer that {@code PolicyRestartingState} sets when it is entered. The
+ * delay is {@code AvailabilityPolicy.restartBackoff(view)}. The timer is cancelled if the session
+ * starts closing in the meantime.
+ *
+ * Handled by: {@code PolicyRestartingState}: moves to {@code POLICY_STARTING}. Other
+ * states ignore it, which also covers an event that has been queued just before the timer was
+ * cancelled.
+ *
+ * It has no payload, so a single shared instance is used.
+ */
+ static final class BackoffElapsedEvent extends EntryEvent {
+
+ static final EntryEvent INSTANCE = new BackoffElapsedEvent();
+
+ private BackoffElapsedEvent() {
+ super("BACKOFF_ELAPSED_EVT");
+ }
+ }
+
+ /**
+ * The session has been closed: the client has been closed. The availability policy has been
+ * stopped before (see {@link PolicyStoppedEvent}) if it had been started.
+ *
+ * Do not confuse it with the requests to close: {@link RemoteCloseEvent} reports that the
+ * connection was closed by somebody else, {@link ClientCloseEvent} is a request of the
+ * pool entry owner. Those start the closing, this one reports that the session has
+ * finished closing.
+ *
+ * Sent by: {@code ClosingState}, when {@code client.close()} has been called.
+ *
+ * Handled by: {@code ClosingState}: moves to the terminal {@code CLOSED} state, which
+ * completes the futures, emits {@code onConnectionClosed} and notifies the owner about the
+ * outcome. Other states ignore it.
+ *
+ * It has no payload, so a single shared instance is used.
+ */
+ static final class ClosedEvent extends EntryEvent {
+
+ static final EntryEvent INSTANCE = new ClosedEvent();
+
+ private ClosedEvent() {
+ super("CLOSED_EVT");
+ }
+ }
+
+ /**
+ * Control event. The connection has been closed from outside: by the remote side or by a
+ * graceful shutdown of the server ({@code CLOSE_BY_REMOTE} and {@code CLOSE_BY_SHUTDOWN} of the
+ * client). A close made by the session itself (see {@link ClosedEvent}) is not reported with this
+ * event.
+ *
+ * Sent by: the pool entry, from the close listener of the client, through {@code
+ * ConnectionSession.remoteClosed(Throwable)}.
+ *
+ * Handled by:
+ *
+ * Sent by:
+ *
+ * Handled by: the same way as {@link RemoteCloseEvent}: a failed outcome and the
+ * closing of the session. {@code InitState}, {@code ClosingState} and {@code ClosedState} ignore
+ * it.
+ */
+ static final class ReconnectEvent extends Control {
+
+ private final Throwable cause;
+
+ ReconnectEvent(Throwable cause) {
+ super("RECONNECT_EVT");
+ this.cause = cause;
+ }
+
+ /**
+ * The reason of the reconnect.
+ *
+ * @return may be null.
+ */
+ Throwable cause() {
+ return this.cause;
+ }
+ }
+
+ /**
+ * Control event. A request to close the session for good: the pool entry is closed and
+ * must not reconnect. "Client" here is the owner of the pool entry, not {@code IProtoClient}.
+ *
+ * Sent by: the pool entry when it is closed, through {@code ConnectionSession.close()}.
+ *
+ * Handled by:
+ *
+ * In every case {@link #closeFuture()} is completed when the session has finished.
+ */
+ static final class ClientCloseEvent extends Control {
+
+ private final CompletableFuture
+ *
+ */
+ static final class PolicyStoppedEvent extends EntryEvent {
+
+ private final Throwable cause;
+
+ PolicyStoppedEvent(Throwable cause) {
+ super("POLICY_STOPPED_EVT");
+ this.cause = cause;
+ }
+
+ /**
+ * The reason why the stop has failed.
+ *
+ * @return null if the stop has completed successfully.
+ */
+ Throwable cause() {
+ return this.cause;
+ }
+ }
+
+ /**
+ * The {@code restartBackoff} delay of the policy has elapsed, so the policy can be started again.
+ *
+ *
+ *
+ */
+ static final class RemoteCloseEvent extends Control {
+
+ private final Throwable cause;
+
+ RemoteCloseEvent(Throwable cause) {
+ super("REMOTE_CLOSE_EVT");
+ this.cause = cause;
+ }
+
+ /**
+ * The reason reported by the client.
+ *
+ * @return may be null.
+ */
+ Throwable cause() {
+ return this.cause;
+ }
+ }
+
+ /**
+ * Control event. A request to recycle the connection: close the current session and let
+ * the pool entry create a new one after {@code reconnectAfter}.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *