|
| 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 |
0 commit comments