Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Tiller | af69180 | 2015-06-23 14:57:07 -0700 | [diff] [blame] | 37 | #include "src/core/client_config/subchannel.h" |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 38 | #include "src/core/transport/connectivity_state.h" |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 39 | |
| 40 | /** A load balancing policy: specified by a vtable and a struct (which |
| 41 | is expected to be extended to contain some parameters) */ |
| 42 | typedef struct grpc_lb_policy grpc_lb_policy; |
| 43 | typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable; |
| 44 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 45 | typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel, |
| 46 | grpc_status_code status, const char *errmsg); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | struct grpc_lb_policy { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 49 | const grpc_lb_policy_vtable *vtable; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 50 | gpr_refcount refs; |
Craig Tiller | 7c0b4d7 | 2015-06-25 14:44:44 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | struct grpc_lb_policy_vtable { |
| 54 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 55 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 57 | |
| 58 | /** implement grpc_lb_policy_pick */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 59 | 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 Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 64 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame^] | 65 | void (*ping_one)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, grpc_pollset *pollset, grpc_closure *closure); |
| 66 | |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 67 | /** try to enter a READY connectivity state */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | void (*exit_idle)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 69 | |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 70 | /** broadcast a transport op to all subchannels */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | void (*broadcast)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 72 | grpc_transport_op *op); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 73 | |
| 74 | /** check the current connectivity of the lb_policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | grpc_connectivity_state (*check_connectivity)(grpc_exec_ctx *exec_ctx, |
| 76 | grpc_lb_policy *policy); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 77 | |
| 78 | /** call notify when the connectivity state of a channel changes from *state. |
| 79 | Updates *state with the new state of the policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | void (*notify_on_state_change)(grpc_exec_ctx *exec_ctx, |
| 81 | grpc_lb_policy *policy, |
| 82 | grpc_connectivity_state *state, |
| 83 | grpc_closure *closure); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 86 | #ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 87 | #define GRPC_LB_POLICY_REF(p, r) \ |
| 88 | grpc_lb_policy_ref((p), __FILE__, __LINE__, (r)) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 89 | #define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \ |
| 90 | grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line, |
| 92 | const char *reason); |
| 93 | void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 94 | const char *file, int line, const char *reason); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 95 | #else |
| 96 | #define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p)) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 97 | #define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | void grpc_lb_policy_ref(grpc_lb_policy *policy); |
| 99 | void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | /** called by concrete implementations to initialize the base struct */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | void grpc_lb_policy_init(grpc_lb_policy *policy, |
| 104 | const grpc_lb_policy_vtable *vtable); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 105 | |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 106 | /** Start shutting down (fail any pending picks) */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | void grpc_lb_policy_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 108 | |
| 109 | /** Given initial metadata in \a initial_metadata, find an appropriate |
| 110 | target for this rpc, and 'return' it by calling \a on_complete after setting |
| 111 | \a target. |
| 112 | Picking can be asynchronous. Any IO should be done under \a pollset. */ |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 113 | int grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 114 | grpc_pollset *pollset, |
| 115 | grpc_metadata_batch *initial_metadata, |
| 116 | grpc_subchannel **target, grpc_closure *on_complete); |
| 117 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame^] | 118 | void grpc_lb_policy_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, grpc_pollset *pollset, grpc_closure *closure); |
| 119 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 120 | void grpc_lb_policy_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 121 | grpc_subchannel **target); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 122 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | void grpc_lb_policy_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 124 | grpc_transport_op *op); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 125 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | 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] | 127 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx, |
| 129 | grpc_lb_policy *policy, |
| 130 | grpc_connectivity_state *state, |
| 131 | grpc_closure *closure); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 132 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | grpc_connectivity_state grpc_lb_policy_check_connectivity( |
| 134 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 135 | |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 136 | #endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_H */ |