Use foo.snode as peerstats unique id, test file-backed db

pull/1312/head
Stephen Shelton 4 years ago
parent a30806b375
commit cc6e9c882a
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -16,7 +16,7 @@ namespace llarp
file,
make_table(
"peerstats",
make_column("routerId", &PeerStats::routerIdHex, primary_key(), unique()),
make_column("routerId", &PeerStats::routerId, primary_key(), unique()),
make_column("numConnectionAttempts", &PeerStats::numConnectionAttempts),
make_column("numConnectionSuccesses", &PeerStats::numConnectionSuccesses),
make_column("numConnectionRejections", &PeerStats::numConnectionRejections),

@ -19,10 +19,29 @@ namespace llarp
// use if file is an empty-optional
std::string fileString;
if (file.has_value())
{
fileString = file.value().native();
LogInfo("Loading PeerDb from file ", fileString);
}
else
{
LogInfo("Loading memory-backed PeerDb");
}
m_storage = std::make_unique<PeerDbStorage>(initStorage(fileString));
m_storage->sync_schema(true); // true for "preserve" as in "don't nuke" (how cute!)
auto allStats = m_storage->get_all<PeerStats>();
LogInfo("Loading ", allStats.size(), " PeerStats from table peerstats...");
for (const PeerStats& stats : allStats)
{
RouterID id;
if (not id.FromString(stats.routerId))
throw std::runtime_error(
stringify("Database contains invalid PeerStats with id ", stats.routerId));
m_peerStats[id] = stats;
}
}
void
@ -41,8 +60,8 @@ namespace llarp
for (const auto& entry : copy)
{
// call me paranoid...
assert(not entry.second.routerIdHex.empty());
assert(entry.first.ToHex() == entry.second.routerIdHex);
assert(not entry.second.routerId.empty());
assert(entry.first.ToString() == entry.second.routerId);
m_storage->replace(entry.second);
}
@ -51,9 +70,9 @@ namespace llarp
void
PeerDb::accumulatePeerStats(const RouterID& routerId, const PeerStats& delta)
{
if (routerId.ToHex() != delta.routerIdHex)
if (routerId.ToString() != delta.routerId)
throw std::invalid_argument(
stringify("routerId ", routerId, " doesn't match ", delta.routerIdHex));
stringify("routerId ", routerId, " doesn't match ", delta.routerId));
std::lock_guard gaurd(m_statsLock);
auto itr = m_peerStats.find(routerId);

@ -2,9 +2,11 @@
namespace llarp
{
PeerStats::PeerStats(const RouterID& routerId)
PeerStats::PeerStats() = default;
PeerStats::PeerStats(const RouterID& routerId_)
{
routerIdHex = routerId.ToHex();
routerId = routerId_.ToString();
}
PeerStats&
@ -35,7 +37,7 @@ namespace llarp
bool
PeerStats::operator==(const PeerStats& other)
{
return routerIdHex == other.routerIdHex and numConnectionAttempts == other.numConnectionAttempts
return routerId == other.routerId and numConnectionAttempts == other.numConnectionAttempts
and numConnectionSuccesses == other.numConnectionSuccesses
and numConnectionRejections == other.numConnectionRejections
and numConnectionTimeouts == other.numConnectionTimeouts

@ -13,7 +13,7 @@ namespace llarp
// Struct containing stats we know about a peer
struct PeerStats
{
std::string routerIdHex;
std::string routerId;
int32_t numConnectionAttempts = 0;
int32_t numConnectionSuccesses = 0;
@ -33,6 +33,7 @@ namespace llarp
int64_t longestRCReceiveIntervalMs = 0;
int64_t mostExpiredRCMs = 0;
PeerStats();
PeerStats(const RouterID& routerId);
PeerStats&

@ -59,18 +59,31 @@ TEST_CASE("Test PeerDb nukes stats on load", "[PeerDb]")
CHECK(db.getCurrentPeerStats(id).has_value() == false);
}
/*
TEST_CASE("Test file-backed database", "[PeerDb]")
TEST_CASE("Test PeerDb file-backed database reloads properly", "[PeerDb]")
{
llarp::PeerDb db;
db.loadDatabase("/tmp/peerdb_test_tmp.db.sqlite");
const std::string filename = "/tmp/peerdb_test_tmp2.db.sqlite";
const llarp::RouterID id = llarp::test::makeBuf<llarp::RouterID>(0x02);
const llarp::RouterID id = llarp::test::makeBuf<llarp::RouterID>(0x01);
llarp::PeerStats stats(id);
stats.numConnectionAttempts = 42;
{
llarp::PeerDb db;
db.loadDatabase(filename);
db.accumulatePeerStats(id, stats);
llarp::PeerStats stats(id);
stats.numConnectionAttempts = 43;
db.accumulatePeerStats(id, stats);
db.flushDatabase();
}
{
llarp::PeerDb db;
db.loadDatabase(filename);
auto stats = db.getCurrentPeerStats(id);
CHECK(stats.has_value() == true);
CHECK(stats.value().numConnectionAttempts == 43);
}
db.flushDatabase();
fs::remove(filename);
}
*/

Loading…
Cancel
Save