diff options
author | jannaerin <golden.janna@gmail.com> | 2019-02-22 17:32:05 -0500 |
---|---|---|
committer | jannaerin <golden.janna@gmail.com> | 2019-02-27 15:30:51 -0500 |
commit | 4b114240edbfe4446f9f4de25c10bd67350b0f40 (patch) | |
tree | 72a144b6a7eeab39de2675ffe48922dfa6608e78 /src/mongo/s/would_change_owning_shard_exception.h | |
parent | 2ecd83f822b242db1c13f2abc3ff3593ba622149 (diff) | |
download | mongo-4b114240edbfe4446f9f4de25c10bd67350b0f40.tar.gz |
SERVER-39778 Create WouldChangeOwningShard exception
Diffstat (limited to 'src/mongo/s/would_change_owning_shard_exception.h')
-rw-r--r-- | src/mongo/s/would_change_owning_shard_exception.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/mongo/s/would_change_owning_shard_exception.h b/src/mongo/s/would_change_owning_shard_exception.h new file mode 100644 index 00000000000..ad5f36081b6 --- /dev/null +++ b/src/mongo/s/would_change_owning_shard_exception.h @@ -0,0 +1,98 @@ +/** + * Copyright (C) 2019-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 "mongo/base/error_extra_info.h" +#include "mongo/bson/bsonobj.h" +#include "mongo/bson/bsonobjbuilder.h" + +namespace mongo { + +/** + * This error is thrown when an update would cause a document to be owned by a different + * shard. If the update is part of a multi statement transaction, we will attach the + * query from the original update and the post image returned by the update stage. MongoS + * will use these to delete the original doc and insert the new doc. If the update is a + * retryable write, we will attach both the query and the update fields from the original + * update command. MongoS will use these to start an internal transaction and re-run the + * update command. + */ +class WouldChangeOwningShardInfo final : public ErrorExtraInfo { +public: + static constexpr auto code = ErrorCodes::WouldChangeOwningShard; + + explicit WouldChangeOwningShardInfo(const BSONObj& originalQueryPredicate, + const boost::optional<BSONObj>& originalUpdate, + const boost::optional<BSONObj>& postImage) + : _originalQueryPredicate(originalQueryPredicate.getOwned()) { + invariant(!(originalUpdate && postImage)); + invariant(originalUpdate || postImage); + + if (originalUpdate) + _originalUpdate = originalUpdate->getOwned(); + if (postImage) + _postImage = postImage->getOwned(); + } + + const auto& getOriginalQueryPredicate() const { + return _originalQueryPredicate; + } + + const auto& getOriginalUpdate() const { + return _originalUpdate; + } + + const auto& getPostImage() const { + return _postImage; + } + + BSONObj toBSON() const { + BSONObjBuilder bob; + serialize(&bob); + return bob.obj(); + } + + void serialize(BSONObjBuilder* bob) const override; + static std::shared_ptr<const ErrorExtraInfo> parse(const BSONObj&); + static WouldChangeOwningShardInfo parseFromCommandError(const BSONObj& commandError); + +private: + // the 'q' portion of the original update comamand + BSONObj _originalQueryPredicate; + + // The 'u' portion of the original update command + boost::optional<BSONObj> _originalUpdate; + + // The post image returned by the update stage + boost::optional<BSONObj> _postImage; +}; +using WouldChangeOwningShardException = ExceptionFor<ErrorCodes::WouldChangeOwningShard>; + +} // namespace mongo |