diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2019-08-08 17:20:50 -0500 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2019-08-08 17:20:50 -0500 |
commit | 55087521f6db9aaa874cccd86e424d77e6c820c3 (patch) | |
tree | fdc87b595d29c8eb383ab85aa0173259c0558d25 /controller/DBMirrorSet.cpp | |
parent | 760ae07d7243b8e7ff6ec03cf508058c287c219f (diff) | |
download | infinitytier-55087521f6db9aaa874cccd86e424d77e6c820c3.tar.gz infinitytier-55087521f6db9aaa874cccd86e424d77e6c820c3.zip |
Add periodic sync check when syncing LF<>another DB
Diffstat (limited to 'controller/DBMirrorSet.cpp')
-rw-r--r-- | controller/DBMirrorSet.cpp | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/controller/DBMirrorSet.cpp b/controller/DBMirrorSet.cpp index 5b491216..852e70f3 100644 --- a/controller/DBMirrorSet.cpp +++ b/controller/DBMirrorSet.cpp @@ -29,8 +29,52 @@ namespace ZeroTier { DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener) : - _listener(listener) -{ + _listener(listener), + _running(true) +{ + _syncCheckerThread = std::thread([this]() { + for(;;) { + for(int i=0;i<120;++i) { // 1 minute delay between checks + if (!_running) + return; + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + + std::vector< std::shared_ptr<DB> > dbs; + { + std::lock_guard<std::mutex> l(_dbs_l); + if (_dbs.size() <= 1) + continue; // no need to do this if there's only one DB, so skip the iteration + dbs = _dbs; + } + + for(auto db=dbs.begin();db!=dbs.end();++db) { + (*db)->each([this,&dbs,&db](uint64_t networkId,const nlohmann::json &network,uint64_t memberId,const nlohmann::json &member) { + if (memberId == 0) { + for(auto db2=dbs.begin();db2!=dbs.end();++db2) { + if (db->get() != db2->get()) { + nlohmann::json nw2; + if ((!(*db2)->get(networkId,nw2))||(OSUtils::jsonInt(nw2["revision"],0) < OSUtils::jsonInt(network["revision"],0))) { + nw2 = network; + (*db2)->save(nw2,false); + } + } + } + } else { + for(auto db2=dbs.begin();db2!=dbs.end();++db2) { + if (db->get() != db2->get()) { + nlohmann::json nw2,m2; + if ((!(*db2)->get(networkId,nw2,memberId,m2))||(OSUtils::jsonInt(nw2["revision"],0) < OSUtils::jsonInt(network["revision"],0))) { + m2 = member; + (*db2)->save(m2,false); + } + } + } + } + }); + } + } + }); } DBMirrorSet::~DBMirrorSet() |