blob: 3c4604c4022f20a9f5b25ea37808eb6a7205678a [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 Tiller6f417882017-02-16 14:09:39 -080065 grpc_grpclb_server *server = gpr_zalloc(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)) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -070072 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream));
Craig Tillereb841e22016-02-11 15:49:16 -080073 return false;
74 }
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -070075 dec_arg->servers[dec_arg->decoding_idx++] = server;
Craig Tillereb841e22016-02-11 15:49:16 -080076 }
77
78 return true;
79}
80
81grpc_grpclb_request *grpc_grpclb_request_create(const char *lb_service_name) {
82 grpc_grpclb_request *req = gpr_malloc(sizeof(grpc_grpclb_request));
83
84 req->has_client_stats = 0; /* TODO(dgq): add support for stats once defined */
85 req->has_initial_request = 1;
86 req->initial_request.has_name = 1;
87 strncpy(req->initial_request.name, lb_service_name,
88 GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH);
89 return req;
90}
91
Craig Tillerd41a4a72016-10-26 16:16:06 -070092grpc_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) {
Craig Tillereb841e22016-02-11 15:49:16 -080093 size_t encoded_length;
94 pb_ostream_t sizestream;
95 pb_ostream_t outputstream;
Craig Tillerd41a4a72016-10-26 16:16:06 -070096 grpc_slice slice;
Craig Tillereb841e22016-02-11 15:49:16 -080097 memset(&sizestream, 0, sizeof(pb_ostream_t));
David Garcia Quintas7f0793a2016-04-25 12:35:58 -070098 pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request);
Craig Tillereb841e22016-02-11 15:49:16 -080099 encoded_length = sizestream.bytes_written;
100
Craig Tillerd41a4a72016-10-26 16:16:06 -0700101 slice = grpc_slice_malloc(encoded_length);
Craig Tillereb841e22016-02-11 15:49:16 -0800102 outputstream =
Craig Tiller618e67d2016-10-26 21:08:10 -0700103 pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length);
David Garcia Quintas7f0793a2016-04-25 12:35:58 -0700104 GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields,
Craig Tillereb841e22016-02-11 15:49:16 -0800105 request) != 0);
106 return slice;
107}
108
109void grpc_grpclb_request_destroy(grpc_grpclb_request *request) {
110 gpr_free(request);
111}
112
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700113typedef grpc_lb_v1_LoadBalanceResponse grpc_grpclb_response;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700114grpc_grpclb_initial_response *grpc_grpclb_initial_response_parse(
Craig Tillerd41a4a72016-10-26 16:16:06 -0700115 grpc_slice encoded_grpc_grpclb_response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800116 pb_istream_t stream =
Craig Tiller618e67d2016-10-26 21:08:10 -0700117 pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response),
118 GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response));
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700119 grpc_grpclb_response res;
120 memset(&res, 0, sizeof(grpc_grpclb_response));
121 if (!pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res)) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -0700122 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700123 return NULL;
124 }
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700125 grpc_grpclb_initial_response *initial_res =
126 gpr_malloc(sizeof(grpc_grpclb_initial_response));
127 memcpy(initial_res, &res.initial_response,
128 sizeof(grpc_grpclb_initial_response));
129
130 return initial_res;
Craig Tillereb841e22016-02-11 15:49:16 -0800131}
132
133grpc_grpclb_serverlist *grpc_grpclb_response_parse_serverlist(
Craig Tillerd41a4a72016-10-26 16:16:06 -0700134 grpc_slice encoded_grpc_grpclb_response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800135 bool status;
136 decode_serverlist_arg arg;
137 pb_istream_t stream =
Craig Tiller618e67d2016-10-26 21:08:10 -0700138 pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response),
139 GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response));
Craig Tillereb841e22016-02-11 15:49:16 -0800140 pb_istream_t stream_at_start = stream;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700141 grpc_grpclb_response res;
142 memset(&res, 0, sizeof(grpc_grpclb_response));
Craig Tillereb841e22016-02-11 15:49:16 -0800143 memset(&arg, 0, sizeof(decode_serverlist_arg));
144
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700145 res.server_list.servers.funcs.decode = decode_serverlist;
146 res.server_list.servers.arg = &arg;
147 arg.first_pass = true;
148 status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res);
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700149 if (!status) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -0700150 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700151 return NULL;
152 }
Craig Tillereb841e22016-02-11 15:49:16 -0800153
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700154 arg.first_pass = false;
Craig Tillereb841e22016-02-11 15:49:16 -0800155 status =
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700156 pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields, &res);
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700157 if (!status) {
David Garcia Quintas18bc43b2016-08-22 15:05:32 -0700158 gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700159 return NULL;
160 }
Craig Tillereb841e22016-02-11 15:49:16 -0800161
Craig Tiller6f417882017-02-16 14:09:39 -0800162 grpc_grpclb_serverlist *sl = gpr_zalloc(sizeof(grpc_grpclb_serverlist));
Craig Tillereb841e22016-02-11 15:49:16 -0800163 sl->num_servers = arg.num_servers;
164 sl->servers = arg.servers;
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700165 if (res.server_list.has_expiration_interval) {
166 sl->expiration_interval = res.server_list.expiration_interval;
Craig Tillereb841e22016-02-11 15:49:16 -0800167 }
Craig Tillereb841e22016-02-11 15:49:16 -0800168 return sl;
169}
170
171void grpc_grpclb_destroy_serverlist(grpc_grpclb_serverlist *serverlist) {
David Garcia Quintasbf2e73d2016-04-04 18:03:47 -0700172 if (serverlist == NULL) {
173 return;
174 }
175 for (size_t i = 0; i < serverlist->num_servers; i++) {
Craig Tillereb841e22016-02-11 15:49:16 -0800176 gpr_free(serverlist->servers[i]);
177 }
178 gpr_free(serverlist->servers);
179 gpr_free(serverlist);
180}
181
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700182grpc_grpclb_serverlist *grpc_grpclb_serverlist_copy(
183 const grpc_grpclb_serverlist *sl) {
Craig Tiller6f417882017-02-16 14:09:39 -0800184 grpc_grpclb_serverlist *copy = gpr_zalloc(sizeof(grpc_grpclb_serverlist));
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700185 copy->num_servers = sl->num_servers;
186 memcpy(&copy->expiration_interval, &sl->expiration_interval,
187 sizeof(grpc_grpclb_duration));
David Garcia Quintas8782d1b2016-06-15 23:58:44 -0700188 copy->servers = gpr_malloc(sizeof(grpc_grpclb_server *) * sl->num_servers);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700189 for (size_t i = 0; i < sl->num_servers; i++) {
190 copy->servers[i] = gpr_malloc(sizeof(grpc_grpclb_server));
191 memcpy(copy->servers[i], sl->servers[i], sizeof(grpc_grpclb_server));
192 }
193 return copy;
194}
195
196bool grpc_grpclb_serverlist_equals(const grpc_grpclb_serverlist *lhs,
197 const grpc_grpclb_serverlist *rhs) {
198 if ((lhs == NULL) || (rhs == NULL)) {
199 return false;
200 }
201 if (lhs->num_servers != rhs->num_servers) {
202 return false;
203 }
204 if (grpc_grpclb_duration_compare(&lhs->expiration_interval,
205 &rhs->expiration_interval) != 0) {
206 return false;
207 }
208 for (size_t i = 0; i < lhs->num_servers; i++) {
209 if (!grpc_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) {
210 return false;
211 }
212 }
213 return true;
214}
215
216bool grpc_grpclb_server_equals(const grpc_grpclb_server *lhs,
217 const grpc_grpclb_server *rhs) {
218 return memcmp(lhs, rhs, sizeof(grpc_grpclb_server)) == 0;
219}
220
221int grpc_grpclb_duration_compare(const grpc_grpclb_duration *lhs,
222 const grpc_grpclb_duration *rhs) {
223 GPR_ASSERT(lhs && rhs);
224 if (lhs->has_seconds && rhs->has_seconds) {
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700225 if (lhs->seconds < rhs->seconds) return -1;
226 if (lhs->seconds > rhs->seconds) return 1;
227 } else if (lhs->has_seconds) {
228 return 1;
229 } else if (rhs->has_seconds) {
230 return -1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700231 }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700232
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700233 GPR_ASSERT(lhs->seconds == rhs->seconds);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700234 if (lhs->has_nanos && rhs->has_nanos) {
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700235 if (lhs->nanos < rhs->nanos) return -1;
236 if (lhs->nanos > rhs->nanos) return 1;
237 } else if (lhs->has_nanos) {
238 return 1;
239 } else if (rhs->has_nanos) {
240 return -1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700241 }
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700242
243 return 0;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700244}
245
David Garcia Quintas1d5cb2a2016-07-18 12:56:53 -0700246void grpc_grpclb_initial_response_destroy(
247 grpc_grpclb_initial_response *response) {
Craig Tillereb841e22016-02-11 15:49:16 -0800248 gpr_free(response);
249}