load_balancer_api.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <string.h>
24 
25 #include <algorithm>
26 
29 
31 #include <grpc/support/log.h>
32 #include <grpc/support/time.h>
33 
36 
37 namespace grpc_core {
38 
39 bool GrpcLbServer::operator==(const GrpcLbServer& other) const {
40  if (ip_size != other.ip_size) return false;
41  int r = memcmp(ip_addr, other.ip_addr, ip_size);
42  if (r != 0) return false;
43  if (port != other.port) return false;
44  r = strncmp(load_balance_token, other.load_balance_token,
45  sizeof(load_balance_token));
46  if (r != 0) return false;
47  return drop == other.drop;
48 }
49 
50 namespace {
51 
52 grpc_slice grpc_grpclb_request_encode(
54  size_t buf_length;
55  char* buf =
57  return grpc_slice_from_copied_buffer(buf, buf_length);
58 }
59 
60 } // namespace
61 
62 grpc_slice GrpcLbRequestCreate(const char* lb_service_name, upb_Arena* arena) {
64  grpc_lb_v1_InitialLoadBalanceRequest* initial_request =
66  size_t name_len = std::min(strlen(lb_service_name),
69  initial_request,
70  upb_StringView_FromDataAndSize(lb_service_name, name_len));
71  return grpc_grpclb_request_encode(req, arena);
72 }
73 
74 namespace {
75 
76 void google_protobuf_Timestamp_assign(google_protobuf_Timestamp* timestamp,
77  const gpr_timespec& value) {
79  google_protobuf_Timestamp_set_nanos(timestamp, value.tv_nsec);
80 }
81 
82 } // namespace
83 
89  upb_Arena* arena) {
91  grpc_lb_v1_ClientStats* req_stats =
93  google_protobuf_Timestamp_assign(
102  if (drop_token_counts != nullptr) {
103  for (size_t i = 0; i < drop_token_counts->size(); ++i) {
104  const GrpcLbClientStats::DropTokenCount& cur = (*drop_token_counts)[i];
107  const size_t token_len = strlen(cur.token.get());
108  char* token = reinterpret_cast<char*>(upb_Arena_Malloc(arena, token_len));
109  memcpy(token, cur.token.get(), token_len);
111  cur_msg, upb_StringView_FromDataAndSize(token, token_len));
113  }
114  }
115  return grpc_grpclb_request_encode(req, arena);
116 }
117 
118 namespace {
119 
120 bool ParseServerList(const grpc_lb_v1_LoadBalanceResponse& response,
121  std::vector<GrpcLbServer>* server_list) {
122  // Determine the number of servers.
123  const grpc_lb_v1_ServerList* server_list_msg =
125  if (server_list_msg == nullptr) return false;
126  size_t server_count = 0;
127  const grpc_lb_v1_Server* const* servers =
128  grpc_lb_v1_ServerList_servers(server_list_msg, &server_count);
129  // Populate servers.
130  if (server_count > 0) {
131  server_list->reserve(server_count);
132  for (size_t i = 0; i < server_count; ++i) {
133  GrpcLbServer& cur = *server_list->emplace(server_list->end());
135  if (address.size == 0) {
136  ; // Nothing to do because cur->ip_address is an empty string.
137  } else if (address.size <= GRPC_GRPCLB_SERVER_IP_ADDRESS_MAX_SIZE) {
138  cur.ip_size = static_cast<int32_t>(address.size);
139  memcpy(cur.ip_addr, address.data, address.size);
140  }
143  if (token.size == 0) {
144  ; // Nothing to do because cur->load_balance_token is an empty string.
146  memcpy(cur.load_balance_token, token.data, token.size);
147  } else {
149  "grpc_lb_v1_LoadBalanceResponse has too long token. len=%zu",
150  token.size);
151  }
153  }
154  }
155  return true;
156 }
157 
158 Duration ParseDuration(const google_protobuf_Duration* duration_pb) {
161  google_protobuf_Duration_nanos(duration_pb));
162 }
163 
164 } // namespace
165 
166 bool GrpcLbResponseParse(const grpc_slice& serialized_response,
170  reinterpret_cast<const char*>(
171  GRPC_SLICE_START_PTR(serialized_response)),
172  GRPC_SLICE_LENGTH(serialized_response), arena);
173  // Handle serverlist responses.
174  if (ParseServerList(*response, &result->serverlist)) {
175  result->type = result->SERVERLIST;
176  return true;
177  }
178  // Handle initial responses.
179  auto* initial_response =
181  if (initial_response != nullptr) {
182  result->type = result->INITIAL;
183  const google_protobuf_Duration* client_stats_report_interval =
185  initial_response);
186  if (client_stats_report_interval != nullptr) {
187  result->client_stats_report_interval =
188  ParseDuration(client_stats_report_interval);
189  }
190  return true;
191  }
192  // Handle fallback.
194  result->type = result->FALLBACK;
195  return true;
196  }
197  // Unknown response type.
198  return false;
199 }
200 
201 } // namespace grpc_core
grpc_lb_v1_ClientStats_set_num_calls_finished_with_client_failed_to_send
UPB_INLINE void grpc_lb_v1_ClientStats_set_num_calls_finished_with_client_failed_to_send(grpc_lb_v1_ClientStats *msg, int64_t value)
Definition: load_balancer.upb.h:326
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc_lb_v1_LoadBalanceRequest
struct grpc_lb_v1_LoadBalanceRequest grpc_lb_v1_LoadBalanceRequest
Definition: load_balancer.upb.h:32
log.h
grpc_lb_v1_ClientStatsPerToken
struct grpc_lb_v1_ClientStatsPerToken grpc_lb_v1_ClientStatsPerToken
Definition: load_balancer.upb.h:34
grpc_lb_v1_LoadBalanceResponse_initial_response
const UPB_INLINE grpc_lb_v1_InitialLoadBalanceResponse * grpc_lb_v1_LoadBalanceResponse_initial_response(const grpc_lb_v1_LoadBalanceResponse *msg)
Definition: load_balancer.upb.h:391
grpc_lb_v1_ServerList_servers
const UPB_INLINE grpc_lb_v1_Server *const * grpc_lb_v1_ServerList_servers(const grpc_lb_v1_ServerList *msg, size_t *len)
Definition: load_balancer.upb.h:575
google_protobuf_Timestamp_set_seconds
UPB_INLINE void google_protobuf_Timestamp_set_seconds(google_protobuf_Timestamp *msg, int64_t value)
Definition: timestamp.upb.h:73
grpc_lb_v1_LoadBalanceRequest_new
UPB_INLINE grpc_lb_v1_LoadBalanceRequest * grpc_lb_v1_LoadBalanceRequest_new(upb_Arena *arena)
Definition: load_balancer.upb.h:59
grpc_core
Definition: call_metric_recorder.h:31
upb_StringView::data
const char * data
Definition: upb/upb/upb.h:73
grpc_lb_v1_Server_load_balance_token
UPB_INLINE upb_StringView grpc_lb_v1_Server_load_balance_token(const grpc_lb_v1_Server *msg)
Definition: load_balancer.upb.h:638
grpc_lb_v1_Server
struct grpc_lb_v1_Server grpc_lb_v1_Server
Definition: load_balancer.upb.h:40
string.h
benchmark.request
request
Definition: benchmark.py:77
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
num_calls_started
size_t num_calls_started
Definition: grpclb_end2end_test.cc:157
grpc_lb_v1_ClientStats
struct grpc_lb_v1_ClientStats grpc_lb_v1_ClientStats
Definition: load_balancer.upb.h:35
grpc_core::GrpcLbResponse
Definition: load_balancer_api.h:53
grpc_core::GrpcLbLoadReportRequestCreate
grpc_slice GrpcLbLoadReportRequestCreate(int64_t num_calls_started, int64_t num_calls_finished, int64_t num_calls_finished_with_client_failed_to_send, int64_t num_calls_finished_known_received, const GrpcLbClientStats::DroppedCallCounts *drop_token_counts, upb_Arena *arena)
Definition: load_balancer_api.cc:84
grpc_lb_v1_LoadBalanceRequest_mutable_initial_request
UPB_INLINE struct grpc_lb_v1_InitialLoadBalanceRequest * grpc_lb_v1_LoadBalanceRequest_mutable_initial_request(grpc_lb_v1_LoadBalanceRequest *msg, upb_Arena *arena)
Definition: load_balancer.upb.h:118
time.h
num_calls_finished
size_t num_calls_finished
Definition: grpclb_end2end_test.cc:158
grpc_lb_v1_LoadBalanceResponse
struct grpc_lb_v1_LoadBalanceResponse grpc_lb_v1_LoadBalanceResponse
Definition: load_balancer.upb.h:36
grpc_lb_v1_ClientStats_mutable_timestamp
UPB_INLINE struct google_protobuf_Timestamp * grpc_lb_v1_ClientStats_mutable_timestamp(grpc_lb_v1_ClientStats *msg, upb_Arena *arena)
Definition: load_balancer.upb.h:311
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
grpc_core::GrpcLbServer::operator==
bool operator==(const GrpcLbServer &other) const
Definition: load_balancer_api.cc:39
grpc_lb_v1_Server_drop
UPB_INLINE bool grpc_lb_v1_Server_drop(const grpc_lb_v1_Server *msg)
Definition: load_balancer.upb.h:644
memory.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
grpc_lb_v1_Server_port
UPB_INLINE int32_t grpc_lb_v1_Server_port(const grpc_lb_v1_Server *msg)
Definition: load_balancer.upb.h:632
grpc_core::ParseDuration
Duration ParseDuration(const google_protobuf_Duration *proto_duration)
Definition: xds_common_types.h:39
grpc_core::GrpcLbServer::ip_addr
char ip_addr[GRPC_GRPCLB_SERVER_IP_ADDRESS_MAX_SIZE]
Definition: load_balancer_api.h:45
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
upb_StringView::size
size_t size
Definition: upb/upb/upb.h:74
req
static uv_connect_t req
Definition: test-connection-fail.c:30
grpc_lb_v1_InitialLoadBalanceResponse_client_stats_report_interval
UPB_INLINE const struct google_protobuf_Duration * grpc_lb_v1_InitialLoadBalanceResponse_client_stats_report_interval(const grpc_lb_v1_InitialLoadBalanceResponse *msg)
Definition: load_balancer.upb.h:520
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_core::GrpcLbResponseParse
bool GrpcLbResponseParse(const grpc_slice &serialized_response, upb_Arena *arena, GrpcLbResponse *result)
Definition: load_balancer_api.cc:166
upb_Arena_Malloc
UPB_INLINE void * upb_Arena_Malloc(upb_Arena *a, size_t size)
Definition: upb/upb/upb.h:222
load_balancer.upb.h
grpc_core::GrpcLbServer::drop
bool drop
Definition: load_balancer_api.h:48
grpc_core::Duration::FromSecondsAndNanoseconds
static Duration FromSecondsAndNanoseconds(int64_t seconds, int32_t nanos)
Definition: src/core/lib/gprpp/time.h:274
google_protobuf_Duration_nanos
UPB_INLINE int32_t google_protobuf_Duration_nanos(const google_protobuf_Duration *msg)
Definition: duration.upb.h:69
grpc_lb_v1_LoadBalanceRequest_serialize
UPB_INLINE char * grpc_lb_v1_LoadBalanceRequest_serialize(const grpc_lb_v1_LoadBalanceRequest *msg, upb_Arena *arena, size_t *len)
Definition: load_balancer.upb.h:81
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_lb_v1_ServerList
struct grpc_lb_v1_ServerList grpc_lb_v1_ServerList
Definition: load_balancer.upb.h:39
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_lb_v1_ClientStatsPerToken_set_num_calls
UPB_INLINE void grpc_lb_v1_ClientStatsPerToken_set_num_calls(grpc_lb_v1_ClientStatsPerToken *msg, int64_t value)
Definition: load_balancer.upb.h:229
google_protobuf_Timestamp_set_nanos
UPB_INLINE void google_protobuf_Timestamp_set_nanos(google_protobuf_Timestamp *msg, int32_t value)
Definition: timestamp.upb.h:76
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
google_protobuf_Duration_seconds
UPB_INLINE int64_t google_protobuf_Duration_seconds(const google_protobuf_Duration *msg)
Definition: duration.upb.h:63
min
#define min(a, b)
Definition: qsort.h:83
grpc_core::GrpcLbServer::ip_size
int32_t ip_size
Definition: load_balancer_api.h:44
grpc_lb_v1_LoadBalanceResponse_parse
UPB_INLINE grpc_lb_v1_LoadBalanceResponse * grpc_lb_v1_LoadBalanceResponse_parse(const char *buf, size_t size, upb_Arena *arena)
Definition: load_balancer.upb.h:350
grpc_lb_v1_LoadBalanceResponse_server_list
const UPB_INLINE grpc_lb_v1_ServerList * grpc_lb_v1_LoadBalanceResponse_server_list(const grpc_lb_v1_LoadBalanceResponse *msg)
Definition: load_balancer.upb.h:400
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
google_protobuf_Timestamp
struct google_protobuf_Timestamp google_protobuf_Timestamp
Definition: timestamp.upb.h:24
grpc_core::GrpcLbClientStats::DropTokenCount
Definition: grpclb_client_stats.h:42
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
gpr_types.h
grpc_core::GrpcLbRequestCreate
grpc_slice GrpcLbRequestCreate(const char *lb_service_name, upb_Arena *arena)
Definition: load_balancer_api.cc:62
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_lb_v1_LoadBalanceResponse_has_fallback_response
UPB_INLINE bool grpc_lb_v1_LoadBalanceResponse_has_fallback_response(const grpc_lb_v1_LoadBalanceResponse *msg)
Definition: load_balancer.upb.h:403
grpc_lb_v1_Server_ip_address
UPB_INLINE upb_StringView grpc_lb_v1_Server_ip_address(const grpc_lb_v1_Server *msg)
Definition: load_balancer.upb.h:626
memory_diff.cur
def cur
Definition: memory_diff.py:83
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH
#define GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH
Definition: load_balancer_api.h:35
upb_StringView
Definition: upb/upb/upb.h:72
GRPC_GRPCLB_SERVER_IP_ADDRESS_MAX_SIZE
#define GRPC_GRPCLB_SERVER_IP_ADDRESS_MAX_SIZE
Definition: load_balancer_api.h:36
grpc_lb_v1_ClientStats_set_num_calls_started
UPB_INLINE void grpc_lb_v1_ClientStats_set_num_calls_started(grpc_lb_v1_ClientStats *msg, int64_t value)
Definition: load_balancer.upb.h:320
num_calls_finished_known_received
size_t num_calls_finished_known_received
Definition: grpclb_end2end_test.cc:160
fix_build_deps.r
r
Definition: fix_build_deps.py:491
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
upb_StringView_FromDataAndSize
UPB_INLINE upb_StringView upb_StringView_FromDataAndSize(const char *data, size_t size)
Definition: upb/upb/upb.h:77
grpc_lb_v1_ClientStats_set_num_calls_finished
UPB_INLINE void grpc_lb_v1_ClientStats_set_num_calls_finished(grpc_lb_v1_ClientStats *msg, int64_t value)
Definition: load_balancer.upb.h:323
grpc_lb_v1_ClientStats_set_num_calls_finished_known_received
UPB_INLINE void grpc_lb_v1_ClientStats_set_num_calls_finished_known_received(grpc_lb_v1_ClientStats *msg, int64_t value)
Definition: load_balancer.upb.h:329
google_protobuf_Duration
struct google_protobuf_Duration google_protobuf_Duration
Definition: duration.upb.h:24
GRPC_GRPCLB_SERVER_LOAD_BALANCE_TOKEN_MAX_SIZE
#define GRPC_GRPCLB_SERVER_LOAD_BALANCE_TOKEN_MAX_SIZE
Definition: load_balancer_api.h:37
grpc_core::GrpcLbServer::port
int32_t port
Definition: load_balancer_api.h:46
drop_token_counts
std::map< std::string, size_t > drop_token_counts
Definition: grpclb_end2end_test.cc:161
grpc_core::GrpcLbServer::load_balance_token
char load_balance_token[GRPC_GRPCLB_SERVER_LOAD_BALANCE_TOKEN_MAX_SIZE]
Definition: load_balancer_api.h:47
timestamp.upb.h
grpc_core::GrpcLbServer
Definition: load_balancer_api.h:43
grpc_lb_v1_InitialLoadBalanceRequest_set_name
UPB_INLINE void grpc_lb_v1_InitialLoadBalanceRequest_set_name(grpc_lb_v1_InitialLoadBalanceRequest *msg, upb_StringView value)
Definition: load_balancer.upb.h:178
Duration
Definition: bloaty/third_party/protobuf/src/google/protobuf/duration.pb.h:69
duration.upb.h
absl::InlinedVector
Definition: abseil-cpp/absl/container/inlined_vector.h:69
gpr_timespec
Definition: gpr_types.h:50
num_calls_finished_with_client_failed_to_send
size_t num_calls_finished_with_client_failed_to_send
Definition: grpclb_end2end_test.cc:159
grpc_lb_v1_ClientStatsPerToken_set_load_balance_token
UPB_INLINE void grpc_lb_v1_ClientStatsPerToken_set_load_balance_token(grpc_lb_v1_ClientStatsPerToken *msg, upb_StringView value)
Definition: load_balancer.upb.h:226
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
load_balancer_api.h
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
grpc_lb_v1_ClientStats_add_calls_finished_with_drop
UPB_INLINE struct grpc_lb_v1_ClientStatsPerToken * grpc_lb_v1_ClientStats_add_calls_finished_with_drop(grpc_lb_v1_ClientStats *msg, upb_Arena *arena)
Definition: load_balancer.upb.h:338
grpc_lb_v1_InitialLoadBalanceRequest
struct grpc_lb_v1_InitialLoadBalanceRequest grpc_lb_v1_InitialLoadBalanceRequest
Definition: load_balancer.upb.h:33
upb_Arena
Definition: upb_internal.h:36
run_interop_tests.servers
servers
Definition: run_interop_tests.py:1288
grpc_lb_v1_LoadBalanceRequest_mutable_client_stats
UPB_INLINE struct grpc_lb_v1_ClientStats * grpc_lb_v1_LoadBalanceRequest_mutable_client_stats(grpc_lb_v1_LoadBalanceRequest *msg, upb_Arena *arena)
Definition: load_balancer.upb.h:130
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:29