std::vector<ReplicationExecutor::RemoteCommandRequest> FreshnessChecker::Algorithm::getRequests() const { invariant(_targets.size()); const MemberConfig& selfConfig = _rsConfig.getMemberAt(_selfIndex); // gather all not-down nodes, get their fullnames(or hostandport's) // schedule fresh command for each node BSONObjBuilder freshCmdBuilder; freshCmdBuilder.append("replSetFresh", 1); freshCmdBuilder.append("set", _rsConfig.getReplSetName()); freshCmdBuilder.append("opTime", Date_t(_lastOpTimeApplied.asDate())); freshCmdBuilder.append("who", selfConfig.getHostAndPort().toString()); freshCmdBuilder.appendIntOrLL("cfgver", _rsConfig.getConfigVersion()); freshCmdBuilder.append("id", selfConfig.getId()); const BSONObj replSetFreshCmd = freshCmdBuilder.obj(); std::vector<ReplicationExecutor::RemoteCommandRequest> requests; for (std::vector<HostAndPort>::const_iterator it = _targets.begin(); it != _targets.end(); ++it) { invariant(*it != selfConfig.getHostAndPort()); requests.push_back(ReplicationExecutor::RemoteCommandRequest( *it, "admin", replSetFreshCmd, Milliseconds(30*1000))); // trying to match current Socket timeout } return requests; }
std::vector<RemoteCommandRequest> ElectCmdRunner::Algorithm::getRequests() const { const MemberConfig& selfConfig = _rsConfig.getMemberAt(_selfIndex); std::vector<RemoteCommandRequest> requests; BSONObjBuilder electCmdBuilder; electCmdBuilder.append("replSetElect", 1); electCmdBuilder.append("set", _rsConfig.getReplSetName()); electCmdBuilder.append("who", selfConfig.getHostAndPort().toString()); electCmdBuilder.append("whoid", selfConfig.getId()); electCmdBuilder.appendIntOrLL("cfgver", _rsConfig.getConfigVersion()); electCmdBuilder.append("round", _round); const BSONObj replSetElectCmd = electCmdBuilder.obj(); // Schedule a RemoteCommandRequest for each non-DOWN node for (std::vector<HostAndPort>::const_iterator it = _targets.begin(); it != _targets.end(); ++it) { invariant(*it != selfConfig.getHostAndPort()); requests.push_back(RemoteCommandRequest( *it, "admin", replSetElectCmd, Milliseconds(30 * 1000))); // trying to match current Socket timeout } return requests; }
BSONObj ReplSetHeartbeatArgs::toBSON() const { invariant(isInitialized()); BSONObjBuilder builder; builder.append("replSetHeartbeat", _setName); builder.appendIntOrLL("pv", _protocolVersion); builder.appendIntOrLL("v", _configVersion); builder.append("from", _hasSenderHost ? _senderHost.toString() : ""); if (_hasSenderId) { builder.appendIntOrLL("fromId", _senderId); } if (_hasCheckEmpty) { builder.append("checkEmpty", _checkEmpty); } return builder.obj(); }
BSONObj ReplicaSetConfig::toBSON() const { BSONObjBuilder configBuilder; configBuilder.append(kIdFieldName, _replSetName); configBuilder.appendIntOrLL(kVersionFieldName, _version); if (_configServer) { // Only include "configsvr" field if true configBuilder.append(kConfigServerFieldName, _configServer); } if (_protocolVersion > 0) { configBuilder.append(kProtocolVersionFieldName, _protocolVersion); } BSONArrayBuilder members(configBuilder.subarrayStart(kMembersFieldName)); for (MemberIterator mem = membersBegin(); mem != membersEnd(); mem++) { members.append(mem->toBSON(getTagConfig())); } members.done(); BSONObjBuilder settingsBuilder(configBuilder.subobjStart(kSettingsFieldName)); settingsBuilder.append(kChainingAllowedFieldName, _chainingAllowed); settingsBuilder.appendIntOrLL(kHeartbeatIntervalFieldName, durationCount<Milliseconds>(_heartbeatInterval)); settingsBuilder.appendIntOrLL(kHeartbeatTimeoutFieldName, durationCount<Seconds>(_heartbeatTimeoutPeriod)); settingsBuilder.appendIntOrLL(kElectionTimeoutFieldName, durationCount<Milliseconds>(_electionTimeoutPeriod)); BSONObjBuilder gleModes(settingsBuilder.subobjStart(kGetLastErrorModesFieldName)); for (StringMap<ReplicaSetTagPattern>::const_iterator mode = _customWriteConcernModes.begin(); mode != _customWriteConcernModes.end(); ++mode) { if (mode->first[0] == '$') { // Filter out internal modes continue; } BSONObjBuilder modeBuilder(gleModes.subobjStart(mode->first)); for (ReplicaSetTagPattern::ConstraintIterator itr = mode->second.constraintsBegin(); itr != mode->second.constraintsEnd(); itr++) { modeBuilder.append(_tagConfig.getTagKey(ReplicaSetTag(itr->getKeyIndex(), 0)), itr->getMinCount()); } modeBuilder.done(); } gleModes.done(); settingsBuilder.append(kGetLastErrorDefaultsFieldName, _defaultWriteConcern.toBSON()); settingsBuilder.done(); return configBuilder.obj(); }
void MockReplicaSet::mockIsMasterCmd() { for (ReplNodeMap::iterator nodeIter = _nodeMap.begin(); nodeIter != _nodeMap.end(); ++nodeIter) { const string& hostAndPort = nodeIter->first; BSONObjBuilder builder; builder.append("setName", _setName); const MemberConfig* member = _replConfig.findMemberByHostAndPort(HostAndPort(hostAndPort)); if (!member) { builder.append("ismaster", false); builder.append("secondary", false); vector<string> hostList; builder.append("hosts", hostList); } else { const bool isPrimary = hostAndPort == getPrimary(); builder.append("ismaster", isPrimary); builder.append("secondary", !isPrimary); { // TODO: add passives & arbiters vector<string> hostList; hostList.push_back(getPrimary()); const vector<string> secondaries = getSecondaries(); for (vector<string>::const_iterator secIter = secondaries.begin(); secIter != secondaries.end(); ++secIter) { hostList.push_back(*secIter); } builder.append("hosts", hostList); } builder.append("primary", getPrimary()); if (member->isArbiter()) { builder.append("arbiterOnly", true); } if (member->getPriority() == 0 && !member->isArbiter()) { builder.append("passive", true); } if (member->getSlaveDelay().count()) { builder.appendIntOrLL("slaveDelay", durationCount<Seconds>(member->getSlaveDelay())); } if (member->isHidden()) { builder.append("hidden", true); } if (!member->shouldBuildIndexes()) { builder.append("buildIndexes", false); } const ReplicaSetTagConfig tagConfig = _replConfig.getTagConfig(); if (member->hasTags(tagConfig)) { BSONObjBuilder tagBuilder; for (MemberConfig::TagIterator tag = member->tagsBegin(); tag != member->tagsEnd(); ++tag) { std::string tagKey = tagConfig.getTagKey(*tag); if (tagKey[0] == '$') { // Filter out internal tags continue; } tagBuilder.append(tagKey, tagConfig.getTagValue(*tag)); } builder.append("tags", tagBuilder.done()); } } builder.append("me", hostAndPort); builder.append("ok", true); nodeIter->second->setCommandReply("ismaster", builder.done()); } }
static BSONObj bamboo2bson(const dclass::DistributedType *type, DatagramIterator &dgi) { // The BSON library's weird data model doesn't allow elements to exist on // their own; they must be part of an object. Therefore, we always return // results in a single BSONObj with key "_" BSONObjBuilder b; switch(type->get_type()) { case dclass::Type::T_INT8: { b << "_" << dgi.read_int8(); } break; case dclass::Type::T_INT16: { b << "_" << dgi.read_int16(); } break; case dclass::Type::T_INT32: { b << "_" << dgi.read_int32(); } break; case dclass::Type::T_INT64: { b.appendIntOrLL("_", dgi.read_int64()); } break; case dclass::Type::T_UINT8: { b << "_" << dgi.read_uint8(); } break; case dclass::Type::T_UINT16: { b << "_" << dgi.read_uint16(); } break; case dclass::Type::T_UINT32: { b << "_" << dgi.read_uint32(); } break; case dclass::Type::T_UINT64: { b.appendIntOrLL("_", dgi.read_uint64()); } break; case dclass::Type::T_CHAR: { unsigned char c = dgi.read_uint8(); string str(c, 1); b << "_" << str; } break; case dclass::Type::T_FLOAT32: { b << "_" << dgi.read_float32(); } break; case dclass::Type::T_FLOAT64: { b << "_" << dgi.read_float64(); } break; case dclass::Type::T_STRING: { vector<uint8_t> vec = dgi.read_data(type->get_size()); string str((const char *)vec.data(), vec.size()); b << "_" << str; } case dclass::Type::T_VARSTRING: { b << "_" << dgi.read_string(); } break; case dclass::Type::T_BLOB: { vector<uint8_t> blob = dgi.read_data(type->get_size()); b.appendBinData("_", blob.size(), BinDataGeneral, blob.data()); } break; case dclass::Type::T_VARBLOB: { vector<uint8_t> blob = dgi.read_blob(); b.appendBinData("_", blob.size(), BinDataGeneral, blob.data()); } break; case dclass::Type::T_ARRAY: { const dclass::ArrayType *array = type->as_array(); BSONArrayBuilder ab; for(size_t i = 0; i < array->get_array_size(); i++) { ab << bamboo2bson(array->get_element_type(), dgi)["_"]; } b << "_" << ab.arr(); } break; case dclass::Type::T_VARARRAY: { const dclass::ArrayType *array = type->as_array(); dgsize_t array_length = dgi.read_size(); dgsize_t starting_size = dgi.tell(); BSONArrayBuilder ab; while(dgi.tell() != starting_size + array_length) { ab << bamboo2bson(array->get_element_type(), dgi)["_"]; if(dgi.tell() > starting_size + array_length) { throw mongo::DBException("Discovered corrupt array-length tag!", 0); } } b << "_" << ab.arr(); } break; case dclass::Type::T_STRUCT: { const dclass::Struct *s = type->as_struct(); size_t fields = s->get_num_fields(); BSONObjBuilder ob; for(unsigned int i = 0; i < fields; ++i) { const dclass::Field *field = s->get_field(i); ob << field->get_name() << bamboo2bson(field->get_type(), dgi)["_"]; } b << "_" << ob.obj(); } break; case dclass::Type::T_METHOD: { const dclass::Method *m = type->as_method(); size_t parameters = m->get_num_parameters(); BSONObjBuilder ob; for(unsigned int i = 0; i < parameters; ++i) { const dclass::Parameter *parameter = m->get_parameter(i); string name = parameter->get_name(); if(name.empty()) { stringstream n; n << "_" << i; name = n.str(); } ob << name << bamboo2bson(parameter->get_type(), dgi)["_"]; } b << "_" << ob.obj(); } break; case dclass::Type::T_INVALID: default: assert(false); break; } return b.obj(); }