1 // 2 // Copyright 2018 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef GRPC_SRC_CORE_LOAD_BALANCING_CHILD_POLICY_HANDLER_H 18 #define GRPC_SRC_CORE_LOAD_BALANCING_CHILD_POLICY_HANDLER_H 19 #include <grpc/support/port_platform.h> 20 21 #include <utility> 22 23 #include "absl/status/status.h" 24 #include "absl/strings/string_view.h" 25 26 #include "src/core/lib/channel/channel_args.h" 27 #include "src/core/lib/debug/trace.h" 28 #include "src/core/lib/gprpp/orphanable.h" 29 #include "src/core/lib/gprpp/ref_counted_ptr.h" 30 #include "src/core/load_balancing/lb_policy.h" 31 32 namespace grpc_core { 33 34 // A class that makes it easy to gracefully switch child policies. 35 // 36 // Callers should instantiate this instead of using 37 // CoreConfiguration::Get().lb_policy_registry().CreateLoadBalancingPolicy(). 38 // Once instantiated, this object will automatically take care of constructing 39 // the child policy as needed upon receiving an update. 40 class ChildPolicyHandler : public LoadBalancingPolicy { 41 public: ChildPolicyHandler(Args args,TraceFlag * tracer)42 ChildPolicyHandler(Args args, TraceFlag* tracer) 43 : LoadBalancingPolicy(std::move(args)), tracer_(tracer) {} 44 name()45 absl::string_view name() const override { return "child_policy_handler"; } 46 47 absl::Status UpdateLocked(UpdateArgs args) override; 48 void ExitIdleLocked() override; 49 void ResetBackoffLocked() override; 50 51 // Returns true if transitioning from the old config to the new config 52 // requires instantiating a new policy object. 53 virtual bool ConfigChangeRequiresNewPolicyInstance( 54 LoadBalancingPolicy::Config* old_config, 55 LoadBalancingPolicy::Config* new_config) const; 56 57 // Instantiates a new policy of the specified name. 58 // May be overridden by subclasses to avoid recursion when an LB 59 // policy factory returns a ChildPolicyHandler. 60 virtual OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( 61 absl::string_view name, LoadBalancingPolicy::Args args) const; 62 63 private: 64 class Helper; 65 66 void ShutdownLocked() override; 67 68 OrphanablePtr<LoadBalancingPolicy> CreateChildPolicy( 69 absl::string_view child_policy_name, const ChannelArgs& args); 70 71 // Passed in from caller at construction time. 72 TraceFlag* tracer_; 73 74 bool shutting_down_ = false; 75 76 // The most recent config passed to UpdateLocked(). 77 // If pending_child_policy_ is non-null, this is the config passed to 78 // pending_child_policy_; otherwise, it's the config passed to child_policy_. 79 RefCountedPtr<LoadBalancingPolicy::Config> current_config_; 80 81 // Child LB policy. 82 OrphanablePtr<LoadBalancingPolicy> child_policy_; 83 OrphanablePtr<LoadBalancingPolicy> pending_child_policy_; 84 }; 85 86 } // namespace grpc_core 87 88 #endif // GRPC_SRC_CORE_LOAD_BALANCING_CHILD_POLICY_HANDLER_H 89