@@ -2,7 +2,6 @@ package topo
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"strings"
7
6
8
7
"go.mongodb.org/mongo-driver/bson"
@@ -121,7 +120,6 @@ func ClusterMembers(ctx context.Context, m *mongo.Client) ([]Shard, error) {
121
120
return nil , errors .Wrap (err , "define cluster state" )
122
121
}
123
122
124
- // sharded cluster topo
125
123
var shards []Shard
126
124
if inf .IsMongos () || inf .IsSharded () {
127
125
members , err := getShardMapImpl (ctx , m )
@@ -137,15 +135,10 @@ func ClusterMembers(ctx context.Context, m *mongo.Client) ([]Shard, error) {
137
135
return shards , nil
138
136
}
139
137
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
- }
145
138
shards = []Shard {{
146
139
ID : inf .SetName ,
147
140
RS : inf .SetName ,
148
- Host : fmt . Sprintf ( "%s/%s" , inf . SetName , strings .Join (hosts , "," ) ),
141
+ Host : inf . SetName + "/" + strings .Join (inf . Hosts , "," ),
149
142
}}
150
143
return shards , nil
151
144
}
@@ -160,44 +153,18 @@ func getShardMapImpl(ctx context.Context, m *mongo.Client) (map[ReplsetName]Shar
160
153
// if shard name is not set, mongodb will provide unique name for it
161
154
// (e.g. the replset name of the shard)
162
155
// for configsvr, key name is "config"
163
- // hosts field is used to discover hidden members, which are not present in map field
164
- var shardMap struct {
165
- Map map [string ]string
166
- Hosts map [string ]string
167
- }
156
+ var shardMap struct { Map map [string ]string }
168
157
if err := res .Decode (& shardMap ); err != nil {
169
158
return nil , errors .Wrap (err , "decode" )
170
159
}
171
160
172
- // Example of the hosts field from command output:
173
- // hosts: {
174
- // 'rs103:27017': 'rs1',
175
- // 'rs101:27017': 'rs1',
176
- // 'cfg02:27017': 'config',
177
- // ...
178
- // }
179
- // hostsByShard will contain even hidden RS members
180
- hostsByShard := map [string ][]string {}
181
- for host , shardID := range shardMap .Hosts {
182
- hostsByShard [shardID ] = append (hostsByShard [shardID ], host )
183
- }
184
-
185
- // for PSMDB 6 and below, config srv is not reported within hosts field
186
- if len (hostsByShard ["config" ]) == 0 {
187
- cfgHostgs , err := GetReplsetHosts (ctx , m )
188
- if err != nil {
189
- return nil , errors .Wrap (err , "get all hosts for config RS" )
190
- }
191
- hostsByShard ["config" ] = cfgHostgs
192
- }
193
-
194
161
shards := make (map [string ]Shard , len (shardMap .Map ))
195
162
for id , host := range shardMap .Map {
196
163
rs , _ , _ := strings .Cut (host , "/" )
197
164
shards [rs ] = Shard {
198
165
ID : id ,
199
166
RS : rs ,
200
- Host : fmt . Sprintf ( "%s/%s" , rs , strings . Join ( hostsByShard [ id ], "," )) ,
167
+ Host : host ,
201
168
}
202
169
}
203
170
0 commit comments