lb_policy.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
27 
28 namespace grpc_core {
29 
30 DebugOnlyTraceFlag grpc_trace_lb_policy_refcount(false, "lb_policy_refcount");
31 
32 //
33 // LoadBalancingPolicy
34 //
35 
39  ? "LoadBalancingPolicy"
40  : nullptr,
41  initial_refcount),
42  work_serializer_(std::move(args.work_serializer)),
44  channel_control_helper_(std::move(args.channel_control_helper)) {}
45 
48 }
49 
52  Unref(DEBUG_LOCATION, "Orphan");
53 }
54 
55 //
56 // LoadBalancingPolicy::UpdateArgs
57 //
58 
60  : addresses(other.addresses),
61  config(other.config),
62  resolution_note(other.resolution_note),
64 
66  : addresses(std::move(other.addresses)),
67  config(std::move(other.config)),
68  resolution_note(std::move(other.resolution_note)),
69  // TODO(roth): Use std::move() once channel args is converted to C++.
70  args(other.args) {
71  other.args = nullptr;
72 }
73 
75  const UpdateArgs& other) {
76  if (&other == this) return *this;
77  addresses = other.addresses;
78  config = other.config;
79  resolution_note = other.resolution_note;
82  return *this;
83 }
84 
86  UpdateArgs&& other) noexcept {
87  addresses = std::move(other.addresses);
88  config = std::move(other.config);
89  resolution_note = std::move(other.resolution_note);
90  // TODO(roth): Use std::move() once channel args is converted to C++.
92  args = other.args;
93  other.args = nullptr;
94  return *this;
95 }
96 
97 //
98 // LoadBalancingPolicy::QueuePicker
99 //
100 
102  PickArgs /*args*/) {
103  // We invoke the parent's ExitIdleLocked() via a closure instead
104  // of doing it directly here, for two reasons:
105  // 1. ExitIdleLocked() may cause the policy's state to change and
106  // a new picker to be delivered to the channel. If that new
107  // picker is delivered before ExitIdleLocked() returns, then by
108  // the time this function returns, the pick will already have
109  // been processed, and we'll be trying to re-process the same
110  // pick again, leading to a crash.
111  // 2. We are currently running in the data plane mutex, but we
112  // need to bounce into the control plane work_serializer to call
113  // ExitIdleLocked().
114  if (!exit_idle_called_ && parent_ != nullptr) {
115  exit_idle_called_ = true;
116  auto* parent = parent_->Ref().release(); // ref held by lambda.
119  [](void* arg, grpc_error_handle /*error*/) {
120  auto* parent = static_cast<LoadBalancingPolicy*>(arg);
121  parent->work_serializer()->Run(
122  [parent]() {
123  parent->ExitIdleLocked();
124  parent->Unref();
125  },
127  },
128  parent, nullptr),
130  }
131  return PickResult::Queue();
132 }
133 
134 } // namespace grpc_core
grpc_core::LoadBalancingPolicy::QueuePicker::Pick
PickResult Pick(PickArgs args) override
Definition: lb_policy.cc:101
grpc_core::LoadBalancingPolicy::UpdateArgs::args
const grpc_channel_args * args
Definition: lb_policy.h:330
grpc_core::LoadBalancingPolicy::UpdateArgs::resolution_note
std::string resolution_note
Definition: lb_policy.h:326
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
grpc_core::LoadBalancingPolicy::UpdateArgs
Definition: lb_policy.h:315
grpc_core::LoadBalancingPolicy
Definition: lb_policy.h:95
grpc_core::LoadBalancingPolicy::UpdateArgs::UpdateArgs
UpdateArgs()=default
grpc_core::InternallyRefCounted< LoadBalancingPolicy >::Unref
void Unref()
Definition: orphanable.h:100
grpc_core
Definition: call_metric_recorder.h:31
grpc_pollset_set_create
grpc_pollset_set * grpc_pollset_set_create()
Definition: pollset_set.cc:29
lb_policy.h
closure.h
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
GRPC_TRACE_FLAG_ENABLED
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition: debug/trace.h:114
grpc_core::LoadBalancingPolicy::PickResult
The result of picking a subchannel for a call.
Definition: lb_policy.h:188
grpc_core::LoadBalancingPolicy::UpdateArgs::config
RefCountedPtr< Config > config
The LB policy config.
Definition: lb_policy.h:320
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::grpc_trace_lb_policy_refcount
DebugOnlyTraceFlag grpc_trace_lb_policy_refcount(false, "lb_policy_refcount")
Definition: lb_policy.h:54
grpc_core::LoadBalancingPolicy::PickArgs
Arguments used when picking a subchannel for a call.
Definition: lb_policy.h:141
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
config
struct config_s config
grpc_pollset_set_destroy
void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set)
Definition: pollset_set.cc:33
pollset_set.h
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
arg
Definition: cmdline.cc:40
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
error.h
grpc_core::InternallyRefCounted
Definition: orphanable.h:73
grpc_core::LoadBalancingPolicy::~LoadBalancingPolicy
~LoadBalancingPolicy() override
Definition: lb_policy.cc:46
grpc_core::LoadBalancingPolicy::Args
Args used to instantiate an LB policy.
Definition: lb_policy.h:343
grpc_core::LoadBalancingPolicy::ShutdownLocked
virtual void ShutdownLocked()=0
Shuts down the policy.
grpc_core::LoadBalancingPolicy::UpdateArgs::addresses
absl::StatusOr< ServerAddressList > addresses
Definition: lb_policy.h:318
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::LoadBalancingPolicy::UpdateArgs::operator=
UpdateArgs & operator=(const UpdateArgs &other)
Definition: lb_policy.cc:74
arg
struct arg arg
exec_ctx.h
grpc_core::LoadBalancingPolicy::LoadBalancingPolicy
LoadBalancingPolicy(Args args, intptr_t initial_refcount=1)
Definition: lb_policy.cc:36
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
grpc_core::LoadBalancingPolicy::Orphan
void Orphan() override
Definition: lb_policy.cc:50
grpc_core::LoadBalancingPolicy::work_serializer
std::shared_ptr< WorkSerializer > work_serializer() const
Definition: lb_policy.h:420
grpc_core::LoadBalancingPolicy::interested_parties_
grpc_pollset_set * interested_parties_
Owned pointer to interested parties in load balancing decisions.
Definition: lb_policy.h:437
work_serializer_
std::shared_ptr< WorkSerializer > work_serializer_
Definition: google_c2p_resolver.cc:134
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: bloaty/third_party/googletest/googletest/test/googletest-break-on-failure-unittest.py:76
grpc_error
Definition: error_internal.h:42
parent_
RefCountedPtr< GrpcLb > parent_
Definition: grpclb.cc:438
grpc_core::DebugOnlyTraceFlag
TraceFlag DebugOnlyTraceFlag
Definition: debug/trace.h:117
grpc_core::LoadBalancingPolicy::PickResult::Queue
Definition: lb_policy.h:210
interested_parties_
grpc_pollset_set * interested_parties_
Definition: oob_backend_metric.cc:169
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:15