docs, and make RethinkDB controller DB driver upsert into the Controller DB and also update the hostname field.

This commit is contained in:
Adam Ierymenko 2017-12-07 13:39:25 -08:00
parent ada611d597
commit 8d9464c414
8 changed files with 60 additions and 15 deletions

View file

@ -27,9 +27,10 @@ using json = nlohmann::json;
namespace ZeroTier {
DB::DB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) :
DB::DB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
_controller(nc),
_myAddress(myAddress),
_myId(myId),
_myAddress(myId.address()),
_path((path) ? path : "")
{
char tmp[32];

View file

@ -20,7 +20,7 @@
#define ZT_CONTROLLER_DB_HPP
#include "../node/Constants.hpp"
#include "../node/Address.hpp"
#include "../node/Identity.hpp"
#include "../node/InetAddress.hpp"
#include "../osdep/OSUtils.hpp"
#include "../osdep/BlockingQueue.hpp"
@ -58,7 +58,7 @@ public:
int64_t mostRecentDeauthTime;
};
DB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
DB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path);
virtual ~DB();
virtual bool waitForReady() = 0;
@ -104,6 +104,7 @@ protected:
void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
EmbeddedNetworkController *const _controller;
const Identity _myId;
const Address _myAddress;
const std::string _path;
std::string _myAddressStr;

View file

@ -477,10 +477,10 @@ void EmbeddedNetworkController::init(const Identity &signingId,Sender *sender)
_signingIdAddressString = signingId.address().toString(tmp);
#ifdef ZT_CONTROLLER_USE_RETHINKDB
if ((_path.length() > 10)&&(_path.substr(0,10) == "rethinkdb:"))
_db.reset(new RethinkDB(this,_signingId.address(),_path.c_str()));
_db.reset(new RethinkDB(this,_signingId,_path.c_str()));
else // else use FileDB after endif
#endif
_db.reset(new FileDB(this,_signingId.address(),_path.c_str()));
_db.reset(new FileDB(this,_signingId,_path.c_str()));
_db->waitForReady();
}

View file

@ -21,8 +21,8 @@
namespace ZeroTier
{
FileDB::FileDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) :
DB(nc,myAddress,path),
FileDB::FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
DB(nc,myId,path),
_networksPath(_path + ZT_PATH_SEPARATOR_S + "network")
{
OSUtils::mkdir(_path.c_str());

View file

@ -27,7 +27,7 @@ namespace ZeroTier
class FileDB : public DB
{
public:
FileDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path);
virtual ~FileDB();
virtual bool waitForReady();

View file

@ -18,6 +18,8 @@
#ifdef ZT_CONTROLLER_USE_RETHINKDB
#include <unistd.h>
#include "RethinkDB.hpp"
#include "EmbeddedNetworkController.hpp"
@ -34,8 +36,8 @@ using json = nlohmann::json;
namespace ZeroTier {
RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) :
DB(nc,myAddress,path),
RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
DB(nc,myId,path),
_ready(2), // two tables need to be synchronized before we're ready, so this is ready when it reaches 0
_run(1),
_waitNoticePrinted(false)
@ -317,16 +319,44 @@ RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddres
_heartbeatThread = std::thread([this]() {
try {
char tmp[1024];
R::Object controllerRecord;
std::unique_ptr<R::Connection> rdb;
{
char publicId[1024];
char secretId[1024];
char hostname[1024];
this->_myId.toString(publicId,false);
this->_myId.toString(secretId,true);
if (gethostname(hostname,sizeof(hostname)) != 0) {
hostname[0] = (char)0;
} else {
for(int i=0;i<sizeof(hostname);++i) {
if ((hostname[i] == '.')||(hostname[i] == 0)) {
hostname[i] = (char)0;
break;
}
}
}
controllerRecord["id"] = this->_myAddressStr.c_str();
controllerRecord["publicIdentity"] = publicId;
controllerRecord["secretIdentity"] = secretId;
if (hostname[0])
controllerRecord["clusterHost"] = hostname;
controllerRecord["vMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
controllerRecord["vMinor"] = ZEROTIER_ONE_VERSION_MINOR;
controllerRecord["vRev"] = ZEROTIER_ONE_VERSION_REVISION;
controllerRecord["vBuild"] = ZEROTIER_ONE_VERSION_BUILD;
}
while (_run == 1) {
try {
if (!rdb)
rdb = R::connect(this->_host,this->_port,this->_auth);
if (rdb) {
OSUtils::ztsnprintf(tmp,sizeof(tmp),"{\"id\":\"%s\",\"lastAlive\":%lld,\"version\":\"%d.%d.%d\"}",this->_myAddressStr.c_str(),(long long)OSUtils::now(),ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
controllerRecord["lastAlive"] = OSUtils::now();
//printf("HEARTBEAT: %s" ZT_EOL_S,tmp);
R::db(this->_db).table("Controller").update(R::Datum::from_json(tmp)).run(*rdb);
R::db(this->_db).table("Controller",R::optargs("read_mode","outdated")).insert(controllerRecord,R::optargs("conflict","update")).run(*rdb);
}
} catch ( ... ) {
rdb.reset();

View file

@ -37,7 +37,7 @@ namespace ZeroTier
class RethinkDB : public DB
{
public:
RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
RethinkDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path);
virtual ~RethinkDB();
virtual bool waitForReady();