Namespaces | Macros | Functions | Variables
grpclb.cc File Reference
#include <grpc/support/port_platform.h>
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h"
#include <grpc/event_engine/event_engine.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/inlined_vector.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/types/optional.h"
#include "absl/types/variant.h"
#include "upb/upb.hpp"
#include <grpc/byte_buffer.h>
#include <grpc/byte_buffer_reader.h>
#include <grpc/grpc.h>
#include <grpc/impl/codegen/connectivity_state.h>
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/impl/codegen/propagation_bits.h>
#include <grpc/slice.h>
#include <grpc/status.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/client_channel.h"
#include "src/core/ext/filters/client_channel/lb_policy.h"
#include "src/core/ext/filters/client_channel/lb_policy/child_policy_handler.h"
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.h"
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h"
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h"
#include "src/core/ext/filters/client_channel/lb_policy_factory.h"
#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
#include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
#include "src/core/ext/filters/client_channel/subchannel_interface.h"
#include "src/core/lib/address_utils/sockaddr_utils.h"
#include "src/core/lib/backoff/backoff.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack_builder.h"
#include "src/core/lib/channel/channelz.h"
#include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/debug/trace.h"
#include "src/core/lib/event_engine/event_engine_factory.h"
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/gprpp/orphanable.h"
#include "src/core/lib/gprpp/ref_counted.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/gprpp/time.h"
#include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/pollset_set.h"
#include "src/core/lib/iomgr/resolved_address.h"
#include "src/core/lib/iomgr/sockaddr.h"
#include "src/core/lib/iomgr/socket_utils.h"
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/iomgr/work_serializer.h"
#include "src/core/lib/json/json.h"
#include "src/core/lib/resolver/resolver.h"
#include "src/core/lib/resolver/server_address.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "src/core/lib/slice/slice.h"
#include "src/core/lib/slice/slice_refcount.h"
#include "src/core/lib/slice/slice_string_helpers.h"
#include "src/core/lib/surface/call.h"
#include "src/core/lib/surface/channel.h"
#include "src/core/lib/surface/channel_init.h"
#include "src/core/lib/surface/channel_stack_type.h"
#include "src/core/lib/transport/connectivity_state.h"
#include "src/core/lib/transport/metadata_batch.h"
#include "src/core/lib/uri/uri_parser.h"

Go to the source code of this file.

Namespaces

 grpc_core
 

Macros

#define GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS   10000
 
#define GRPC_GRPCLB_DEFAULT_SUBCHANNEL_DELETION_DELAY_MS   10000
 
#define GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS   1
 
#define GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER   1.6
 
#define GRPC_GRPCLB_RECONNECT_JITTER   0.2
 
#define GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS   120
 

Functions

void grpc_lb_policy_grpclb_init ()
 
void grpc_lb_policy_grpclb_shutdown ()
 
void grpc_core::RegisterGrpcLbLoadReportingFilter (CoreConfiguration::Builder *builder)
 

Variables

TraceFlag grpc_core::grpc_lb_glb_trace (false, "glb")
 
const char grpc_core::kGrpcLbAddressAttributeKey [] = "grpclb"
 

Macro Definition Documentation

◆ GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS

#define GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS   10000

Definition at line 150 of file grpclb.cc.

◆ GRPC_GRPCLB_DEFAULT_SUBCHANNEL_DELETION_DELAY_MS

#define GRPC_GRPCLB_DEFAULT_SUBCHANNEL_DELETION_DELAY_MS   10000

Definition at line 151 of file grpclb.cc.

◆ GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS

#define GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS   1

Implementation of the gRPC LB policy.

This policy takes as input a list of resolved addresses, which must include at least one balancer address.

An internal channel (lb_channel_) is created for the addresses from that are balancers. This channel behaves just like a regular channel that uses pick_first to select from the list of balancer addresses.

When we get our initial update, we instantiate the internal streaming call to the LB server (whichever address pick_first chose). The call will be complete when either the balancer sends status or when we cancel the call (e.g., because we are shutting down). In needed, we retry the call. If we received at least one valid message from the server, a new call attempt will be made immediately; otherwise, we apply back-off delays between attempts.

We maintain an internal round_robin policy instance for distributing requests across backends. Whenever we receive a new serverlist from the balancer, we update the round_robin policy with the new list of addresses. If we cannot communicate with the balancer on startup, however, we may enter fallback mode, in which case we will populate the child policy's addresses from the backend addresses returned by the resolver.

Once a child policy instance is in place (and getting updated as described), calls for a pick, a ping, or a cancellation will be serviced right away by forwarding them to the child policy instance. Any time there's no child policy available (i.e., right after the creation of the gRPCLB policy), pick requests are queued.

See also
https://github.com/grpc/grpc/blob/master/doc/load-balancing.md for the high level design and details.

Definition at line 146 of file grpclb.cc.

◆ GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER

#define GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER   1.6

Definition at line 147 of file grpclb.cc.

◆ GRPC_GRPCLB_RECONNECT_JITTER

#define GRPC_GRPCLB_RECONNECT_JITTER   0.2

Definition at line 149 of file grpclb.cc.

◆ GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS

#define GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS   120

Definition at line 148 of file grpclb.cc.

Function Documentation

◆ grpc_lb_policy_grpclb_init()

void grpc_lb_policy_grpclb_init ( )

Definition at line 1930 of file grpclb.cc.

◆ grpc_lb_policy_grpclb_shutdown()

void grpc_lb_policy_grpclb_shutdown ( )

Definition at line 1936 of file grpclb.cc.

Variable Documentation

◆ args_

grpc_channel_args* args_ = nullptr
private

Definition at line 513 of file grpclb.cc.

◆ cached_subchannels_

std::map<Timestamp , std::vector<RefCountedPtr<SubchannelInterface> > > cached_subchannels_
private

Definition at line 568 of file grpclb.cc.

◆ child_picker_

std::unique_ptr<SubchannelPicker> child_picker_
private

Definition at line 419 of file grpclb.cc.

◆ child_policy_

OrphanablePtr<LoadBalancingPolicy> child_policy_
private

Definition at line 181 of file grpclb.cc.

◆ child_policy_ready_

bool child_policy_ready_ = false
private

Definition at line 562 of file grpclb.cc.

◆ client_load_report_done_closure_

grpc_closure client_load_report_done_closure_
private

Definition at line 268 of file grpclb.cc.

◆ client_load_report_handle_

absl::optional<EventEngine::TaskHandle> client_load_report_handle_
private

Definition at line 264 of file grpclb.cc.

◆ client_load_report_is_due_

bool client_load_report_is_due_ = false
private

Definition at line 266 of file grpclb.cc.

◆ client_stats_

RefCountedPtr<GrpcLbClientStats> client_stats_
private

Definition at line 262 of file grpclb.cc.

◆ client_stats_report_interval_

Duration client_stats_report_interval_
private

Definition at line 263 of file grpclb.cc.

◆ config_

RefCountedPtr<GrpcLbConfig> config_
private

Definition at line 510 of file grpclb.cc.

◆ drop_index_

size_t drop_index_ = 0
private

Definition at line 368 of file grpclb.cc.

◆ fallback_at_startup_checks_pending_

bool fallback_at_startup_checks_pending_ = false
private

Definition at line 555 of file grpclb.cc.

◆ fallback_at_startup_timeout_

const Duration fallback_at_startup_timeout_
private

Definition at line 554 of file grpclb.cc.

◆ fallback_backend_addresses_

absl::StatusOr<ServerAddressList> fallback_backend_addresses_
private

Definition at line 547 of file grpclb.cc.

◆ fallback_mode_

bool fallback_mode_ = false
private

Definition at line 545 of file grpclb.cc.

◆ grpclb_policy_

RefCountedPtr<LoadBalancingPolicy> grpclb_policy_
private

Definition at line 236 of file grpclb.cc.

◆ last_client_load_report_counters_were_zero_

bool last_client_load_report_counters_were_zero_ = false
private

Definition at line 265 of file grpclb.cc.

◆ lb_call_

grpc_call* lb_call_ = nullptr
private

Definition at line 239 of file grpclb.cc.

◆ lb_call_backoff_

BackOff lb_call_backoff_
private

Definition at line 535 of file grpclb.cc.

◆ lb_call_retry_timer_

grpc_timer lb_call_retry_timer_
private

Definition at line 537 of file grpclb.cc.

◆ lb_call_status_

grpc_status_code lb_call_status_
private

Definition at line 257 of file grpclb.cc.

◆ lb_call_status_details_

grpc_slice lb_call_status_details_
private

Definition at line 258 of file grpclb.cc.

◆ lb_call_timeout_

const Duration lb_call_timeout_
private

Definition at line 533 of file grpclb.cc.

◆ lb_calld_

OrphanablePtr<BalancerCallState> lb_calld_
private

Definition at line 531 of file grpclb.cc.

◆ lb_channel_

grpc_channel* lb_channel_ = nullptr
private

Definition at line 519 of file grpclb.cc.

◆ lb_fallback_timer_

grpc_timer lb_fallback_timer_
private

Definition at line 556 of file grpclb.cc.

◆ lb_initial_metadata_recv_

grpc_metadata_array lb_initial_metadata_recv_
private

Definition at line 242 of file grpclb.cc.

◆ lb_on_balancer_message_received_

grpc_closure lb_on_balancer_message_received_
private

Definition at line 250 of file grpclb.cc.

◆ lb_on_balancer_status_received_

grpc_closure lb_on_balancer_status_received_
private

Definition at line 255 of file grpclb.cc.

◆ lb_on_call_retry_

grpc_closure lb_on_call_retry_
private

Definition at line 538 of file grpclb.cc.

◆ lb_on_fallback_

grpc_closure lb_on_fallback_
private

Definition at line 557 of file grpclb.cc.

◆ lb_on_initial_request_sent_

grpc_closure lb_on_initial_request_sent_
private

Definition at line 246 of file grpclb.cc.

◆ lb_policy_

RefCountedPtr<GrpcLb> lb_policy_
private

Definition at line 291 of file grpclb.cc.

◆ lb_token_

std::string lb_token_
private

Definition at line 292 of file grpclb.cc.

◆ lb_trailing_metadata_recv_

grpc_metadata_array lb_trailing_metadata_recv_
private

Definition at line 256 of file grpclb.cc.

◆ on_subchannel_cache_timer_

grpc_closure on_subchannel_cache_timer_
private

Definition at line 570 of file grpclb.cc.

◆ original_call_tracker_

std::unique_ptr<SubchannelCallTrackerInterface> original_call_tracker_
private

Definition at line 413 of file grpclb.cc.

◆ parent_

RefCountedPtr<GrpcLb> parent_
private

Definition at line 438 of file grpclb.cc.

◆ parent_channelz_node_

RefCountedPtr<channelz::ChannelNode> parent_channelz_node_
private

Definition at line 524 of file grpclb.cc.

◆ recv_message_payload_

grpc_byte_buffer* recv_message_payload_ = nullptr
private

Definition at line 249 of file grpclb.cc.

◆ resolution_note_

std::string resolution_note_
private

Definition at line 550 of file grpclb.cc.

◆ response_generator_

RefCountedPtr<FakeResolverResponseGenerator> response_generator_
private

Definition at line 522 of file grpclb.cc.

◆ retry_timer_callback_pending_

bool retry_timer_callback_pending_ = false
private

Definition at line 536 of file grpclb.cc.

◆ seen_initial_response_

bool seen_initial_response_ = false
private

Definition at line 251 of file grpclb.cc.

◆ seen_serverlist_

bool seen_serverlist_ = false
private

Definition at line 252 of file grpclb.cc.

◆ send_message_payload_

grpc_byte_buffer* send_message_payload_ = nullptr
private

Definition at line 245 of file grpclb.cc.

◆ server_name_

std::string server_name_
private

Definition at line 508 of file grpclb.cc.

◆ serverlist_

RefCountedPtr<Serverlist> serverlist_
private

Definition at line 363 of file grpclb.cc.

◆ service_name_

std::string service_name_
private

Definition at line 182 of file grpclb.cc.

◆ shutting_down_

bool shutting_down_ = false
private

Definition at line 516 of file grpclb.cc.

◆ subchannel_cache_interval_

const Duration subchannel_cache_interval_
private

Definition at line 565 of file grpclb.cc.

◆ subchannel_cache_timer_

grpc_timer subchannel_cache_timer_
private

Definition at line 569 of file grpclb.cc.

◆ subchannel_cache_timer_pending_

bool subchannel_cache_timer_pending_ = false
private

Definition at line 571 of file grpclb.cc.

◆ watcher_

StateWatcher* watcher_ = nullptr
private

Definition at line 520 of file grpclb.cc.



grpc
Author(s):
autogenerated on Fri May 16 2025 03:01:16