diff options
author | Amirsaman Memaripour <amirsaman.memaripour@10gen.com> | 2020-02-10 17:25:40 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2020-02-10 17:25:40 +0000 |
commit | 35dfac345cfa35754d0e3c1c57c6a237a90b69fa (patch) | |
tree | 23a22547e31626224f6c38f8ecba7bb6a43d233c /src/mongo/db/mirroring_sampler.h | |
parent | 73acb91e3989754ffa0ea98dc908362cad76c1ef (diff) | |
download | mongo-35dfac345cfa35754d0e3c1c57c6a237a90b69fa.tar.gz |
SERVER-45816 Write sampling function for command mirroring
Diffstat (limited to 'src/mongo/db/mirroring_sampler.h')
-rw-r--r-- | src/mongo/db/mirroring_sampler.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/mongo/db/mirroring_sampler.h b/src/mongo/db/mirroring_sampler.h new file mode 100644 index 00000000000..cdda905692e --- /dev/null +++ b/src/mongo/db/mirroring_sampler.h @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2020-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 <functional> +#include <memory> +#include <random> +#include <vector> + +#include "mongo/db/repl/is_master_response.h" +#include "mongo/util/net/hostandport.h" + +namespace mongo { + +/** + * Populates a random subset of eligible secondaries (using `IsMasterResponse` + * and a ratio) for mirroring. An empty subset for eligible secondaries indicates + * no mirroring is necessary/possible. + */ +class MirroringSampler final { +public: + using RandomFunc = std::function<int()>; + + /** + * Sampler function for mirroring commands to eligible secondaries. + * The caller must be the primary node of the replica-set. + * @arg isMaster replica-set topology that determines mirroring targets. + * @arg ratio the mirroring ratio (must be between 0 and 1 inclusive). + * @arg rnd the random generator function (default is `std::rand()`). + * @arg rndMax the maximum value that `rnd()` returns (default is `RAND_MAX`). + */ + static std::vector<HostAndPort> getMirroringTargets( + std::shared_ptr<const repl::IsMasterResponse> isMaster, + const double ratio, + RandomFunc rnd = std::rand, + const int rndMax = RAND_MAX) noexcept; +}; + +} // namespace mongo |