| 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 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/load_balancing/lb_policy.h" |
| 22 |
|
| 23 |
#include "src/core/lib/iomgr/closure.h" |
| 24 |
#include "src/core/lib/iomgr/error.h" |
| 25 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 26 |
#include "src/core/lib/iomgr/pollset_set.h" |
| 27 |
|
| 28 |
namespace grpc_core { |
| 29 |
|
| 30 |
DebugOnlyTraceFlag grpc_trace_lb_policy_refcount(false, "lb_policy_refcount"); |
| 31 |
|
| 32 |
// |
| 33 |
// LoadBalancingPolicy |
| 34 |
// |
| 35 |
|
| 36 |
LoadBalancingPolicy::LoadBalancingPolicy(Args args, intptr_t initial_refcount) |
| 37 |
: InternallyRefCounted( |
| 38 |
GRPC_TRACE_FLAG_ENABLED(grpc_trace_lb_policy_refcount) |
| 39 |
? "LoadBalancingPolicy" |
| 40 |
: nullptr, |
| 41 |
initial_refcount), |
| 42 |
work_serializer_(std::move(args.work_serializer)), |
| 43 |
interested_parties_(grpc_pollset_set_create()), |
| 44 |
channel_control_helper_(std::move(args.channel_control_helper)), |
| 45 |
channel_args_(std::move(args.args)) {} |
| 46 |
|
| 47 |
LoadBalancingPolicy::~LoadBalancingPolicy() { |
| 48 |
grpc_pollset_set_destroy(interested_parties_); |
| 49 |
} |
| 50 |
|
| 51 |
void LoadBalancingPolicy::Orphan() { |
| 52 |
ShutdownLocked(); |
| 53 |
Unref(DEBUG_LOCATION, "Orphan"); |
| 54 |
} |
| 55 |
|
| 56 |
// |
| 57 |
// LoadBalancingPolicy::QueuePicker |
| 58 |
// |
| 59 |
|
| 60 |
LoadBalancingPolicy::PickResult LoadBalancingPolicy::QueuePicker::Pick( |
| 61 |
PickArgs /*args*/) { |
| 62 |
// We invoke the parent's ExitIdleLocked() via a closure instead |
| 63 |
// of doing it directly here, for two reasons: |
| 64 |
// 1. ExitIdleLocked() may cause the policy's state to change and |
| 65 |
// a new picker to be delivered to the channel. If that new |
| 66 |
// picker is delivered before ExitIdleLocked() returns, then by |
| 67 |
// the time this function returns, the pick will already have |
| 68 |
// been processed, and we'll be trying to re-process the same |
| 69 |
// pick again, leading to a crash. |
| 70 |
// 2. We are currently running in the data plane mutex, but we |
| 71 |
// need to bounce into the control plane work_serializer to call |
| 72 |
// ExitIdleLocked(). |
| 73 |
if (!exit_idle_called_ && parent_ != nullptr) { |
| 74 |
exit_idle_called_ = true; |
| 75 |
auto* parent = parent_->Ref().release(); // ref held by lambda. |
| 76 |
ExecCtx::Run(DEBUG_LOCATION, |
| 77 |
GRPC_CLOSURE_CREATE( |
| 78 |
[](void* arg, grpc_error_handle /*error*/) { |
| 79 |
auto* parent = static_cast<LoadBalancingPolicy*>(arg); |
| 80 |
parent->work_serializer()->Run( |
| 81 |
[parent]() { |
| 82 |
parent->ExitIdleLocked(); |
| 83 |
parent->Unref(); |
| 84 |
}, |
| 85 |
DEBUG_LOCATION); |
| 86 |
}, |
| 87 |
parent, nullptr), |
| 88 |
absl::OkStatus()); |
| 89 |
} |
| 90 |
return PickResult::Queue(); |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace grpc_core |