Skip to content

Fix/reconnect multinode timeout - #1898

Open
Ente3 wants to merge 10 commits into
CloudNetService:nightlyfrom
Ente3:fix/reconnect-multinode-timeout
Open

Ente3 wants to merge 10 commits into
CloudNetService:nightlyfrom
Ente3:fix/reconnect-multinode-timeout

Conversation

@Ente3

@Ente3 Ente3 commented Sep 30, 2026

Copy link
Copy Markdown

Motivation

When the connection between two nodes dropped, even for a moment, both nodes removed each
other from the cluster immediately and never tried to reconnect. The cluster stayed split
until a node was restarted, and each node continued as its own head node. This happens
more often with nodes in different datacenters and with more nodes in the cluster.

The reconnect grace period (cloudnet.max.node.disconnect.millis) only applied to nodes
that stopped sending updates while their socket stayed open. When the socket itself was
closed, the channel close handlers called NodeServer#close() directly. The reconnect
handshake had also never really been used, and it had several problems once it was.

Modification

  • A lost node connection now enters the reconnect grace period when
    cloudnet.max.node.disconnect.millis is greater than 0, instead of removing the node
    directly. The default of 0 keeps the previous behaviour.
  • Only one reconnect attempt per node is in flight at a time, because concurrent
    connections can break the reconnect handshake. Nodes stuck in the syncing state after
    an interrupted reconnect are removed once the grace period expires.
  • Nodes send their startup time when they authorize. The other node can then detect a
    restart during the grace period and let the node join as a newly started node, instead
    of reconnecting it into its old session (which failed with an NullPointerException in
    AuthorizationResponsePacketListener). Nodes on older versions don't send it, and
    older nodes ignore it, so mixed versions keep working.
  • Remote service providers look up the current channel of their node on each call,
    instead of keeping the channel that was used during registration. That channel is
    closed after a reconnect.
  • The service sync during a reconnect now applies the reconnecting node's services.
    Before, they were skipped because the node wasn't available() yet.
  • Queued packets are flushed when a reconnect ends in a plain authorization, and local
    services are re-published to the other node.
  • The head node is re-selected when a node becomes ready again, so all nodes agree on
    the same head node.
  • The grace period is read when NodeDisconnectHandler is created, so it can be set in
    tests.
  • Added unit tests for NodeDisconnectHandler, NodeDisconnectTrackerTask,
    AuthorizationPacketListener and AuthorizationResponsePacketListener.
  • Updated the launcher.cnl description of cloudnet.max.node.disconnect.millis.

Result

With a grace period configured, nodes reconnect after short network interruptions and
agree on the same head node afterwards. A node that restarts within the grace period
joins cleanly as a new node. If a node does not come back within the grace period, it is
removed like before. With the default setting, behaviour is unchanged.

Tested manually with two nodes (grace period 30s/60s):

  • Killing the node connection (ss -K): before, both nodes dropped each other and never
    reconnected. Now both are READY again within a few seconds, with the same head node.
  • Blocking the connection with a firewall (idle timeout): reconnects, and the head node
    now matches on both nodes (before, each node showed itself as head).
  • Crashing and restarting a node within the grace period: the restart is detected and
    the node joins as a new node without errors.
  • Outage longer than the grace period: nodes are removed after the grace period as before.
  • Grace period 0: same behaviour as before.
Other context

Known limitations:

  • If a node does not reconnect within the grace period, the cluster stays split until a
    node is restarted, like before.
  • During the grace period, services and players of the disconnected node stay registered.
  • The service-related changes in DefaultCloudServiceManager are not covered by unit
    tests, as the class needs most of the node to be constructed.

Tests:
https://hastebin.baxnetzwerk.de/ct-Z4gFrfyk
With cloudnet.max.node.disconnect.millis set to 60.000:
https://hastebin.baxnetzwerk.de/dzvj7KguKXk
With cloudnet.max.node.disconnect.millis set to 0 or default:
https://hastebin.baxnetzwerk.de/Bz+rvVdSEei
(the links are available for 1 year.)

Fixes #1897

Ente3 and others added 5 commits September 30, 2026 17:36
A dropped node connection removed both nodes from the cluster right
away, and nothing tried to reconnect. A lost connection now enters the
reconnect grace period if cloudnet.max.node.disconnect.millis is
greater than 0. The default (0) keeps the old behaviour.

- only one reconnect attempt per node at a time
- remove nodes stuck in syncing once the grace period expires
- send the startup time on authorization, so a restart during the
  grace period is treated as a new node
- remote service providers look up the current node channel on each
  call instead of keeping the one from registration
- apply the reconnecting node's services during service sync
- flush queued packets and re-publish local services after a plain
  authorization
- re-select the head node when a node becomes ready again
- read the grace period when NodeDisconnectHandler is created, not at
  class load, so tests can set it
- don't track reconnect attempts for the local node
- add Javadoc (@SInCE 4.0) to the new public methods in
  NodeDisconnectHandler
@0utplay

0utplay commented Oct 15, 2026

Copy link
Copy Markdown
Member

Hi, thanks for your PR. I will take a look, I will just need some more time.

I also think that when we now try to properly fix it we will need to look into service sync issues this might introduce.

@Ente3

Ente3 commented Oct 15, 2026

Copy link
Copy Markdown
Author

Hi, thanks for your PR. I will take a look, I will just need some more time.

I also think that when we now try to properly fix it we will need to look into service sync issues this might introduce.

Hi thank you.

I am running this fix since a few weeks on my cloud instance. Till now i couldn't find a service sync issue. Is it know already or could it appear by this change?

If you figure out anything let me now.

Best regards

@Ente3

Ente3 commented Nov 4, 2026

Copy link
Copy Markdown
Author

Hi @0utplay, thanks for the heads-up. I went looking for the service sync issues you mentioned and tested them on a real two-node cluster. It ran this branch with a 60s grace period and only PREPARED services. A small TCP proxy between the nodes let me reset or cut the link on demand.

What works

  • When both nodes see the connection drop, services deleted or created on either side during the outage converge after the reconnect. The UUIDs and the head node match on both sides.
  • A node killed with kill -9 and restarted within the grace period is detected as a restart. Its old services are removed and it rejoins cleanly.
  • While a node is DISCONNECTED, the head doesn't auto-start services (the existing idlingNode check). It resumes after the reconnect.

What doesn't (reproduced)
If only one node notices the connection loss, the service lists diverge permanently.

  1. Cut only the connecting node's socket, so the other node still has it as READY.
  2. Delete one service and create another on each node.
  3. Let the link come back.

The node that noticed the loss keeps an entry for a service that no longer exists on its peer. That entry can't be deleted and was still there minutes later. That node also never learns about the peer's new service. The other direction is fine.

Why this happens:

  1. When the peer isn't DISCONNECTED yet, AuthorizationPacketListener replies with a plain auth success and no data sync.
  2. The services sync only adds or updates entries. It never removes a service its owner no longer reports.
  3. Messages the unaware node sent into the dead channel were never queued.

There is also a smaller issue. In the symmetric case the sync logs an ERROR with NullPointerException: Node Network Channel has to be present to register service. It happens when a local service was deleted during the split, because the sync tries to register the local node's own service again. Only that one entry is skipped, so it is harmless but noisy.

Not tested yet: three or more nodes, mixed versions, and packet loss between the drain and the channel switch (found by reading the code).

Proposal

  1. In this PR, treat a reconnect from a known peer (same startup time) as a real reconnect even if the local state isn't DISCONNECTED yet. That runs the same handshake with the data sync. Also skip the local node's own services in the services sync writer, which fixes the NPE.
  2. Reconcile on reconnect. Each node reports the services it owns, and the other side removes that node's services that aren't in the list. This needs an optional trailing field in the handshake packets, like the startup time, so mixed versions keep working. I can add it here or as a follow-up PR, whichever you prefer.
  3. Document that during the grace period the disconnected node's services stay listed and auto-start is paused cluster-wide. The default of 0 keeps the old behaviour.

Does that match what you had in mind?

Ente3 and others added 3 commits November 4, 2026 18:08
A node that reconnected before the other node noticed the lost
connection was authorized like a new node, without the data sync of a
reconnect. Changes made while the connection was down never reached
the node that had not noticed the loss.

- treat a reconnect with an unchanged startup time as a reconnect even
  if the node is still READY, by marking it disconnected first. This
  only applies if a grace period is configured and the node sends its
  startup time, other nodes are authorized as before
- read the sync data of the authorization response the way it is
  written. It is written as a nullable object but was read as a plain
  data buf, so the presence marker was taken as the size and the
  whole sync was dropped
- don't write the services of the local node during the service sync.
  The local node is the authority over its own services: outdated
  remote data no longer tries to re-register a removed service (which
  failed with a NullPointerException) or overwrite a known one
- a ready node that reconnects with the same startup time starts a
  reconnect. Nodes without a grace period, without a startup time or
  without a previous session are authorized as before, and a restarted
  node still joins as a new node
- the sync data of a reconnect authorization response reaches the data
  sync registry. The test uses the real packet, so it fails if the data
  is read differently than it is written

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cluster nodes never reconnect after a short connection loss

2 participants