1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <set>
#include <vector>
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/simple_bsonobj_comparator.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/s/balancer/cluster_statistics.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/shard_id.h"
namespace mongo {
struct ZoneRange {
ZoneRange(const BSONObj& a_min, const BSONObj& a_max, const std::string& _zone);
std::string toString() const;
BSONObj min;
BSONObj max;
std::string zone;
};
struct MigrateInfo {
MigrateInfo(const ShardId& a_to, const ChunkType& a_chunk);
std::string getName() const;
BSONObj getMigrationTypeQuery() const;
std::string toString() const;
NamespaceString nss;
ShardId to;
ShardId from;
BSONObj minKey;
BSONObj maxKey;
ChunkVersion version;
};
typedef std::vector<ClusterStatistics::ShardStatistics> ShardStatisticsVector;
typedef std::map<ShardId, std::vector<ChunkType>> ShardToChunksMap;
/**
* This class constitutes a cache of the chunk distribution across the entire cluster along with the
* zone boundaries imposed on it. This information is stored in format, which makes it efficient to
* query utilization statististics and to decide what to balance.
*/
class DistributionStatus {
DistributionStatus(const DistributionStatus&) = delete;
DistributionStatus& operator=(const DistributionStatus&) = delete;
public:
DistributionStatus(NamespaceString nss, ShardToChunksMap shardToChunksMap);
DistributionStatus(DistributionStatus&&) = default;
/**
* Returns the namespace for which this balance status applies.
*/
const NamespaceString& nss() const {
return _nss;
}
/**
* Appends the specified range to the set of ranges tracked for this collection and checks if
* it overlaps with existing ranges.
*/
Status addRangeToZone(const ZoneRange& range);
/**
* Returns total number of chunks across all shards.
*/
size_t totalChunks() const;
/**
* Returns the total number of chunks across all shards, which fall into the specified zone's
* range.
*/
size_t totalChunksWithTag(const std::string& tag) const;
/**
* Returns number of chunks in the specified shard.
*/
size_t numberOfChunksInShard(const ShardId& shardId) const;
/**
* Returns number of chunks in the specified shard, which have the given tag.
*/
size_t numberOfChunksInShardWithTag(const ShardId& shardId, const std::string& tag) const;
/**
* Returns all chunks for the specified shard.
*/
const std::vector<ChunkType>& getChunks(const ShardId& shardId) const;
/**
* Returns all tag ranges defined for the collection.
*/
const BSONObjIndexedMap<ZoneRange>& tagRanges() const {
return _zoneRanges;
}
/**
* Returns all tags defined for the collection.
*/
const std::set<std::string>& tags() const {
return _allTags;
}
/**
* Using the set of tags defined for the collection, returns what tag corresponds to the
* specified chunk. If the chunk doesn't fall into any tag returns the empty string.
*/
std::string getTagForChunk(const ChunkType& chunk) const;
/**
* Returns a BSON/string representation of this distribution status.
*/
void report(BSONObjBuilder* builder) const;
std::string toString() const;
private:
// Namespace for which this distribution applies
NamespaceString _nss;
// Map of what chunks are owned by each shard
ShardToChunksMap _shardChunks;
// Map of zone max key to the zone description
BSONObjIndexedMap<ZoneRange> _zoneRanges;
// Set of all zones defined for this collection
std::set<std::string> _allTags;
};
class BalancerPolicy {
public:
/**
* Determines whether a shard with the specified utilization statistics would be able to accept
* a chunk with the specified tag. According to the policy a shard cannot accept chunks if its
* size is maxed out and if the chunk's tag conflicts with the tag of the shard.
*/
static Status isShardSuitableReceiver(const ClusterStatistics::ShardStatistics& stat,
const std::string& chunkTag);
/**
* Returns a suggested set of chunks to move whithin a collection's shards, given the specified
* state of the shards (draining, max size reached, etc) and the number of chunks for that
* collection. If the policy doesn't recommend anything to move, it returns an empty vector. The
* entries in the vector do are all for separate source/destination shards and as such do not
* need to be done serially and can be scheduled in parallel.
*
* The balancing logic calculates the optimum number of chunks per shard for each zone and if
* any of the shards have chunks, which are sufficiently higher than this number, suggests
* moving chunks to shards, which are under this number.
*
* The usedShards parameter is in/out and it contains the set of shards, which have already been
* used for migrations. Used so we don't return multiple conflicting migrations for the same
* shard.
*/
static std::vector<MigrateInfo> balance(const ShardStatisticsVector& shardStats,
const DistributionStatus& distribution,
std::set<ShardId>* usedShards);
/**
* Using the specified distribution information, returns a suggested better location for the
* specified chunk if one is available.
*/
static boost::optional<MigrateInfo> balanceSingleChunk(const ChunkType& chunk,
const ShardStatisticsVector& shardStats,
const DistributionStatus& distribution);
private:
/**
* Return the shard with the specified tag, which has the least number of chunks. If the tag is
* empty, considers all shards.
*/
static ShardId _getLeastLoadedReceiverShard(const ShardStatisticsVector& shardStats,
const DistributionStatus& distribution,
const std::string& tag,
const std::set<ShardId>& excludedShards);
/**
* Return the shard which has the least number of chunks with the specified tag. If the tag is
* empty, considers all chunks.
*/
static ShardId _getMostOverloadedShard(const ShardStatisticsVector& shardStats,
const DistributionStatus& distribution,
const std::string& chunkTag,
const std::set<ShardId>& excludedShards);
/**
* Selects one chunk for the specified zone (if appropriate) to be moved in order to bring the
* deviation of the shards chunk contents closer to even across all shards in the specified
* zone. Takes into account and updates the shards, which have already been used for migrations.
*
* The 'idealNumberOfChunksPerShardForTag' indicates what is the ideal number of chunks which
* each shard must have and is used to determine the imbalance and also to prevent chunks from
* moving when not necessary.
*
* Returns true if a migration was suggested, false otherwise. This method is intented to be
* called multiple times until all posible migrations for a zone have been selected.
*/
static bool _singleZoneBalance(const ShardStatisticsVector& shardStats,
const DistributionStatus& distribution,
const std::string& tag,
size_t idealNumberOfChunksPerShardForTag,
std::vector<MigrateInfo>* migrations,
std::set<ShardId>* usedShards);
};
} // namespace mongo
|