blob: 837e9c1113788f6db014f1522071d2d14a32d885 [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)) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -070060 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream));
Craig Tillereb841e22016-02-11 15:49:16 -080061 return false;
62 }
63 dec_arg->num_servers++;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070064 } else { /* second pass. Actually decode. */
Craig Tillereb841e22016-02-11 15:49:16 -080065 grpc_grpclb_server *server = gpr_malloc(sizeof(grpc_grpclb_server));
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070066 memset(server, 0, sizeof(grpc_grpclb_server));
Craig Tillereb841e22016-02-11 15:49:16 -080067 GPR_ASSERT(dec_arg->num_servers > 0);
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070068 if (dec_arg->decoding_idx == 0) { /* first iteration of second pass */
Craig Tillereb841e22016-02-11 15:49:16 -080069 dec_arg->servers =
70 gpr_malloc(sizeof(grpc_grpclb_server *) * dec_arg->num_servers);
71 }
David Garcia Quintas7f0793a2016-04-25 12:35:58 -070072 if (!pb_decode(stream, grpc_lb_v1_Server_fields, server)) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -070073 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream));
Craig Tillereb841e22016-02-11 15:49:16 -080074 return false;
75 }
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070076 dec_arg->servers[dec_arg->decoding_idx++] = server;
Craig Tillereb841e22016-02-11 15:49:16 -080077 }
78
79 return true;
80}
81
82grpc_grpclb_request *grpc_grpclb_request_create(const char *lb_service_name) {
83 grpc_grpclb_request *req = gpr_malloc(sizeof(grpc_grpclb_request));
84
85 req->has_client_stats = 0; /* TODO(dgq): add support for stats once defined */
86 req->has_initial_request = 1;
87 req->initial_request.has_name = 1;
88 strncpy(req->initial_request.name, lb_service_name,
89 GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH);
90 return req;
91}
92
Craig Tillerd41a4a72016-10-26 16:16:06 -070093grpc_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) {
Craig Tillereb841e22016-02-11 15:49:16 -080094 size_t encoded_length;
95 pb_ostream_t sizestream;
96 pb_ostream_t outputstream;
Craig Tillerd41a4a72016-10-26 16:16:06 -070097 grpc_slice slice;
Craig Tillereb841e22016-02-11 15:49:16 -080098 memset(&sizestream, 0, sizeof(pb_ostream_t));
David Garcia Quintas7f0793a2016-04-25 12:35:58 -070099 pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request);
Craig Tillereb841e22016-02-11 15:49:16 -0800100 encoded_length = sizestream.bytes_written;
101
Craig Tillerd41a4a72016-10-26 16:16:06 -0700102 slice = grpc_slice_malloc(encoded_length);
Craig Tillereb841e22016-02-11 15:49:16 -0800103 outputstream =
Craig Tiller618e67d2016-10-26 21:08:10 -0700104 pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length);
David Garcia Quintas7f0793a2016-04-25 12:35:58 -0700105 GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields,
Craig Tillereb841e22016-02-11 15:49:16 -0800106 request) != 0);
107 return slice;
108}
109
110void grpc_grpclb_request_destroy(grpc_grpclb_request *request) {
111 gpr_free(request);
112}
113
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700114typedef grpc_lb_v1_LoadBalanceResponse grpc_grpclb_response;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700115grpc_grpclb_initial_response *grpc_grpclb_initial_response_parse(
Craig Tillerd41a4a72016-10-26 16:16:06 -0700116 grpc_slice encoded_grpc_grpclb_response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800117 pb_istream_t stream =
Craig Tiller618e67d2016-10-26 21:08:10 -0700118 pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response),
119 GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response));
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700120 grpc_grpclb_response res;
121 memset(&res, 0, sizeof(grpc_grpclb_response));
122 if (!pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res)) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -0700123 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700124 return NULL;
125 }
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700126 grpc_grpclb_initial_response *initial_res =
127 gpr_malloc(sizeof(grpc_grpclb_initial_response));
128 memcpy(initial_res, &res.initial_response,
129 sizeof(grpc_grpclb_initial_response));
130
131 return initial_res;
Craig Tillereb841e22016-02-11 15:49:16 -0800132}
133
134grpc_grpclb_serverlist *grpc_grpclb_response_parse_serverlist(
Craig Tillerd41a4a72016-10-26 16:16:06 -0700135 grpc_slice encoded_grpc_grpclb_response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800136 bool status;
137 decode_serverlist_arg arg;
138 pb_istream_t stream =
Craig Tiller618e67d2016-10-26 21:08:10 -0700139 pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response),
140 GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response));
Craig Tillereb841e22016-02-11 15:49:16 -0800141 pb_istream_t stream_at_start = stream;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700142 grpc_grpclb_response res;
143 memset(&res, 0, sizeof(grpc_grpclb_response));
Craig Tillereb841e22016-02-11 15:49:16 -0800144 memset(&arg, 0, sizeof(decode_serverlist_arg));
145
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700146 res.server_list.servers.funcs.decode = decode_serverlist;
147 res.server_list.servers.arg = &arg;
148 arg.first_pass = true;
149 status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res);
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700150 if (!status) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -0700151 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700152 return NULL;
153 }
Craig Tillereb841e22016-02-11 15:49:16 -0800154
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700155 arg.first_pass = false;
Craig Tillereb841e22016-02-11 15:49:16 -0800156 status =
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700157 pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields, &res);
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700158 if (!status) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -0700159 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700160 return NULL;
161 }
Craig Tillereb841e22016-02-11 15:49:16 -0800162
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700163 grpc_grpclb_serverlist *sl = gpr_malloc(sizeof(grpc_grpclb_serverlist));
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700164 memset(sl, 0, sizeof(*sl));
Craig Tillereb841e22016-02-11 15:49:16 -0800165 sl->num_servers = arg.num_servers;
166 sl->servers = arg.servers;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700167 if (res.server_list.has_expiration_interval) {
168 sl->expiration_interval = res.server_list.expiration_interval;
Craig Tillereb841e22016-02-11 15:49:16 -0800169 }
Craig Tillereb841e22016-02-11 15:49:16 -0800170 return sl;
171}
172
173void grpc_grpclb_destroy_serverlist(grpc_grpclb_serverlist *serverlist) {
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700174 if (serverlist == NULL) {
175 return;
176 }
177 for (size_t i = 0; i < serverlist->num_servers; i++) {
Craig Tillereb841e22016-02-11 15:49:16 -0800178 gpr_free(serverlist->servers[i]);
179 }
180 gpr_free(serverlist->servers);
181 gpr_free(serverlist);
182}
183
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700184grpc_grpclb_serverlist *grpc_grpclb_serverlist_copy(
185 const grpc_grpclb_serverlist *sl) {
186 grpc_grpclb_serverlist *copy = gpr_malloc(sizeof(grpc_grpclb_serverlist));
187 memset(copy, 0, sizeof(grpc_grpclb_serverlist));
188 copy->num_servers = sl->num_servers;
189 memcpy(&copy->expiration_interval, &sl->expiration_interval,
190 sizeof(grpc_grpclb_duration));
David Garcia Quintas8782d1b2016-06-15 23:58:44 -0700191 copy->servers = gpr_malloc(sizeof(grpc_grpclb_server *) * sl->num_servers);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700192 for (size_t i = 0; i < sl->num_servers; i++) {
193 copy->servers[i] = gpr_malloc(sizeof(grpc_grpclb_server));
194 memcpy(copy->servers[i], sl->servers[i], sizeof(grpc_grpclb_server));
195 }
196 return copy;
197}
198
199bool grpc_grpclb_serverlist_equals(const grpc_grpclb_serverlist *lhs,
200 const grpc_grpclb_serverlist *rhs) {
201 if ((lhs == NULL) || (rhs == NULL)) {
202 return false;
203 }
204 if (lhs->num_servers != rhs->num_servers) {
205 return false;
206 }
207 if (grpc_grpclb_duration_compare(&lhs->expiration_interval,
208 &rhs->expiration_interval) != 0) {
209 return false;
210 }
211 for (size_t i = 0; i < lhs->num_servers; i++) {
212 if (!grpc_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) {
213 return false;
214 }
215 }
216 return true;
217}
218
219bool grpc_grpclb_server_equals(const grpc_grpclb_server *lhs,
220 const grpc_grpclb_server *rhs) {
221 return memcmp(lhs, rhs, sizeof(grpc_grpclb_server)) == 0;
222}
223
224int grpc_grpclb_duration_compare(const grpc_grpclb_duration *lhs,
225 const grpc_grpclb_duration *rhs) {
226 GPR_ASSERT(lhs && rhs);
227 if (lhs->has_seconds && rhs->has_seconds) {
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700228 if (lhs->seconds < rhs->seconds) return -1;
229 if (lhs->seconds > rhs->seconds) return 1;
230 } else if (lhs->has_seconds) {
231 return 1;
232 } else if (rhs->has_seconds) {
233 return -1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700234 }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700235
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700236 GPR_ASSERT(lhs->seconds == rhs->seconds);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700237 if (lhs->has_nanos && rhs->has_nanos) {
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700238 if (lhs->nanos < rhs->nanos) return -1;
239 if (lhs->nanos > rhs->nanos) return 1;
240 } else if (lhs->has_nanos) {
241 return 1;
242 } else if (rhs->has_nanos) {
243 return -1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700244 }
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700245
246 return 0;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700247}
248
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700249void grpc_grpclb_initial_response_destroy(
250 grpc_grpclb_initial_response *response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800251 gpr_free(response);
252}