BigTableStatusWriter.cpp 4.8 KB

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