blob: f4720a13455209dfc0d7623d93b4b7f30f30b1a7 [file] [log] [blame]
Craig Tillereb841e22016-02-11 15:49:16 -08001/*
2 *
3 * Copyright 2016, 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
Craig Tiller14e0d982016-03-28 17:11:26 -070034#include "src/core/ext/lb_policy/grpclb/load_balancer_api.h"
Craig Tillereb841e22016-02-11 15:49:16 -080035#include "third_party/nanopb/pb_decode.h"
36#include "third_party/nanopb/pb_encode.h"
37
38#include <grpc/support/alloc.h>
39
40typedef struct decode_serverlist_arg {
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070041 /* The first pass counts the number of servers in the server list. The second
42 * one allocates and decodes. */
43 bool first_pass;
44 /* The decoding callback is invoked once per server in serverlist. Remember
45 * which index of the serverlist are we currently decoding */
46 size_t decoding_idx;
47 /* Populated after the first pass. Number of server in the input serverlist */
Craig Tillereb841e22016-02-11 15:49:16 -080048 size_t num_servers;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070049 /* The decoded serverlist */
Craig Tillereb841e22016-02-11 15:49:16 -080050 grpc_grpclb_server **servers;
51} decode_serverlist_arg;
52
53/* invoked once for every Server in ServerList */
54static bool decode_serverlist(pb_istream_t *stream, const pb_field_t *field,
55 void **arg) {
56 decode_serverlist_arg *dec_arg = *arg;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070057 if (dec_arg->first_pass) { /* count how many server do we have */
Craig Tillereb841e22016-02-11 15:49:16 -080058 grpc_grpclb_server server;
David Garcia Quintas7f0793a2016-04-25 12:35:58 -070059 if (!pb_decode(stream, grpc_lb_v1_Server_fields, &server)) {
Craig Tillereb841e22016-02-11 15:49:16 -080060 return false;
61 }
62 dec_arg->num_servers++;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070063 } else { /* second pass. Actually decode. */
Craig Tillereb841e22016-02-11 15:49:16 -080064 grpc_grpclb_server *server = gpr_malloc(sizeof(grpc_grpclb_server));
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070065 memset(server, 0, sizeof(grpc_grpclb_server));
Craig Tillereb841e22016-02-11 15:49:16 -080066 GPR_ASSERT(dec_arg->num_servers > 0);
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070067 if (dec_arg->decoding_idx == 0) { /* first iteration of second pass */
Craig Tillereb841e22016-02-11 15:49:16 -080068 dec_arg->servers =
69 gpr_malloc(sizeof(grpc_grpclb_server *) * dec_arg->num_servers);
70 }
David Garcia Quintas7f0793a2016-04-25 12:35:58 -070071 if (!pb_decode(stream, grpc_lb_v1_Server_fields, server)) {
Craig Tillereb841e22016-02-11 15:49:16 -080072 return false;
73 }
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070074 dec_arg->servers[dec_arg->decoding_idx++] = server;
Craig Tillereb841e22016-02-11 15:49:16 -080075 }
76
77 return true;
78}
79
80grpc_grpclb_request *grpc_grpclb_request_create(const char *lb_service_name) {
81 grpc_grpclb_request *req = gpr_malloc(sizeof(grpc_grpclb_request));
82
83 req->has_client_stats = 0; /* TODO(dgq): add support for stats once defined */
84 req->has_initial_request = 1;
85 req->initial_request.has_name = 1;
86 strncpy(req->initial_request.name, lb_service_name,
87 GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH);
88 return req;
89}
90
91gpr_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) {
92 size_t encoded_length;
93 pb_ostream_t sizestream;
94 pb_ostream_t outputstream;
95 gpr_slice slice;
96 memset(&sizestream, 0, sizeof(pb_ostream_t));
David Garcia Quintas7f0793a2016-04-25 12:35:58 -070097 pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request);
Craig Tillereb841e22016-02-11 15:49:16 -080098 encoded_length = sizestream.bytes_written;
99
100 slice = gpr_slice_malloc(encoded_length);
101 outputstream =
102 pb_ostream_from_buffer(GPR_SLICE_START_PTR(slice), encoded_length);
David Garcia Quintas7f0793a2016-04-25 12:35:58 -0700103 GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields,
Craig Tillereb841e22016-02-11 15:49:16 -0800104 request) != 0);
105 return slice;
106}
107
108void grpc_grpclb_request_destroy(grpc_grpclb_request *request) {
109 gpr_free(request);
110}
111
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700112typedef grpc_lb_v1_LoadBalanceResponse grpc_grpclb_response;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700113grpc_grpclb_initial_response *grpc_grpclb_initial_response_parse(
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700114 gpr_slice encoded_grpc_grpclb_response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800115 pb_istream_t stream =
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700116 pb_istream_from_buffer(GPR_SLICE_START_PTR(encoded_grpc_grpclb_response),
117 GPR_SLICE_LENGTH(encoded_grpc_grpclb_response));
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700118 grpc_grpclb_response res;
119 memset(&res, 0, sizeof(grpc_grpclb_response));
120 if (!pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res)) {
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700121 return NULL;
122 }
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700123 grpc_grpclb_initial_response *initial_res =
124 gpr_malloc(sizeof(grpc_grpclb_initial_response));
125 memcpy(initial_res, &res.initial_response,
126 sizeof(grpc_grpclb_initial_response));
127
128 return initial_res;
Craig Tillereb841e22016-02-11 15:49:16 -0800129}
130
131grpc_grpclb_serverlist *grpc_grpclb_response_parse_serverlist(
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700132 gpr_slice encoded_grpc_grpclb_response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800133 bool status;
134 decode_serverlist_arg arg;
135 pb_istream_t stream =
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700136 pb_istream_from_buffer(GPR_SLICE_START_PTR(encoded_grpc_grpclb_response),
137 GPR_SLICE_LENGTH(encoded_grpc_grpclb_response));
Craig Tillereb841e22016-02-11 15:49:16 -0800138 pb_istream_t stream_at_start = stream;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700139 grpc_grpclb_response res;
140 memset(&res, 0, sizeof(grpc_grpclb_response));
Craig Tillereb841e22016-02-11 15:49:16 -0800141 memset(&arg, 0, sizeof(decode_serverlist_arg));
142
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700143 res.server_list.servers.funcs.decode = decode_serverlist;
144 res.server_list.servers.arg = &arg;
145 arg.first_pass = true;
146 status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res);
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700147 if (!status) {
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700148 return NULL;
149 }
Craig Tillereb841e22016-02-11 15:49:16 -0800150
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700151 arg.first_pass = false;
Craig Tillereb841e22016-02-11 15:49:16 -0800152 status =
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700153 pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields, &res);
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700154 if (!status) {
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700155 return NULL;
156 }
Craig Tillereb841e22016-02-11 15:49:16 -0800157
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700158 grpc_grpclb_serverlist *sl = gpr_malloc(sizeof(grpc_grpclb_serverlist));
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700159 memset(sl, 0, sizeof(*sl));
Craig Tillereb841e22016-02-11 15:49:16 -0800160 sl->num_servers = arg.num_servers;
161 sl->servers = arg.servers;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700162 if (res.server_list.has_expiration_interval) {
163 sl->expiration_interval = res.server_list.expiration_interval;
Craig Tillereb841e22016-02-11 15:49:16 -0800164 }
Craig Tillereb841e22016-02-11 15:49:16 -0800165 return sl;
166}
167
168void grpc_grpclb_destroy_serverlist(grpc_grpclb_serverlist *serverlist) {
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700169 if (serverlist == NULL) {
170 return;
171 }
172 for (size_t i = 0; i < serverlist->num_servers; i++) {
Craig Tillereb841e22016-02-11 15:49:16 -0800173 gpr_free(serverlist->servers[i]);
174 }
175 gpr_free(serverlist->servers);
176 gpr_free(serverlist);
177}
178
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700179grpc_grpclb_serverlist *grpc_grpclb_serverlist_copy(
180 const grpc_grpclb_serverlist *sl) {
181 grpc_grpclb_serverlist *copy = gpr_malloc(sizeof(grpc_grpclb_serverlist));
182 memset(copy, 0, sizeof(grpc_grpclb_serverlist));
183 copy->num_servers = sl->num_servers;
184 memcpy(&copy->expiration_interval, &sl->expiration_interval,
185 sizeof(grpc_grpclb_duration));
David Garcia Quintas8782d1b2016-06-15 23:58:44 -0700186 copy->servers = gpr_malloc(sizeof(grpc_grpclb_server *) * sl->num_servers);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700187 for (size_t i = 0; i < sl->num_servers; i++) {
188 copy->servers[i] = gpr_malloc(sizeof(grpc_grpclb_server));
189 memcpy(copy->servers[i], sl->servers[i], sizeof(grpc_grpclb_server));
190 }
191 return copy;
192}
193
194bool grpc_grpclb_serverlist_equals(const grpc_grpclb_serverlist *lhs,
195 const grpc_grpclb_serverlist *rhs) {
196 if ((lhs == NULL) || (rhs == NULL)) {
197 return false;
198 }
199 if (lhs->num_servers != rhs->num_servers) {
200 return false;
201 }
202 if (grpc_grpclb_duration_compare(&lhs->expiration_interval,
203 &rhs->expiration_interval) != 0) {
204 return false;
205 }
206 for (size_t i = 0; i < lhs->num_servers; i++) {
207 if (!grpc_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) {
208 return false;
209 }
210 }
211 return true;
212}
213
214bool grpc_grpclb_server_equals(const grpc_grpclb_server *lhs,
215 const grpc_grpclb_server *rhs) {
216 return memcmp(lhs, rhs, sizeof(grpc_grpclb_server)) == 0;
217}
218
219int grpc_grpclb_duration_compare(const grpc_grpclb_duration *lhs,
220 const grpc_grpclb_duration *rhs) {
221 GPR_ASSERT(lhs && rhs);
222 if (lhs->has_seconds && rhs->has_seconds) {
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700223 if (lhs->seconds < rhs->seconds) return -1;
224 if (lhs->seconds > rhs->seconds) return 1;
225 } else if (lhs->has_seconds) {
226 return 1;
227 } else if (rhs->has_seconds) {
228 return -1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700229 }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700230
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700231 GPR_ASSERT(lhs->seconds == rhs->seconds);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700232 if (lhs->has_nanos && rhs->has_nanos) {
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700233 if (lhs->nanos < rhs->nanos) return -1;
234 if (lhs->nanos > rhs->nanos) return 1;
235 } else if (lhs->has_nanos) {
236 return 1;
237 } else if (rhs->has_nanos) {
238 return -1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700239 }
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700240
241 return 0;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700242}
243
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700244void grpc_grpclb_initial_response_destroy(
245 grpc_grpclb_initial_response *response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800246 gpr_free(response);
247}