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);
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*/,
#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 {
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;
}