diff options
author | Israel Hsu <israel.hsu@mongodb.com> | 2023-02-15 22:18:29 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-02-16 04:34:51 +0000 |
commit | 924f756576cfaad5a4253736394ae3e8973c79bb (patch) | |
tree | d895f49e0884fade8df5f349dd01e786602b432b /src/mongo/s/query_analysis_sampler_util.cpp | |
parent | 2942a939de7e5c6a4fe0ea93fc3ae2fb2d815f75 (diff) | |
download | mongo-924f756576cfaad5a4253736394ae3e8973c79bb.tar.gz |
SERVER-70997 Implement query sampler counters for $currentOp
Diffstat (limited to 'src/mongo/s/query_analysis_sampler_util.cpp')
-rw-r--r-- | src/mongo/s/query_analysis_sampler_util.cpp | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/src/mongo/s/query_analysis_sampler_util.cpp b/src/mongo/s/query_analysis_sampler_util.cpp index e263f5a81e8..d051dec5b0e 100644 --- a/src/mongo/s/query_analysis_sampler_util.cpp +++ b/src/mongo/s/query_analysis_sampler_util.cpp @@ -29,6 +29,7 @@ #include "mongo/s/query_analysis_sampler_util.h" +#include "mongo/idl/idl_parser.h" #include "mongo/platform/random.h" #include "mongo/s/analyze_shard_key_role.h" #include "mongo/s/query_analysis_sampler.h" @@ -49,32 +50,83 @@ auto sampleIter(C&& c) { return std::next(c.begin(), (*random)->nextInt64(c.size())); } +StringData adjustCmdNameCase(const StringData& cmdName) { + if (cmdName == "findandmodify") { + return StringData("findAndModify"); + } else { + return cmdName; + } +} + } // namespace -boost::optional<UUID> tryGenerateSampleId(OperationContext* opCtx, const NamespaceString& nss) { +boost::optional<UUID> tryGenerateSampleId(OperationContext* opCtx, + const NamespaceString& nss, + const SampledCommandNameEnum cmdName) { return supportsSamplingQueries() - ? QueryAnalysisSampler::get(opCtx).tryGenerateSampleId(opCtx, nss) + ? QueryAnalysisSampler::get(opCtx).tryGenerateSampleId(opCtx, nss, cmdName) : boost::none; } +boost::optional<UUID> tryGenerateSampleId(OperationContext* opCtx, + const NamespaceString& nss, + const StringData& cmdName) { + return tryGenerateSampleId(opCtx, + nss, + SampledCommandName_parse(IDLParserContext("tryGenerateSampleId"), + adjustCmdNameCase(cmdName))); +} + boost::optional<TargetedSampleId> tryGenerateTargetedSampleId(OperationContext* opCtx, const NamespaceString& nss, + const SampledCommandNameEnum cmdName, const std::set<ShardId>& shardIds) { - if (auto sampleId = tryGenerateSampleId(opCtx, nss)) { + if (auto sampleId = tryGenerateSampleId(opCtx, nss, cmdName)) { return TargetedSampleId{*sampleId, getRandomShardId(shardIds)}; } return boost::none; } +boost::optional<TargetedSampleId> tryGenerateTargetedSampleId(OperationContext* opCtx, + const NamespaceString& nss, + const StringData& cmdName, + const std::set<ShardId>& shardIds) { + return tryGenerateTargetedSampleId( + opCtx, + nss, + SampledCommandName_parse(IDLParserContext("tryGenerateTargetedSampleId"), + adjustCmdNameCase(cmdName)), + shardIds); +} + boost::optional<TargetedSampleId> tryGenerateTargetedSampleId( OperationContext* opCtx, const NamespaceString& nss, + const SampledCommandNameEnum cmdName, const std::vector<ShardEndpoint>& endpoints) { - if (auto sampleId = tryGenerateSampleId(opCtx, nss)) { + if (auto sampleId = tryGenerateSampleId(opCtx, nss, cmdName)) { return TargetedSampleId{*sampleId, getRandomShardId(endpoints)}; } return boost::none; } +boost::optional<TargetedSampleId> tryGenerateTargetedSampleId( + OperationContext* opCtx, + const NamespaceString& nss, + const BatchedCommandRequest::BatchType batchType, + const std::vector<ShardEndpoint>& endpoints) { + auto cmdName = [&] { + switch (batchType) { + case BatchedCommandRequest::BatchType::BatchType_Delete: + return SampledCommandNameEnum::kDelete; + case BatchedCommandRequest::BatchType::BatchType_Insert: + return SampledCommandNameEnum::kInsert; + case BatchedCommandRequest::BatchType::BatchType_Update: + return SampledCommandNameEnum::kUpdate; + } + MONGO_UNREACHABLE; + }(); + return tryGenerateTargetedSampleId(opCtx, nss, cmdName, endpoints); +} ShardId getRandomShardId(const std::set<ShardId>& shardIds) { return *sampleIter(shardIds); |