Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions envoy/upstream/cluster_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ class ClusterManager {
*/
virtual ClusterInfoMaps clusters() const PURE;

/**
* Iterates over all active clusters and invokes the callback for each.
* @param cb the callback to invoke for each active cluster.
*
* NOTE: This method is only thread safe on the main thread. It should not be called elsewhere.
*/
virtual void forEachActiveCluster(std::function<void(const Cluster&)> cb) const PURE;

/**
* Receives a cluster name and returns an active cluster (if found).
* @param cluster_name the name of the cluster.
Expand Down
7 changes: 7 additions & 0 deletions source/common/upstream/cluster_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ class ClusterManagerImpl : public ClusterManager,
return clusters_maps;
}

void forEachActiveCluster(std::function<void(const Cluster&)> cb) const override {
ASSERT_IS_MAIN_OR_TEST_THREAD();
for (const auto& [unused_name, cluster_data] : active_clusters_) {
cb(*cluster_data->cluster_);
}
}

OptRef<const Cluster> getActiveCluster(const std::string& cluster_name) const override {
ASSERT_IS_MAIN_OR_TEST_THREAD();
if (const auto& it = active_clusters_.find(cluster_name); it != active_clusters_.end()) {
Expand Down
8 changes: 4 additions & 4 deletions source/common/upstream/host_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ void HostUtility::forEachHostMetric(
const ClusterManager& cluster_manager,
const std::function<void(Stats::PrimitiveCounterSnapshot&& metric)>& counter_cb,
const std::function<void(Stats::PrimitiveGaugeSnapshot&& metric)>& gauge_cb) {
for (const auto& [unused_name, cluster_ref] : cluster_manager.clusters().active_clusters_) {
Upstream::ClusterInfoConstSharedPtr cluster_info = cluster_ref.get().info();
cluster_manager.forEachActiveCluster([&](const Cluster& cluster) {
Upstream::ClusterInfoConstSharedPtr cluster_info = cluster.info();
if (cluster_info->perEndpointStatsEnabled()) {
const std::string cluster_name =
Stats::Utility::sanitizeStatsName(cluster_info->observabilityName());

const Stats::TagVector& fixed_tags = cluster_info->statsScope().store().fixedTags();

for (auto& host_set : cluster_ref.get().prioritySet().hostSetsPerPriority()) {
for (auto& host_set : cluster.prioritySet().hostSetsPerPriority()) {
for (auto& host : host_set->hosts()) {

Stats::TagVector tags;
Expand Down Expand Up @@ -234,7 +234,7 @@ void HostUtility::forEachHostMetric(
}
}
}
}
});
}

} // namespace Upstream
Expand Down
12 changes: 12 additions & 0 deletions test/mocks/upstream/cluster_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ MockClusterManager::MockClusterManager()
OptRef<xds::core::v3::ResourceLocator>,
ProtobufMessage::ValidationVisitor&) { return MockOdCdsApiHandle::create(); }));
ON_CALL(*this, addOrUpdateCluster(_, _, _)).WillByDefault(Return(false));
ON_CALL(*this, forEachActiveCluster(_))
.WillByDefault(Invoke([this](std::function<void(const Cluster&)> cb) {
for (const auto& [unused_name, cluster_ref] : clusters().active_clusters_) {
cb(cluster_ref.get());
}
}));
ON_CALL(*this, hasActiveClusters()).WillByDefault(Return(false));
}

Expand All @@ -56,6 +62,12 @@ void MockClusterManager::initializeClusters(const std::vector<std::string>& acti
}

ON_CALL(*this, clusters()).WillByDefault(Return(info_map));
ON_CALL(*this, forEachActiveCluster(_))
.WillByDefault(Invoke([this](std::function<void(const Cluster&)> cb) {
for (const auto& [unused_name, cluster] : active_clusters_) {
cb(*cluster);
}
}));
ON_CALL(*this, getActiveCluster(_))
.WillByDefault(Invoke([this](const std::string& cluster_name) -> OptRef<const Cluster> {
if (const auto& it = active_clusters_.find(cluster_name); it != active_clusters_.end()) {
Expand Down
1 change: 1 addition & 0 deletions test/mocks/upstream/cluster_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MockClusterManager : public ClusterManager {
MOCK_METHOD(absl::Status, initializeSecondaryClusters,
(const envoy::config::bootstrap::v3::Bootstrap& bootstrap));
MOCK_METHOD(ClusterInfoMaps, clusters, (), (const));
MOCK_METHOD(void, forEachActiveCluster, (std::function<void(const Cluster&)>), (const));
MOCK_METHOD(OptRef<const Cluster>, getActiveCluster, (const std::string& cluster_name), (const));
MOCK_METHOD(OptRef<const Cluster>, getActiveOrWarmingCluster, (const std::string& cluster_name),
(const));
Expand Down
6 changes: 6 additions & 0 deletions test/server/admin/stats_handler_speed_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class FastMockClusterManager : public testing::StrictMock<Upstream::MockClusterM
public:
ClusterInfoMaps clusters() const override { return clusters_; }

void forEachActiveCluster(std::function<void(const Upstream::Cluster&)> cb) const override {
for (const auto& [unused_name, cluster_ref] : clusters_.active_clusters_) {
cb(cluster_ref.get());
}
}

ClusterInfoMaps clusters_;
std::vector<std::unique_ptr<FastMockCluster>> clusters_storage_;
Stats::TestUtil::TestStore store_;
Expand Down
1 change: 1 addition & 0 deletions test/server/server_stats_flush_benchmark_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Envoy {
class FastMockClusterManager : public testing::StrictMock<Upstream::MockClusterManager> {
public:
ClusterInfoMaps clusters() const override { return ClusterInfoMaps{}; }
void forEachActiveCluster(std::function<void(const Upstream::Cluster&)>) const override {}
};

class TestSinkPredicates : public Stats::SinkPredicates {
Expand Down
Loading