From 8ffe7bec9676eaeb51d55dec83d6b70bfcc08252 Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Fri, 1 Nov 2024 15:31:29 +0100 Subject: [PATCH] [CLIENT-3156] Fix as issue where rack policy always returns the master node --- CHANGELOG.md | 7 +++++++ partition.go | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d20206f4..5f57fb4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change History +## November 1 2024: v7.7.2 + + Minor fix release. + +- **Fixes** + - [CLIENT-3156] Fix an issue where rack policy always returns the master node. Resolves #455 + ## September 18 2024: v7.7.1 Hot Fix release. This version fixes a major bug introduced in v7.7.0. You should use this release instead. diff --git a/partition.go b/partition.go index 58396273..38b63633 100644 --- a/partition.go +++ b/partition.go @@ -249,21 +249,25 @@ func (ptn *Partition) getSequenceNode(cluster *Cluster) (*Node, Error) { func (ptn *Partition) getRackNode(cluster *Cluster) (*Node, Error) { replicas := ptn.partitions.Replicas + // Try to find a node on the same rack first: for _, rackId := range cluster.clientPolicy.RackIds { seq := ptn.sequence for range replicas { - index := ptn.sequence % len(replicas) + index := seq % len(replicas) node := replicas[index][ptn.PartitionId] if node != nil && node != ptn.prevNode && node.hasRack(ptn.Namespace, rackId) && node.IsActive() { ptn.prevNode = node - ptn.sequence = seq + ptn.sequence = seq + 1 // start from the next node and save a comparison return node, nil } seq++ } } + // A node on the same Rack was not found, so try other options. + // Same node as the previous will be the last option, + // since it is the least desirable. for range replicas { index := ptn.sequence % len(replicas) node := replicas[index][ptn.PartitionId]