Forget paths to peers if we are handing them off.

This commit is contained in:
Adam Ierymenko 2015-10-27 14:37:38 -07:00
parent f692cec763
commit 40976c02a4
3 changed files with 56 additions and 61 deletions

View File

@ -563,17 +563,14 @@ void Cluster::removeMember(uint16_t memberId)
_memberIds = newMemberIds; _memberIds = newMemberIds;
} }
InetAddress Cluster::findBetterEndpoint(const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload) bool Cluster::findBetterEndpoint(InetAddress &redirectTo,const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload)
{ {
if (!peerPhysicalAddress) // sanity check
return InetAddress();
if (_addressToLocationFunction) { if (_addressToLocationFunction) {
// Pick based on location if it can be determined // Pick based on location if it can be determined
int px = 0,py = 0,pz = 0; int px = 0,py = 0,pz = 0;
if (_addressToLocationFunction(_addressToLocationFunctionArg,reinterpret_cast<const struct sockaddr_storage *>(&peerPhysicalAddress),&px,&py,&pz) == 0) { if (_addressToLocationFunction(_addressToLocationFunctionArg,reinterpret_cast<const struct sockaddr_storage *>(&peerPhysicalAddress),&px,&py,&pz) == 0) {
TRACE("no geolocation data for %s (geo-lookup is lazy/async so it may work next time)",peerPhysicalAddress.toIpString().c_str()); TRACE("no geolocation data for %s (geo-lookup is lazy/async so it may work next time)",peerPhysicalAddress.toIpString().c_str());
return InetAddress(); return false;
} }
// Find member closest to this peer // Find member closest to this peer
@ -603,14 +600,15 @@ InetAddress Cluster::findBetterEndpoint(const Address &peerAddress,const InetAdd
for(std::vector<InetAddress>::const_iterator a(best.begin());a!=best.end();++a) { for(std::vector<InetAddress>::const_iterator a(best.begin());a!=best.end();++a) {
if (a->ss_family == peerPhysicalAddress.ss_family) { if (a->ss_family == peerPhysicalAddress.ss_family) {
TRACE("%s at [%d,%d,%d] is %f from us but %f from %u, can redirect to %s",peerAddress.toString().c_str(),px,py,pz,currentDistance,bestDistance,bestMember,a->toString().c_str()); TRACE("%s at [%d,%d,%d] is %f from us but %f from %u, can redirect to %s",peerAddress.toString().c_str(),px,py,pz,currentDistance,bestDistance,bestMember,a->toString().c_str());
return *a; redirectTo = *a;
return true;
} }
} }
TRACE("%s at [%d,%d,%d] is %f from us, no better endpoints found",peerAddress.toString().c_str(),px,py,pz,currentDistance); TRACE("%s at [%d,%d,%d] is %f from us, no better endpoints found",peerAddress.toString().c_str(),px,py,pz,currentDistance);
return InetAddress(); return false;
} else { } else {
// TODO: pick based on load if no location info? // TODO: pick based on load if no location info?
return InetAddress(); return false;
} }
} }

View File

@ -264,22 +264,15 @@ public:
void removeMember(uint16_t memberId); void removeMember(uint16_t memberId);
/** /**
* Find a better cluster endpoint for this peer * Find a better cluster endpoint for this peer (if any)
*
* If this endpoint appears to be the best, a NULL/0 InetAddres is returned.
* Otherwise the InetAddress of a better endpoint is returned and the peer
* can then then be told to contact us there.
*
* Redirection is only done within the same address family, so the returned
* endpoint will always be the same ss_family as the supplied physical
* address.
* *
* @param redirectTo InetAddress to be set to a better endpoint (if there is one)
* @param peerAddress Address of peer to (possibly) redirect * @param peerAddress Address of peer to (possibly) redirect
* @param peerPhysicalAddress Physical address of peer's current best path (where packet was most recently received or getBestPath()->address()) * @param peerPhysicalAddress Physical address of peer's current best path (where packet was most recently received or getBestPath()->address())
* @param offload Always redirect if possible -- can be used to offload peers during shutdown * @param offload Always redirect if possible -- can be used to offload peers during shutdown
* @return InetAddress or NULL if there does not seem to be a better endpoint * @return True if redirectTo was set to a new address, false if redirectTo was not modified
*/ */
InetAddress findBetterEndpoint(const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload); bool findBetterEndpoint(InetAddress &redirectTo,const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload);
/** /**
* Fill out ZT_ClusterStatus structure (from core API) * Fill out ZT_ClusterStatus structure (from core API)

View File

@ -81,47 +81,49 @@ void Peer::received(
Packet::Verb inReVerb) Packet::Verb inReVerb)
{ {
#ifdef ZT_ENABLE_CLUSTER #ifdef ZT_ENABLE_CLUSTER
bool redirected = false; InetAddress redirectTo;
if ((RR->cluster)&&(hops == 0)&&(verb != Packet::VERB_OK)&&(verb != Packet::VERB_ERROR)&&(verb != Packet::VERB_RENDEZVOUS)&&(verb != Packet::VERB_PUSH_DIRECT_PATHS)) { if ((RR->cluster)&&(hops == 0)) {
InetAddress redirectTo(RR->cluster->findBetterEndpoint(_id.address(),remoteAddr,false)); // Note: findBetterEndpoint() is first since we still want to check
if ((redirectTo.ss_family == AF_INET)||(redirectTo.ss_family == AF_INET6)) { // for a better endpoint even if we don't actually send a redirect.
if (_vProto >= 5) { if ( (RR->cluster->findBetterEndpoint(redirectTo,_id.address(),remoteAddr,false)) && (verb != Packet::VERB_OK)&&(verb != Packet::VERB_ERROR)&&(verb != Packet::VERB_RENDEZVOUS)&&(verb != Packet::VERB_PUSH_DIRECT_PATHS) ) {
// For newer peers we can send a more idiomatic verb: PUSH_DIRECT_PATHS. if ((redirectTo.ss_family == AF_INET)||(redirectTo.ss_family == AF_INET6)) {
Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS); if (_vProto >= 5) {
outp.append((uint16_t)1); // count == 1 // For newer peers we can send a more idiomatic verb: PUSH_DIRECT_PATHS.
outp.append((uint8_t)0); // no flags Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
outp.append((uint16_t)0); // no extensions outp.append((uint16_t)1); // count == 1
if (redirectTo.ss_family == AF_INET) { outp.append((uint8_t)0); // no flags
outp.append((uint8_t)4); outp.append((uint16_t)0); // no extensions
outp.append((uint8_t)6); if (redirectTo.ss_family == AF_INET) {
outp.append(redirectTo.rawIpData(),4); outp.append((uint8_t)4);
outp.append((uint8_t)6);
outp.append(redirectTo.rawIpData(),4);
} else {
outp.append((uint8_t)6);
outp.append((uint8_t)18);
outp.append(redirectTo.rawIpData(),16);
}
outp.append((uint16_t)redirectTo.port());
outp.armor(_key,true);
RR->antiRec->logOutgoingZT(outp.data(),outp.size());
RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
} else { } else {
outp.append((uint8_t)6); // For older peers we use RENDEZVOUS to coax them into contacting us elsewhere.
outp.append((uint8_t)18); Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
outp.append(redirectTo.rawIpData(),16); outp.append((uint8_t)0); // no flags
RR->identity.address().appendTo(outp);
outp.append((uint16_t)redirectTo.port());
if (redirectTo.ss_family == AF_INET) {
outp.append((uint8_t)4);
outp.append(redirectTo.rawIpData(),4);
} else {
outp.append((uint8_t)16);
outp.append(redirectTo.rawIpData(),16);
}
outp.armor(_key,true);
RR->antiRec->logOutgoingZT(outp.data(),outp.size());
RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
} }
outp.append((uint16_t)redirectTo.port());
outp.armor(_key,true);
RR->antiRec->logOutgoingZT(outp.data(),outp.size());
RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
} else {
// For older peers we use RENDEZVOUS to coax them into contacting us elsewhere.
Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
outp.append((uint8_t)0); // no flags
RR->identity.address().appendTo(outp);
outp.append((uint16_t)redirectTo.port());
if (redirectTo.ss_family == AF_INET) {
outp.append((uint8_t)4);
outp.append(redirectTo.rawIpData(),4);
} else {
outp.append((uint8_t)16);
outp.append(redirectTo.rawIpData(),16);
}
outp.armor(_key,true);
RR->antiRec->logOutgoingZT(outp.data(),outp.size());
RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
} }
redirected = true;
} }
} }
#endif #endif
@ -140,11 +142,13 @@ void Peer::received(
_lastMulticastFrame = now; _lastMulticastFrame = now;
#ifdef ZT_ENABLE_CLUSTER #ifdef ZT_ENABLE_CLUSTER
// If we're in cluster mode and have sent the peer a better endpoint, stop // If we're in cluster mode and there's a better endpoint, stop here and don't
// here and don't confirm paths, replicate multicast info, etc. The new // learn or confirm paths. Also reset any existing paths, since they should
// endpoint should do that. // go there and no longer talk to us here.
if (redirected) if (redirectTo) {
_numPaths = 0;
return; return;
}
#endif #endif
if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) { if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {