blob: a696c3ce64561040317dd0bb29701fc4568907a3 [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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
34#ifndef GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_H
35#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_H
36
Craig Tilleraf691802015-06-23 14:57:07 -070037#include "src/core/client_config/subchannel.h"
Craig Tiller5795da72015-09-17 15:27:13 -070038#include "src/core/transport/connectivity_state.h"
Craig Tiller9d0e0472015-06-23 09:12:48 -070039
40/** A load balancing policy: specified by a vtable and a struct (which
41 is expected to be extended to contain some parameters) */
42typedef struct grpc_lb_policy grpc_lb_policy;
43typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
44
Craig Tillera82950e2015-09-22 12:33:20 -070045typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel,
46 grpc_status_code status, const char *errmsg);
Craig Tiller9d0e0472015-06-23 09:12:48 -070047
Craig Tillera82950e2015-09-22 12:33:20 -070048struct grpc_lb_policy {
Craig Tillereb3b12e2015-06-26 14:42:49 -070049 const grpc_lb_policy_vtable *vtable;
Craig Tillerd7b68e72015-06-28 11:41:09 -070050 gpr_refcount refs;
Craig Tiller7c0b4d72015-06-25 14:44:44 -070051};
52
Craig Tillera82950e2015-09-22 12:33:20 -070053struct grpc_lb_policy_vtable {
54 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070055
Craig Tillera82950e2015-09-22 12:33:20 -070056 void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070057
58 /** implement grpc_lb_policy_pick */
Craig Tiller577c9b22015-11-02 14:11:15 -080059 int (*pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
60 grpc_pollset *pollset, grpc_metadata_batch *initial_metadata,
61 grpc_subchannel **target, grpc_closure *on_complete);
62 void (*cancel_pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
63 grpc_subchannel **target);
Craig Tillerc7b5f762015-06-27 11:48:42 -070064
Craig Tiller48cb07c2015-07-15 16:16:15 -070065 /** try to enter a READY connectivity state */
Craig Tillera82950e2015-09-22 12:33:20 -070066 void (*exit_idle)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -070067
Craig Tillerc7b5f762015-06-27 11:48:42 -070068 /** broadcast a transport op to all subchannels */
Craig Tillera82950e2015-09-22 12:33:20 -070069 void (*broadcast)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
70 grpc_transport_op *op);
Craig Tillerc7b5f762015-06-27 11:48:42 -070071
72 /** check the current connectivity of the lb_policy */
Craig Tillera82950e2015-09-22 12:33:20 -070073 grpc_connectivity_state (*check_connectivity)(grpc_exec_ctx *exec_ctx,
74 grpc_lb_policy *policy);
Craig Tillerc7b5f762015-06-27 11:48:42 -070075
76 /** call notify when the connectivity state of a channel changes from *state.
77 Updates *state with the new state of the policy */
Craig Tillera82950e2015-09-22 12:33:20 -070078 void (*notify_on_state_change)(grpc_exec_ctx *exec_ctx,
79 grpc_lb_policy *policy,
80 grpc_connectivity_state *state,
81 grpc_closure *closure);
Craig Tiller9d0e0472015-06-23 09:12:48 -070082};
83
Craig Tillerd7b68e72015-06-28 11:41:09 -070084#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
Craig Tiller4ab82d22015-06-29 09:40:33 -070085#define GRPC_LB_POLICY_REF(p, r) \
86 grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -070087#define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \
88 grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tillera82950e2015-09-22 12:33:20 -070089void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line,
90 const char *reason);
91void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
92 const char *file, int line, const char *reason);
Craig Tillerd7b68e72015-06-28 11:41:09 -070093#else
94#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -070095#define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p))
Craig Tillera82950e2015-09-22 12:33:20 -070096void grpc_lb_policy_ref(grpc_lb_policy *policy);
97void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tillerd7b68e72015-06-28 11:41:09 -070098#endif
99
100/** called by concrete implementations to initialize the base struct */
Craig Tillera82950e2015-09-22 12:33:20 -0700101void grpc_lb_policy_init(grpc_lb_policy *policy,
102 const grpc_lb_policy_vtable *vtable);
Craig Tillerf5f17122015-06-25 08:47:26 -0700103
Craig Tiller9d0e0472015-06-23 09:12:48 -0700104/** Start shutting down (fail any pending picks) */
Craig Tillera82950e2015-09-22 12:33:20 -0700105void grpc_lb_policy_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700106
107/** Given initial metadata in \a initial_metadata, find an appropriate
108 target for this rpc, and 'return' it by calling \a on_complete after setting
109 \a target.
110 Picking can be asynchronous. Any IO should be done under \a pollset. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800111int grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
112 grpc_pollset *pollset,
113 grpc_metadata_batch *initial_metadata,
114 grpc_subchannel **target, grpc_closure *on_complete);
115
116void grpc_lb_policy_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
117 grpc_subchannel **target);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700118
Craig Tillera82950e2015-09-22 12:33:20 -0700119void grpc_lb_policy_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
120 grpc_transport_op *op);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700121
Craig Tillera82950e2015-09-22 12:33:20 -0700122void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700123
Craig Tillera82950e2015-09-22 12:33:20 -0700124void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx,
125 grpc_lb_policy *policy,
126 grpc_connectivity_state *state,
127 grpc_closure *closure);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700128
Craig Tillera82950e2015-09-22 12:33:20 -0700129grpc_connectivity_state grpc_lb_policy_check_connectivity(
130 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700131
Craig Tiller9d0e0472015-06-23 09:12:48 -0700132#endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_H */