mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
Revert "Revert "Use serverID in HostAndPort::isSelf SERVER-1515""
This reverts commit 3d1faf35fc
.
This commit is contained in:
parent
40bcd07cbf
commit
9dc8c7d008
@ -26,6 +26,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include "../db/cmdline.h"
|
||||
#include "../db/commands.h"
|
||||
#include "../client/dbclient.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
@ -716,6 +717,29 @@ namespace mongo {
|
||||
map<string, bool> isSelfCache; // host, isSelf
|
||||
}
|
||||
|
||||
const OID& getServerID(){
|
||||
static OID serverID;
|
||||
ONCE {
|
||||
serverID.init();
|
||||
}
|
||||
return serverID;
|
||||
}
|
||||
|
||||
class ServerIDCommand : public Command {
|
||||
public:
|
||||
ServerIDCommand() : Command( "serverID" ) { }
|
||||
virtual bool slaveOk() const { return true; }
|
||||
virtual LockType locktype() const { return NONE; }
|
||||
virtual void help(stringstream& h) const { h << "internal"; }
|
||||
virtual bool adminOnly() const { return true; }
|
||||
|
||||
virtual bool run(const string& db, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
|
||||
result.append("serverID", getServerID());
|
||||
return 1;
|
||||
}
|
||||
|
||||
} serverIDCommand;
|
||||
|
||||
bool HostAndPort::isSelf() const {
|
||||
int p = _port == -1 ? CmdLine::DefaultDBPort : _port;
|
||||
|
||||
@ -729,15 +753,22 @@ namespace mongo {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
SockAddr addr (_host.c_str(), 0); // port 0 is dynamically assigned
|
||||
SOCKET sock = ::socket(addr.getType(), SOCK_STREAM, 0);
|
||||
assert(sock != INVALID_SOCKET);
|
||||
bool ret = false;
|
||||
|
||||
try {
|
||||
DBClientConnection c (false, NULL, 0.001); // 1ms timeout
|
||||
c.connect(this->toString());
|
||||
|
||||
BSONObj out;
|
||||
if (c.runCommand("admin", BSON("serverID"<<1), out) && out["serverID"].OID() == getServerID()){
|
||||
ret = true;
|
||||
}
|
||||
} catch (...) {
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
bool ret = (::bind(sock, addr.raw(), addr.addressSize) == 0);
|
||||
isSelfCache[_host] = ret;
|
||||
|
||||
closesocket(sock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user