blob: b07824ae1baf401c18efe74ef2a7113cc27adcee [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller9d0e0472015-06-23 09:12:48 -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
Craig Tiller5aceb272016-03-31 13:50:42 -070034#ifndef GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H
35#define GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H
Craig Tiller9d0e0472015-06-23 09:12:48 -070036
Craig Tillerd4c98332016-03-31 13:45:47 -070037#include "src/core/ext/client_config/subchannel.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070038#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/transport/connectivity_state.h"
Craig Tiller9d0e0472015-06-23 09:12:48 -070040
41/** A load balancing policy: specified by a vtable and a struct (which
42 is expected to be extended to contain some parameters) */
43typedef struct grpc_lb_policy grpc_lb_policy;
44typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
45
Craig Tillera82950e2015-09-22 12:33:20 -070046typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel,
47 grpc_status_code status, const char *errmsg);
Craig Tiller9d0e0472015-06-23 09:12:48 -070048
Craig Tillera82950e2015-09-22 12:33:20 -070049struct grpc_lb_policy {
Craig Tillereb3b12e2015-06-26 14:42:49 -070050 const grpc_lb_policy_vtable *vtable;
Craig Tiller48613042015-11-29 14:45:11 -080051 gpr_atm ref_pair;
Craig Tiller69b093b2016-02-25 19:04:07 -080052 /* owned pointer to interested parties in load balancing decisions */
53 grpc_pollset_set *interested_parties;
Craig Tiller7c0b4d72015-06-25 14:44:44 -070054};
55
Craig Tillera82950e2015-09-22 12:33:20 -070056struct grpc_lb_policy_vtable {
57 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070058
Craig Tillera82950e2015-09-22 12:33:20 -070059 void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070060
61 /** implement grpc_lb_policy_pick */
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070062 int (*pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
63 grpc_polling_entity *pollent,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -070064 grpc_metadata_batch *initial_metadata,
Craig Tiller8c0d96f2016-03-11 14:27:52 -080065 uint32_t initial_metadata_flags,
Craig Tillerb5585d42015-11-17 07:18:31 -080066 grpc_connected_subchannel **target, grpc_closure *on_complete);
Craig Tiller577c9b22015-11-02 14:11:15 -080067 void (*cancel_pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Craig Tillerb5585d42015-11-17 07:18:31 -080068 grpc_connected_subchannel **target);
Craig Tiller8c0d96f2016-03-11 14:27:52 -080069 void (*cancel_picks)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
70 uint32_t initial_metadata_flags_mask,
71 uint32_t initial_metadata_flags_eq);
Craig Tillerc7b5f762015-06-27 11:48:42 -070072
Craig Tillere2c62372015-12-07 16:11:03 -080073 void (*ping_one)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
74 grpc_closure *closure);
Craig Tiller26dab312015-12-07 14:43:47 -080075
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070076 /** Try to enter a READY connectivity state */
Craig Tillera82950e2015-09-22 12:33:20 -070077 void (*exit_idle)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -070078
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070079 /** Check the current connectivity */
Craig Tillera82950e2015-09-22 12:33:20 -070080 grpc_connectivity_state (*check_connectivity)(grpc_exec_ctx *exec_ctx,
81 grpc_lb_policy *policy);
Craig Tillerc7b5f762015-06-27 11:48:42 -070082
83 /** call notify when the connectivity state of a channel changes from *state.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070084 Updates *state with the new state of the policy. Calling with a NULL \a
85 state cancels the subscription.
86 */
Craig Tillera82950e2015-09-22 12:33:20 -070087 void (*notify_on_state_change)(grpc_exec_ctx *exec_ctx,
88 grpc_lb_policy *policy,
89 grpc_connectivity_state *state,
90 grpc_closure *closure);
Craig Tiller9d0e0472015-06-23 09:12:48 -070091};
92
Craig Tiller54914ee2015-12-01 06:23:44 -080093/*#define GRPC_LB_POLICY_REFCOUNT_DEBUG*/
Craig Tillerd7b68e72015-06-28 11:41:09 -070094#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
Craig Tiller4ab82d22015-06-29 09:40:33 -070095#define GRPC_LB_POLICY_REF(p, r) \
96 grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -070097#define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \
98 grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tiller48613042015-11-29 14:45:11 -080099#define GRPC_LB_POLICY_WEAK_REF(p, r) \
100 grpc_lb_policy_weak_ref((p), __FILE__, __LINE__, (r))
101#define GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, p, r) \
102 grpc_lb_policy_weak_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tillera82950e2015-09-22 12:33:20 -0700103void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line,
104 const char *reason);
105void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
106 const char *file, int line, const char *reason);
Craig Tiller48613042015-11-29 14:45:11 -0800107void grpc_lb_policy_weak_ref(grpc_lb_policy *policy, const char *file, int line,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800108 const char *reason);
Craig Tiller48613042015-11-29 14:45:11 -0800109void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800110 const char *file, int line, const char *reason);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700111#else
112#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -0700113#define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p))
Craig Tiller48613042015-11-29 14:45:11 -0800114#define GRPC_LB_POLICY_WEAK_REF(p, r) grpc_lb_policy_weak_ref((p))
115#define GRPC_LB_POLICY_WEAK_UNREF(cl, p, r) grpc_lb_policy_weak_unref((cl), (p))
Craig Tillera82950e2015-09-22 12:33:20 -0700116void grpc_lb_policy_ref(grpc_lb_policy *policy);
117void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48613042015-11-29 14:45:11 -0800118void grpc_lb_policy_weak_ref(grpc_lb_policy *policy);
119void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700120#endif
121
122/** called by concrete implementations to initialize the base struct */
Craig Tillera82950e2015-09-22 12:33:20 -0700123void grpc_lb_policy_init(grpc_lb_policy *policy,
124 const grpc_lb_policy_vtable *vtable);
Craig Tillerf5f17122015-06-25 08:47:26 -0700125
Craig Tiller9d0e0472015-06-23 09:12:48 -0700126/** Given initial metadata in \a initial_metadata, find an appropriate
127 target for this rpc, and 'return' it by calling \a on_complete after setting
128 \a target.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700129 Picking can be asynchronous. Any IO should be done under \a pollent. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800130int grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700131 grpc_polling_entity *pollent,
132 grpc_metadata_batch *initial_metadata,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800133 uint32_t initial_metadata_flags,
Craig Tillerab33b482015-11-21 08:11:04 -0800134 grpc_connected_subchannel **target,
135 grpc_closure *on_complete);
Craig Tiller577c9b22015-11-02 14:11:15 -0800136
Craig Tillere2c62372015-12-07 16:11:03 -0800137void grpc_lb_policy_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
138 grpc_closure *closure);
Craig Tiller26dab312015-12-07 14:43:47 -0800139
Craig Tiller577c9b22015-11-02 14:11:15 -0800140void grpc_lb_policy_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Craig Tillerb5585d42015-11-17 07:18:31 -0800141 grpc_connected_subchannel **target);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700142
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800143/** Cancel all pending picks which have:
144 (initial_metadata_flags & initial_metadata_flags_mask) ==
145 initial_metadata_flags_eq */
146void grpc_lb_policy_cancel_picks(grpc_exec_ctx *exec_ctx,
147 grpc_lb_policy *policy,
148 uint32_t initial_metadata_flags_mask,
149 uint32_t initial_metadata_flags_eq);
150
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700151/** Try to enter a READY connectivity state */
Craig Tillera82950e2015-09-22 12:33:20 -0700152void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700153
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700154/* Call notify when the connectivity state of a channel changes from \a *state.
155 * Updates \a *state with the new state of the policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700156void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx,
157 grpc_lb_policy *policy,
158 grpc_connectivity_state *state,
159 grpc_closure *closure);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700160
Craig Tillera82950e2015-09-22 12:33:20 -0700161grpc_connectivity_state grpc_lb_policy_check_connectivity(
162 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700163
Craig Tiller5aceb272016-03-31 13:50:42 -0700164#endif /* GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H */