Re-integrate in-filesystem DB into new controller DB structure.

This commit is contained in:
Adam Ierymenko 2017-11-07 14:44:46 -08:00
parent 7fc9094d8e
commit 1613f42d00
11 changed files with 654 additions and 362 deletions

View file

@ -16,112 +16,35 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define ZT_CONTROLLER_USE_RETHINKDB
#ifdef ZT_CONTROLLER_USE_RETHINKDB
#ifndef ZT_CONTROLLER_RETHINKDB_HPP
#define ZT_CONTROLLER_RETHINKDB_HPP
#include "../node/Constants.hpp"
#include "../node/Address.hpp"
#include "../node/InetAddress.hpp"
#include "../osdep/OSUtils.hpp"
#include "../osdep/BlockingQueue.hpp"
#include <memory>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <atomic>
#include "../ext/json/json.hpp"
#include "DB.hpp"
#define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 2
namespace ZeroTier
{
class EmbeddedNetworkController;
class RethinkDB
class RethinkDB : public DB
{
public:
struct NetworkSummaryInfo
{
NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
std::vector<Address> activeBridges;
std::vector<InetAddress> allocatedIps;
unsigned long authorizedMemberCount;
unsigned long totalMemberCount;
int64_t mostRecentDeauthTime;
};
RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
~RethinkDB();
virtual ~RethinkDB();
inline void waitForReady() const
{
while (_ready > 0) {
if (!_waitNoticePrinted) {
_waitNoticePrinted = true;
fprintf(stderr,"NOTICE: controller RethinkDB waiting for initial data download..." ZT_EOL_S);
}
_readyLock.lock();
_readyLock.unlock();
}
}
virtual void waitForReady() const;
inline bool hasNetwork(const uint64_t networkId) const
{
std::lock_guard<std::mutex> l(_networks_l);
return (_networks.find(networkId) != _networks.end());
}
virtual void save(const nlohmann::json &record);
bool get(const uint64_t networkId,nlohmann::json &network);
bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
virtual void eraseNetwork(const uint64_t networkId);
bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
virtual void eraseMember(const uint64_t networkId,const uint64_t memberId);
void networks(std::vector<uint64_t> &networks);
void save(const nlohmann::json &record);
void eraseNetwork(const uint64_t networkId);
void eraseMember(const uint64_t networkId,const uint64_t memberId);
private:
struct _Network
{
_Network() : mostRecentDeauthTime(0) {}
nlohmann::json config;
std::unordered_map<uint64_t,nlohmann::json> members;
std::unordered_set<uint64_t> activeBridgeMembers;
std::unordered_set<uint64_t> authorizedMembers;
std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
int64_t mostRecentDeauthTime;
std::mutex lock;
};
void _memberChanged(nlohmann::json &old,nlohmann::json &member);
void _networkChanged(nlohmann::json &old,nlohmann::json &network);
inline void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
{
for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab)
info.activeBridges.push_back(Address(*ab));
for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
info.allocatedIps.push_back(*ip);
info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
info.totalMemberCount = (unsigned long)nw->members.size();
info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
}
EmbeddedNetworkController *const _controller;
const Address _myAddress;
std::string _myAddressStr;
protected:
std::string _host;
std::string _db;
std::string _auth;
@ -132,10 +55,6 @@ private:
std::thread _networksDbWatcher;
std::thread _membersDbWatcher;
std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
mutable std::mutex _networks_l;
BlockingQueue< nlohmann::json * > _commitQueue;
std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];