diff options
author | Jason Zhang <jason.zhang@mongodb.com> | 2022-10-13 22:33:47 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-10-13 23:25:53 +0000 |
commit | be1848a26189a8c773cf00a4ce6ff053b8dc0cac (patch) | |
tree | e34ef920ee3d62589360d50c2a17c835158d1d30 /src/mongo/s/write_ops/write_without_shard_key_util.cpp | |
parent | 69f11e092a68749b684bad807bbd6ef2aa097baa (diff) | |
download | mongo-be1848a26189a8c773cf00a4ce6ff053b8dc0cac.tar.gz |
SERVER-69708 Detect a write without a shard key or _id
Diffstat (limited to 'src/mongo/s/write_ops/write_without_shard_key_util.cpp')
-rw-r--r-- | src/mongo/s/write_ops/write_without_shard_key_util.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/mongo/s/write_ops/write_without_shard_key_util.cpp b/src/mongo/s/write_ops/write_without_shard_key_util.cpp new file mode 100644 index 00000000000..820a813c399 --- /dev/null +++ b/src/mongo/s/write_ops/write_without_shard_key_util.cpp @@ -0,0 +1,55 @@ +/** + * Copyright (C) 2022-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. + */ + +#include "mongo/bson/bsonobj.h" +#include "mongo/logv2/log.h" +#include "mongo/s/catalog_cache.h" +#include "mongo/s/grid.h" + +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding + +namespace mongo { +namespace write_without_shard_key { + +bool canTargetQueryByShardKeyOrId(OperationContext* opCtx, + NamespaceString ns, + const BSONObj& query) { + auto cm = + uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, ns)); + auto shardKey = + uassertStatusOK(cm.getShardKeyPattern().extractShardKeyFromQuery(opCtx, ns, query)); + + if (shardKey.isEmpty() && !query.hasField("_id")) { + LOGV2(6970800, "A write without shard key or _id detected"); + return false; + } + return true; +} +} // namespace write_without_shard_key +} // namespace mongo |