Skip to content

Commit acb584a

Browse files
lb: unified the lifetime mechanism of worker load lb 2 (#45170)
Commit Message: lb: unified the lifetime mechanism of worker load lb 2 Additional Description: Similar to previous #45100 but this PR have updated all left LB. Risk Level: Testing: Docs Changes: Release Notes: Platform Specific Features: [Optional Runtime guard:] [Optional Fixes #Issue] [Optional Fixes commit #PR or SHA] [Optional Deprecated:] [Optional [API Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):] --------- Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com> Signed-off-by: code <wbphub@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 4616750 commit acb584a

11 files changed

Lines changed: 202 additions & 48 deletions

File tree

source/extensions/clusters/aggregate/cluster.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class AggregateLoadBalancerFactory : public Upstream::LoadBalancerFactory {
153153
cluster_.info(), cluster_.cluster_manager_, cluster_.runtime(), cluster_.random(),
154154
cluster_.clusters_);
155155
}
156+
bool recreateOnHostChange() const override { return false; }
156157

157158
const Cluster& cluster_;
158159
};

source/extensions/clusters/composite/cluster.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class CompositeLoadBalancerFactory : public Upstream::LoadBalancerFactory {
9595
return std::make_unique<CompositeClusterLoadBalancer>(
9696
cluster_.info(), cluster_.cluster_manager_, cluster_.clusters_);
9797
}
98+
bool recreateOnHostChange() const override { return false; }
9899

99100
const Cluster& cluster_;
100101
};

source/extensions/clusters/dynamic_forward_proxy/cluster.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class Cluster : public Upstream::BaseDynamicClusterImpl,
187187
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams) override {
188188
return std::make_unique<LoadBalancer>(cluster_);
189189
}
190+
bool recreateOnHostChange() const override { return false; }
190191

191192
private:
192193
Cluster& cluster_;

source/extensions/clusters/dynamic_modules/cluster.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct DynamicModuleThreadAwareLoadBalancer : public Upstream::ThreadAwareLoadBa
3535
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams params) override {
3636
return std::make_unique<DynamicModuleLoadBalancer>(handle_, params.priority_set);
3737
}
38+
bool recreateOnHostChange() const override { return false; }
3839

3940
DynamicModuleClusterHandleSharedPtr handle_;
4041
};

source/extensions/clusters/original_dst/original_dst_cluster.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ class OriginalDstCluster : public ClusterImplBase {
8888
*/
8989
class LoadBalancer : public Upstream::LoadBalancer {
9090
public:
91-
LoadBalancer(const OriginalDstClusterHandleSharedPtr& parent)
91+
LoadBalancer(const OriginalDstClusterHandleSharedPtr& parent, const PrioritySet& priority_set)
9292
: parent_(parent), http_header_name_(parent->cluster_->httpHeaderName()),
9393
metadata_key_(parent->cluster_->metadataKey()),
9494
port_override_(parent->cluster_->portOverride()),
95-
host_map_(parent->cluster_->getCurrentHostMap()) {}
95+
host_map_(parent->cluster_->getCurrentHostMap()) {
96+
member_update_cb_ =
97+
priority_set.addMemberUpdateCb([this](const HostVector&, const HostVector&) {
98+
host_map_ = parent_->cluster_->getCurrentHostMap();
99+
});
100+
}
96101

97102
// Upstream::LoadBalancer
98103
HostSelectionResponse chooseHost(LoadBalancerContext* context) override;
@@ -121,6 +126,7 @@ class OriginalDstCluster : public ClusterImplBase {
121126
const absl::optional<Config::MetadataKey>& metadata_key_;
122127
const absl::optional<uint32_t> port_override_;
123128
HostMultiMapConstSharedPtr host_map_;
129+
Common::CallbackHandlePtr member_update_cb_;
124130
};
125131

126132
const absl::optional<Http::LowerCaseString>& httpHeaderName() { return http_header_name_; }
@@ -139,9 +145,10 @@ class OriginalDstCluster : public ClusterImplBase {
139145
LoadBalancerFactory(const OriginalDstClusterHandleSharedPtr& cluster) : cluster_(cluster) {}
140146

141147
// Upstream::LoadBalancerFactory
142-
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams) override {
143-
return std::make_unique<LoadBalancer>(cluster_);
148+
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams params) override {
149+
return std::make_unique<LoadBalancer>(cluster_, params.priority_set);
144150
}
151+
bool recreateOnHostChange() const override { return false; }
145152

146153
const OriginalDstClusterHandleSharedPtr cluster_;
147154
};

source/extensions/clusters/redis/redis_cluster_lb.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,24 @@ void RedisClusterLoadBalancerFactory::onHostHealthUpdate() {
101101
}
102102
}
103103

104-
Upstream::LoadBalancerPtr RedisClusterLoadBalancerFactory::create(Upstream::LoadBalancerParams) {
105-
absl::ReaderMutexLock lock(mutex_);
106-
return std::make_unique<RedisClusterLoadBalancer>(slot_array_, shard_vector_, random_);
104+
Upstream::LoadBalancerPtr
105+
RedisClusterLoadBalancerFactory::create(Upstream::LoadBalancerParams params) {
106+
return std::make_unique<RedisClusterLoadBalancer>(shared_from_this(), params.priority_set);
107+
}
108+
109+
RedisClusterLoadBalancerFactory::RedisClusterLoadBalancer::RedisClusterLoadBalancer(
110+
std::shared_ptr<RedisClusterLoadBalancerFactory> factory,
111+
const Upstream::PrioritySet& priority_set)
112+
: factory_(factory), random_(factory->random_) {
113+
refresh();
114+
member_update_cb_ = priority_set.addMemberUpdateCb(
115+
[this](const Upstream::HostVector&, const Upstream::HostVector&) { refresh(); });
116+
}
117+
118+
void RedisClusterLoadBalancerFactory::RedisClusterLoadBalancer::refresh() {
119+
absl::ReaderMutexLock lock(factory_->mutex_);
120+
slot_array_ = factory_->slot_array_;
121+
shard_vector_ = factory_->shard_vector_;
107122
}
108123

109124
namespace {

source/extensions/clusters/redis/redis_cluster_lb.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ using ClusterSlotUpdateCallBackSharedPtr = std::shared_ptr<ClusterSlotUpdateCall
175175
* This factory is created and returned by RedisCluster's factory() method, the create() method will
176176
* be called on each thread to create a thread local RedisClusterLoadBalancer.
177177
*/
178-
class RedisClusterLoadBalancerFactory : public ClusterSlotUpdateCallBack,
179-
public Upstream::LoadBalancerFactory {
178+
class RedisClusterLoadBalancerFactory
179+
: public ClusterSlotUpdateCallBack,
180+
public Upstream::LoadBalancerFactory,
181+
public std::enable_shared_from_this<RedisClusterLoadBalancerFactory> {
180182
public:
181183
RedisClusterLoadBalancerFactory(Random::RandomGenerator& random) : random_(random) {}
182184

@@ -186,7 +188,8 @@ class RedisClusterLoadBalancerFactory : public ClusterSlotUpdateCallBack,
186188
void onHostHealthUpdate() override;
187189

188190
// Upstream::LoadBalancerFactory
189-
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams) override;
191+
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams params) override;
192+
bool recreateOnHostChange() const override { return false; }
190193

191194
private:
192195
class RedisShard {
@@ -267,10 +270,8 @@ class RedisClusterLoadBalancerFactory : public ClusterSlotUpdateCallBack,
267270
*/
268271
class RedisClusterLoadBalancer : public Upstream::LoadBalancer {
269272
public:
270-
RedisClusterLoadBalancer(SlotArraySharedPtr slot_array, ShardVectorSharedPtr shard_vector,
271-
Random::RandomGenerator& random)
272-
: slot_array_(std::move(slot_array)), shard_vector_(std::move(shard_vector)),
273-
random_(random) {}
273+
RedisClusterLoadBalancer(std::shared_ptr<RedisClusterLoadBalancerFactory> factory,
274+
const Upstream::PrioritySet& priority_set);
274275

275276
// Upstream::LoadBalancerBase
276277
Upstream::HostSelectionResponse chooseHost(Upstream::LoadBalancerContext*) override;
@@ -290,9 +291,14 @@ class RedisClusterLoadBalancerFactory : public ClusterSlotUpdateCallBack,
290291
}
291292

292293
private:
293-
const SlotArraySharedPtr slot_array_;
294-
const ShardVectorSharedPtr shard_vector_;
294+
// Re-snapshots the topology from the parent factory under its mutex.
295+
void refresh();
296+
297+
const std::shared_ptr<RedisClusterLoadBalancerFactory> factory_;
298+
SlotArraySharedPtr slot_array_;
299+
ShardVectorSharedPtr shard_vector_;
295300
Random::RandomGenerator& random_;
301+
::Envoy::Common::CallbackHandlePtr member_update_cb_;
296302
};
297303

298304
absl::Mutex mutex_;

source/extensions/clusters/reverse_connection/reverse_connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class RevConCluster : public Upstream::ClusterImplBase {
183183
// Upstream::LoadBalancerFactory.
184184
Upstream::LoadBalancerPtr create() { return std::make_unique<LoadBalancer>(cluster_); }
185185
Upstream::LoadBalancerPtr create(Upstream::LoadBalancerParams) override { return create(); }
186+
bool recreateOnHostChange() const override { return false; }
186187

187188
const std::shared_ptr<RevConCluster> cluster_;
188189
};

source/extensions/load_balancing_policies/override_host/load_balancer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class OverrideHostLoadBalancer : public Upstream::ThreadAwareLoadBalancer,
189189
// Called by worker threads to create a thread-local load balancer.
190190
LoadBalancerPtr create(LoadBalancerParams params) override;
191191

192+
bool recreateOnHostChange() const override { return false; }
193+
192194
private:
193195
// Hosts in the load balancer. Owned by the cluster manager.
194196
const OverrideHostLbConfig& config_;

0 commit comments

Comments
 (0)