blob: 90401b586f2ab4852986230ad3144fb0bed299fc [file] [log] [blame]
Craig Tilleraf691802015-06-23 14:57:07 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tilleraf691802015-06-23 14:57:07 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Mark D. Roth2137cd82016-09-14 09:04:00 -070034#include "src/core/ext/client_channel/lb_policy.h"
Craig Tiller7c0b4d72015-06-25 14:44:44 -070035
Craig Tiller48613042015-11-29 14:45:11 -080036#define WEAK_REF_BITS 16
37
Craig Tillera82950e2015-09-22 12:33:20 -070038void grpc_lb_policy_init(grpc_lb_policy *policy,
39 const grpc_lb_policy_vtable *vtable) {
Craig Tiller4ab82d22015-06-29 09:40:33 -070040 policy->vtable = vtable;
Craig Tiller48613042015-11-29 14:45:11 -080041 gpr_atm_no_barrier_store(&policy->ref_pair, 1 << WEAK_REF_BITS);
Craig Tiller69b093b2016-02-25 19:04:07 -080042 policy->interested_parties = grpc_pollset_set_create();
Craig Tillerd7b68e72015-06-28 11:41:09 -070043}
Craig Tiller7c0b4d72015-06-25 14:44:44 -070044
Craig Tillerd7b68e72015-06-28 11:41:09 -070045#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
Craig Tiller48613042015-11-29 14:45:11 -080046#define REF_FUNC_EXTRA_ARGS , const char *file, int line, const char *reason
47#define REF_MUTATE_EXTRA_ARGS REF_FUNC_EXTRA_ARGS, const char *purpose
48#define REF_FUNC_PASS_ARGS(new_reason) , file, line, new_reason
49#define REF_MUTATE_PASS_ARGS(purpose) , file, line, reason, purpose
Craig Tillerd7b68e72015-06-28 11:41:09 -070050#else
Craig Tiller48613042015-11-29 14:45:11 -080051#define REF_FUNC_EXTRA_ARGS
52#define REF_MUTATE_EXTRA_ARGS
53#define REF_FUNC_PASS_ARGS(new_reason)
54#define REF_MUTATE_PASS_ARGS(x)
Craig Tillerd7b68e72015-06-28 11:41:09 -070055#endif
Craig Tiller48613042015-11-29 14:45:11 -080056
Craig Tiller1d881fb2015-12-01 07:39:04 -080057static gpr_atm ref_mutate(grpc_lb_policy *c, gpr_atm delta,
58 int barrier REF_MUTATE_EXTRA_ARGS) {
59 gpr_atm old_val = barrier ? gpr_atm_full_fetch_add(&c->ref_pair, delta)
60 : gpr_atm_no_barrier_fetch_add(&c->ref_pair, delta);
Craig Tiller48613042015-11-29 14:45:11 -080061#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
Craig Tiller1d881fb2015-12-01 07:39:04 -080062 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
David Garcia Quintas2a6c6832016-06-15 18:11:08 -070063 "LB_POLICY: 0x%" PRIxPTR " %12s 0x%" PRIxPTR " -> 0x%" PRIxPTR
64 " [%s]",
65 (intptr_t)c, purpose, old_val, old_val + delta, reason);
Craig Tiller48613042015-11-29 14:45:11 -080066#endif
67 return old_val;
Craig Tillerd7b68e72015-06-28 11:41:09 -070068}
69
Craig Tiller48613042015-11-29 14:45:11 -080070void grpc_lb_policy_ref(grpc_lb_policy *policy REF_FUNC_EXTRA_ARGS) {
71 ref_mutate(policy, 1 << WEAK_REF_BITS, 0 REF_MUTATE_PASS_ARGS("STRONG_REF"));
72}
73
Craig Tiller1d881fb2015-12-01 07:39:04 -080074void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx,
75 grpc_lb_policy *policy REF_FUNC_EXTRA_ARGS) {
76 gpr_atm old_val =
77 ref_mutate(policy, (gpr_atm)1 - (gpr_atm)(1 << WEAK_REF_BITS),
78 1 REF_MUTATE_PASS_ARGS("STRONG_UNREF"));
Craig Tiller48613042015-11-29 14:45:11 -080079 gpr_atm mask = ~(gpr_atm)((1 << WEAK_REF_BITS) - 1);
80 gpr_atm check = 1 << WEAK_REF_BITS;
81 if ((old_val & mask) == check) {
82 policy->vtable->shutdown(exec_ctx, policy);
83 }
Craig Tiller1d881fb2015-12-01 07:39:04 -080084 grpc_lb_policy_weak_unref(exec_ctx,
85 policy REF_FUNC_PASS_ARGS("strong-unref"));
Craig Tiller48613042015-11-29 14:45:11 -080086}
87
88void grpc_lb_policy_weak_ref(grpc_lb_policy *policy REF_FUNC_EXTRA_ARGS) {
89 ref_mutate(policy, 1, 0 REF_MUTATE_PASS_ARGS("WEAK_REF"));
90}
91
Craig Tiller1d881fb2015-12-01 07:39:04 -080092void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx,
93 grpc_lb_policy *policy REF_FUNC_EXTRA_ARGS) {
94 gpr_atm old_val =
95 ref_mutate(policy, -(gpr_atm)1, 1 REF_MUTATE_PASS_ARGS("WEAK_UNREF"));
Craig Tiller48613042015-11-29 14:45:11 -080096 if (old_val == 1) {
Craig Tiller9e5ac1b2017-02-14 22:25:50 -080097 grpc_pollset_set_destroy(exec_ctx, policy->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -070098 policy->vtable->destroy(exec_ctx, policy);
99 }
Craig Tiller7c0b4d72015-06-25 14:44:44 -0700100}
101
Craig Tiller577c9b22015-11-02 14:11:15 -0800102int grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
David Garcia Quintas8aace512016-08-15 14:55:12 -0700103 const grpc_lb_policy_pick_args *pick_args,
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700104 grpc_connected_subchannel **target, void **user_data,
Craig Tillerab33b482015-11-21 08:11:04 -0800105 grpc_closure *on_complete) {
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700106 return policy->vtable->pick(exec_ctx, policy, pick_args, target, user_data,
107 on_complete);
Craig Tiller577c9b22015-11-02 14:11:15 -0800108}
109
110void grpc_lb_policy_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700111 grpc_connected_subchannel **target,
112 grpc_error *error) {
113 policy->vtable->cancel_pick(exec_ctx, policy, target, error);
Craig Tiller7c0b4d72015-06-25 14:44:44 -0700114}
Craig Tillerca3e9d32015-06-27 18:37:27 -0700115
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800116void grpc_lb_policy_cancel_picks(grpc_exec_ctx *exec_ctx,
117 grpc_lb_policy *policy,
118 uint32_t initial_metadata_flags_mask,
Mark D. Rothe65ff112016-09-09 13:48:38 -0700119 uint32_t initial_metadata_flags_eq,
120 grpc_error *error) {
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800121 policy->vtable->cancel_picks(exec_ctx, policy, initial_metadata_flags_mask,
Mark D. Rothe65ff112016-09-09 13:48:38 -0700122 initial_metadata_flags_eq, error);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800123}
124
Craig Tillera82950e2015-09-22 12:33:20 -0700125void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy) {
126 policy->vtable->exit_idle(exec_ctx, policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700127}
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700128
Craig Tillere2c62372015-12-07 16:11:03 -0800129void grpc_lb_policy_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
130 grpc_closure *closure) {
Craig Tiller28bf8912015-12-07 16:07:04 -0800131 policy->vtable->ping_one(exec_ctx, policy, closure);
132}
133
Craig Tillera82950e2015-09-22 12:33:20 -0700134void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx,
135 grpc_lb_policy *policy,
136 grpc_connectivity_state *state,
137 grpc_closure *closure) {
138 policy->vtable->notify_on_state_change(exec_ctx, policy, state, closure);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700139}
140
Craig Tillera82950e2015-09-22 12:33:20 -0700141grpc_connectivity_state grpc_lb_policy_check_connectivity(
Craig Tiller804ff712016-05-05 16:25:40 -0700142 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
143 grpc_error **connectivity_error) {
144 return policy->vtable->check_connectivity(exec_ctx, policy,
145 connectivity_error);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700146}