Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tarantool-core/src/main/java/io/tarantool/core/utils/CFUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2026 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY
* All Rights Reserved.
*/

package io.tarantool.core.utils;

import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;

/** A class containing utility methods to work with {@link CompletionStage}. */
public final class CFUtils {

private CFUtils() {}

public static <T> void linkFuture(CompletionStage<T> root, CompletionStage<T> linkable) {
if (linkable == null) {
return;
}

root.whenComplete(
(r, e) -> {
if (e == null) {
linkable.toCompletableFuture().complete(r);
} else {
final Throwable unwrap = unwrap(e);
linkable.toCompletableFuture().completeExceptionally(unwrap);
}
});
}

public static Throwable unwrap(Throwable throwable) {
if (throwable == null) {
return null;
}
Throwable current = throwable;
while ((current instanceof CompletionException || current instanceof ExecutionException)
&& current.getCause() != null) {
current = current.getCause();
}
return current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import io.tarantool.core.protocol.IProtoResponse;
import io.tarantool.core.state.Event;

abstract class PolicyEvent implements Event {
abstract class HeartbeatEvent implements Event {

private final String name;

PolicyEvent(String name) {
HeartbeatEvent(String name) {
this.name = name;
}

Expand All @@ -23,7 +23,7 @@ public String toString() {
return this.name;
}

static final class StartEvent extends PolicyEvent {
static final class StartEvent extends HeartbeatEvent {

private final CompletableFuture<Void> startFuture;

Expand All @@ -38,7 +38,7 @@ CompletableFuture<Void> startFuture() {
}

/** An event sent when a response to a ping request is received. */
static final class PongEvent extends PolicyEvent {
static final class PongEvent extends HeartbeatEvent {

private final IProtoResponse response;

Expand All @@ -62,15 +62,16 @@ Throwable exception() {
/**
* An event sent when the ping timer expires, indicating that a ping request should be dispatched.
*/
static final class SendPingEvent extends PolicyEvent {
static final PolicyEvent INSTANCE = new SendPingEvent();
static final class SendPingEvent extends HeartbeatEvent {

static final HeartbeatEvent INSTANCE = new SendPingEvent();

private SendPingEvent() {
super("HEARTBEAT_SEND_PING_EVT");
}
}

static final class StopEvent extends PolicyEvent implements Control {
static final class StopEvent extends Control {

private final CompletableFuture<Void> closeFuture;

Expand All @@ -96,8 +97,9 @@ Throwable cause() {
* HeartbeatConstants#ACTIVE_STATE}. That is, to make the connection available to the load
* balancer.
*/
static final class ActivateEvent extends PolicyEvent implements Control {
static final PolicyEvent INSTANCE = new ActivateEvent();
static final class ActivateEvent extends Control {

static final HeartbeatEvent INSTANCE = new ActivateEvent();

private ActivateEvent() {
super("HEARTBEAT_ACTIVATE_EVT");
Expand All @@ -109,8 +111,9 @@ private ActivateEvent() {
* HeartbeatConstants#INVALIDATE_STATE}. That is, this connection is no longer available to the
* load balancer.
*/
static final class InvalidateEvent extends PolicyEvent implements Control {
static final PolicyEvent INSTANCE = new InvalidateEvent();
static final class InvalidateEvent extends Control {

static final HeartbeatEvent INSTANCE = new InvalidateEvent();

private InvalidateEvent() {
super("HEARTBEAT_INVALIDATE_EVT");
Expand All @@ -121,23 +124,27 @@ private InvalidateEvent() {
* An event sent when the connection state changes to {@link HeartbeatConstants#KILL_STATE},
* signaling that the connection must be reconnected.
*/
static final class KillEvent extends PolicyEvent implements Control {
static final class KillEvent extends Control {

static final PolicyEvent INSTANCE = new KillEvent();
static final HeartbeatEvent INSTANCE = new KillEvent();

private KillEvent() {
super("HEARTBEAT_KILL_EVT");
}
}

/**
* A marker interface. All event classes implementing this interface act as control signals and
* have the highest priority over regular events.
* A marker class. All event classes extending this class act as control signals and have the
* highest priority over regular events.
*/
private interface Control extends Event {
private abstract static class Control extends HeartbeatEvent {

Control(String name) {
super(name);
}

@Override
default boolean isControl() {
public final boolean isControl() {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2026 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY
* All Rights Reserved.
*/

package io.tarantool.pool.availability.heartbeat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.tarantool.core.state.State;
import io.tarantool.core.utils.CFUtils;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.ActivateEvent;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.InvalidateEvent;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.KillEvent;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.PongEvent;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.SendPingEvent;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.StartEvent;
import io.tarantool.pool.availability.heartbeat.HeartbeatEvent.StopEvent;

/**
* The base class of the internal states of the default availability policy. A state works with the
* machine and with its context without any restrictions.
*/
abstract class HeartbeatState implements State<HeartbeatEvent> {

protected final Logger logger = LoggerFactory.getLogger(getClass());

protected final HeartbeatStateMachine machine;
private final String name;

HeartbeatState(HeartbeatStateMachine machine, String name) {
this.machine = machine;
this.name = name;
}

@Override
public void handle(HeartbeatEvent event) {
if (event == null) {
this.logger.debug("[{}][{}]: got a null event. Skipping...", machine.identity(), this);
return;
}
this.logger.debug("[{}][{}]: received '{}' event", machine.identity(), this, event);

if (event instanceof StartEvent) onStart((StartEvent) event);
else if (event instanceof StopEvent) onStop((StopEvent) event);
else if (event instanceof SendPingEvent) onSendPing((SendPingEvent) event);
else if (event instanceof PongEvent) onPong((PongEvent) event);
else if (event instanceof ActivateEvent) onActivate((ActivateEvent) event);
else if (event instanceof InvalidateEvent) onInvalidate((InvalidateEvent) event);
else if (event instanceof KillEvent) onKill((KillEvent) event);
else
this.logger.debug(
"[{}][{}]: received an unexpected '{}' event", machine.identity(), this, event);
}

protected void onStart(StartEvent e) {}

protected void onStop(StopEvent e) {}

protected void onSendPing(SendPingEvent e) {}

protected void onPong(PongEvent e) {}

protected void onActivate(ActivateEvent e) {}

protected void onInvalidate(InvalidateEvent e) {}

protected void onKill(KillEvent e) {}

protected final void logStartHandleEvent(HeartbeatEvent event) {
this.logger.debug("[{}][{}]: start handle '{}' event", this.machine.identity(), this, event);
}

protected final void transitionTo(HeartbeatState nextState) {
this.machine.state(nextState);
this.logger.debug(
"[{}][{}]: transitioned to '{}' state", this.machine.identity(), this, nextState);
}

protected final void completeStop(StopEvent event) {
CFUtils.linkFuture(machine.rootCloseFuture(), event.closeFuture());
if (event.cause() != null) {
this.logger.debug(
"[{}][{}]: stop policy with cause", machine.identity(), this, event.cause());
}
this.machine.rootCloseFuture().complete(null);
}

@Override
public String toString() {
return this.name;
}
}
Loading
Loading