BigTableStatusWriter.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "BigTableStatusWriter.hpp"
  2. #include "ControllerConfig.hpp"
  3. #include "PubSubWriter.hpp"
  4. #include <google/cloud/bigtable/mutations.h>
  5. #include <google/cloud/bigtable/row.h>
  6. #include <google/cloud/bigtable/table.h>
  7. #include <opentelemetry/trace/provider.h>
  8. namespace cbt = google::cloud::bigtable;
  9. namespace ZeroTier {
  10. const std::string nodeInfoColumnFamily = "node_info";
  11. const std::string checkInColumnFamily = "check_in";
  12. const std::string osColumn = "os";
  13. const std::string archColumn = "arch";
  14. const std::string versionColumn = "version";
  15. const std::string ipv4Column = "ipv4";
  16. const std::string ipv6Column = "ipv6";
  17. const std::string lastSeenColumn = "last_seen";
  18. BigTableStatusWriter::BigTableStatusWriter(
  19. const std::string& project_id,
  20. const std::string& instance_id,
  21. const std::string& table_id)
  22. : _project_id(project_id)
  23. , _instance_id(instance_id)
  24. , _table_id(table_id)
  25. , _table(nullptr)
  26. {
  27. _table = new cbt::Table(cbt::MakeDataConnection(), cbt::TableResource(_project_id, _instance_id, _table_id));
  28. fprintf(
  29. stderr, "BigTableStatusWriter for project %s instance %s table %s\n", project_id.c_str(), instance_id.c_str(),
  30. table_id.c_str());
  31. }
  32. BigTableStatusWriter::~BigTableStatusWriter()
  33. {
  34. writePending();
  35. if (_table != nullptr) {
  36. delete _table;
  37. _table = nullptr;
  38. }
  39. }
  40. void BigTableStatusWriter::updateNodeStatus(
  41. const std::string& network_id,
  42. const std::string& node_id,
  43. const std::string& os,
  44. const std::string& arch,
  45. const std::string& version,
  46. const InetAddress& address,
  47. int64_t last_seen,
  48. const std::string& frontend)
  49. {
  50. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  51. auto tracer = provider->GetTracer("BigTableStatusWriter");
  52. auto span = tracer->StartSpan("BigTableStatusWriter::updateNodeStatus");
  53. auto scope = tracer->WithActiveSpan(span);
  54. std::lock_guard<std::mutex> l(_lock);
  55. _pending.push_back({ network_id, node_id, os, arch, version, address, last_seen, frontend });
  56. }
  57. size_t BigTableStatusWriter::queueLength() const
  58. {
  59. std::lock_guard<std::mutex> l(_lock);
  60. return _pending.size();
  61. }
  62. void BigTableStatusWriter::writePending()
  63. {
  64. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  65. auto tracer = provider->GetTracer("BigTableStatusWriter");
  66. auto span = tracer->StartSpan("BigTableStatusWriter::writePending");
  67. auto scope = tracer->WithActiveSpan(span);
  68. std::vector<PendingStatusEntry> toWrite;
  69. {
  70. std::lock_guard<std::mutex> l(_lock);
  71. toWrite.swap(_pending);
  72. }
  73. if (toWrite.empty()) {
  74. return;
  75. }
  76. fprintf(stderr, "Writing %zu pending status entries to BigTable\n", toWrite.size());
  77. cbt::BulkMutation bulk;
  78. for (const auto& entry : toWrite) {
  79. std::string row_key = entry.network_id + "#" + entry.node_id;
  80. // read the latest values from BigTable for this row key
  81. std::map<std::string, std::string> latest_values;
  82. try {
  83. auto row = _table->ReadRow(row_key, cbt::Filter::Latest(1));
  84. if (row->first) {
  85. for (const auto& cell : row->second.cells()) {
  86. if (cell.family_name() == nodeInfoColumnFamily) {
  87. latest_values[cell.column_qualifier()] = cell.value();
  88. }
  89. }
  90. }
  91. }
  92. catch (const std::exception& e) {
  93. fprintf(stderr, "Exception reading from BigTable: %s\n", e.what());
  94. }
  95. cbt::SingleRowMutation m(row_key);
  96. // only update if value has changed
  97. if (latest_values[osColumn] != entry.os) {
  98. m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, osColumn, entry.os));
  99. }
  100. if (latest_values[archColumn] != entry.arch) {
  101. m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, archColumn, entry.arch));
  102. }
  103. if (latest_values[versionColumn] != entry.version) {
  104. m.emplace_back(cbt::SetCell(nodeInfoColumnFamily, versionColumn, entry.version));
  105. }
  106. char buf[64] = { 0 };
  107. std::string addressStr = entry.address.toString(buf);
  108. if (entry.address.ss_family == AF_INET) {
  109. m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv4Column, std::move(addressStr)));
  110. }
  111. else if (entry.address.ss_family == AF_INET6) {
  112. m.emplace_back(cbt::SetCell(checkInColumnFamily, ipv6Column, std::move(addressStr)));
  113. }
  114. int64_t ts = entry.last_seen;
  115. m.emplace_back(cbt::SetCell(checkInColumnFamily, lastSeenColumn, std::move(ts)));
  116. bulk.emplace_back(m);
  117. }
  118. fprintf(stderr, "Applying %zu mutations to BigTable\n", bulk.size());
  119. try {
  120. std::vector<cbt::FailedMutation> failures = _table->BulkApply(std::move(bulk));
  121. fprintf(stderr, "BigTable write completed with %zu failures\n", failures.size());
  122. for (auto const& r : failures) {
  123. // Handle error (log it, retry, etc.)
  124. std::cerr << "Error writing to BigTable: " << r.status() << "\n";
  125. }
  126. }
  127. catch (const std::exception& e) {
  128. fprintf(stderr, "Exception writing to BigTable: %s\n", e.what());
  129. span->SetAttribute("error", e.what());
  130. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  131. return;
  132. }
  133. }
  134. } // namespace ZeroTier