Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 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 | |
Craig Tiller | 5aceb27 | 2016-03-31 13:50:42 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H |
| 35 | #define GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 36 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 37 | #include "src/core/ext/client_config/subchannel.h" |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 39 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 40 | |
| 41 | /** A load balancing policy: specified by a vtable and a struct (which |
| 42 | is expected to be extended to contain some parameters) */ |
| 43 | typedef struct grpc_lb_policy grpc_lb_policy; |
| 44 | typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable; |
| 45 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel, |
| 47 | grpc_status_code status, const char *errmsg); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 48 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | struct grpc_lb_policy { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | const grpc_lb_policy_vtable *vtable; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 51 | gpr_atm ref_pair; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 52 | /* owned pointer to interested parties in load balancing decisions */ |
| 53 | grpc_pollset_set *interested_parties; |
Craig Tiller | 7c0b4d7 | 2015-06-25 14:44:44 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | struct grpc_lb_policy_vtable { |
| 57 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 58 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 60 | |
| 61 | /** implement grpc_lb_policy_pick */ |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 62 | int (*pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 63 | grpc_polling_entity *pollent, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 64 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 65 | uint32_t initial_metadata_flags, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 66 | grpc_connected_subchannel **target, grpc_closure *on_complete); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 67 | void (*cancel_pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 68 | grpc_connected_subchannel **target); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 69 | 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 Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 72 | |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 73 | void (*ping_one)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 74 | grpc_closure *closure); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 75 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame^] | 76 | /** Try to enter a READY connectivity state */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | void (*exit_idle)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 78 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame^] | 79 | /** Check the current connectivity */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | grpc_connectivity_state (*check_connectivity)(grpc_exec_ctx *exec_ctx, |
| 81 | grpc_lb_policy *policy); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 82 | |
| 83 | /** call notify when the connectivity state of a channel changes from *state. |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame^] | 84 | Updates *state with the new state of the policy. Calling with a NULL \a |
| 85 | state cancels the subscription. |
| 86 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | 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 Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Craig Tiller | 54914ee | 2015-12-01 06:23:44 -0800 | [diff] [blame] | 93 | /*#define GRPC_LB_POLICY_REFCOUNT_DEBUG*/ |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 94 | #ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 95 | #define GRPC_LB_POLICY_REF(p, r) \ |
| 96 | grpc_lb_policy_ref((p), __FILE__, __LINE__, (r)) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 97 | #define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \ |
| 98 | grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r)) |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 99 | #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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line, |
| 104 | const char *reason); |
| 105 | void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 106 | const char *file, int line, const char *reason); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 107 | void grpc_lb_policy_weak_ref(grpc_lb_policy *policy, const char *file, int line, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 108 | const char *reason); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 109 | void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 110 | const char *file, int line, const char *reason); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 111 | #else |
| 112 | #define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p)) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 113 | #define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p)) |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 114 | #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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | void grpc_lb_policy_ref(grpc_lb_policy *policy); |
| 117 | void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 118 | void grpc_lb_policy_weak_ref(grpc_lb_policy *policy); |
| 119 | void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | /** called by concrete implementations to initialize the base struct */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | void grpc_lb_policy_init(grpc_lb_policy *policy, |
| 124 | const grpc_lb_policy_vtable *vtable); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 125 | |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 126 | /** 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 Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame^] | 129 | Picking can be asynchronous. Any IO should be done under \a pollent. */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 130 | int grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 131 | grpc_polling_entity *pollent, |
| 132 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 133 | uint32_t initial_metadata_flags, |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 134 | grpc_connected_subchannel **target, |
| 135 | grpc_closure *on_complete); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 136 | |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 137 | void grpc_lb_policy_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 138 | grpc_closure *closure); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 139 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 140 | void grpc_lb_policy_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 141 | grpc_connected_subchannel **target); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 142 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 143 | /** Cancel all pending picks which have: |
| 144 | (initial_metadata_flags & initial_metadata_flags_mask) == |
| 145 | initial_metadata_flags_eq */ |
| 146 | void 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 Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame^] | 151 | /** Try to enter a READY connectivity state */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 153 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame^] | 154 | /* 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | void 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 Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 160 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | grpc_connectivity_state grpc_lb_policy_check_connectivity( |
| 162 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 163 | |
Craig Tiller | 5aceb27 | 2016-03-31 13:50:42 -0700 | [diff] [blame] | 164 | #endif /* GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H */ |