Skip to content

Commit bd3d104

Browse files
committed
Fix topo.ClusterMembers to get hidden members ...
for the sharded cluster Previously the bug was present, and hidden memebers were not present in the resulting array of the RS members.
1 parent 4aca65b commit bd3d104

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pbm/topo/cluster.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package topo
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

78
"go.mongodb.org/mongo-driver/bson"
@@ -153,18 +154,35 @@ func getShardMapImpl(ctx context.Context, m *mongo.Client) (map[ReplsetName]Shar
153154
// if shard name is not set, mongodb will provide unique name for it
154155
// (e.g. the replset name of the shard)
155156
// for configsvr, key name is "config"
156-
var shardMap struct{ Map map[string]string }
157+
// hosts field is used to discover hidden members, which are not present in map field
158+
var shardMap struct {
159+
Map map[string]string
160+
Hosts map[string]string
161+
}
157162
if err := res.Decode(&shardMap); err != nil {
158163
return nil, errors.Wrap(err, "decode")
159164
}
160165

166+
// Example of the hosts field from command output:
167+
// hosts: {
168+
// 'rs103:27017': 'rs1',
169+
// 'rs101:27017': 'rs1',
170+
// 'cfg02:27017': 'config',
171+
// ...
172+
// }
173+
// hostsByShard will contain even hidden RS members
174+
hostsByShard := map[string][]string{}
175+
for host, shardID := range shardMap.Hosts {
176+
hostsByShard[shardID] = append(hostsByShard[shardID], host)
177+
}
178+
161179
shards := make(map[string]Shard, len(shardMap.Map))
162180
for id, host := range shardMap.Map {
163181
rs, _, _ := strings.Cut(host, "/")
164182
shards[rs] = Shard{
165183
ID: id,
166184
RS: rs,
167-
Host: host,
185+
Host: fmt.Sprintf("%s/%s", rs, strings.Join(hostsByShard[id], ",")),
168186
}
169187
}
170188

0 commit comments

Comments
 (0)