misc bugfixes

This commit is contained in:
Grant Limberg 2025-09-06 12:37:28 -07:00
parent 7ad264df92
commit 3d72a43a19
No known key found for this signature in database
GPG key ID: 8F2F97D3BE8D7735
8 changed files with 130 additions and 23 deletions

View file

@ -31,12 +31,22 @@ BigTableStatusWriter::BigTableStatusWriter(
, _instance_id(instance_id)
, _table_id(table_id)
, _pubsubWriter(pubsubWriter)
, _table(nullptr)
{
_table = new cbt::Table(cbt::MakeDataConnection(), cbt::TableResource(_project_id, _instance_id, _table_id));
fprintf(
stderr, "BigTableStatusWriter for project %s instance %s table %s\n", project_id.c_str(), instance_id.c_str(),
table_id.c_str());
}
BigTableStatusWriter::~BigTableStatusWriter()
{
writePending();
if (_table != nullptr) {
delete _table;
_table = nullptr;
}
}
void BigTableStatusWriter::updateNodeStatus(
@ -79,11 +89,8 @@ void BigTableStatusWriter::writePending()
if (toWrite.empty()) {
return;
}
fprintf(stderr, "Writing %zu pending status entries to BigTable\n", toWrite.size());
namespace cbt = google::cloud::bigtable;
cbt::Table table(cbt::MakeDataConnection(), cbt::TableResource(_project_id, _instance_id, _table_id));
cbt::BulkMutation bulk;
for (const auto& entry : toWrite) {
std::string row_key = entry.network_id + "#" + entry.node_id;
cbt::SingleRowMutation m(row_key);
@ -91,26 +98,74 @@ void BigTableStatusWriter::writePending()
m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, archColumn, entry.arch));
m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, versionColumn, entry.version));
char buf[64] = { 0 };
std::string addressStr = entry.address.toString(buf);
if (entry.address.ss_family == AF_INET) {
m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv4Column, entry.address.toString(buf)));
m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv4Column, std::move(addressStr)));
}
else if (entry.address.ss_family == AF_INET6) {
m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv6Column, entry.address.toString(buf)));
m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv6Column, std::move(addressStr)));
}
int64_t ts = entry.last_seen;
m.emplace_back(cbt::SetCell(checkInColumnFamily, lastSeenColumn, std::move(ts)));
bulk.push_back(std::move(m));
// TODO: Check performance on this. May need to bach these.
_pubsubWriter->publishStatusChange(
entry.target, entry.network_id, entry.node_id, entry.os, entry.arch, entry.version, entry.last_seen);
try {
auto status = _table->Apply(std::move(m));
if (! status.ok()) {
fprintf(stderr, "Error writing to BigTable: %s\n", status.message().c_str());
}
else {
_pubsubWriter->publishStatusChange(
entry.target, entry.network_id, entry.node_id, entry.os, entry.arch, entry.version,
entry.last_seen);
}
}
catch (const std::exception& e) {
fprintf(stderr, "Exception writing to BigTable: %s\n", e.what());
span->SetAttribute("error", e.what());
span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
return;
}
}
// cbt::BulkMutation bulk;
// for (const auto& entry : toWrite) {
// std::string row_key = entry.network_id + "#" + entry.node_id;
// cbt::SingleRowMutation m(row_key);
// m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, osColumn, entry.os));
// m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, archColumn, entry.arch));
// m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, versionColumn, entry.version));
// char buf[64] = { 0 };
// std::string addressStr = entry.address.toString(buf);
// if (entry.address.ss_family == AF_INET) {
// m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv4Column, std::move(addressStr)));
// }
// else if (entry.address.ss_family == AF_INET6) {
// m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv6Column, std::move(addressStr)));
// }
// int64_t ts = entry.last_seen;
// m.emplace_back(cbt::SetCell(checkInColumnFamily, lastSeenColumn, std::move(ts)));
// bulk.emplace_back(m);
std::vector<cbt::FailedMutation> failures = table.BulkApply(bulk);
for (auto const& r : failures) {
// Handle error (log it, retry, etc.)
std::cerr << "Error writing to BigTable: " << r.status() << "\n";
}
// // TODO: Check performance on this. May need to bach these.
// _pubsubWriter->publishStatusChange(
// entry.target, entry.network_id, entry.node_id, entry.os, entry.arch, entry.version, entry.last_seen);
// }
// fprintf(stderr, "Applying %zu mutations to BigTable\n", bulk.size());
// try {
// std::vector<cbt::FailedMutation> failures = table.BulkApply(std::move(bulk));
// fprintf(stderr, "BigTable write completed with %zu failures\n", failures.size());
// for (auto const& r : failures) {
// // Handle error (log it, retry, etc.)
// std::cerr << "Error writing to BigTable: " << r.status() << "\n";
// }
// }
// catch (const std::exception& e) {
// fprintf(stderr, "Exception writing to BigTable: %s\n", e.what());
// span->SetAttribute("error", e.what());
// span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
// return;
// }
}
} // namespace ZeroTier