PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/src/mongosDaemon.cpp

https://github.com/deltaforge/nebu-app-mongo
C++ | 81 lines | 60 code | 19 blank | 2 comment | 4 complexity | e5075e5f8fad59aaa3ee8c294c81d922 MD5 | raw file
 1
 2#include "mongoAdapter.h"
 3#include "mongocfgDaemon.h"
 4#include "mongodDaemon.h"
 5#include "mongosDaemon.h"
 6#include "replicaSet.h"
 7
 8#include "nebu-app-framework/commandRunner.h"
 9
10#include "log4cxx/logger.h"
11
12#include "mongo/bson/bson.h"
13
14#include <sstream>
15
16// Using declarations - mongo-cxx-driver
17using mongo::BSONArrayBuilder;
18using mongo::BSONObj;
19// Using declarations - standard library
20using std::set;
21using std::shared_ptr;
22using std::string;
23using std::stringstream;
24
25static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("nebu.mongo.MongocfgDaemon"));
26
27namespace nebu
28{
29	namespace app
30	{
31		namespace mongo
32		{
33
34			bool MongosDaemon::launch()
35			{
36				stringstream ss;
37				ss << "scripts/launch-router.sh" <<
38						" '" << this->getHostname() << "'" <<
39						" '" << this->getPortNo() << "'" <<
40						" '" << this->getConfigServers() << "'";
41
42				int result = NEBU_RUNCOMMAND(ss.str());
43
44				if (result == 0) {
45					this->launched = true;
46						LOG4CXX_DEBUG(logger, "Launch Successfull: " << this->getHostname());
47				}
48				return this->launched;
49			}
50
51			void MongosDaemon::setConfigServers(set<shared_ptr<MongocfgDaemon>> cfgServers)
52			{
53				stringstream configServers;
54				for (set<shared_ptr<MongocfgDaemon>>::iterator cfg = cfgServers.begin();
55					 cfg != cfgServers.end();
56					 cfg++)
57				{
58					configServers << (*cfg)->getHostPortPair() << ",";
59				}
60
61				string configServerList = configServers.str();
62				configServerList.erase(configServerList.end() - 1);
63
64				this->setConfigServers(configServerList);
65			}
66
67			bool MongosDaemon::registerReplicaSet(shared_ptr<ReplicaSet> replicaSet)
68			{
69				stringstream shardID;
70				shardID << replicaSet->getName() << "/" << (*replicaSet->getDataServers().begin())->getHostPortPair();
71
72				BSONObj shardingCommand = BSON("addShard" << shardID.str());
73				BSONObj result = MongoAdapter::getInstance()->executeCommand(this->getHostPortPair(), "admin", shardingCommand);
74
75				return (result["ok"].Number() > 0);
76			}
77
78		}
79	}
80}
81