|
| 1 | +/** |
| 2 | + * Copyright 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 for |
| 21 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 22 | + * with this exception, you may extend this exception to your version of the |
| 23 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 24 | + * delete this exception statement from your version. If you delete this |
| 25 | + * exception statement from all source files in the program, then also delete |
| 26 | + * it in the license file. |
| 27 | + */ |
| 28 | + |
| 29 | +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplication |
| 30 | + |
| 31 | +#include "mongo/platform/basic.h" |
| 32 | + |
| 33 | +#include "mongo/db/repl/freshness_scanner.h" |
| 34 | + |
| 35 | +#include "mongo/base/status.h" |
| 36 | +#include "mongo/db/repl/bson_extract_optime.h" |
| 37 | +#include "mongo/db/repl/optime.h" |
| 38 | +#include "mongo/db/repl/replication_executor.h" |
| 39 | +#include "mongo/db/repl/scatter_gather_runner.h" |
| 40 | +#include "mongo/rpc/get_status_from_command_result.h" |
| 41 | +#include "mongo/util/log.h" |
| 42 | + |
| 43 | +namespace mongo { |
| 44 | +namespace repl { |
| 45 | + |
| 46 | +using executor::RemoteCommandRequest; |
| 47 | + |
| 48 | +FreshnessScanner::Algorithm::Algorithm(const ReplicaSetConfig& rsConfig, |
| 49 | + int myIndex, |
| 50 | + Milliseconds timeout) |
| 51 | + : _rsConfig(rsConfig), _myIndex(myIndex), _timeout(timeout) { |
| 52 | + for (int index = 0; index < _rsConfig.getNumMembers(); index++) { |
| 53 | + if (index != _myIndex) { |
| 54 | + _targets.push_back(_rsConfig.getMemberAt(index).getHostAndPort()); |
| 55 | + } |
| 56 | + } |
| 57 | + _totalRequests = _targets.size(); |
| 58 | +} |
| 59 | + |
| 60 | +std::vector<RemoteCommandRequest> FreshnessScanner::Algorithm::getRequests() const { |
| 61 | + BSONObjBuilder cmdBuilder; |
| 62 | + cmdBuilder << "replSetGetStatus" << 1; |
| 63 | + const BSONObj getStatusCmd = cmdBuilder.obj(); |
| 64 | + |
| 65 | + std::vector<RemoteCommandRequest> requests; |
| 66 | + for (auto& target : _targets) { |
| 67 | + requests.push_back(RemoteCommandRequest(target, "admin", getStatusCmd, _timeout)); |
| 68 | + } |
| 69 | + return requests; |
| 70 | +} |
| 71 | + |
| 72 | +void FreshnessScanner::Algorithm::processResponse(const RemoteCommandRequest& request, |
| 73 | + const ResponseStatus& response) { |
| 74 | + _responsesProcessed++; |
| 75 | + if (!response.isOK()) { // failed response |
| 76 | + LOG(2) << "FreshnessScanner: Got failed response from " << request.target << ": " |
| 77 | + << response.getStatus(); |
| 78 | + } else { |
| 79 | + BSONObj opTimesObj = response.getValue().data.getObjectField("optimes"); |
| 80 | + OpTime lastOpTime; |
| 81 | + Status status = bsonExtractOpTimeField(opTimesObj, "appliedOpTime", &lastOpTime); |
| 82 | + if (!status.isOK()) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + int index = _rsConfig.findMemberIndexByHostAndPort(request.target); |
| 87 | + FreshnessInfo freshnessInfo{index, lastOpTime}; |
| 88 | + |
| 89 | + auto cmp = |
| 90 | + [](const FreshnessInfo& a, const FreshnessInfo& b) { return a.opTime > b.opTime; }; |
| 91 | + auto iter = |
| 92 | + std::upper_bound(_freshnessInfos.begin(), _freshnessInfos.end(), freshnessInfo, cmp); |
| 93 | + _freshnessInfos.insert(iter, freshnessInfo); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +bool FreshnessScanner::Algorithm::hasReceivedSufficientResponses() const { |
| 98 | + return _responsesProcessed == _totalRequests; |
| 99 | +} |
| 100 | + |
| 101 | +FreshnessScanner::Result FreshnessScanner::Algorithm::getResult() const { |
| 102 | + invariant(hasReceivedSufficientResponses()); |
| 103 | + return _freshnessInfos; |
| 104 | +} |
| 105 | + |
| 106 | +StatusWith<ReplicationExecutor::EventHandle> FreshnessScanner::start( |
| 107 | + ReplicationExecutor* executor, |
| 108 | + const ReplicaSetConfig& rsConfig, |
| 109 | + int myIndex, |
| 110 | + Milliseconds timeout) { |
| 111 | + _algorithm.reset(new Algorithm(rsConfig, myIndex, timeout)); |
| 112 | + _runner.reset(new ScatterGatherRunner(_algorithm.get(), executor)); |
| 113 | + return _runner->start(); |
| 114 | +} |
| 115 | + |
| 116 | +void FreshnessScanner::cancel() { |
| 117 | + _runner->cancel(); |
| 118 | +} |
| 119 | + |
| 120 | +FreshnessScanner::Result FreshnessScanner::getResult() const { |
| 121 | + return _algorithm->getResult(); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace repl |
| 125 | +} // namespace mongo |
0 commit comments