]> git.feebdaed.xyz Git - 0xmirror/grpc.git/commitdiff
[zviz] Expand pagination api to include the first next id
authorCraig Tiller <ctiller@google.com>
Mon, 17 Nov 2025 17:34:09 +0000 (09:34 -0800)
committerCopybara-Service <copybara-worker@google.com>
Mon, 17 Nov 2025 17:36:56 +0000 (09:36 -0800)
PiperOrigin-RevId: 833375193

src/core/channelz/channelz.cc
src/core/channelz/zviz/environment.h
test/core/channelz/zviz/BUILD
test/core/channelz/zviz/environment_fake.h

index 555bff728204c4756b1c62b8f807f222227095db..986819cb3e0146ad3aab7e31adf255b2c5c87547 100644 (file)
@@ -177,13 +177,15 @@ void BaseNode::SerializeEntity(grpc_channelz_v2_Entity* entity,
   grpc_channelz_v2_Entity_set_id(entity, uuid());
   grpc_channelz_v2_Entity_set_kind(
       entity, StdStringToUpbString(EntityTypeToKind(type_)));
+  std::vector<WeakRefCountedPtr<BaseNode>> parent_nodes;
   {
     MutexLock lock(&parent_mu_);
-    auto* parents =
-        grpc_channelz_v2_Entity_resize_parents(entity, parents_.size(), arena);
-    for (const auto& parent : parents_) {
-      *parents++ = parent->uuid();
-    }
+    parent_nodes.assign(parents_.begin(), parents_.end());
+  }
+  auto* parents = grpc_channelz_v2_Entity_resize_parents(
+      entity, parent_nodes.size(), arena);
+  for (const auto& parent : parent_nodes) {
+    *parents++ = parent->uuid();
   }
   grpc_channelz_v2_Entity_set_orphaned(entity, orphaned_index_ != 0);
 
index 66af7fe778ccba6e14b878967495c1f99f075046..b21978599ad3bd3c4470e8f52df3a46ea3be3fc0 100644 (file)
@@ -33,7 +33,9 @@ class Environment {
       int64_t entity_id) = 0;
   struct GetChildrenResult {
     std::vector<grpc::channelz::v2::Entity> entities;
-    bool end;
+    // If >0, this is the first ID of the next page of results.
+    // If 0, this is the last page of results.
+    int64_t next_id_or_end;
   };
   virtual absl::StatusOr<GetChildrenResult> GetChildrenPaginated(
       int64_t /*entity_id*/, absl::string_view /*kind*/, int64_t /*start*/,
index dee02c485fd96d4e765ba7e0bbcbc891b04d6ea9..f286ab480d0b41f2940a440f15120448d124cde9 100644 (file)
@@ -37,6 +37,7 @@ grpc_cc_library(
     testonly = True,
     hdrs = ["environment_fake.h"],
     external_deps = [
+        "absl/algorithm:container",
         "absl/container:flat_hash_map",
     ],
     deps = [
index 8d08c34446a40a1aa2806bfb1693e0d7dd80cdce..ba02d75b5ab612d470a6dd9b6439458b4fc5991a 100644 (file)
 #ifndef GRPC_TEST_CORE_CHANNELZ_ZVIZ_ENVIRONMENT_FAKE_H
 #define GRPC_TEST_CORE_CHANNELZ_ZVIZ_ENVIRONMENT_FAKE_H
 
+#include <algorithm>
+#include <iterator>
+
 #include "src/core/channelz/zviz/environment.h"
+#include "absl/algorithm/container.h"
 #include "absl/container/flat_hash_map.h"
 
 namespace grpc_zviz {
@@ -44,28 +48,27 @@ class EnvironmentFake final : public Environment {
       size_t max_results) override {
     std::deque<grpc::channelz::v2::Entity> children;
     for (const auto& pair : entities_) {
-      if (pair.second.kind() != kind) continue;
-      for (int64_t parent_id : pair.second.parents()) {
-        if (parent_id == entity_id) {
-          children.push_back(pair.second);
-          break;
-        }
+      if (!kind.empty() && pair.second.kind() != kind) continue;
+      if (absl::c_linear_search(pair.second.parents(), entity_id)) {
+        children.push_back(pair.second);
       }
     }
     std::sort(
         children.begin(), children.end(),
         [](const grpc::channelz::v2::Entity& a,
            const grpc::channelz::v2::Entity& b) { return a.id() < b.id(); });
-    while (!children.empty() && children[0].id() < start) {
-      children.pop_front();
-    }
-    bool end = children.size() < max_results;
-    if (!end) children.resize(max_results);
+    children.erase(children.begin(),
+                   absl::c_find_if(children, [start](const auto& child) {
+                     return child.id() >= start;
+                   }));
     GetChildrenResult result;
-    for (auto& child : children) {
-      result.entities.push_back(std::move(child));
+    result.next_id_or_end = 0;
+    if (children.size() > max_results) {
+      result.next_id_or_end = children[max_results].id();
+      children.resize(max_results);
     }
-    result.end = end;
+    result.entities.assign(std::make_move_iterator(children.begin()),
+                           std::make_move_iterator(children.end()));
     return result;
   }