Browse Source

Fix infinite loop if there are no live roots (never happened before?!? wow!)

Adam Ierymenko 9 years ago
parent
commit
0b82c9ebad
2 changed files with 13 additions and 15 deletions
  1. 7 9
      node/Topology.cpp
  2. 6 6
      service/ControlPlane.cpp

+ 7 - 9
node/Topology.cpp

@@ -202,18 +202,16 @@ SharedPtr<Peer> Topology::getBestRoot(const Address *avoid,unsigned int avoidCou
 		 * circumnavigate the globe rather than bouncing between just two. */
 		 * circumnavigate the globe rather than bouncing between just two. */
 
 
 		if (_rootAddresses.size() > 1) { // gotta be one other than me for this to work
 		if (_rootAddresses.size() > 1) { // gotta be one other than me for this to work
-			std::vector<Address>::const_iterator sna(std::find(_rootAddresses.begin(),_rootAddresses.end(),RR->identity.address()));
-			if (sna != _rootAddresses.end()) { // sanity check -- _amRoot should've been false in this case
-				for(;;) {
-					if (++sna == _rootAddresses.end())
-						sna = _rootAddresses.begin(); // wrap around at end
-					if (*sna != RR->identity.address()) { // pick one other than us -- starting from me+1 in sorted set order
-						SharedPtr<Peer> *p = _peers.get(*sna);
-						if ((p)&&((*p)->hasActiveDirectPath(now))) {
-							bestRoot = *p;
+			for(unsigned long p=0;p<_rootAddresses.size();++p) {
+				if (_rootAddresses[p] == RR->identity.address()) {
+					for(unsigned long q=1;q<_rootAddresses.size();++q) {
+						SharedPtr<Peer> *nextsn = _peers.get(_rootAddresses[(p + q) % _rootAddresses.size()]);
+						if ((nextsn)&&((*nextsn)->hasActiveDirectPath(now))) {
+							bestRoot = *nextsn;
 							break;
 							break;
 						}
 						}
 					}
 					}
+					break;
 				}
 				}
 			}
 			}
 		}
 		}

+ 6 - 6
service/ControlPlane.cpp

@@ -265,7 +265,7 @@ unsigned int ControlPlane::handleRequest(
 	std::string &responseBody,
 	std::string &responseBody,
 	std::string &responseContentType)
 	std::string &responseContentType)
 {
 {
-	char json[1024];
+	char json[8194];
 	unsigned int scode = 404;
 	unsigned int scode = 404;
 	std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
 	std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
 	std::map<std::string,std::string> urlArgs;
 	std::map<std::string,std::string> urlArgs;
@@ -365,11 +365,12 @@ unsigned int ControlPlane::handleRequest(
 					_node->clusterStatus(&cs);
 					_node->clusterStatus(&cs);
 
 
 					if (cs.clusterSize >= 1) {
 					if (cs.clusterSize >= 1) {
-						char t[4096];
-						Utils::snprintf(t,sizeof(t),"{\n\t\t\"myId\": %u,\n\t\t\"clusterSize\": %u,\n\t\t\"members: [\n",cs.myId,cs.clusterSize);
+						char t[1024];
+						Utils::snprintf(t,sizeof(t),"{\n\t\t\"myId\": %u,\n\t\t\"clusterSize\": %u,\n\t\t\"members\": [",cs.myId,cs.clusterSize);
 						clusterJson.append(t);
 						clusterJson.append(t);
 						for(unsigned int i=0;i<cs.clusterSize;++i) {
 						for(unsigned int i=0;i<cs.clusterSize;++i) {
-							Utils::snprintf(t,sizeof(t),"\t\t\t{\n\t\t\t\t\"id\": %u,\n\t\t\t\t\"msSinceLastHeartbeat\": %u,\n\t\t\t\t\"alive\": %s,\n\t\t\t\t\"x\": %d,\n\t\t\t\t\"y\": %d,\n\t\t\t\t\"z\": %d,\n\t\t\t\t\"load\": %llu\n\t\t\t\t\"peers\": %llu\n\t\t\t}%s",
+							Utils::snprintf(t,sizeof(t),"%s\t\t\t{\n\t\t\t\t\"id\": %u,\n\t\t\t\t\"msSinceLastHeartbeat\": %u,\n\t\t\t\t\"alive\": %s,\n\t\t\t\t\"x\": %d,\n\t\t\t\t\"y\": %d,\n\t\t\t\t\"z\": %d,\n\t\t\t\t\"load\": %llu,\n\t\t\t\t\"peers\": %llu\n\t\t\t}",
+								((i == 0) ? "\n" : ",\n"),
 								cs.members[i].id,
 								cs.members[i].id,
 								cs.members[i].msSinceLastHeartbeat,
 								cs.members[i].msSinceLastHeartbeat,
 								(cs.members[i].alive != 0) ? "true" : "false",
 								(cs.members[i].alive != 0) ? "true" : "false",
@@ -377,8 +378,7 @@ unsigned int ControlPlane::handleRequest(
 								cs.members[i].y,
 								cs.members[i].y,
 								cs.members[i].z,
 								cs.members[i].z,
 								cs.members[i].load,
 								cs.members[i].load,
-								cs.members[i].peers,
-								(i == (cs.clusterSize - 1)) ? "," : "");
+								cs.members[i].peers);
 							clusterJson.append(t);
 							clusterJson.append(t);
 						}
 						}
 						clusterJson.append(" ]\n\t\t}");
 						clusterJson.append(" ]\n\t\t}");