1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <boost/optional.hpp>
34 #include <string>
35 #include <vector>
36 
37 #include "mongo/s/catalog/type_chunk.h"
38 #include "mongo/s/chunk_version.h"
39 #include "mongo/s/client/shard.h"
40 
41 namespace mongo {
42 
43 class NamespaceString;
44 class OperationContext;
45 class ShardKeyPattern;
46 class ShardRegistry;
47 template <typename T>
48 class StatusWith;
49 
50 /**
51  * Set of functions used to introspect and manipulate the state of individual shards.
52  */
53 namespace shardutil {
54 
55 /**
56  * Executes the listDatabases command against the specified shard and obtains the total data
57  * size across all databases in bytes (essentially, the totalSize field).
58  *
59  * Returns OK with the total size or an error. Known errors are:
60  *  ShardNotFound if shard by that id is not available on the registry
61  *  NoSuchKey if the total shard size could not be retrieved
62  */
63 StatusWith<long long> retrieveTotalShardSize(OperationContext* opCtx, const ShardId& shardId);
64 
65 /**
66  * Ask the specified shard to figure out the split points for a given chunk.
67  *
68  * shardId The shard id to query.
69  * nss Namespace, which owns the chunk.
70  * shardKeyPattern The shard key which corresponds to this sharded namespace.
71  * chunkRange Bounds of the chunk to be split.
72  * chunkSize Chunk size to target in bytes.
73  * maxObjs Limits the number of objects in each chunk. Zero means max, unspecified means use the
74  *         server default.
75  */
76 StatusWith<std::vector<BSONObj>> selectChunkSplitPoints(OperationContext* opCtx,
77                                                         const ShardId& shardId,
78                                                         const NamespaceString& nss,
79                                                         const ShardKeyPattern& shardKeyPattern,
80                                                         const ChunkRange& chunkRange,
81                                                         long long chunkSizeBytes,
82                                                         boost::optional<int> maxObjs);
83 
84 /**
85  * Asks the specified shard to split the chunk described by min/maxKey into the respective split
86  * points. If split was successful and the shard indicated that one of the resulting chunks should
87  * be moved off the currently owning shard, the return value will contain the bounds of this chunk.
88  *
89  * shardId The shard, which currently owns the chunk.
90  * nss Namespace, which owns the chunk.
91  * shardKeyPattern The shard key which corresponds to this sharded namespace.
92  * collectionVersion The expected collection version when doing the split.
93  * chunkRange Bounds of the chunk to be split.
94  * splitPoints The set of points at which the chunk should be split.
95  */
96 StatusWith<boost::optional<ChunkRange>> splitChunkAtMultiplePoints(
97     OperationContext* opCtx,
98     const ShardId& shardId,
99     const NamespaceString& nss,
100     const ShardKeyPattern& shardKeyPattern,
101     ChunkVersion collectionVersion,
102     const ChunkRange& chunkRange,
103     const std::vector<BSONObj>& splitPoints);
104 
105 }  // namespace shardutil
106 }  // namespace mongo
107