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 #include "src/core/lib/channel/channel_args.h" 26 #include "src/core/lib/debug/trace.h" 27 #include "src/core/load_balancing/lb_policy.h" 28 #include "src/core/util/orphanable.h" 29 #include "src/core/util/ref_counted_ptr.h" 30 31 namespace grpc_core { 32 33 // A class that makes it easy to gracefully switch child policies. 34 // 35 // Callers should instantiate this instead of using 36 // CoreConfiguration::Get().lb_policy_registry().CreateLoadBalancingPolicy(). 37 // Once instantiated, this object will automatically take care of constructing 38 // the child policy as needed upon receiving an update. 39 class ChildPolicyHandler : public LoadBalancingPolicy { 40 public: ChildPolicyHandler(Args args,TraceFlag * tracer)41 ChildPolicyHandler(Args args, TraceFlag* tracer) 42 : LoadBalancingPolicy(std::move(args)), tracer_(tracer) {} 43 name()44 absl::string_view name() const override { return "child_policy_handler"; } 45 46 absl::Status UpdateLocked(UpdateArgs args) override; 47 void ExitIdleLocked() override; 48 void ResetBackoffLocked() override; 49 50 // Returns true if transitioning from the old config to the new config 51 // requires instantiating a new policy object. 52 virtual bool ConfigChangeRequiresNewPolicyInstance( 53 LoadBalancingPolicy::Config* old_config, 54 LoadBalancingPolicy::Config* new_config) const; 55 56 // Instantiates a new policy of the specified name. 57 // May be overridden by subclasses to avoid recursion when an LB 58 // policy factory returns a ChildPolicyHandler. 59 virtual OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( 60 absl::string_view name, LoadBalancingPolicy::Args args) const; 61 62 private: 63 class Helper; 64 65 void ShutdownLocked() override; 66 67 OrphanablePtr<LoadBalancingPolicy> CreateChildPolicy( 68 absl::string_view child_policy_name, const ChannelArgs& args); 69 70 // Passed in from caller at construction time. 71 TraceFlag* tracer_; 72 73 bool shutting_down_ = false; 74 75 // The most recent config passed to UpdateLocked(). 76 // If pending_child_policy_ is non-null, this is the config passed to 77 // pending_child_policy_; otherwise, it's the config passed to child_policy_. 78 RefCountedPtr<LoadBalancingPolicy::Config> current_config_; 79 80 // Child LB policy. 81 OrphanablePtr<LoadBalancingPolicy> child_policy_; 82 OrphanablePtr<LoadBalancingPolicy> pending_child_policy_; 83 }; 84 85 } // namespace grpc_core 86 87 #endif // GRPC_SRC_CORE_LOAD_BALANCING_CHILD_POLICY_HANDLER_H 88