Skip to content

Publish ClusterTopologyChangedEvent after applying the new topology #2860 - #3791

Merged
a-TODO-rov merged 2 commits into
redis:mainfrom
shun-lee:gh-2860-topology-event-after-reload
Jun 17, 2026
Merged

a-TODO-rov merged 2 commits into
redis:mainfrom
shun-lee:gh-2860-topology-event-after-reload

Conversation

@shun-lee

Copy link
Copy Markdown
Contributor

Closes #2860

Problem

In RedisClusterClient.refreshPartitionsAsync() the ClusterTopologyChangedEvent is
published before the new topology is applied:

  getResources().eventBus().publish(new ClusterTopologyChangedEvent(before, after)); // published first
  ...
  this.partitions.reload(loadedPartitions.getPartitions());                          // applied afterwards
  updatePartitionsInConnections();

Since getPartitions() returns the same Partitions instance that reload() mutates in
place, an event listener that inspects the topology while handling the event can observe
stale partitions.

Concrete impact: an application that re-subscribes to master nodes on
ClusterTopologyChangedEvent (using connection.getPartitions() / node selection) can,
right after a failover, select the demoted old masters (now replicas) instead of the
newly promoted masters, and stay subscribed to replicas until the next topology change.

Fix

Capture the before/after snapshots before applying the topology (so the event payload
stays correct), apply the topology via reload() + updatePartitionsInConnections(), and
publish the event only afterwards. Listeners now observe the up-to-date partitions.

@a-TODO-rov a-TODO-rov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good
Thanks @shun-lee

@a-TODO-rov
a-TODO-rov merged commit 73a6005 into redis:main Jun 17, 2026
12 of 13 checks passed
a-TODO-rov added a commit that referenced this pull request Jul 13, 2026
…2860 (#3791)

* Apply cluster topology before publishing ClusterTopologyChangedEvent #2860

* Format

---------

Co-authored-by: aleksandar.todorov <a_t_todorov@yahoo.com>
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.

ClusterTopologyChangedEvent Timing Issue

2 participants

⌂ 换网址