Skip to content

Commit 8c4406c

Browse files
committed
Fix topo.ClusterMember to get hidden members in RS
There was the bug, and hidden and passive memebers wasn't reported as the cluster members for the RS topology.
1 parent bd3d104 commit 8c4406c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pbm/topo/cluster.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func ClusterMembers(ctx context.Context, m *mongo.Client) ([]Shard, error) {
121121
return nil, errors.Wrap(err, "define cluster state")
122122
}
123123

124+
// sharded cluster topo
124125
var shards []Shard
125126
if inf.IsMongos() || inf.IsSharded() {
126127
members, err := getShardMapImpl(ctx, m)
@@ -136,10 +137,15 @@ func ClusterMembers(ctx context.Context, m *mongo.Client) ([]Shard, error) {
136137
return shards, nil
137138
}
138139

140+
// RS topo
141+
hosts, err := GetReplsetHosts(ctx, m)
142+
if err != nil {
143+
return nil, errors.Wrap(err, "get all hosts for RS")
144+
}
139145
shards = []Shard{{
140146
ID: inf.SetName,
141147
RS: inf.SetName,
142-
Host: inf.SetName + "/" + strings.Join(inf.Hosts, ","),
148+
Host: fmt.Sprintf("%s/%s", inf.SetName, strings.Join(hosts, ",")),
143149
}}
144150
return shards, nil
145151
}

pbm/topo/topo.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ func GetReplsetStatus(ctx context.Context, m *mongo.Client) (*ReplsetStatus, err
195195
return status, nil
196196
}
197197

198+
// GetReplsetHosts returns host names for all RS members.
199+
// It includes also hidden and passive RS members.
200+
func GetReplsetHosts(ctx context.Context, m *mongo.Client) ([]string, error) {
201+
s, err := GetReplsetStatus(ctx, m)
202+
if err != nil {
203+
return nil, errors.Wrap(err, "get replset status")
204+
}
205+
206+
hosts := []string{}
207+
for _, m := range s.Members {
208+
hosts = append(hosts, m.Name)
209+
}
210+
return hosts, nil
211+
}
212+
198213
func GetNodeStatus(ctx context.Context, m *mongo.Client, name string) (*NodeStatus, error) {
199214
s, err := GetReplsetStatus(ctx, m)
200215
if err != nil {

0 commit comments

Comments
 (0)