int main(int argc, char *argv[]) { ShoutConfig myConfig(APPID_NORMALIZE_AM, argc, argv); NormalizeAM NormalizeAM(myConfig.getStringValue(ARGUMENT_AUDIOFILE), myConfig.getStringValue(ARGUMENT_META_IN)); return 0; }
void ReplSetImpl::syncThread() { while( 1 ) { // After a reconfig, we may not be in the replica set anymore, so // check that we are in the set (and not an arbiter) before // trying to sync with other replicas. if( ! _self ) { log() << "replSet warning did not receive a valid config yet, sleeping 20 seconds " << rsLog; sleepsecs(20); continue; } if( myConfig().arbiterOnly ) { return; } fassert(16113, !Lock::isLocked()); try { _syncThread(); } catch(const DBException& e) { sethbmsg(str::stream() << "syncThread: " << e.toString()); sleepsecs(10); } catch(...) { sethbmsg("unexpected exception in syncThread()"); // TODO : SET NOT SECONDARY here? sleepsecs(60); } sleepsecs(1); } }
int main(int argc, char *argv[]) { ShoutConfig myConfig(APPID_ADAPT_AM, argc, argv); Adapt_AM Adapt_AM(myConfig.getStringValue(ARGUMENT_TRAINDIR),myConfig.getStringValue(ARGUMENT_AM_PHONE), myConfig.getStringValue(ARGUMENT_AM_CLUSTER),myConfig.getStringValue(ARGUMENT_AM_OUT)); return 0; }
int main() { MyConfig myConfig("test_config"); std::cout << "Default myInt = " << myConfig.myInt << std::endl; myConfig.load(); // Note we could load() in the constructor but then we couldn't print the default value. std::cout << "Loaded myInt = " << myConfig.myInt << std::endl; myConfig.myInt = 40; myConfig.save(); return 0; // Try editing the json and running again. }
int main(int argc, char *argv[]) { ShoutConfig myConfig(APPID_MAKETRAINSET, argc, argv); Shout_MakeTrainSet Shout_MakeTrainSet(myConfig.getStringValue(ARGUMENT_AM_TYPE), myConfig.getStringValue(ARGUMENT_PHONE_LIST), myConfig.getStringValue(ARGUMENT_HYPFILE), myConfig.getStringValue(ARGUMENT_AUDIOFILE), myConfig.getStringValue(ARGUMENT_META_IN), myConfig.getStringValue(ARGUMENT_TRAINDIR), myConfig.getStringValue(ARGUMENT_DECISIONTREE), myConfig.getStringValue(ARGUMENT_PERLINE), myConfig.getStringValue(ARGUMENT_AUDIO_LIST), myConfig.getStringValue(ARGUMENT_SPEAKER_FILTER), myConfig.getFloatValue(ARGUMENT_TRAINSILP) ); return 0; }
// Config Parser. Might drop this. bool CConfig::Parse(CFile& file, CString& sErrorMsg) { CString sLine; unsigned int uLineNum = 0; CConfig *pActiveConfig = this; std::stack<ConfigStackEntry> ConfigStack; bool bCommented = false; // support for /**/ style comments if (!file.Seek(0)) { sErrorMsg = "Could not seek to the beginning of the config."; return false; } while (file.ReadLine(sLine)) { uLineNum++; #define ERROR(arg) do { \ std::stringstream stream; \ stream << "Error on line " << uLineNum << ": " << arg; \ sErrorMsg = stream.str(); \ m_SubConfigs.clear(); \ m_ConfigEntries.clear(); \ return false; \ } while (0) // Remove all leading spaces and trailing line endings sLine.TrimLeft(); sLine.TrimRight("\r\n"); if (bCommented || sLine.Left(2) == "/*") { /* Does this comment end on the same line again? */ bCommented = (sLine.Right(2) != "*/"); continue; } if ((sLine.empty()) || (sLine[0] == '#') || (sLine.Left(2) == "//")) { continue; } if ((sLine.Left(1) == "<") && (sLine.Right(1) == ">")) { sLine.LeftChomp(); sLine.RightChomp(); sLine.Trim(); CString sTag = sLine.Token(0); CString sValue = sLine.Token(1, true); sTag.Trim(); sValue.Trim(); if (sTag.Left(1) == "/") { sTag = sTag.substr(1); if (!sValue.empty()) ERROR("Malformated closing tag. Expected \"</" << sTag << ">\"."); if (ConfigStack.empty()) ERROR("Closing tag \"" << sTag << "\" which is not open."); const struct ConfigStackEntry& entry = ConfigStack.top(); CConfig myConfig(entry.Config); CString sName(entry.sName); if (!sTag.Equals(entry.sTag)) ERROR("Closing tag \"" << sTag << "\" which is not open."); // This breaks entry ConfigStack.pop(); if (ConfigStack.empty()) pActiveConfig = this; else pActiveConfig = &ConfigStack.top().Config; SubConfig &conf = pActiveConfig->m_SubConfigs[sTag.AsLower()]; SubConfig::const_iterator it = conf.find(sName); if (it != conf.end()) ERROR("Duplicate entry for tag \"" << sTag << "\" name \"" << sName << "\"."); conf[sName] = CConfigEntry(myConfig); } else { if (sValue.empty()) ERROR("Empty block name at begin of block."); ConfigStack.push(ConfigStackEntry(sTag.AsLower(), sValue)); pActiveConfig = &ConfigStack.top().Config; } continue; } // If we have a regular line, figure out where it goes CString sName = sLine.Token(0, false, "="); CString sValue = sLine.Token(1, true, "="); // Only remove the first space, people might want // leading spaces (e.g. in the MOTD). if (sValue.Left(1) == " ") sValue.LeftChomp(); // We don't have any names with spaces, trim all // leading/trailing spaces. sName.Trim(); if (sName.empty() || sValue.empty()) ERROR("Malformed line"); CString sNameLower = sName.AsLower(); pActiveConfig->m_ConfigEntries[sNameLower].push_back(sValue); } if (bCommented) ERROR("Comment not closed at end of file."); if (!ConfigStack.empty()) { const CString& sTag = ConfigStack.top().sTag; ERROR("Not all tags are closed at the end of the file. Inner-most open tag is \"" << sTag << "\"."); } return true; }
bool ReplSetImpl::forceSyncFrom(const string& host, string& errmsg, BSONObjBuilder& result) { lock lk(this); // initial sanity check if (iAmArbiterOnly()) { errmsg = "arbiters don't sync"; return false; } if (box.getState().primary()) { errmsg = "primaries don't sync"; return false; } if (_self != NULL && host == _self->fullName()) { errmsg = "I cannot sync from myself"; return false; } // find the member we want to sync from Member *newTarget = 0; for (Member *m = _members.head(); m; m = m->next()) { if (m->fullName() == host) { newTarget = m; break; } } // do some more sanity checks if (!newTarget) { // this will also catch if someone tries to sync a member from itself, as _self is not // included in the _members list. errmsg = "could not find member in replica set"; return false; } if (newTarget->config().arbiterOnly) { errmsg = "I cannot sync from an arbiter"; return false; } if (!newTarget->config().buildIndexes && myConfig().buildIndexes) { errmsg = "I cannot sync from a member who does not build indexes"; return false; } if (newTarget->hbinfo().authIssue) { errmsg = "I cannot authenticate against the requested member"; return false; } if (newTarget->hbinfo().health == 0) { errmsg = "I cannot reach the requested member"; return false; } if (newTarget->hbinfo().opTime.getSecs()+10 < lastOpTimeWritten.getSecs()) { log() << "attempting to sync from " << newTarget->fullName() << ", but its latest opTime is " << newTarget->hbinfo().opTime.getSecs() << " and ours is " << lastOpTimeWritten.getSecs() << " so this may not work" << rsLog; result.append("warning", "requested member is more than 10 seconds behind us"); // not returning false, just warning } // record the previous member we were syncing from const Member *prev = replset::BackgroundSync::get()->getSyncTarget(); if (prev) { result.append("prevSyncTarget", prev->fullName()); } // finally, set the new target _forceSyncTarget = newTarget; return true; }
Status ReplSetImpl::forceSyncFrom(const string& host, BSONObjBuilder* result) { lock lk(this); // initial sanity check if (iAmArbiterOnly()) { return Status(ErrorCodes::NotSecondary, "arbiters don't sync"); } if (box.getState().primary()) { return Status(ErrorCodes::NotSecondary, "primaries don't sync"); } if (_self != NULL && host == _self->fullName()) { return Status(ErrorCodes::InvalidOptions, "I cannot sync from myself"); } // find the member we want to sync from Member *newTarget = 0; for (Member *m = _members.head(); m; m = m->next()) { if (m->fullName() == host) { newTarget = m; break; } } // do some more sanity checks if (!newTarget) { // this will also catch if someone tries to sync a member from itself, as _self is not // included in the _members list. return Status(ErrorCodes::NodeNotFound, "could not find member in replica set"); } if (newTarget->config().arbiterOnly) { return Status(ErrorCodes::InvalidOptions, "I cannot sync from an arbiter"); } if (!newTarget->config().buildIndexes && myConfig().buildIndexes) { return Status(ErrorCodes::InvalidOptions, "I cannot sync from a member who does not build indexes"); } if (newTarget->hbinfo().authIssue) { return Status(ErrorCodes::Unauthorized, "not authorized to communicate with " + newTarget->fullName()); } if (newTarget->hbinfo().health == 0) { return Status(ErrorCodes::HostUnreachable, "I cannot reach the requested member"); } if (newTarget->hbinfo().opTime.getSecs()+10 < lastOpTimeWritten.getSecs()) { log() << "attempting to sync from " << newTarget->fullName() << ", but its latest opTime is " << newTarget->hbinfo().opTime.getSecs() << " and ours is " << lastOpTimeWritten.getSecs() << " so this may not work" << rsLog; result->append("warning", "requested member is more than 10 seconds behind us"); // not returning false, just warning } // record the previous member we were syncing from const HostAndPort prev = BackgroundSync::get()->getSyncTarget(); if (!prev.empty()) { result->append("prevSyncTarget", prev.toString()); } // finally, set the new target _forceSyncTarget = newTarget; return Status::OK(); }
const Member* ReplSetImpl::getMemberToSyncTo() { lock lk(this); // if we have a target we've requested to sync from, use it if (_forceSyncTarget) { Member* target = _forceSyncTarget; _forceSyncTarget = 0; sethbmsg( str::stream() << "syncing to: " << target->fullName() << " by request", 0); return target; } const Member* primary = box.getPrimary(); // wait for 2N pings before choosing a sync target if (_cfg) { int needMorePings = config().members.size()*2 - HeartbeatInfo::numPings; if (needMorePings > 0) { OCCASIONALLY log() << "waiting for " << needMorePings << " pings from other members before syncing" << endl; return NULL; } // If we are only allowed to sync from the primary, return that if (!_cfg->chainingAllowed()) { // Returns NULL if we cannot reach the primary return primary; } } // find the member with the lowest ping time that has more data than me // Find primary's oplog time. Reject sync candidates that are more than // maxSyncSourceLagSecs seconds behind. OpTime primaryOpTime; if (primary) primaryOpTime = primary->hbinfo().opTime; else // choose a time that will exclude no candidates, since we don't see a primary primaryOpTime = OpTime(maxSyncSourceLagSecs, 0); if (primaryOpTime.getSecs() < static_cast<unsigned int>(maxSyncSourceLagSecs)) { // erh - I think this means there was just a new election // and we don't yet know the new primary's optime primaryOpTime = OpTime(maxSyncSourceLagSecs, 0); } OpTime oldestSyncOpTime(primaryOpTime.getSecs() - maxSyncSourceLagSecs, 0); Member *closest = 0; time_t now = 0; // Make two attempts. The first attempt, we ignore those nodes with // slave delay higher than our own. The second attempt includes such // nodes, in case those are the only ones we can reach. // This loop attempts to set 'closest'. for (int attempts = 0; attempts < 2; ++attempts) { for (Member *m = _members.head(); m; m = m->next()) { if (!m->syncable()) continue; if (m->state() == MemberState::RS_SECONDARY) { // only consider secondaries that are ahead of where we are if (m->hbinfo().opTime <= lastOpTimeWritten) continue; // omit secondaries that are excessively behind, on the first attempt at least. if (attempts == 0 && m->hbinfo().opTime < oldestSyncOpTime) continue; } // omit nodes that are more latent than anything we've already considered if (closest && (m->hbinfo().ping > closest->hbinfo().ping)) continue; if (attempts == 0 && (myConfig().slaveDelay < m->config().slaveDelay || m->config().hidden)) { continue; // skip this one in the first attempt } map<string,time_t>::iterator vetoed = _veto.find(m->fullName()); if (vetoed != _veto.end()) { // Do some veto housekeeping if (now == 0) { now = time(0); } // if this was on the veto list, check if it was vetoed in the last "while". // if it was, skip. if (vetoed->second >= now) { if (time(0) % 5 == 0) { log() << "replSet not trying to sync from " << (*vetoed).first << ", it is vetoed for " << ((*vetoed).second - now) << " more seconds" << rsLog; } continue; } _veto.erase(vetoed); // fall through, this is a valid candidate now } // This candidate has passed all tests; set 'closest' closest = m; } if (closest) break; // no need for second attempt } if (!closest) { return NULL; } sethbmsg( str::stream() << "syncing to: " << closest->fullName(), 0); return closest; }
void ReplSetImpl::_fillIsMaster(BSONObjBuilder& b) { lock lk(this); const StateBox::SP sp = box.get(); bool isp = sp.state.primary(); b.append("setName", name()); b.append("setVersion", version()); b.append("ismaster", isp); b.append("secondary", sp.state.secondary()); { vector<string> hosts, passives, arbiters; _fillIsMasterHost(_self, hosts, passives, arbiters); for (Member *m = _members.head(); m; m = m->next()) { verify(m); _fillIsMasterHost(m, hosts, passives, arbiters); } if (hosts.size() > 0) { b.append("hosts", hosts); } if (passives.size() > 0) { b.append("passives", passives); } if (arbiters.size() > 0) { b.append("arbiters", arbiters); } } if (!isp) { const Member *m = sp.primary; if (m) b.append("primary", m->h().toString()); } else { b.append("primary", _self->fullName()); } if (myConfig().arbiterOnly) b.append("arbiterOnly", true); if (myConfig().priority == 0 && !myConfig().arbiterOnly) b.append("passive", true); if (myConfig().slaveDelay) b.append("slaveDelay", myConfig().slaveDelay); if (myConfig().hidden) b.append("hidden", true); if (!myConfig().buildIndexes) b.append("buildIndexes", false); if (!myConfig().tags.empty()) { BSONObjBuilder a; for (map<string,string>::const_iterator i = myConfig().tags.begin(); i != myConfig().tags.end(); i++) { a.append((*i).first, (*i).second); } b.append("tags", a.done()); } b.append("me", myConfig().h.toString()); }
bool NoSettings::Parse(NoFile& file, NoString& error) { NoString line; uint lineNum = 0; NoSettings* activeConfig = this; std::stack<ConfigStackEntry> configStack; bool commented = false; // support for /**/ style comments if (!file.Seek(0)) { error = "Could not seek to the beginning of the config."; return false; } while (file.ReadLine(line)) { lineNum++; #define ERROR(arg) \ do { \ std::stringstream stream; \ stream << "Error on line " << lineNum << ": " << arg; \ error = stream.str(); \ m_subConfigs.clear(); \ m_entries.clear(); \ return false; \ } while (0) // Remove all leading spaces and trailing line endings line.trimLeft(); line.trimRight("\r\n"); if (commented || line.startsWith("/*")) { /* Does this comment end on the same line again? */ commented = !line.endsWith("*/"); continue; } if (line.empty() || line.startsWith("#") || line.startsWith("//")) { continue; } if (line.startsWith("<") && line.endsWith(">")) { line.leftChomp(1); line.rightChomp(1); line.trim(); NoString tag = No::token(line, 0); NoString value = No::tokens(line, 1); tag.trim(); value.trim(); if (tag.startsWith("/")) { tag = tag.substr(1); if (!value.empty()) ERROR("Malformated closing tag. Expected \"</" << tag << ">\"."); if (configStack.empty()) ERROR("Closing tag \"" << tag << "\" which is not open."); const struct ConfigStackEntry& entry = configStack.top(); NoSettings myConfig(entry.config); NoString name(entry.name); if (!tag.equals(entry.tag)) ERROR("Closing tag \"" << tag << "\" which is not open."); // This breaks entry configStack.pop(); if (configStack.empty()) activeConfig = this; else activeConfig = &configStack.top().config; SubConfig& conf = activeConfig->m_subConfigs[tag.toLower()]; SubConfig::const_iterator it = conf.find(name); if (it != conf.end()) ERROR("Duplicate entry for tag \"" << tag << "\" name \"" << name << "\"."); conf[name] = NoSettingsEntry(myConfig); } else { if (value.empty()) ERROR("Empty block name at begin of block."); configStack.push(ConfigStackEntry(tag.toLower(), value)); activeConfig = &configStack.top().config; } continue; } // If we have a regular line, figure out where it goes NoString name = No::token(line, 0, "="); NoString value = No::tokens(line, 1, "="); // Only remove the first space, people might want // leading spaces (e.g. in the MOTD). if (value.startsWith(" ")) value.leftChomp(1); // We don't have any names with spaces, trim all // leading/trailing spaces. name.trim(); if (name.empty() || value.empty()) ERROR("Malformed line"); NoString sNameLower = name.toLower(); activeConfig->m_entries[sNameLower].push_back(value); } if (commented) ERROR("Comment not closed at end of file."); if (!configStack.empty()) { const NoString& tag = configStack.top().tag; ERROR("Not all tags are closed at the end of the file. Inner-most open tag is \"" << tag << "\"."); } return true; }