Skip to content

Commit 52ccff6

Browse files
committed
SERVER-22656 Move _recvChunkStart command parsing to separate class
1 parent 1b0377c commit 52ccff6

File tree

6 files changed

+385
-2
lines changed

6 files changed

+385
-2
lines changed

src/mongo/db/s/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ env.Library(
2323
target='migration_types',
2424
source=[
2525
'migration_session_id.cpp',
26+
'start_chunk_clone_request.cpp',
2627
],
2728
LIBDEPS=[
2829
'$BUILD_DIR/mongo/base',
2930
'$BUILD_DIR/mongo/bson/util/bson_extract',
31+
'$BUILD_DIR/mongo/s/common',
3032
],
3133
)
3234

@@ -79,6 +81,7 @@ env.CppUnitTest(
7981
target='migration_types_test',
8082
source=[
8183
'migration_session_id_test.cpp',
84+
'start_chunk_clone_request_test.cpp',
8285
],
8386
LIBDEPS=[
8487
'migration_types',

src/mongo/db/s/migration_session_id_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "mongo/bson/bsonobjbuilder.h"
3535
#include "mongo/db/jsobj.h"
3636
#include "mongo/unittest/unittest.h"
37-
#include "mongo/util/time_support.h"
3837

3938
namespace mongo {
4039

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/**
2+
* Copyright (C) 2016 MongoDB Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
* As a special exception, the copyright holders give permission to link the
17+
* code of portions of this program with the OpenSSL library under certain
18+
* conditions as described in each individual source file and distribute
19+
* linked combinations including the program with the OpenSSL library. You
20+
* must comply with the GNU Affero General Public License in all respects
21+
* for all of the code used other than as permitted herein. If you modify
22+
* file(s) with this exception, you may extend this exception to your
23+
* version of the file(s), but you are not obligated to do so. If you do not
24+
* wish to do so, delete this exception statement from your version. If you
25+
* delete this exception statement from all source files in the program,
26+
* then also delete it in the license file.
27+
*/
28+
29+
#include "mongo/platform/basic.h"
30+
31+
#include "mongo/db/s/start_chunk_clone_request.h"
32+
33+
#include "mongo/base/status_with.h"
34+
#include "mongo/bson/bsonobjbuilder.h"
35+
#include "mongo/bson/util/bson_extract.h"
36+
37+
namespace mongo {
38+
namespace {
39+
40+
const char kRecvChunkStart[] = "_recvChunkStart";
41+
const char kConfigServerConnectionString[] = "configdb";
42+
const char kFromShardId[] = "fromShard";
43+
const char kToShardId[] = "toShard";
44+
const char kChunkMinKey[] = "min";
45+
const char kChunkMaxKey[] = "max";
46+
const char kShardKeyPattern[] = "shardKeyPattern";
47+
48+
} // namespace
49+
50+
StartChunkCloneRequest::StartChunkCloneRequest(NamespaceString nss,
51+
MigrationSessionId sessionId,
52+
MigrationSecondaryThrottleOptions secondaryThrottle)
53+
: _nss(std::move(nss)),
54+
_sessionId(std::move(sessionId)),
55+
_secondaryThrottle(std::move(secondaryThrottle)) {}
56+
57+
StatusWith<StartChunkCloneRequest> StartChunkCloneRequest::createFromCommand(NamespaceString nss,
58+
const BSONObj& obj) {
59+
auto secondaryThrottleStatus = MigrationSecondaryThrottleOptions::createFromCommand(obj);
60+
if (!secondaryThrottleStatus.isOK()) {
61+
return secondaryThrottleStatus.getStatus();
62+
}
63+
64+
auto sessionIdStatus = MigrationSessionId::extractFromBSON(obj);
65+
if (!sessionIdStatus.isOK()) {
66+
return sessionIdStatus.getStatus();
67+
}
68+
69+
StartChunkCloneRequest request(std::move(nss),
70+
std::move(sessionIdStatus.getValue()),
71+
std::move(secondaryThrottleStatus.getValue()));
72+
73+
{
74+
std::string configServerConnectionString;
75+
Status status = bsonExtractStringField(
76+
obj, kConfigServerConnectionString, &configServerConnectionString);
77+
if (!status.isOK()) {
78+
return status;
79+
}
80+
81+
auto statusConfigServerCS = ConnectionString::parse(configServerConnectionString);
82+
if (!statusConfigServerCS.isOK()) {
83+
return statusConfigServerCS.getStatus();
84+
}
85+
86+
request._configServerCS = std::move(statusConfigServerCS.getValue());
87+
}
88+
89+
{
90+
Status status = bsonExtractStringField(obj, kFromShardId, &request._fromShardId);
91+
if (!status.isOK()) {
92+
return status;
93+
}
94+
}
95+
96+
{
97+
Status status = bsonExtractStringField(obj, kToShardId, &request._toShardId);
98+
if (!status.isOK()) {
99+
return status;
100+
}
101+
}
102+
103+
{
104+
BSONElement elem;
105+
Status status = bsonExtractTypedField(obj, kChunkMinKey, BSONType::Object, &elem);
106+
if (!status.isOK()) {
107+
return status;
108+
}
109+
110+
request._minKey = elem.Obj().getOwned();
111+
112+
if (request._minKey.isEmpty()) {
113+
return Status(ErrorCodes::UnsupportedFormat, "The chunk min key cannot be empty");
114+
}
115+
}
116+
117+
{
118+
BSONElement elem;
119+
Status status = bsonExtractTypedField(obj, kChunkMaxKey, BSONType::Object, &elem);
120+
if (!status.isOK()) {
121+
return status;
122+
}
123+
124+
request._maxKey = elem.Obj().getOwned();
125+
126+
if (request._maxKey.isEmpty()) {
127+
return Status(ErrorCodes::UnsupportedFormat, "The chunk max key cannot be empty");
128+
}
129+
}
130+
131+
{
132+
BSONElement elem;
133+
Status status = bsonExtractTypedField(obj, kShardKeyPattern, BSONType::Object, &elem);
134+
if (!status.isOK()) {
135+
return status;
136+
}
137+
138+
request._shardKeyPattern = elem.Obj().getOwned();
139+
140+
if (request._shardKeyPattern.isEmpty()) {
141+
return Status(ErrorCodes::UnsupportedFormat, "The shard key pattern cannot be empty");
142+
}
143+
}
144+
145+
return request;
146+
}
147+
148+
void StartChunkCloneRequest::appendAsCommand(
149+
BSONObjBuilder* builder,
150+
const NamespaceString& nss,
151+
const MigrationSessionId& shardVersion,
152+
const ConnectionString& configServerConnectionString,
153+
const std::string& fromShardId,
154+
const std::string& toShardId,
155+
const BSONObj& chunkMinKey,
156+
const BSONObj& chunkMaxKey,
157+
const BSONObj& shardKeyPattern,
158+
const MigrationSecondaryThrottleOptions& secondaryThrottle) {
159+
invariant(builder->asTempObj().isEmpty());
160+
invariant(nss.isValid());
161+
162+
builder->append(kRecvChunkStart, nss.ns());
163+
builder->append(kConfigServerConnectionString, configServerConnectionString.toString());
164+
builder->append(kFromShardId, fromShardId);
165+
builder->append(kToShardId, toShardId);
166+
builder->append(kChunkMinKey, chunkMinKey);
167+
builder->append(kChunkMaxKey, chunkMaxKey);
168+
builder->append(kShardKeyPattern, shardKeyPattern);
169+
secondaryThrottle.append(builder);
170+
}
171+
172+
} // namespace mongo
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Copyright (C) 2016 MongoDB Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
* As a special exception, the copyright holders give permission to link the
17+
* code of portions of this program with the OpenSSL library under certain
18+
* conditions as described in each individual source file and distribute
19+
* linked combinations including the program with the OpenSSL library. You
20+
* must comply with the GNU Affero General Public License in all respects
21+
* for all of the code used other than as permitted herein. If you modify
22+
* file(s) with this exception, you may extend this exception to your
23+
* version of the file(s), but you are not obligated to do so. If you do not
24+
* wish to do so, delete this exception statement from your version. If you
25+
* delete this exception statement from all source files in the program,
26+
* then also delete it in the license file.
27+
*/
28+
29+
#pragma once
30+
31+
#include <string>
32+
33+
#include "mongo/client/connection_string.h"
34+
#include "mongo/db/namespace_string.h"
35+
#include "mongo/db/s/migration_session_id.h"
36+
#include "mongo/s/migration_secondary_throttle_options.h"
37+
38+
namespace mongo {
39+
40+
class BSONObjBuilder;
41+
template <typename T>
42+
class StatusWith;
43+
44+
/**
45+
* Parses the arguments for a start chunk clone operation.
46+
*/
47+
class StartChunkCloneRequest {
48+
public:
49+
/**
50+
* Parses the input command and produces a request corresponding to its arguments.
51+
*/
52+
static StatusWith<StartChunkCloneRequest> createFromCommand(NamespaceString nss,
53+
const BSONObj& obj);
54+
55+
/**
56+
* Constructs a start chunk clone command with the specified parameters and writes it to the
57+
* builder, without closing the builder. The builder must be empty, but callers are free to
58+
* append more fields once the command has been constructed.
59+
*/
60+
static void appendAsCommand(BSONObjBuilder* builder,
61+
const NamespaceString& nss,
62+
const MigrationSessionId& sessionId,
63+
const ConnectionString& configServerConnectionString,
64+
const std::string& fromShardId,
65+
const std::string& toShardId,
66+
const BSONObj& chunkMinKey,
67+
const BSONObj& chunkMaxKey,
68+
const BSONObj& shardKeyPattern,
69+
const MigrationSecondaryThrottleOptions& secondaryThrottle);
70+
71+
const NamespaceString& getNss() const {
72+
return _nss;
73+
}
74+
75+
const ConnectionString& getConfigServerCS() const {
76+
return _configServerCS;
77+
}
78+
79+
const std::string& getFromShardId() const {
80+
return _fromShardId;
81+
}
82+
83+
const std::string& getToShardId() const {
84+
return _toShardId;
85+
}
86+
87+
const BSONObj& getMinKey() const {
88+
return _minKey;
89+
}
90+
91+
const BSONObj& getMaxKey() const {
92+
return _maxKey;
93+
}
94+
95+
const BSONObj& getShardKeyPattern() const {
96+
return _shardKeyPattern;
97+
}
98+
99+
const MigrationSecondaryThrottleOptions& getSecondaryThrottle() const {
100+
return _secondaryThrottle;
101+
}
102+
103+
private:
104+
StartChunkCloneRequest(NamespaceString nss,
105+
MigrationSessionId sessionId,
106+
MigrationSecondaryThrottleOptions secondaryThrottle);
107+
108+
// The collection for which this request applies
109+
NamespaceString _nss;
110+
111+
// The session id of this migration
112+
MigrationSessionId _sessionId;
113+
114+
// Connections string for the config server. This is a legacy field and is used in order to
115+
// initialize the sharding state on the donor shard in case it doesn't yet know that it is part
116+
// of a sharded system.
117+
ConnectionString _configServerCS;
118+
119+
// The source shard id
120+
std::string _fromShardId;
121+
122+
// The recipient shard id
123+
std::string _toShardId;
124+
125+
// Exact min and max key of the chunk being moved
126+
BSONObj _minKey;
127+
BSONObj _maxKey;
128+
129+
// Shard key pattern used by the collection
130+
BSONObj _shardKeyPattern;
131+
132+
// The parsed secondary throttle options
133+
MigrationSecondaryThrottleOptions _secondaryThrottle;
134+
};
135+
136+
} // namespace mongo

0 commit comments

Comments
 (0)