2017-11-03 18:39:27 +00:00
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright ( C ) 2011 - 2015 ZeroTier , Inc .
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2017-12-14 16:53:24 +00:00
//#define ZT_CONTROLLER_USE_RETHINKDB
2017-11-03 18:39:27 +00:00
# ifdef ZT_CONTROLLER_USE_RETHINKDB
2017-12-08 17:39:20 +00:00
# include <stdio.h>
# include <stdlib.h>
2017-12-07 21:39:25 +00:00
# include <unistd.h>
2017-12-08 17:39:20 +00:00
# include <time.h>
2017-12-07 21:39:25 +00:00
2017-11-02 14:05:11 +00:00
# include "RethinkDB.hpp"
2017-11-03 18:39:27 +00:00
# include "EmbeddedNetworkController.hpp"
2017-11-02 14:05:11 +00:00
2017-11-09 22:01:16 +00:00
# include "../version.h"
2017-11-02 14:05:11 +00:00
# include <chrono>
# include <algorithm>
# include <stdexcept>
# include "../ext/librethinkdbxx/build/include/rethinkdb.h"
namespace R = RethinkDB ;
2017-11-03 18:39:27 +00:00
using json = nlohmann : : json ;
2017-11-02 14:05:11 +00:00
namespace ZeroTier {
2017-12-08 17:39:20 +00:00
static const char * _timestr ( )
{
time_t t = time ( 0 ) ;
char * ts = ctime ( & t ) ;
char * p = ts ;
2017-12-09 00:00:38 +00:00
if ( ! p )
return " " ;
2017-12-08 17:39:20 +00:00
while ( * p ) {
if ( * p = = ' \n ' ) {
* p = ( char ) 0 ;
break ;
}
+ + p ;
}
return ts ;
}
2017-12-07 21:39:25 +00:00
RethinkDB : : RethinkDB ( EmbeddedNetworkController * const nc , const Identity & myId , const char * path ) :
DB ( nc , myId , path ) ,
2017-11-03 18:39:27 +00:00
_ready ( 2 ) , // two tables need to be synchronized before we're ready, so this is ready when it reaches 0
_run ( 1 ) ,
_waitNoticePrinted ( false )
2017-11-02 14:05:11 +00:00
{
2017-11-07 22:44:46 +00:00
// rethinkdb:host:port:db[:auth]
2017-11-03 18:39:27 +00:00
std : : vector < std : : string > ps ( OSUtils : : split ( path , " : " , " " , " " ) ) ;
2017-11-04 00:55:16 +00:00
if ( ( ps . size ( ) < 4 ) | | ( ps [ 0 ] ! = " rethinkdb " ) )
2017-11-03 18:39:27 +00:00
throw std : : runtime_error ( " invalid rethinkdb database url " ) ;
_host = ps [ 1 ] ;
2017-11-04 00:55:16 +00:00
_port = Utils : : strToInt ( ps [ 2 ] . c_str ( ) ) ;
_db = ps [ 3 ] ;
if ( ps . size ( ) > 4 )
_auth = ps [ 4 ] ;
2017-11-03 18:39:27 +00:00
2017-11-02 14:05:11 +00:00
_readyLock . lock ( ) ;
_membersDbWatcher = std : : thread ( [ this ] ( ) {
2017-11-04 00:55:16 +00:00
try {
while ( _run = = 1 ) {
try {
std : : unique_ptr < R : : Connection > rdb ( R : : connect ( this - > _host , this - > _port , this - > _auth ) ) ;
if ( rdb ) {
_membersDbWatcherConnection = ( void * ) rdb . get ( ) ;
auto cur = R : : db ( this - > _db ) . table ( " Member " , R : : optargs ( " read_mode " , " outdated " ) ) . get_all ( this - > _myAddressStr , R : : optargs ( " index " , " controllerId " ) ) . changes ( R : : optargs ( " squash " , 0.05 , " include_initial " , true , " include_types " , true , " include_states " , true ) ) . run ( * rdb ) ;
while ( cur . has_next ( ) ) {
if ( _run ! = 1 ) break ;
json tmp ( json : : parse ( cur . next ( ) . as_json ( ) ) ) ;
if ( ( tmp [ " type " ] = = " state " ) & & ( tmp [ " state " ] = = " ready " ) ) {
if ( - - this - > _ready = = 0 ) {
if ( _waitNoticePrinted )
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] NOTICE: %.10llx controller RethinkDB data download complete. " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-11-04 00:55:16 +00:00
this - > _readyLock . unlock ( ) ;
}
} else {
try {
json & ov = tmp [ " old_val " ] ;
json & nv = tmp [ " new_val " ] ;
2018-03-09 06:34:51 +00:00
json oldConfig , newConfig ;
if ( ov . is_object ( ) ) oldConfig = ov [ " config " ] ;
if ( nv . is_object ( ) ) newConfig = nv [ " config " ] ;
if ( oldConfig . is_object ( ) | | newConfig . is_object ( ) )
this - > _memberChanged ( oldConfig , newConfig , ( this - > _ready < = 0 ) ) ;
2017-11-04 00:55:16 +00:00
} catch ( . . . ) { } // ignore bad records
2017-11-03 18:39:27 +00:00
}
2017-11-02 14:05:11 +00:00
}
}
2017-11-04 00:55:16 +00:00
} catch ( std : : exception & e ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (member change stream): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . what ( ) ) ;
2017-11-04 00:55:16 +00:00
} catch ( R : : Error & e ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (member change stream): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . message . c_str ( ) ) ;
2017-11-04 00:55:16 +00:00
} catch ( . . . ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (member change stream): unknown exception " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-11-02 14:05:11 +00:00
}
2017-11-04 00:55:16 +00:00
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 250 ) ) ;
2017-11-02 14:05:11 +00:00
}
2017-11-04 00:55:16 +00:00
} catch ( . . . ) { }
2017-11-02 14:05:11 +00:00
} ) ;
_networksDbWatcher = std : : thread ( [ this ] ( ) {
2017-11-04 00:55:16 +00:00
try {
while ( _run = = 1 ) {
try {
std : : unique_ptr < R : : Connection > rdb ( R : : connect ( this - > _host , this - > _port , this - > _auth ) ) ;
if ( rdb ) {
_networksDbWatcherConnection = ( void * ) rdb . get ( ) ;
auto cur = R : : db ( this - > _db ) . table ( " Network " , R : : optargs ( " read_mode " , " outdated " ) ) . get_all ( this - > _myAddressStr , R : : optargs ( " index " , " controllerId " ) ) . changes ( R : : optargs ( " squash " , 0.05 , " include_initial " , true , " include_types " , true , " include_states " , true ) ) . run ( * rdb ) ;
while ( cur . has_next ( ) ) {
if ( _run ! = 1 ) break ;
json tmp ( json : : parse ( cur . next ( ) . as_json ( ) ) ) ;
if ( ( tmp [ " type " ] = = " state " ) & & ( tmp [ " state " ] = = " ready " ) ) {
if ( - - this - > _ready = = 0 ) {
if ( _waitNoticePrinted )
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] NOTICE: %.10llx controller RethinkDB data download complete. " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-11-04 00:55:16 +00:00
this - > _readyLock . unlock ( ) ;
}
} else {
try {
json & ov = tmp [ " old_val " ] ;
json & nv = tmp [ " new_val " ] ;
2018-03-09 06:33:08 +00:00
json oldConfig , newConfig ;
if ( ov . is_object ( ) ) oldConfig = ov [ " config " ] ;
if ( nv . is_object ( ) ) newConfig = nv [ " config " ] ;
if ( oldConfig . is_object ( ) | | newConfig . is_object ( ) )
this - > _networkChanged ( oldConfig , newConfig , ( this - > _ready < = 0 ) ) ;
2017-11-04 00:55:16 +00:00
} catch ( . . . ) { } // ignore bad records
2017-11-03 18:39:27 +00:00
}
2017-11-02 14:05:11 +00:00
}
}
2017-11-04 00:55:16 +00:00
} catch ( std : : exception & e ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (network change stream): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . what ( ) ) ;
2017-11-04 00:55:16 +00:00
} catch ( R : : Error & e ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (network change stream): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . message . c_str ( ) ) ;
2017-11-04 00:55:16 +00:00
} catch ( . . . ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (network change stream): unknown exception " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-11-02 14:05:11 +00:00
}
2017-11-04 00:55:16 +00:00
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 250 ) ) ;
2017-11-02 14:05:11 +00:00
}
2017-11-04 00:55:16 +00:00
} catch ( . . . ) { }
2017-11-02 14:05:11 +00:00
} ) ;
2017-11-03 18:39:27 +00:00
for ( int t = 0 ; t < ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS ; + + t ) {
_commitThread [ t ] = std : : thread ( [ this ] ( ) {
2017-11-04 00:55:16 +00:00
try {
std : : unique_ptr < R : : Connection > rdb ;
nlohmann : : json * config = ( nlohmann : : json * ) 0 ;
while ( ( this - > _commitQueue . get ( config ) ) & & ( _run = = 1 ) ) {
if ( ! config )
continue ;
2017-12-14 16:53:24 +00:00
nlohmann : : json record ;
2017-12-09 00:00:38 +00:00
const char * table = ( const char * ) 0 ;
2017-11-04 00:55:16 +00:00
std : : string deleteId ;
2017-11-04 02:40:26 +00:00
try {
2017-12-09 00:00:38 +00:00
const std : : string objtype = ( * config ) [ " objtype " ] ;
2017-11-04 02:40:26 +00:00
if ( objtype = = " member " ) {
const std : : string nwid = ( * config ) [ " nwid " ] ;
const std : : string id = ( * config ) [ " id " ] ;
record [ " id " ] = nwid + " - " + id ;
record [ " controllerId " ] = this - > _myAddressStr ;
record [ " networkId " ] = nwid ;
record [ " nodeId " ] = id ;
record [ " config " ] = * config ;
table = " Member " ;
} else if ( objtype = = " network " ) {
const std : : string id = ( * config ) [ " id " ] ;
record [ " id " ] = id ;
record [ " controllerId " ] = this - > _myAddressStr ;
2017-12-14 16:53:24 +00:00
record [ " config " ] = * config ;
2017-11-04 02:40:26 +00:00
table = " Network " ;
2017-11-07 22:44:46 +00:00
} else if ( objtype = = " trace " ) {
record = * config ;
table = " RemoteTrace " ;
} else if ( objtype = = " _delete_network " ) {
2017-11-04 02:40:26 +00:00
deleteId = ( * config ) [ " id " ] ;
table = " Network " ;
2017-11-07 22:44:46 +00:00
} else if ( objtype = = " _delete_member " ) {
2017-11-04 02:40:26 +00:00
deleteId = ( * config ) [ " nwid " ] ;
deleteId . push_back ( ' - ' ) ;
const std : : string tmp = ( * config ) [ " id " ] ;
deleteId . append ( tmp ) ;
table = " Member " ;
}
2017-12-09 00:00:38 +00:00
} catch ( std : : exception & e ) {
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update record creation): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . what ( ) ) ;
table = ( const char * ) 0 ;
} catch ( R : : Error & e ) {
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update record creation): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . message . c_str ( ) ) ;
table = ( const char * ) 0 ;
2017-11-04 02:40:26 +00:00
} catch ( . . . ) {
2017-12-09 00:00:38 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update record creation): unknown exception " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
table = ( const char * ) 0 ;
2017-11-04 00:55:16 +00:00
}
2017-12-09 00:00:38 +00:00
delete config ;
if ( ! table )
continue ;
2018-01-12 18:38:19 +00:00
const std : : string jdump ( OSUtils : : jsonDump ( record , - 1 ) ) ;
2017-11-03 18:39:27 +00:00
2017-11-04 00:55:16 +00:00
while ( _run = = 1 ) {
try {
if ( ! rdb )
rdb = R : : connect ( this - > _host , this - > _port , this - > _auth ) ;
if ( rdb ) {
if ( deleteId . length ( ) > 0 ) {
2017-11-04 02:40:26 +00:00
//printf("DELETE: %s" ZT_EOL_S,deleteId.c_str());
2017-11-04 00:55:16 +00:00
R : : db ( this - > _db ) . table ( table ) . get ( deleteId ) . delete_ ( ) . run ( * rdb ) ;
} else {
2017-11-04 02:40:26 +00:00
//printf("UPSERT: %s" ZT_EOL_S,record.dump().c_str());
2018-01-12 18:38:19 +00:00
R : : db ( this - > _db ) . table ( table ) . insert ( R : : Datum : : from_json ( jdump ) , R : : optargs ( " conflict " , " update " , " return_changes " , false ) ) . run ( * rdb ) ;
2017-11-04 00:55:16 +00:00
}
break ;
2017-11-03 18:39:27 +00:00
} else {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update): connect failed (will retry) " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-12-09 00:00:38 +00:00
rdb . reset ( ) ;
2017-11-03 18:39:27 +00:00
}
2017-11-04 00:55:16 +00:00
} catch ( std : : exception & e ) {
2018-01-12 18:38:19 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update): %s [%s] " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . what ( ) , jdump . c_str ( ) ) ;
2017-11-04 00:55:16 +00:00
rdb . reset ( ) ;
} catch ( R : : Error & e ) {
2018-01-12 18:38:19 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update): %s [%s] " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . message . c_str ( ) , jdump . c_str ( ) ) ;
2017-11-04 00:55:16 +00:00
rdb . reset ( ) ;
} catch ( . . . ) {
2018-01-12 18:38:19 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update): unknown exception [%s] " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , jdump . c_str ( ) ) ;
2017-11-04 00:55:16 +00:00
rdb . reset ( ) ;
2017-11-03 18:39:27 +00:00
}
2017-11-04 00:55:16 +00:00
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 250 ) ) ;
2017-11-03 18:39:27 +00:00
}
}
2017-12-09 00:00:38 +00:00
} catch ( std : : exception & e ) {
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update outer loop): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . what ( ) ) ;
} catch ( R : : Error & e ) {
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update outer loop): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . message . c_str ( ) ) ;
} catch ( . . . ) {
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (insert/update outer loop): unknown exception " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
}
2017-11-03 18:39:27 +00:00
} ) ;
}
2017-11-04 00:55:16 +00:00
2017-11-08 19:06:14 +00:00
_onlineNotificationThread = std : : thread ( [ this ] ( ) {
2017-11-09 22:01:16 +00:00
int64_t lastUpdatedNetworkStatus = 0 ;
std : : unordered_map < std : : pair < uint64_t , uint64_t > , int64_t , _PairHasher > lastOnlineCumulative ;
2017-11-08 19:06:14 +00:00
try {
std : : unique_ptr < R : : Connection > rdb ;
while ( _run = = 1 ) {
try {
if ( ! rdb )
rdb = R : : connect ( this - > _host , this - > _port , this - > _auth ) ;
if ( rdb ) {
R : : Array batch ;
R : : Object tmpobj ;
2017-11-09 22:01:16 +00:00
std : : unordered_map < std : : pair < uint64_t , uint64_t > , std : : pair < int64_t , InetAddress > , _PairHasher > lastOnline ;
{
std : : lock_guard < std : : mutex > l ( _lastOnline_l ) ;
lastOnline . swap ( _lastOnline ) ;
}
for ( auto i = lastOnline . begin ( ) ; i ! = lastOnline . end ( ) ; + + i ) {
lastOnlineCumulative [ i - > first ] = i - > second . first ;
2017-11-09 01:19:46 +00:00
char tmp [ 64 ] , tmp2 [ 64 ] ;
2017-11-08 19:32:01 +00:00
OSUtils : : ztsnprintf ( tmp , sizeof ( tmp ) , " %.16llx-%.10llx " , i - > first . first , i - > first . second ) ;
tmpobj [ " id " ] = tmp ;
2017-11-09 01:19:46 +00:00
tmpobj [ " ts " ] = i - > second . first ;
tmpobj [ " phy " ] = i - > second . second . toIpString ( tmp2 ) ;
2017-11-08 19:06:14 +00:00
batch . emplace_back ( tmpobj ) ;
2018-01-12 18:38:19 +00:00
if ( batch . size ( ) > = 1024 ) {
2017-11-09 22:01:16 +00:00
R : : db ( this - > _db ) . table ( " MemberStatus " , R : : optargs ( " read_mode " , " outdated " ) ) . insert ( batch , R : : optargs ( " conflict " , " update " ) ) . run ( * rdb ) ;
batch . clear ( ) ;
}
}
if ( batch . size ( ) > 0 ) {
R : : db ( this - > _db ) . table ( " MemberStatus " , R : : optargs ( " read_mode " , " outdated " ) ) . insert ( batch , R : : optargs ( " conflict " , " update " ) ) . run ( * rdb ) ;
batch . clear ( ) ;
}
tmpobj . clear ( ) ;
const int64_t now = OSUtils : : now ( ) ;
if ( ( now - lastUpdatedNetworkStatus ) > 10000 ) {
lastUpdatedNetworkStatus = now ;
std : : vector < std : : pair < uint64_t , std : : shared_ptr < _Network > > > networks ;
{
std : : lock_guard < std : : mutex > l ( _networks_l ) ;
networks . reserve ( _networks . size ( ) + 1 ) ;
for ( auto i = _networks . begin ( ) ; i ! = _networks . end ( ) ; + + i )
networks . push_back ( * i ) ;
}
for ( auto i = networks . begin ( ) ; i ! = networks . end ( ) ; + + i ) {
char tmp [ 64 ] ;
Utils : : hex ( i - > first , tmp ) ;
tmpobj [ " id " ] = tmp ;
{
std : : lock_guard < std : : mutex > l2 ( i - > second - > lock ) ;
tmpobj [ " authorizedMemberCount " ] = i - > second - > authorizedMembers . size ( ) ;
tmpobj [ " totalMemberCount " ] = i - > second - > members . size ( ) ;
2017-12-05 00:21:56 +00:00
unsigned long onlineMemberCount = 0 ;
2017-11-09 22:01:16 +00:00
for ( auto m = i - > second - > members . begin ( ) ; m ! = i - > second - > members . end ( ) ; + + m ) {
auto lo = lastOnlineCumulative . find ( std : : pair < uint64_t , uint64_t > ( i - > first , m - > first ) ) ;
if ( lo ! = lastOnlineCumulative . end ( ) ) {
if ( ( now - lo - > second ) < = ( ZT_NETWORK_AUTOCONF_DELAY * 2 ) )
2017-12-05 00:21:56 +00:00
+ + onlineMemberCount ;
2017-11-09 22:01:16 +00:00
else lastOnlineCumulative . erase ( lo ) ;
}
}
2017-12-05 00:21:56 +00:00
tmpobj [ " onlineMemberCount " ] = onlineMemberCount ;
2017-11-09 22:01:16 +00:00
tmpobj [ " bridgeCount " ] = i - > second - > activeBridgeMembers . size ( ) ;
2017-12-05 00:21:56 +00:00
tmpobj [ " ts " ] = now ;
2017-11-09 22:01:16 +00:00
}
batch . emplace_back ( tmpobj ) ;
if ( batch . size ( ) > = 1024 ) {
R : : db ( this - > _db ) . table ( " NetworkStatus " , R : : optargs ( " read_mode " , " outdated " ) ) . insert ( batch , R : : optargs ( " conflict " , " update " ) ) . run ( * rdb ) ;
batch . clear ( ) ;
}
}
if ( batch . size ( ) > 0 ) {
R : : db ( this - > _db ) . table ( " NetworkStatus " , R : : optargs ( " read_mode " , " outdated " ) ) . insert ( batch , R : : optargs ( " conflict " , " update " ) ) . run ( * rdb ) ;
2017-11-08 19:06:14 +00:00
batch . clear ( ) ;
}
}
}
} catch ( std : : exception & e ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (node status update): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . what ( ) ) ;
2017-11-08 19:06:14 +00:00
rdb . reset ( ) ;
} catch ( R : : Error & e ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (node status update): %s " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) , e . message . c_str ( ) ) ;
2017-11-08 19:06:14 +00:00
rdb . reset ( ) ;
} catch ( . . . ) {
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] ERROR: %.10llx controller RethinkDB (node status update): unknown exception " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-11-08 19:06:14 +00:00
rdb . reset ( ) ;
}
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 250 ) ) ;
}
} catch ( . . . ) { }
} ) ;
2017-11-04 00:55:16 +00:00
_heartbeatThread = std : : thread ( [ this ] ( ) {
try {
2017-12-07 21:39:25 +00:00
R : : Object controllerRecord ;
2017-11-04 00:55:16 +00:00
std : : unique_ptr < R : : Connection > rdb ;
2017-12-07 21:39:25 +00:00
{
char publicId [ 1024 ] ;
2017-12-08 00:04:41 +00:00
//char secretId[1024];
2017-12-07 21:39:25 +00:00
char hostname [ 1024 ] ;
2017-12-07 21:45:43 +00:00
this - > _myId . toString ( false , publicId ) ;
2017-12-08 00:04:41 +00:00
//this->_myId.toString(true,secretId);
2017-12-07 21:39:25 +00:00
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 ;
2017-12-08 00:04:41 +00:00
//controllerRecord["secretIdentity"] = secretId;
2017-12-07 21:39:25 +00:00
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 ;
}
2017-11-04 00:55:16 +00:00
while ( _run = = 1 ) {
try {
if ( ! rdb )
rdb = R : : connect ( this - > _host , this - > _port , this - > _auth ) ;
if ( rdb ) {
2017-12-07 21:39:25 +00:00
controllerRecord [ " lastAlive " ] = OSUtils : : now ( ) ;
2017-11-04 02:40:26 +00:00
//printf("HEARTBEAT: %s" ZT_EOL_S,tmp);
2017-12-07 21:39:25 +00:00
R : : db ( this - > _db ) . table ( " Controller " , R : : optargs ( " read_mode " , " outdated " ) ) . insert ( controllerRecord , R : : optargs ( " conflict " , " update " ) ) . run ( * rdb ) ;
2017-11-04 00:55:16 +00:00
}
} catch ( . . . ) {
rdb . reset ( ) ;
}
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 1000 ) ) ;
}
} catch ( . . . ) { }
} ) ;
2017-11-02 14:05:11 +00:00
}
RethinkDB : : ~ RethinkDB ( )
{
_run = 0 ;
2017-11-03 18:39:27 +00:00
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 100 ) ) ;
_commitQueue . stop ( ) ;
for ( int t = 0 ; t < ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS ; + + t )
_commitThread [ t ] . join ( ) ;
2017-11-04 02:40:26 +00:00
if ( _membersDbWatcherConnection )
( ( R : : Connection * ) _membersDbWatcherConnection ) - > close ( ) ;
if ( _networksDbWatcherConnection )
( ( R : : Connection * ) _networksDbWatcherConnection ) - > close ( ) ;
_membersDbWatcher . join ( ) ;
_networksDbWatcher . join ( ) ;
2017-11-04 00:55:16 +00:00
_heartbeatThread . join ( ) ;
2017-11-08 19:06:14 +00:00
_onlineNotificationThread . join ( ) ;
2017-11-02 14:05:11 +00:00
}
2017-11-08 19:06:14 +00:00
bool RethinkDB : : waitForReady ( )
2017-11-02 14:05:11 +00:00
{
2017-11-07 22:44:46 +00:00
while ( _ready > 0 ) {
if ( ! _waitNoticePrinted ) {
_waitNoticePrinted = true ;
2017-12-08 17:39:20 +00:00
fprintf ( stderr , " [%s] NOTICE: %.10llx controller RethinkDB waiting for initial data download... " ZT_EOL_S , _timestr ( ) , ( unsigned long long ) _myAddress . toInt ( ) ) ;
2017-11-07 22:44:46 +00:00
}
_readyLock . lock ( ) ;
_readyLock . unlock ( ) ;
2017-11-04 02:40:26 +00:00
}
2017-11-08 19:06:14 +00:00
return true ;
2017-11-03 18:39:27 +00:00
}
2017-11-08 19:06:14 +00:00
void RethinkDB : : save ( nlohmann : : json * orig , nlohmann : : json & record )
2017-11-03 18:39:27 +00:00
{
2017-11-08 19:06:14 +00:00
if ( ! record . is_object ( ) ) // sanity check
return ;
2017-11-03 18:39:27 +00:00
waitForReady ( ) ;
2017-11-08 19:06:14 +00:00
if ( orig ) {
if ( * orig ! = record ) {
2017-12-14 16:53:24 +00:00
record [ " revision " ] = OSUtils : : jsonInt ( record [ " revision " ] , 0ULL ) + 1 ;
_commitQueue . post ( new nlohmann : : json ( record ) ) ;
2017-11-08 19:06:14 +00:00
}
} else {
record [ " revision " ] = 1 ;
_commitQueue . post ( new nlohmann : : json ( record ) ) ;
}
2017-11-03 18:39:27 +00:00
}
void RethinkDB : : eraseNetwork ( const uint64_t networkId )
{
char tmp2 [ 24 ] ;
waitForReady ( ) ;
Utils : : hex ( networkId , tmp2 ) ;
2017-11-03 20:59:36 +00:00
json * tmp = new json ( ) ;
( * tmp ) [ " id " ] = tmp2 ;
2017-11-07 22:44:46 +00:00
( * tmp ) [ " objtype " ] = " _delete_network " ; // pseudo-type, tells thread to delete network
2017-11-03 20:59:36 +00:00
_commitQueue . post ( tmp ) ;
2017-11-03 18:39:27 +00:00
}
void RethinkDB : : eraseMember ( const uint64_t networkId , const uint64_t memberId )
{
char tmp2 [ 24 ] ;
2017-11-03 20:59:36 +00:00
json * tmp = new json ( ) ;
2017-11-03 18:39:27 +00:00
waitForReady ( ) ;
Utils : : hex ( networkId , tmp2 ) ;
2017-11-03 20:59:36 +00:00
( * tmp ) [ " nwid " ] = tmp2 ;
2017-11-03 18:39:27 +00:00
Utils : : hex10 ( memberId , tmp2 ) ;
2017-11-03 20:59:36 +00:00
( * tmp ) [ " id " ] = tmp2 ;
2017-11-07 22:44:46 +00:00
( * tmp ) [ " objtype " ] = " _delete_member " ; // pseudo-type, tells thread to delete network
2017-11-03 20:59:36 +00:00
_commitQueue . post ( tmp ) ;
2017-11-03 18:39:27 +00:00
}
2017-11-09 01:19:46 +00:00
void RethinkDB : : nodeIsOnline ( const uint64_t networkId , const uint64_t memberId , const InetAddress & physicalAddress )
2017-11-08 19:06:14 +00:00
{
std : : lock_guard < std : : mutex > l ( _lastOnline_l ) ;
2017-11-09 01:19:46 +00:00
std : : pair < int64_t , InetAddress > & i = _lastOnline [ std : : pair < uint64_t , uint64_t > ( networkId , memberId ) ] ;
i . first = OSUtils : : now ( ) ;
if ( physicalAddress )
i . second = physicalAddress ;
2017-11-08 19:06:14 +00:00
}
2017-11-02 14:05:11 +00:00
} // namespace ZeroTier
2017-11-03 18:39:27 +00:00
# endif // ZT_CONTROLLER_USE_RETHINKDB