From 16e20a9e79791c947b56c2acd90b3436558e708d Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 15 Jul 2019 12:56:09 -0400 Subject: [PATCH] try fixing bootstrap --- llarp/context.cpp | 3 +- llarp/dht/context.cpp | 2 +- llarp/router/router.cpp | 107 +++++++++++++++++++++++++++------------ llarp/router/router.hpp | 8 +++ llarp/router_contact.cpp | 19 +++++-- llarp/router_contact.hpp | 9 ++++ 6 files changed, 110 insertions(+), 38 deletions(-) diff --git a/llarp/context.cpp b/llarp/context.cpp index b9b95ebc6..a3deb4c9f 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -61,7 +61,8 @@ namespace llarp auto threads = config->router.workerThreads(); if(threads <= 0) threads = 1; - worker = std::make_shared< llarp::thread::ThreadPool >(threads, 1024, "llarp-worker"); + worker = std::make_shared< llarp::thread::ThreadPool >(threads, 1024, + "llarp-worker"); nodedb_dir = config->netdb.nodedbDir(); diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 0188a4bbb..f96c26890 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -645,7 +645,7 @@ namespace llarp // ourKey should never be in the connected list // requester is likely in the connected list // 4 or connection nodes (minus a potential requestor), whatever is less - if(!_nodes->GetManyNearExcluding(t, found, 1, + if(!_nodes->GetManyNearExcluding(t, found, nodeCount >= 4 ? 4 : 1, std::set< Key_t >{ourKey, requester})) { llarp::LogError( diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 0bb99be30..a06463097 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -920,7 +920,6 @@ namespace llarp std::make_unique< FileLogStream >(diskworker(), logfile, 100, true); } - netConfig.insert(conf->dns.netConfig.begin(), conf->dns.netConfig.end()); std::vector< std::string > configRouters = conf->connect.routers; @@ -1074,6 +1073,33 @@ namespace llarp > 0; } + bool + Router::ShouldReportStats(llarp_time_t now) const + { + static constexpr llarp_time_t ReportStatsInterval = 30 * 1000; + return now - m_LastStatsReport > ReportStatsInterval; + } + + void + Router::ReportStats() + { + const auto now = Now(); + LogInfo("---- BEGIN REPORT ----"); + LogInfo(nodedb()->num_loaded(), " RCs loaded"); + LogInfo(bootstrapRCList.size(), " bootstrap peers"); + LogInfo(NumberOfConnectedRouters(), " router connections"); + if(IsServiceNode()) + { + LogInfo(NumberOfConnectedClients(), " client connections"); + LogInfo(_rc.Age(now), " ms since we last updated our RC"); + LogInfo(_rc.TimeUntilExpires(now), " ms until our RC expires"); + } + LogInfo(now, " system time"); + LogInfo(m_LastStatsReport, " last reported stats"); + LogInfo("----- END REPORT -----"); + m_LastStatsReport = now; + } + void Router::Tick() { @@ -1084,7 +1110,51 @@ namespace llarp routerProfiling().Tick(); - if(IsServiceNode()) + if(ShouldReportStats(now)) + { + ReportStats(); + } + + const auto insertRouters = [&](const std::vector< RouterContact > &res) { + // store found routers + for(const auto &rc : res) + { + // don't accept expired RCs + if(rc.Verify(Now(), false)) + nodedb()->InsertAsync(rc); + } + }; + + const bool isSvcNode = IsServiceNode(); + + // try looking up stale routers + nodedb()->VisitInsertedBefore( + [&](const RouterContact &rc) { + if(HasPendingRouterLookup(rc.pubkey)) + return; + LookupRouter(rc.pubkey, insertRouters); + }, + now - RouterContact::UpdateInterval); + + std::set< RouterID > removeStale; + // remove stale routers + const auto timeout = isSvcNode + ? RouterContact::Lifetime / 8 + : RouterContact::UpdateWindow * RouterContact::UpdateTries; + nodedb()->VisitInsertedBefore( + [&](const RouterContact &rc) { + if(IsBootstrapNode(rc.pubkey)) + return; + LogInfo("removing stale router: ", RouterID(rc.pubkey)); + removeStale.insert(rc.pubkey); + }, + now - timeout); + + nodedb()->RemoveIf([removeStale](const RouterContact &rc) -> bool { + return removeStale.count(rc.pubkey) > 0; + }); + + if(isSvcNode) { if(_rc.ExpiresSoon(now, randint() % 10000) || (now - _rc.last_updated) > rcRegenInterval) @@ -1103,36 +1173,7 @@ namespace llarp }); */ } - else - { - // try looking up stale routers - nodedb()->VisitInsertedBefore( - [&](const RouterContact &rc) { - if(HasPendingRouterLookup(rc.pubkey)) - return; - LookupRouter(rc.pubkey, - [&](const std::vector< RouterContact > &result) { - // store found routers - for(const auto &rc : result) - nodedb()->InsertAsync(rc); - }); - }, - now - RouterContact::UpdateInterval); - std::set< RouterID > removeStale; - // remove stale routers - nodedb()->VisitInsertedBefore( - [&](const RouterContact &rc) { - if(IsBootstrapNode(rc.pubkey)) - return; - removeStale.insert(rc.pubkey); - }, - now - ((RouterContact::UpdateInterval * 3) / 2)); - - nodedb()->RemoveIf([removeStale](const RouterContact &rc) -> bool { - return removeStale.count(rc.pubkey) > 0; - }); - } - // expire transit paths + // expire paths paths.ExpirePaths(now); { auto itr = pendingEstablishJobs.begin(); @@ -1215,7 +1256,7 @@ namespace llarp ConnectToRandomRouters(dlt); } - if(!IsServiceNode()) + if(!isSvcNode) { _hiddenServiceContext.Tick(now); } diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 9044c0ae1..6c1c880fa 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -549,6 +549,14 @@ namespace llarp std::atomic< bool > _stopping; std::atomic< bool > _running; + llarp_time_t m_LastStatsReport = 0; + + bool + ShouldReportStats(llarp_time_t now) const; + + void + ReportStats(); + bool UpdateOurRC(bool rotateKeys = false); diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index a2c636980..d5710d04d 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -36,6 +36,8 @@ namespace llarp llarp_time_t RouterContact::UpdateInterval = 30 * 60 * 1000; // 1 minute window for update llarp_time_t RouterContact::UpdateWindow = 60 * 1000; + // try 5 times to update + int RouterContact::UpdateTries = 5; NetID::NetID(const byte_t *val) : AlignedBuffer< 8 >() { @@ -235,16 +237,27 @@ namespace llarp bool RouterContact::IsExpired(llarp_time_t now) const + { + return Age(now) >= Lifetime; + } + + llarp_time_t + RouterContact::TimeUntilExpires(llarp_time_t now) const { const auto expiresAt = last_updated + Lifetime; - return now >= expiresAt; + return now < expiresAt ? expiresAt - now : 0; + } + + llarp_time_t + RouterContact::Age(llarp_time_t now) const + { + return now > last_updated ? now - last_updated : 0; } bool RouterContact::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const { - const auto expiresAt = last_updated + Lifetime; - return expiresAt <= now || expiresAt - now <= dlt; + return TimeUntilExpires(now) <= dlt; } std::string diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index f8586f7bf..75d20641f 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -71,6 +71,7 @@ namespace llarp static llarp_time_t Lifetime; static llarp_time_t UpdateInterval; static llarp_time_t UpdateWindow; + static int UpdateTries; RouterContact() { @@ -179,6 +180,14 @@ namespace llarp bool IsExpired(llarp_time_t now) const; + /// returns time in ms until we expire or 0 if we have expired + llarp_time_t + TimeUntilExpires(llarp_time_t now) const; + + /// get the age of this RC in ms + llarp_time_t + Age(llarp_time_t now) const; + bool OtherIsNewer(const RouterContact &other) const {