3
3
#include < chrono>
4
4
#include < cstdint>
5
5
#include < functional>
6
- #include < list>
7
6
#include < memory>
8
7
#include < string>
9
8
#include < vector>
@@ -66,20 +65,12 @@ void ClusterManagerInitHelper::addCluster(ClusterManagerCluster& cm_cluster) {
66
65
Cluster& cluster = cm_cluster.cluster ();
67
66
if (cluster.initializePhase () == Cluster::InitializePhase::Primary) {
68
67
// Remove the previous cluster before the cluster object is destroyed.
69
- primary_init_clusters_.remove_if (
70
- [name_to_remove = cluster.info ()->name ()](ClusterManagerCluster* cluster_iter) {
71
- return cluster_iter->cluster ().info ()->name () == name_to_remove;
72
- });
73
- primary_init_clusters_.push_back (&cm_cluster);
68
+ primary_init_clusters_.insert_or_assign (cm_cluster.cluster ().info ()->name (), &cm_cluster);
74
69
cluster.initialize (initialize_cb);
75
70
} else {
76
71
ASSERT (cluster.initializePhase () == Cluster::InitializePhase::Secondary);
77
72
// Remove the previous cluster before the cluster object is destroyed.
78
- secondary_init_clusters_.remove_if (
79
- [name_to_remove = cluster.info ()->name ()](ClusterManagerCluster* cluster_iter) {
80
- return cluster_iter->cluster ().info ()->name () == name_to_remove;
81
- });
82
- secondary_init_clusters_.push_back (&cm_cluster);
73
+ secondary_init_clusters_.insert_or_assign (cm_cluster.cluster ().info ()->name (), &cm_cluster);
83
74
if (started_secondary_initialize_) {
84
75
// This can happen if we get a second CDS update that adds new clusters after we have
85
76
// already started secondary init. In this case, just immediately initialize.
@@ -104,17 +95,20 @@ void ClusterManagerInitHelper::removeCluster(ClusterManagerCluster& cluster) {
104
95
105
96
// There is a remote edge case where we can remove a cluster via CDS that has not yet been
106
97
// initialized. When called via the remove cluster API this code catches that case.
107
- std::list< ClusterManagerCluster*>* cluster_list ;
98
+ absl::flat_hash_map<std::string, ClusterManagerCluster*>* cluster_map ;
108
99
if (cluster.cluster ().initializePhase () == Cluster::InitializePhase::Primary) {
109
- cluster_list = &primary_init_clusters_;
100
+ cluster_map = &primary_init_clusters_;
110
101
} else {
111
102
ASSERT (cluster.cluster ().initializePhase () == Cluster::InitializePhase::Secondary);
112
- cluster_list = &secondary_init_clusters_;
103
+ cluster_map = &secondary_init_clusters_;
113
104
}
114
105
115
106
// It is possible that the cluster we are removing has already been initialized, and is not
116
- // present in the initializer list. If so, this is fine.
117
- cluster_list->remove (&cluster);
107
+ // present in the initializer map. If so, this is fine.
108
+ absl::string_view cluster_name = cluster.cluster ().info ()->name ();
109
+ if (cluster_map->at (cluster_name) == &cluster) {
110
+ cluster_map->erase (cluster_name);
111
+ }
118
112
ENVOY_LOG (debug, " cm init: init complete: cluster={} primary={} secondary={}" ,
119
113
cluster.cluster ().info ()->name (), primary_init_clusters_.size (),
120
114
secondary_init_clusters_.size ());
@@ -123,13 +117,13 @@ void ClusterManagerInitHelper::removeCluster(ClusterManagerCluster& cluster) {
123
117
124
118
void ClusterManagerInitHelper::initializeSecondaryClusters () {
125
119
started_secondary_initialize_ = true ;
126
- // Cluster::initialize() method can modify the list of secondary_init_clusters_ to remove
120
+ // Cluster::initialize() method can modify the map of secondary_init_clusters_ to remove
127
121
// the item currently being initialized, so we eschew range-based-for and do this complicated
128
122
// dance to increment the iterator before calling initialize.
129
123
for (auto iter = secondary_init_clusters_.begin (); iter != secondary_init_clusters_.end ();) {
130
- ClusterManagerCluster* cluster = *iter;
124
+ ClusterManagerCluster* cluster = iter->second ;
125
+ ENVOY_LOG (debug, " initializing secondary cluster {}" , iter->first );
131
126
++iter;
132
- ENVOY_LOG (debug, " initializing secondary cluster {}" , cluster->cluster ().info ()->name ());
133
127
cluster->cluster ().initialize ([cluster, this ] { onClusterInit (*cluster); });
134
128
}
135
129
}
0 commit comments