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