| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028 | /* * * Copyright 2017 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include <deque>#include <memory>#include <mutex>#include <set>#include <sstream>#include <string>#include <thread>#include "absl/strings/str_cat.h"#include "absl/strings/str_format.h"#include <grpc/grpc.h>#include <grpc/support/alloc.h>#include <grpc/support/log.h>#include <grpc/support/time.h>#include <grpcpp/channel.h>#include <grpcpp/client_context.h>#include <grpcpp/create_channel.h>#include <grpcpp/impl/codegen/sync.h>#include <grpcpp/server.h>#include <grpcpp/server_builder.h>#include "src/core/ext/filters/client_channel/backup_poller.h"#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"#include "src/core/ext/filters/client_channel/parse_address.h"#include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"#include "src/core/ext/filters/client_channel/server_address.h"#include "src/core/ext/filters/client_channel/service_config.h"#include "src/core/lib/gpr/env.h"#include "src/core/lib/gprpp/ref_counted_ptr.h"#include "src/core/lib/iomgr/sockaddr.h"#include "src/core/lib/security/credentials/fake/fake_credentials.h"#include "src/cpp/client/secure_credentials.h"#include "src/cpp/server/secure_server_credentials.h"#include "test/core/util/port.h"#include "test/core/util/test_config.h"#include "test/cpp/end2end/test_service_impl.h"#include "src/proto/grpc/lb/v1/load_balancer.grpc.pb.h"#include "src/proto/grpc/testing/echo.grpc.pb.h"#include <gmock/gmock.h>#include <gtest/gtest.h>// TODO(dgq): Other scenarios in need of testing:// - Send a serverlist with faulty ip:port addresses (port > 2^16, etc).// - Test reception of invalid serverlist// - Test against a non-LB server.// - Random LB server closing the stream unexpectedly.//// Findings from end to end testing to be covered here:// - Handling of LB servers restart, including reconnection after backing-off//   retries.// - Destruction of load balanced channel (and therefore of grpclb instance)//   while://   1) the internal LB call is still active. This should work by virtue//   of the weak reference the LB call holds. The call should be terminated as//   part of the grpclb shutdown process.//   2) the retry timer is active. Again, the weak reference it holds should//   prevent a premature call to \a glb_destroy.using std::chrono::system_clock;using grpc::lb::v1::LoadBalancer;using grpc::lb::v1::LoadBalanceRequest;using grpc::lb::v1::LoadBalanceResponse;namespace grpc {namespace testing {namespace {constexpr char kDefaultServiceConfig[] =    "{\n"    "  \"loadBalancingConfig\":[\n"    "    { \"grpclb\":{} }\n"    "  ]\n"    "}";template <typename ServiceType>class CountedService : public ServiceType { public:  size_t request_count() {    grpc::internal::MutexLock lock(&mu_);    return request_count_;  }  size_t response_count() {    grpc::internal::MutexLock lock(&mu_);    return response_count_;  }  void IncreaseResponseCount() {    grpc::internal::MutexLock lock(&mu_);    ++response_count_;  }  void IncreaseRequestCount() {    grpc::internal::MutexLock lock(&mu_);    ++request_count_;  }  void ResetCounters() {    grpc::internal::MutexLock lock(&mu_);    request_count_ = 0;    response_count_ = 0;  } protected:  grpc::internal::Mutex mu_; private:  size_t request_count_ = 0;  size_t response_count_ = 0;};using BackendService = CountedService<TestServiceImpl>;using BalancerService = CountedService<LoadBalancer::Service>;const char g_kCallCredsMdKey[] = "Balancer should not ...";const char g_kCallCredsMdValue[] = "... receive me";class BackendServiceImpl : public BackendService { public:  BackendServiceImpl() {}  Status Echo(ServerContext* context, const EchoRequest* request,              EchoResponse* response) override {    // Backend should receive the call credentials metadata.    auto call_credentials_entry =        context->client_metadata().find(g_kCallCredsMdKey);    EXPECT_NE(call_credentials_entry, context->client_metadata().end());    if (call_credentials_entry != context->client_metadata().end()) {      EXPECT_EQ(call_credentials_entry->second, g_kCallCredsMdValue);    }    IncreaseRequestCount();    const auto status = TestServiceImpl::Echo(context, request, response);    IncreaseResponseCount();    AddClient(context->peer());    return status;  }  void Start() {}  void Shutdown() {}  std::set<std::string> clients() {    grpc::internal::MutexLock lock(&clients_mu_);    return clients_;  } private:  void AddClient(const std::string& client) {    grpc::internal::MutexLock lock(&clients_mu_);    clients_.insert(client);  }  grpc::internal::Mutex mu_;  grpc::internal::Mutex clients_mu_;  std::set<std::string> clients_;};std::string Ip4ToPackedString(const char* ip_str) {  struct in_addr ip4;  GPR_ASSERT(inet_pton(AF_INET, ip_str, &ip4) == 1);  return std::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4));}struct ClientStats {  size_t num_calls_started = 0;  size_t num_calls_finished = 0;  size_t num_calls_finished_with_client_failed_to_send = 0;  size_t num_calls_finished_known_received = 0;  std::map<std::string, size_t> drop_token_counts;  ClientStats& operator+=(const ClientStats& other) {    num_calls_started += other.num_calls_started;    num_calls_finished += other.num_calls_finished;    num_calls_finished_with_client_failed_to_send +=        other.num_calls_finished_with_client_failed_to_send;    num_calls_finished_known_received +=        other.num_calls_finished_known_received;    for (const auto& p : other.drop_token_counts) {      drop_token_counts[p.first] += p.second;    }    return *this;  }  void Reset() {    num_calls_started = 0;    num_calls_finished = 0;    num_calls_finished_with_client_failed_to_send = 0;    num_calls_finished_known_received = 0;    drop_token_counts.clear();  }};class BalancerServiceImpl : public BalancerService { public:  using Stream = ServerReaderWriter<LoadBalanceResponse, LoadBalanceRequest>;  using ResponseDelayPair = std::pair<LoadBalanceResponse, int>;  explicit BalancerServiceImpl(int client_load_reporting_interval_seconds)      : client_load_reporting_interval_seconds_(            client_load_reporting_interval_seconds) {}  Status BalanceLoad(ServerContext* context, Stream* stream) override {    gpr_log(GPR_INFO, "LB[%p]: BalanceLoad", this);    {      grpc::internal::MutexLock lock(&mu_);      if (serverlist_done_) goto done;    }    {      // Balancer shouldn't receive the call credentials metadata.      EXPECT_EQ(context->client_metadata().find(g_kCallCredsMdKey),                context->client_metadata().end());      LoadBalanceRequest request;      std::vector<ResponseDelayPair> responses_and_delays;      if (!stream->Read(&request)) {        goto done;      } else {        if (request.has_initial_request()) {          grpc::internal::MutexLock lock(&mu_);          service_names_.push_back(request.initial_request().name());        }      }      IncreaseRequestCount();      gpr_log(GPR_INFO, "LB[%p]: received initial message '%s'", this,              request.DebugString().c_str());      // TODO(juanlishen): Initial response should always be the first response.      if (client_load_reporting_interval_seconds_ > 0) {        LoadBalanceResponse initial_response;        initial_response.mutable_initial_response()            ->mutable_client_stats_report_interval()            ->set_seconds(client_load_reporting_interval_seconds_);        stream->Write(initial_response);      }      {        grpc::internal::MutexLock lock(&mu_);        responses_and_delays = responses_and_delays_;      }      for (const auto& response_and_delay : responses_and_delays) {        SendResponse(stream, response_and_delay.first,                     response_and_delay.second);      }      {        grpc::internal::MutexLock lock(&mu_);        serverlist_cond_.WaitUntil(&mu_, [this] { return serverlist_done_; });      }      if (client_load_reporting_interval_seconds_ > 0) {        request.Clear();        while (stream->Read(&request)) {          gpr_log(GPR_INFO, "LB[%p]: received client load report message '%s'",                  this, request.DebugString().c_str());          GPR_ASSERT(request.has_client_stats());          ClientStats load_report;          load_report.num_calls_started =              request.client_stats().num_calls_started();          load_report.num_calls_finished =              request.client_stats().num_calls_finished();          load_report.num_calls_finished_with_client_failed_to_send =              request.client_stats()                  .num_calls_finished_with_client_failed_to_send();          load_report.num_calls_finished_known_received =              request.client_stats().num_calls_finished_known_received();          for (const auto& drop_token_count :               request.client_stats().calls_finished_with_drop()) {            load_report                .drop_token_counts[drop_token_count.load_balance_token()] =                drop_token_count.num_calls();          }          // We need to acquire the lock here in order to prevent the notify_one          // below from firing before its corresponding wait is executed.          grpc::internal::MutexLock lock(&mu_);          load_report_queue_.emplace_back(std::move(load_report));          if (load_report_cond_ != nullptr) load_report_cond_->Signal();        }      }    }  done:    gpr_log(GPR_INFO, "LB[%p]: done", this);    return Status::OK;  }  void add_response(const LoadBalanceResponse& response, int send_after_ms) {    grpc::internal::MutexLock lock(&mu_);    responses_and_delays_.push_back(std::make_pair(response, send_after_ms));  }  void Start() {    grpc::internal::MutexLock lock(&mu_);    serverlist_done_ = false;    responses_and_delays_.clear();    load_report_queue_.clear();  }  void Shutdown() {    NotifyDoneWithServerlists();    gpr_log(GPR_INFO, "LB[%p]: shut down", this);  }  static LoadBalanceResponse BuildResponseForBackends(      const std::vector<int>& backend_ports,      const std::map<std::string, size_t>& drop_token_counts) {    LoadBalanceResponse response;    for (const auto& drop_token_count : drop_token_counts) {      for (size_t i = 0; i < drop_token_count.second; ++i) {        auto* server = response.mutable_server_list()->add_servers();        server->set_drop(true);        server->set_load_balance_token(drop_token_count.first);      }    }    for (const int& backend_port : backend_ports) {      auto* server = response.mutable_server_list()->add_servers();      server->set_ip_address(Ip4ToPackedString("127.0.0.1"));      server->set_port(backend_port);      static int token_count = 0;      server->set_load_balance_token(          absl::StrFormat("token%03d", ++token_count));    }    return response;  }  ClientStats WaitForLoadReport() {    grpc::internal::MutexLock lock(&mu_);    grpc::internal::CondVar cv;    if (load_report_queue_.empty()) {      load_report_cond_ = &cv;      load_report_cond_->WaitUntil(          &mu_, [this] { return !load_report_queue_.empty(); });      load_report_cond_ = nullptr;    }    ClientStats load_report = std::move(load_report_queue_.front());    load_report_queue_.pop_front();    return load_report;  }  void NotifyDoneWithServerlists() {    grpc::internal::MutexLock lock(&mu_);    if (!serverlist_done_) {      serverlist_done_ = true;      serverlist_cond_.Broadcast();    }  }  std::vector<std::string> service_names() {    grpc::internal::MutexLock lock(&mu_);    return service_names_;  } private:  void SendResponse(Stream* stream, const LoadBalanceResponse& response,                    int delay_ms) {    gpr_log(GPR_INFO, "LB[%p]: sleeping for %d ms...", this, delay_ms);    if (delay_ms > 0) {      gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(delay_ms));    }    gpr_log(GPR_INFO, "LB[%p]: Woke up! Sending response '%s'", this,            response.DebugString().c_str());    IncreaseResponseCount();    stream->Write(response);  }  const int client_load_reporting_interval_seconds_;  std::vector<ResponseDelayPair> responses_and_delays_;  std::vector<std::string> service_names_;  grpc::internal::Mutex mu_;  grpc::internal::CondVar serverlist_cond_;  bool serverlist_done_ = false;  grpc::internal::CondVar* load_report_cond_ = nullptr;  std::deque<ClientStats> load_report_queue_;};class GrpclbEnd2endTest : public ::testing::Test { protected:  GrpclbEnd2endTest(size_t num_backends, size_t num_balancers,                    int client_load_reporting_interval_seconds)      : server_host_("localhost"),        num_backends_(num_backends),        num_balancers_(num_balancers),        client_load_reporting_interval_seconds_(            client_load_reporting_interval_seconds) {}  static void SetUpTestCase() {    // Make the backup poller poll very frequently in order to pick up    // updates from all the subchannels's FDs.    GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1);#if TARGET_OS_IPHONE    // Workaround Apple CFStream bug    gpr_setenv("grpc_cfstream", "0");#endif    grpc_init();  }  static void TearDownTestCase() { grpc_shutdown(); }  void SetUp() override {    response_generator_ =        grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();    // Start the backends.    for (size_t i = 0; i < num_backends_; ++i) {      backends_.emplace_back(new ServerThread<BackendServiceImpl>("backend"));      backends_.back()->Start(server_host_);    }    // Start the load balancers.    for (size_t i = 0; i < num_balancers_; ++i) {      balancers_.emplace_back(new ServerThread<BalancerServiceImpl>(          "balancer", client_load_reporting_interval_seconds_));      balancers_.back()->Start(server_host_);    }    ResetStub();  }  void TearDown() override {    ShutdownAllBackends();    for (auto& balancer : balancers_) balancer->Shutdown();  }  void StartAllBackends() {    for (auto& backend : backends_) backend->Start(server_host_);  }  void StartBackend(size_t index) { backends_[index]->Start(server_host_); }  void ShutdownAllBackends() {    for (auto& backend : backends_) backend->Shutdown();  }  void ShutdownBackend(size_t index) { backends_[index]->Shutdown(); }  void ResetStub(int fallback_timeout = 0,                 const std::string& expected_targets = "") {    ChannelArguments args;    if (fallback_timeout > 0) args.SetGrpclbFallbackTimeout(fallback_timeout);    args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,                    response_generator_.get());    if (!expected_targets.empty()) {      args.SetString(GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS, expected_targets);    }    std::ostringstream uri;    uri << "fake:///" << kApplicationTargetName_;    // TODO(dgq): templatize tests to run everything using both secure and    // insecure channel credentials.    grpc_channel_credentials* channel_creds =        grpc_fake_transport_security_credentials_create();    grpc_call_credentials* call_creds = grpc_md_only_test_credentials_create(        g_kCallCredsMdKey, g_kCallCredsMdValue, false);    std::shared_ptr<ChannelCredentials> creds(        new SecureChannelCredentials(grpc_composite_channel_credentials_create(            channel_creds, call_creds, nullptr)));    call_creds->Unref();    channel_creds->Unref();    channel_ = ::grpc::CreateCustomChannel(uri.str(), creds, args);    stub_ = grpc::testing::EchoTestService::NewStub(channel_);  }  void ResetBackendCounters() {    for (auto& backend : backends_) backend->service_.ResetCounters();  }  ClientStats WaitForLoadReports() {    ClientStats client_stats;    for (auto& balancer : balancers_) {      client_stats += balancer->service_.WaitForLoadReport();    }    return client_stats;  }  bool SeenAllBackends(size_t start_index = 0, size_t stop_index = 0) {    if (stop_index == 0) stop_index = backends_.size();    for (size_t i = start_index; i < stop_index; ++i) {      if (backends_[i]->service_.request_count() == 0) return false;    }    return true;  }  void SendRpcAndCount(int* num_total, int* num_ok, int* num_failure,                       int* num_drops) {    const Status status = SendRpc();    if (status.ok()) {      ++*num_ok;    } else {      if (status.error_message() == "Call dropped by load balancing policy") {        ++*num_drops;      } else {        ++*num_failure;      }    }    ++*num_total;  }  std::tuple<int, int, int> WaitForAllBackends(int num_requests_multiple_of = 1,                                               size_t start_index = 0,                                               size_t stop_index = 0) {    int num_ok = 0;    int num_failure = 0;    int num_drops = 0;    int num_total = 0;    while (!SeenAllBackends(start_index, stop_index)) {      SendRpcAndCount(&num_total, &num_ok, &num_failure, &num_drops);    }    while (num_total % num_requests_multiple_of != 0) {      SendRpcAndCount(&num_total, &num_ok, &num_failure, &num_drops);    }    ResetBackendCounters();    gpr_log(GPR_INFO,            "Performed %d warm up requests (a multiple of %d) against the "            "backends. %d succeeded, %d failed, %d dropped.",            num_total, num_requests_multiple_of, num_ok, num_failure,            num_drops);    return std::make_tuple(num_ok, num_failure, num_drops);  }  void WaitForBackend(size_t backend_idx) {    do {      (void)SendRpc();    } while (backends_[backend_idx]->service_.request_count() == 0);    ResetBackendCounters();  }  struct AddressData {    int port;    std::string balancer_name;  };  static grpc_core::ServerAddressList CreateLbAddressesFromAddressDataList(      const std::vector<AddressData>& address_data) {    grpc_core::ServerAddressList addresses;    for (const auto& addr : address_data) {      std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", addr.port);      grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);      GPR_ASSERT(lb_uri != nullptr);      grpc_resolved_address address;      GPR_ASSERT(grpc_parse_uri(lb_uri, &address));      grpc_arg arg =          grpc_core::CreateGrpclbBalancerNameArg(addr.balancer_name.c_str());      grpc_channel_args* args =          grpc_channel_args_copy_and_add(nullptr, &arg, 1);      addresses.emplace_back(address.addr, address.len, args);      grpc_uri_destroy(lb_uri);    }    return addresses;  }  static grpc_core::Resolver::Result MakeResolverResult(      const std::vector<AddressData>& balancer_address_data,      const std::vector<AddressData>& backend_address_data = {},      const char* service_config_json = kDefaultServiceConfig) {    grpc_core::Resolver::Result result;    result.addresses =        CreateLbAddressesFromAddressDataList(backend_address_data);    grpc_error* error = GRPC_ERROR_NONE;    result.service_config =        grpc_core::ServiceConfig::Create(service_config_json, &error);    GPR_ASSERT(error == GRPC_ERROR_NONE);    grpc_core::ServerAddressList balancer_addresses =        CreateLbAddressesFromAddressDataList(balancer_address_data);    grpc_arg arg = CreateGrpclbBalancerAddressesArg(&balancer_addresses);    result.args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);    return result;  }  void SetNextResolutionAllBalancers(      const char* service_config_json = kDefaultServiceConfig) {    std::vector<AddressData> addresses;    for (size_t i = 0; i < balancers_.size(); ++i) {      addresses.emplace_back(AddressData{balancers_[i]->port_, ""});    }    SetNextResolution(addresses, {}, service_config_json);  }  void SetNextResolution(      const std::vector<AddressData>& balancer_address_data,      const std::vector<AddressData>& backend_address_data = {},      const char* service_config_json = kDefaultServiceConfig) {    grpc_core::ExecCtx exec_ctx;    grpc_core::Resolver::Result result = MakeResolverResult(        balancer_address_data, backend_address_data, service_config_json);    response_generator_->SetResponse(std::move(result));  }  void SetNextReresolutionResponse(      const std::vector<AddressData>& balancer_address_data,      const std::vector<AddressData>& backend_address_data = {},      const char* service_config_json = kDefaultServiceConfig) {    grpc_core::ExecCtx exec_ctx;    grpc_core::Resolver::Result result = MakeResolverResult(        balancer_address_data, backend_address_data, service_config_json);    response_generator_->SetReresolutionResponse(std::move(result));  }  const std::vector<int> GetBackendPorts(size_t start_index = 0,                                         size_t stop_index = 0) const {    if (stop_index == 0) stop_index = backends_.size();    std::vector<int> backend_ports;    for (size_t i = start_index; i < stop_index; ++i) {      backend_ports.push_back(backends_[i]->port_);    }    return backend_ports;  }  void ScheduleResponseForBalancer(size_t i,                                   const LoadBalanceResponse& response,                                   int delay_ms) {    balancers_[i]->service_.add_response(response, delay_ms);  }  Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000,                 bool wait_for_ready = false,                 const Status& expected_status = Status::OK) {    const bool local_response = (response == nullptr);    if (local_response) response = new EchoResponse;    EchoRequest request;    request.set_message(kRequestMessage_);    if (!expected_status.ok()) {      auto* error = request.mutable_param()->mutable_expected_error();      error->set_code(expected_status.error_code());      error->set_error_message(expected_status.error_message());    }    ClientContext context;    context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms));    if (wait_for_ready) context.set_wait_for_ready(true);    Status status = stub_->Echo(&context, request, response);    if (local_response) delete response;    return status;  }  void CheckRpcSendOk(const size_t times = 1, const int timeout_ms = 1000,                      bool wait_for_ready = false) {    for (size_t i = 0; i < times; ++i) {      EchoResponse response;      const Status status = SendRpc(&response, timeout_ms, wait_for_ready);      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  void CheckRpcSendFailure() {    const Status status = SendRpc();    EXPECT_FALSE(status.ok());  }  template <typename T>  struct ServerThread {    template <typename... Args>    explicit ServerThread(const std::string& type, Args&&... args)        : port_(grpc_pick_unused_port_or_die()),          type_(type),          service_(std::forward<Args>(args)...) {}    void Start(const std::string& server_host) {      gpr_log(GPR_INFO, "starting %s server on port %d", type_.c_str(), port_);      GPR_ASSERT(!running_);      running_ = true;      service_.Start();      grpc::internal::Mutex mu;      // We need to acquire the lock here in order to prevent the notify_one      // by ServerThread::Serve from firing before the wait below is hit.      grpc::internal::MutexLock lock(&mu);      grpc::internal::CondVar cond;      thread_.reset(new std::thread(          std::bind(&ServerThread::Serve, this, server_host, &mu, &cond)));      cond.Wait(&mu);      gpr_log(GPR_INFO, "%s server startup complete", type_.c_str());    }    void Serve(const std::string& server_host, grpc::internal::Mutex* mu,               grpc::internal::CondVar* cond) {      // We need to acquire the lock here in order to prevent the notify_one      // below from firing before its corresponding wait is executed.      grpc::internal::MutexLock lock(mu);      std::ostringstream server_address;      server_address << server_host << ":" << port_;      ServerBuilder builder;      std::shared_ptr<ServerCredentials> creds(new SecureServerCredentials(          grpc_fake_transport_security_server_credentials_create()));      builder.AddListeningPort(server_address.str(), creds);      builder.RegisterService(&service_);      server_ = builder.BuildAndStart();      cond->Signal();    }    void Shutdown() {      if (!running_) return;      gpr_log(GPR_INFO, "%s about to shutdown", type_.c_str());      service_.Shutdown();      server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));      thread_->join();      gpr_log(GPR_INFO, "%s shutdown completed", type_.c_str());      running_ = false;    }    const int port_;    std::string type_;    T service_;    std::unique_ptr<Server> server_;    std::unique_ptr<std::thread> thread_;    bool running_ = false;  };  const std::string server_host_;  const size_t num_backends_;  const size_t num_balancers_;  const int client_load_reporting_interval_seconds_;  std::shared_ptr<Channel> channel_;  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;  std::vector<std::unique_ptr<ServerThread<BackendServiceImpl>>> backends_;  std::vector<std::unique_ptr<ServerThread<BalancerServiceImpl>>> balancers_;  grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>      response_generator_;  const std::string kRequestMessage_ = "Live long and prosper.";  const std::string kApplicationTargetName_ = "application_target_name";};class SingleBalancerTest : public GrpclbEnd2endTest { public:  SingleBalancerTest() : GrpclbEnd2endTest(4, 1, 0) {}};TEST_F(SingleBalancerTest, Vanilla) {  SetNextResolutionAllBalancers();  const size_t kNumRpcsPerAddress = 100;  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // We need to wait for all backends to come online.  WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count());  }  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // Check LB policy name for the channel.  EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName());}TEST_F(SingleBalancerTest, ReturnServerStatus) {  SetNextResolutionAllBalancers();  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  // We need to wait for all backends to come online.  WaitForAllBackends();  // Send a request that the backend will fail, and make sure we get  // back the right status.  Status expected(StatusCode::INVALID_ARGUMENT, "He's dead, Jim!");  Status actual = SendRpc(/*response=*/nullptr, /*timeout_ms=*/1000,                          /*wait_for_ready=*/false, expected);  EXPECT_EQ(actual.error_code(), expected.error_code());  EXPECT_EQ(actual.error_message(), expected.error_message());}TEST_F(SingleBalancerTest, SelectGrpclbWithMigrationServiceConfig) {  SetNextResolutionAllBalancers(      "{\n"      "  \"loadBalancingConfig\":[\n"      "    { \"does_not_exist\":{} },\n"      "    { \"grpclb\":{} }\n"      "  ]\n"      "}");  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  CheckRpcSendOk(1, 1000 /* timeout_ms */, true /* wait_for_ready */);  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // Check LB policy name for the channel.  EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName());}TEST_F(SingleBalancerTest,       SelectGrpclbWithMigrationServiceConfigAndNoAddresses) {  const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  SetNextResolution({}, {},                    "{\n"                    "  \"loadBalancingConfig\":[\n"                    "    { \"does_not_exist\":{} },\n"                    "    { \"grpclb\":{} }\n"                    "  ]\n"                    "}");  // Try to connect.  EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true));  // Should go into state TRANSIENT_FAILURE when we enter fallback mode.  const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(1);  grpc_connectivity_state state;  while ((state = channel_->GetState(false)) !=         GRPC_CHANNEL_TRANSIENT_FAILURE) {    ASSERT_TRUE(channel_->WaitForStateChange(state, deadline));  }  // Check LB policy name for the channel.  EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName());}TEST_F(SingleBalancerTest, UsePickFirstChildPolicy) {  SetNextResolutionAllBalancers(      "{\n"      "  \"loadBalancingConfig\":[\n"      "    { \"grpclb\":{\n"      "      \"childPolicy\":[\n"      "        { \"pick_first\":{} }\n"      "      ]\n"      "    } }\n"      "  ]\n"      "}");  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  const size_t kNumRpcs = num_backends_ * 2;  CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);  balancers_[0]->service_.NotifyDoneWithServerlists();  // Check that all requests went to the first backend.  This verifies  // that we used pick_first instead of round_robin as the child policy.  EXPECT_EQ(backends_[0]->service_.request_count(), kNumRpcs);  for (size_t i = 1; i < backends_.size(); ++i) {    EXPECT_EQ(backends_[i]->service_.request_count(), 0UL);  }  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // Check LB policy name for the channel.  EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName());}TEST_F(SingleBalancerTest, SwapChildPolicy) {  SetNextResolutionAllBalancers(      "{\n"      "  \"loadBalancingConfig\":[\n"      "    { \"grpclb\":{\n"      "      \"childPolicy\":[\n"      "        { \"pick_first\":{} }\n"      "      ]\n"      "    } }\n"      "  ]\n"      "}");  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  const size_t kNumRpcs = num_backends_ * 2;  CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);  // Check that all requests went to the first backend.  This verifies  // that we used pick_first instead of round_robin as the child policy.  EXPECT_EQ(backends_[0]->service_.request_count(), kNumRpcs);  for (size_t i = 1; i < backends_.size(); ++i) {    EXPECT_EQ(backends_[i]->service_.request_count(), 0UL);  }  // Send new resolution that removes child policy from service config.  SetNextResolutionAllBalancers();  WaitForAllBackends();  CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);  // Check that every backend saw the same number of requests.  This verifies  // that we used round_robin.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(backends_[i]->service_.request_count(), 2UL);  }  // Done.  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // Check LB policy name for the channel.  EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName());}TEST_F(SingleBalancerTest, SameBackendListedMultipleTimes) {  SetNextResolutionAllBalancers();  // Same backend listed twice.  std::vector<int> ports;  ports.push_back(backends_[0]->port_);  ports.push_back(backends_[0]->port_);  const size_t kNumRpcsPerAddress = 10;  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(ports, {}), 0);  // We need to wait for the backend to come online.  WaitForBackend(0);  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * ports.size());  // Backend should have gotten 20 requests.  EXPECT_EQ(kNumRpcsPerAddress * 2, backends_[0]->service_.request_count());  // And they should have come from a single client port, because of  // subchannel sharing.  EXPECT_EQ(1UL, backends_[0]->service_.clients().size());  balancers_[0]->service_.NotifyDoneWithServerlists();}TEST_F(SingleBalancerTest, SecureNaming) {  ResetStub(0, kApplicationTargetName_ + ";lb");  SetNextResolution({AddressData{balancers_[0]->port_, "lb"}});  const size_t kNumRpcsPerAddress = 100;  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // We need to wait for all backends to come online.  WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count());  }  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // Check LB policy name for the channel.  EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName());}TEST_F(SingleBalancerTest, SecureNamingDeathTest) {  ::testing::FLAGS_gtest_death_test_style = "threadsafe";  // Make sure that we blow up (via abort() from the security connector) when  // the name from the balancer doesn't match expectations.  ASSERT_DEATH_IF_SUPPORTED(      {        ResetStub(0, kApplicationTargetName_ + ";lb");        SetNextResolution({AddressData{balancers_[0]->port_, "woops"}});        channel_->WaitForConnected(grpc_timeout_seconds_to_deadline(1));      },      "");}TEST_F(SingleBalancerTest, InitiallyEmptyServerlist) {  SetNextResolutionAllBalancers();  const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor();  const int kCallDeadlineMs = kServerlistDelayMs * 2;  // First response is an empty serverlist, sent right away.  ScheduleResponseForBalancer(0, LoadBalanceResponse(), 0);  // Send non-empty serverlist only after kServerlistDelayMs  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      kServerlistDelayMs);  const auto t0 = system_clock::now();  // Client will block: LB will initially send empty serverlist.  CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */);  const auto ellapsed_ms =      std::chrono::duration_cast<std::chrono::milliseconds>(          system_clock::now() - t0);  // but eventually, the LB sends a serverlist update that allows the call to  // proceed. The call delay must be larger than the delay in sending the  // populated serverlist but under the call's deadline (which is enforced by  // the call's deadline).  EXPECT_GT(ellapsed_ms.count(), kServerlistDelayMs);  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent two responses.  EXPECT_EQ(2U, balancers_[0]->service_.response_count());}TEST_F(SingleBalancerTest, AllServersUnreachableFailFast) {  SetNextResolutionAllBalancers();  const size_t kNumUnreachableServers = 5;  std::vector<int> ports;  for (size_t i = 0; i < kNumUnreachableServers; ++i) {    ports.push_back(grpc_pick_unused_port_or_die());  }  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(ports, {}), 0);  const Status status = SendRpc();  // The error shouldn't be DEADLINE_EXCEEDED.  EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code());  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());}TEST_F(SingleBalancerTest, Fallback) {  SetNextResolutionAllBalancers();  const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor();  const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor();  const size_t kNumBackendsInResolution = backends_.size() / 2;  ResetStub(kFallbackTimeoutMs);  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  std::vector<AddressData> backend_addresses;  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""});  }  SetNextResolution(balancer_addresses, backend_addresses);  // Send non-empty serverlist only after kServerlistDelayMs.  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          GetBackendPorts(kNumBackendsInResolution /* start_index */), {}),      kServerlistDelayMs);  // Wait until all the fallback backends are reachable.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    WaitForBackend(i);  }  // The first request.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(kNumBackendsInResolution);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // Fallback is used: each backend returned by the resolver should have  // gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(1U, backends_[i]->service_.request_count());  }  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    EXPECT_EQ(0U, backends_[i]->service_.request_count());  }  // Wait until the serverlist reception has been processed and all backends  // in the serverlist are reachable.  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    WaitForBackend(i);  }  // Send out the second request.  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(backends_.size() - kNumBackendsInResolution);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // Serverlist is used: each backend returned by the balancer should  // have gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(0U, backends_[i]->service_.request_count());  }  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    EXPECT_EQ(1U, backends_[i]->service_.request_count());  }  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());}TEST_F(SingleBalancerTest, FallbackUpdate) {  SetNextResolutionAllBalancers();  const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor();  const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor();  const size_t kNumBackendsInResolution = backends_.size() / 3;  const size_t kNumBackendsInResolutionUpdate = backends_.size() / 3;  ResetStub(kFallbackTimeoutMs);  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  std::vector<AddressData> backend_addresses;  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""});  }  SetNextResolution(balancer_addresses, backend_addresses);  // Send non-empty serverlist only after kServerlistDelayMs.  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          GetBackendPorts(kNumBackendsInResolution +                          kNumBackendsInResolutionUpdate /* start_index */),          {}),      kServerlistDelayMs);  // Wait until all the fallback backends are reachable.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    WaitForBackend(i);  }  // The first request.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(kNumBackendsInResolution);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // Fallback is used: each backend returned by the resolver should have  // gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(1U, backends_[i]->service_.request_count());  }  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    EXPECT_EQ(0U, backends_[i]->service_.request_count());  }  balancer_addresses.clear();  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  backend_addresses.clear();  for (size_t i = kNumBackendsInResolution;       i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) {    backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""});  }  SetNextResolution(balancer_addresses, backend_addresses);  // Wait until the resolution update has been processed and all the new  // fallback backends are reachable.  for (size_t i = kNumBackendsInResolution;       i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) {    WaitForBackend(i);  }  // Send out the second request.  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(kNumBackendsInResolutionUpdate);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // The resolution update is used: each backend in the resolution update should  // have gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(0U, backends_[i]->service_.request_count());  }  for (size_t i = kNumBackendsInResolution;       i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) {    EXPECT_EQ(1U, backends_[i]->service_.request_count());  }  for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate;       i < backends_.size(); ++i) {    EXPECT_EQ(0U, backends_[i]->service_.request_count());  }  // Wait until the serverlist reception has been processed and all backends  // in the serverlist are reachable.  for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate;       i < backends_.size(); ++i) {    WaitForBackend(i);  }  // Send out the third request.  gpr_log(GPR_INFO, "========= BEFORE THIRD BATCH ==========");  CheckRpcSendOk(backends_.size() - kNumBackendsInResolution -                 kNumBackendsInResolutionUpdate);  gpr_log(GPR_INFO, "========= DONE WITH THIRD BATCH ==========");  // Serverlist is used: each backend returned by the balancer should  // have gotten one request.  for (size_t i = 0;       i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) {    EXPECT_EQ(0U, backends_[i]->service_.request_count());  }  for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate;       i < backends_.size(); ++i) {    EXPECT_EQ(1U, backends_[i]->service_.request_count());  }  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());}TEST_F(SingleBalancerTest,       FallbackAfterStartup_LoseContactWithBalancerThenBackends) {  // First two backends are fallback, last two are pointed to by balancer.  const size_t kNumFallbackBackends = 2;  const size_t kNumBalancerBackends = backends_.size() - kNumFallbackBackends;  std::vector<AddressData> backend_addresses;  for (size_t i = 0; i < kNumFallbackBackends; ++i) {    backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""});  }  std::vector<AddressData> balancer_addresses;  for (size_t i = 0; i < balancers_.size(); ++i) {    balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""});  }  SetNextResolution(balancer_addresses, backend_addresses);  ScheduleResponseForBalancer(0,                              BalancerServiceImpl::BuildResponseForBackends(                                  GetBackendPorts(kNumFallbackBackends), {}),                              0);  // Try to connect.  channel_->GetState(true /* try_to_connect */);  WaitForAllBackends(1 /* num_requests_multiple_of */,                     kNumFallbackBackends /* start_index */);  // Stop balancer.  RPCs should continue going to backends from balancer.  balancers_[0]->Shutdown();  CheckRpcSendOk(100 * kNumBalancerBackends);  for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) {    EXPECT_EQ(100UL, backends_[i]->service_.request_count());  }  // Stop backends from balancer.  This should put us in fallback mode.  for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) {    ShutdownBackend(i);  }  WaitForAllBackends(1 /* num_requests_multiple_of */, 0 /* start_index */,                     kNumFallbackBackends /* stop_index */);  // Restart the backends from the balancer.  We should *not* start  // sending traffic back to them at this point (although the behavior  // in xds may be different).  for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) {    StartBackend(i);  }  CheckRpcSendOk(100 * kNumBalancerBackends);  for (size_t i = 0; i < kNumFallbackBackends; ++i) {    EXPECT_EQ(100UL, backends_[i]->service_.request_count());  }  // Now start the balancer again.  This should cause us to exit  // fallback mode.  balancers_[0]->Start(server_host_);  ScheduleResponseForBalancer(0,                              BalancerServiceImpl::BuildResponseForBackends(                                  GetBackendPorts(kNumFallbackBackends), {}),                              0);  WaitForAllBackends(1 /* num_requests_multiple_of */,                     kNumFallbackBackends /* start_index */);}TEST_F(SingleBalancerTest,       FallbackAfterStartup_LoseContactWithBackendsThenBalancer) {  // First two backends are fallback, last two are pointed to by balancer.  const size_t kNumFallbackBackends = 2;  const size_t kNumBalancerBackends = backends_.size() - kNumFallbackBackends;  std::vector<AddressData> backend_addresses;  for (size_t i = 0; i < kNumFallbackBackends; ++i) {    backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""});  }  std::vector<AddressData> balancer_addresses;  for (size_t i = 0; i < balancers_.size(); ++i) {    balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""});  }  SetNextResolution(balancer_addresses, backend_addresses);  ScheduleResponseForBalancer(0,                              BalancerServiceImpl::BuildResponseForBackends(                                  GetBackendPorts(kNumFallbackBackends), {}),                              0);  // Try to connect.  channel_->GetState(true /* try_to_connect */);  WaitForAllBackends(1 /* num_requests_multiple_of */,                     kNumFallbackBackends /* start_index */);  // Stop backends from balancer.  Since we are still in contact with  // the balancer at this point, RPCs should be failing.  for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) {    ShutdownBackend(i);  }  CheckRpcSendFailure();  // Stop balancer.  This should put us in fallback mode.  balancers_[0]->Shutdown();  WaitForAllBackends(1 /* num_requests_multiple_of */, 0 /* start_index */,                     kNumFallbackBackends /* stop_index */);  // Restart the backends from the balancer.  We should *not* start  // sending traffic back to them at this point (although the behavior  // in xds may be different).  for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) {    StartBackend(i);  }  CheckRpcSendOk(100 * kNumBalancerBackends);  for (size_t i = 0; i < kNumFallbackBackends; ++i) {    EXPECT_EQ(100UL, backends_[i]->service_.request_count());  }  // Now start the balancer again.  This should cause us to exit  // fallback mode.  balancers_[0]->Start(server_host_);  ScheduleResponseForBalancer(0,                              BalancerServiceImpl::BuildResponseForBackends(                                  GetBackendPorts(kNumFallbackBackends), {}),                              0);  WaitForAllBackends(1 /* num_requests_multiple_of */,                     kNumFallbackBackends /* start_index */);}TEST_F(SingleBalancerTest, FallbackEarlyWhenBalancerChannelFails) {  const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  // Return an unreachable balancer and one fallback backend.  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(      AddressData{grpc_pick_unused_port_or_die(), ""});  std::vector<AddressData> backend_addresses;  backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""});  SetNextResolution(balancer_addresses, backend_addresses);  // Send RPC with deadline less than the fallback timeout and make sure it  // succeeds.  CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000,                 /* wait_for_ready */ false);}TEST_F(SingleBalancerTest, FallbackEarlyWhenBalancerCallFails) {  const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  // Return one balancer and one fallback backend.  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  std::vector<AddressData> backend_addresses;  backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""});  SetNextResolution(balancer_addresses, backend_addresses);  // Balancer drops call without sending a serverlist.  balancers_[0]->service_.NotifyDoneWithServerlists();  // Send RPC with deadline less than the fallback timeout and make sure it  // succeeds.  CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000,                 /* wait_for_ready */ false);}TEST_F(SingleBalancerTest, FallbackControlledByBalancer_BeforeFirstServerlist) {  const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  // Return one balancer and one fallback backend.  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  std::vector<AddressData> backend_addresses;  backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""});  SetNextResolution(balancer_addresses, backend_addresses);  // Balancer explicitly tells client to fallback.  LoadBalanceResponse resp;  resp.mutable_fallback_response();  ScheduleResponseForBalancer(0, resp, 0);  // Send RPC with deadline less than the fallback timeout and make sure it  // succeeds.  CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000,                 /* wait_for_ready */ false);}TEST_F(SingleBalancerTest, FallbackControlledByBalancer_AfterFirstServerlist) {  // Return one balancer and one fallback backend (backend 0).  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  std::vector<AddressData> backend_addresses;  backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""});  SetNextResolution(balancer_addresses, backend_addresses);  // Balancer initially sends serverlist, then tells client to fall back,  // then sends the serverlist again.  // The serverlist points to backend 1.  LoadBalanceResponse serverlist_resp =      BalancerServiceImpl::BuildResponseForBackends({backends_[1]->port_}, {});  LoadBalanceResponse fallback_resp;  fallback_resp.mutable_fallback_response();  ScheduleResponseForBalancer(0, serverlist_resp, 0);  ScheduleResponseForBalancer(0, fallback_resp, 100);  ScheduleResponseForBalancer(0, serverlist_resp, 100);  // Requests initially go to backend 1, then go to backend 0 in  // fallback mode, then go back to backend 1 when we exit fallback.  WaitForBackend(1);  WaitForBackend(0);  WaitForBackend(1);}TEST_F(SingleBalancerTest, BackendsRestart) {  SetNextResolutionAllBalancers();  const size_t kNumRpcsPerAddress = 100;  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Stop backends.  RPCs should fail.  ShutdownAllBackends();  CheckRpcSendFailure();  // Restart backends.  RPCs should start succeeding again.  StartAllBackends();  CheckRpcSendOk(1 /* times */, 2000 /* timeout_ms */,                 true /* wait_for_ready */);  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());}TEST_F(SingleBalancerTest, ServiceNameFromLbPolicyConfig) {  constexpr char kServiceConfigWithTarget[] =      "{\n"      "  \"loadBalancingConfig\":[\n"      "    { \"grpclb\":{\n"      "      \"serviceName\":\"test_service\"\n"      "    }}\n"      "  ]\n"      "}";  SetNextResolutionAllBalancers(kServiceConfigWithTarget);  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // We need to wait for all backends to come online.  WaitForAllBackends();  EXPECT_EQ(balancers_[0]->service_.service_names().back(), "test_service");}class UpdatesTest : public GrpclbEnd2endTest { public:  UpdatesTest() : GrpclbEnd2endTest(4, 3, 0) {}};TEST_F(UpdatesTest, UpdateBalancersButKeepUsingOriginalBalancer) {  SetNextResolutionAllBalancers();  const std::vector<int> first_backend{GetBackendPorts()[0]};  const std::vector<int> second_backend{GetBackendPorts()[1]};  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);  ScheduleResponseForBalancer(      1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);  // Wait until the first backend is ready.  WaitForBackend(0);  // Send 10 requests.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->service_.request_count());  // Balancer 0 got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  EXPECT_EQ(0U, balancers_[1]->service_.request_count());  EXPECT_EQ(0U, balancers_[1]->service_.response_count());  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());  std::vector<AddressData> addresses;  addresses.emplace_back(AddressData{balancers_[1]->port_, ""});  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 ==========");  SetNextResolution(addresses);  gpr_log(GPR_INFO, "========= UPDATE 1 DONE ==========");  EXPECT_EQ(0U, backends_[1]->service_.request_count());  gpr_timespec deadline = gpr_time_add(      gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(10000, GPR_TIMESPAN));  // Send 10 seconds worth of RPCs  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // The current LB call is still working, so grpclb continued using it to the  // first balancer, which doesn't assign the second backend.  EXPECT_EQ(0U, backends_[1]->service_.request_count());  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  EXPECT_EQ(0U, balancers_[1]->service_.request_count());  EXPECT_EQ(0U, balancers_[1]->service_.response_count());  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());}// Send an update with the same set of LBs as the one in SetUp() in order to// verify that the LB channel inside grpclb keeps the initial connection (which// by definition is also present in the update).TEST_F(UpdatesTest, UpdateBalancersRepeated) {  SetNextResolutionAllBalancers();  const std::vector<int> first_backend{GetBackendPorts()[0]};  const std::vector<int> second_backend{GetBackendPorts()[0]};  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);  ScheduleResponseForBalancer(      1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);  // Wait until the first backend is ready.  WaitForBackend(0);  // Send 10 requests.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->service_.request_count());  balancers_[0]->service_.NotifyDoneWithServerlists();  // Balancer 0 got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  EXPECT_EQ(0U, balancers_[1]->service_.request_count());  EXPECT_EQ(0U, balancers_[1]->service_.response_count());  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());  std::vector<AddressData> addresses;  addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  addresses.emplace_back(AddressData{balancers_[1]->port_, ""});  addresses.emplace_back(AddressData{balancers_[2]->port_, ""});  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 ==========");  SetNextResolution(addresses);  gpr_log(GPR_INFO, "========= UPDATE 1 DONE ==========");  EXPECT_EQ(0U, backends_[1]->service_.request_count());  gpr_timespec deadline = gpr_time_add(      gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(10000, GPR_TIMESPAN));  // Send 10 seconds worth of RPCs  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // grpclb continued using the original LB call to the first balancer, which  // doesn't assign the second backend.  EXPECT_EQ(0U, backends_[1]->service_.request_count());  balancers_[0]->service_.NotifyDoneWithServerlists();  addresses.clear();  addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  addresses.emplace_back(AddressData{balancers_[1]->port_, ""});  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 2 ==========");  SetNextResolution(addresses);  gpr_log(GPR_INFO, "========= UPDATE 2 DONE ==========");  EXPECT_EQ(0U, backends_[1]->service_.request_count());  deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),                          gpr_time_from_millis(10000, GPR_TIMESPAN));  // Send 10 seconds worth of RPCs  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // grpclb continued using the original LB call to the first balancer, which  // doesn't assign the second backend.  EXPECT_EQ(0U, backends_[1]->service_.request_count());  balancers_[0]->service_.NotifyDoneWithServerlists();}TEST_F(UpdatesTest, UpdateBalancersDeadUpdate) {  std::vector<AddressData> addresses;  addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  SetNextResolution(addresses);  const std::vector<int> first_backend{GetBackendPorts()[0]};  const std::vector<int> second_backend{GetBackendPorts()[1]};  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);  ScheduleResponseForBalancer(      1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);  // Start servers and send 10 RPCs per server.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->service_.request_count());  // Kill balancer 0  gpr_log(GPR_INFO, "********** ABOUT TO KILL BALANCER 0 *************");  balancers_[0]->Shutdown();  gpr_log(GPR_INFO, "********** KILLED BALANCER 0 *************");  // This is serviced by the existing RR policy  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // All 10 requests should again have gone to the first backend.  EXPECT_EQ(20U, backends_[0]->service_.request_count());  EXPECT_EQ(0U, backends_[1]->service_.request_count());  // Balancer 0 got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  EXPECT_EQ(0U, balancers_[1]->service_.request_count());  EXPECT_EQ(0U, balancers_[1]->service_.response_count());  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());  addresses.clear();  addresses.emplace_back(AddressData{balancers_[1]->port_, ""});  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 ==========");  SetNextResolution(addresses);  gpr_log(GPR_INFO, "========= UPDATE 1 DONE ==========");  // Wait until update has been processed, as signaled by the second backend  // receiving a request. In the meantime, the client continues to be serviced  // (by the first backend) without interruption.  EXPECT_EQ(0U, backends_[1]->service_.request_count());  WaitForBackend(1);  // This is serviced by the updated RR policy  backends_[1]->service_.ResetCounters();  gpr_log(GPR_INFO, "========= BEFORE THIRD BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH THIRD BATCH ==========");  // All 10 requests should have gone to the second backend.  EXPECT_EQ(10U, backends_[1]->service_.request_count());  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // The second balancer, published as part of the first update, may end up  // getting two requests (that is, 1 <= #req <= 2) if the LB call retry timer  // firing races with the arrival of the update containing the second  // balancer.  EXPECT_GE(balancers_[1]->service_.request_count(), 1U);  EXPECT_GE(balancers_[1]->service_.response_count(), 1U);  EXPECT_LE(balancers_[1]->service_.request_count(), 2U);  EXPECT_LE(balancers_[1]->service_.response_count(), 2U);  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());}TEST_F(UpdatesTest, ReresolveDeadBackend) {  ResetStub(500);  // The first resolution contains the addresses of a balancer that never  // responds, and a fallback backend.  std::vector<AddressData> balancer_addresses;  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  std::vector<AddressData> backend_addresses;  backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""});  SetNextResolution(balancer_addresses, backend_addresses);  // Ask channel to connect to trigger resolver creation.  channel_->GetState(true);  // The re-resolution result will contain the addresses of the same balancer  // and a new fallback backend.  balancer_addresses.clear();  balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  backend_addresses.clear();  backend_addresses.emplace_back(AddressData{backends_[1]->port_, ""});  SetNextReresolutionResponse(balancer_addresses, backend_addresses);  // Start servers and send 10 RPCs per server.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the fallback backend.  EXPECT_EQ(10U, backends_[0]->service_.request_count());  // Kill backend 0.  gpr_log(GPR_INFO, "********** ABOUT TO KILL BACKEND 0 *************");  backends_[0]->Shutdown();  gpr_log(GPR_INFO, "********** KILLED BACKEND 0 *************");  // Wait until re-resolution has finished, as signaled by the second backend  // receiving a request.  WaitForBackend(1);  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // All 10 requests should have gone to the second backend.  EXPECT_EQ(10U, backends_[1]->service_.request_count());  balancers_[0]->service_.NotifyDoneWithServerlists();  balancers_[1]->service_.NotifyDoneWithServerlists();  balancers_[2]->service_.NotifyDoneWithServerlists();  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  EXPECT_EQ(0U, balancers_[0]->service_.response_count());  EXPECT_EQ(0U, balancers_[1]->service_.request_count());  EXPECT_EQ(0U, balancers_[1]->service_.response_count());  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());}// TODO(juanlishen): Should be removed when the first response is always the// initial response. Currently, if client load reporting is not enabled, the// balancer doesn't send initial response. When the backend shuts down, an// unexpected re-resolution will happen. This test configuration is a workaround// for test ReresolveDeadBalancer.class UpdatesWithClientLoadReportingTest : public GrpclbEnd2endTest { public:  UpdatesWithClientLoadReportingTest() : GrpclbEnd2endTest(4, 3, 2) {}};TEST_F(UpdatesWithClientLoadReportingTest, ReresolveDeadBalancer) {  const std::vector<int> first_backend{GetBackendPorts()[0]};  const std::vector<int> second_backend{GetBackendPorts()[1]};  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0);  ScheduleResponseForBalancer(      1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0);  // Ask channel to connect to trigger resolver creation.  channel_->GetState(true);  std::vector<AddressData> addresses;  addresses.emplace_back(AddressData{balancers_[0]->port_, ""});  SetNextResolution(addresses);  addresses.clear();  addresses.emplace_back(AddressData{balancers_[1]->port_, ""});  SetNextReresolutionResponse(addresses);  // Start servers and send 10 RPCs per server.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->service_.request_count());  // Kill backend 0.  gpr_log(GPR_INFO, "********** ABOUT TO KILL BACKEND 0 *************");  backends_[0]->Shutdown();  gpr_log(GPR_INFO, "********** KILLED BACKEND 0 *************");  CheckRpcSendFailure();  // Balancer 0 got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  EXPECT_EQ(0U, balancers_[1]->service_.request_count());  EXPECT_EQ(0U, balancers_[1]->service_.response_count());  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());  // Kill balancer 0.  gpr_log(GPR_INFO, "********** ABOUT TO KILL BALANCER 0 *************");  balancers_[0]->Shutdown();  gpr_log(GPR_INFO, "********** KILLED BALANCER 0 *************");  // Wait until re-resolution has finished, as signaled by the second backend  // receiving a request.  WaitForBackend(1);  // This is serviced by the new serverlist.  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // All 10 requests should have gone to the second backend.  EXPECT_EQ(10U, backends_[1]->service_.request_count());  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  // After balancer 0 is killed, we restart an LB call immediately (because we  // disconnect to a previously connected balancer). Although we will cancel  // this call when the re-resolution update is done and another LB call restart  // is needed, this old call may still succeed reaching the LB server if  // re-resolution is slow. So balancer 1 may have received 2 requests and sent  // 2 responses.  EXPECT_GE(balancers_[1]->service_.request_count(), 1U);  EXPECT_GE(balancers_[1]->service_.response_count(), 1U);  EXPECT_LE(balancers_[1]->service_.request_count(), 2U);  EXPECT_LE(balancers_[1]->service_.response_count(), 2U);  EXPECT_EQ(0U, balancers_[2]->service_.request_count());  EXPECT_EQ(0U, balancers_[2]->service_.response_count());}TEST_F(SingleBalancerTest, Drop) {  SetNextResolutionAllBalancers();  const size_t kNumRpcsPerAddress = 100;  const int num_of_drop_by_rate_limiting_addresses = 1;  const int num_of_drop_by_load_balancing_addresses = 2;  const int num_of_drop_addresses = num_of_drop_by_rate_limiting_addresses +                                    num_of_drop_by_load_balancing_addresses;  const int num_total_addresses = num_backends_ + num_of_drop_addresses;  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          GetBackendPorts(),          {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},           {"load_balancing", num_of_drop_by_load_balancing_addresses}}),      0);  // Wait until all backends are ready.  WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs for each server and drop address.  size_t num_drops = 0;  for (size_t i = 0; i < kNumRpcsPerAddress * num_total_addresses; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  EXPECT_EQ(kNumRpcsPerAddress * num_of_drop_addresses, num_drops);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count());  }  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());}TEST_F(SingleBalancerTest, DropAllFirst) {  SetNextResolutionAllBalancers();  // All registered addresses are marked as "drop".  const int num_of_drop_by_rate_limiting_addresses = 1;  const int num_of_drop_by_load_balancing_addresses = 1;  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          {}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},               {"load_balancing", num_of_drop_by_load_balancing_addresses}}),      0);  const Status status = SendRpc(nullptr, 1000, true);  EXPECT_FALSE(status.ok());  EXPECT_EQ(status.error_message(), "Call dropped by load balancing policy");}TEST_F(SingleBalancerTest, DropAll) {  SetNextResolutionAllBalancers();  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  const int num_of_drop_by_rate_limiting_addresses = 1;  const int num_of_drop_by_load_balancing_addresses = 1;  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          {}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},               {"load_balancing", num_of_drop_by_load_balancing_addresses}}),      1000);  // First call succeeds.  CheckRpcSendOk();  // But eventually, the update with only dropped servers is processed and calls  // fail.  Status status;  do {    status = SendRpc(nullptr, 1000, true);  } while (status.ok());  EXPECT_FALSE(status.ok());  EXPECT_EQ(status.error_message(), "Call dropped by load balancing policy");}class SingleBalancerWithClientLoadReportingTest : public GrpclbEnd2endTest { public:  SingleBalancerWithClientLoadReportingTest() : GrpclbEnd2endTest(4, 1, 3) {}};TEST_F(SingleBalancerWithClientLoadReportingTest, Vanilla) {  SetNextResolutionAllBalancers();  const size_t kNumRpcsPerAddress = 100;  ScheduleResponseForBalancer(      0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}),      0);  // Wait until all backends are ready.  int num_ok = 0;  int num_failure = 0;  int num_drops = 0;  std::tie(num_ok, num_failure, num_drops) = WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count());  }  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  ClientStats client_stats;  do {    client_stats += WaitForLoadReports();  } while (client_stats.num_calls_finished !=           kNumRpcsPerAddress * num_backends_ + num_ok);  EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok,            client_stats.num_calls_started);  EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok,            client_stats.num_calls_finished);  EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send);  EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + (num_ok + num_drops),            client_stats.num_calls_finished_known_received);  EXPECT_THAT(client_stats.drop_token_counts, ::testing::ElementsAre());}TEST_F(SingleBalancerWithClientLoadReportingTest, BalancerRestart) {  SetNextResolutionAllBalancers();  const size_t kNumBackendsFirstPass = 2;  const size_t kNumBackendsSecondPass =      backends_.size() - kNumBackendsFirstPass;  // Balancer returns backends starting at index 1.  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          GetBackendPorts(0, kNumBackendsFirstPass), {}),      0);  // Wait until all backends returned by the balancer are ready.  int num_ok = 0;  int num_failure = 0;  int num_drops = 0;  std::tie(num_ok, num_failure, num_drops) =      WaitForAllBackends(/* num_requests_multiple_of */ 1, /* start_index */ 0,                         /* stop_index */ kNumBackendsFirstPass);  balancers_[0]->service_.NotifyDoneWithServerlists();  ClientStats client_stats = WaitForLoadReports();  EXPECT_EQ(static_cast<size_t>(num_ok), client_stats.num_calls_started);  EXPECT_EQ(static_cast<size_t>(num_ok), client_stats.num_calls_finished);  EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send);  EXPECT_EQ(static_cast<size_t>(num_ok),            client_stats.num_calls_finished_known_received);  EXPECT_THAT(client_stats.drop_token_counts, ::testing::ElementsAre());  // Shut down the balancer.  balancers_[0]->Shutdown();  // Send 10 more requests per backend.  This will continue using the  // last serverlist we received from the balancer before it was shut down.  ResetBackendCounters();  CheckRpcSendOk(kNumBackendsFirstPass);  // Each backend should have gotten 1 request.  for (size_t i = 0; i < kNumBackendsFirstPass; ++i) {    EXPECT_EQ(1UL, backends_[i]->service_.request_count());  }  // Now restart the balancer, this time pointing to all backends.  balancers_[0]->Start(server_host_);  ScheduleResponseForBalancer(0,                              BalancerServiceImpl::BuildResponseForBackends(                                  GetBackendPorts(kNumBackendsFirstPass), {}),                              0);  // Wait for queries to start going to one of the new backends.  // This tells us that we're now using the new serverlist.  do {    CheckRpcSendOk();  } while (backends_[2]->service_.request_count() == 0 &&           backends_[3]->service_.request_count() == 0);  // Send one RPC per backend.  CheckRpcSendOk(kNumBackendsSecondPass);  balancers_[0]->service_.NotifyDoneWithServerlists();  // Check client stats.  client_stats = WaitForLoadReports();  EXPECT_EQ(kNumBackendsSecondPass + 1, client_stats.num_calls_started);  EXPECT_EQ(kNumBackendsSecondPass + 1, client_stats.num_calls_finished);  EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send);  EXPECT_EQ(kNumBackendsSecondPass + 1,            client_stats.num_calls_finished_known_received);  EXPECT_THAT(client_stats.drop_token_counts, ::testing::ElementsAre());}TEST_F(SingleBalancerWithClientLoadReportingTest, Drop) {  SetNextResolutionAllBalancers();  const size_t kNumRpcsPerAddress = 3;  const int num_of_drop_by_rate_limiting_addresses = 2;  const int num_of_drop_by_load_balancing_addresses = 1;  const int num_of_drop_addresses = num_of_drop_by_rate_limiting_addresses +                                    num_of_drop_by_load_balancing_addresses;  const int num_total_addresses = num_backends_ + num_of_drop_addresses;  ScheduleResponseForBalancer(      0,      BalancerServiceImpl::BuildResponseForBackends(          GetBackendPorts(),          {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},           {"load_balancing", num_of_drop_by_load_balancing_addresses}}),      0);  // Wait until all backends are ready.  int num_warmup_ok = 0;  int num_warmup_failure = 0;  int num_warmup_drops = 0;  std::tie(num_warmup_ok, num_warmup_failure, num_warmup_drops) =      WaitForAllBackends(num_total_addresses /* num_requests_multiple_of */);  const int num_total_warmup_requests =      num_warmup_ok + num_warmup_failure + num_warmup_drops;  size_t num_drops = 0;  for (size_t i = 0; i < kNumRpcsPerAddress * num_total_addresses; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  EXPECT_EQ(kNumRpcsPerAddress * num_of_drop_addresses, num_drops);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count());  }  balancers_[0]->service_.NotifyDoneWithServerlists();  // The balancer got a single request.  EXPECT_EQ(1U, balancers_[0]->service_.request_count());  // and sent a single response.  EXPECT_EQ(1U, balancers_[0]->service_.response_count());  const ClientStats client_stats = WaitForLoadReports();  EXPECT_EQ(      kNumRpcsPerAddress * num_total_addresses + num_total_warmup_requests,      client_stats.num_calls_started);  EXPECT_EQ(      kNumRpcsPerAddress * num_total_addresses + num_total_warmup_requests,      client_stats.num_calls_finished);  EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send);  EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_warmup_ok,            client_stats.num_calls_finished_known_received);  // The number of warmup request is a multiple of the number of addresses.  // Therefore, all addresses in the scheduled balancer response are hit the  // same number of times.  const int num_times_drop_addresses_hit =      num_warmup_drops / num_of_drop_addresses;  EXPECT_THAT(      client_stats.drop_token_counts,      ::testing::ElementsAre(          ::testing::Pair("load_balancing",                          (kNumRpcsPerAddress + num_times_drop_addresses_hit)),          ::testing::Pair(              "rate_limiting",              (kNumRpcsPerAddress + num_times_drop_addresses_hit) * 2)));}}  // namespace}  // namespace testing}  // namespace grpcint main(int argc, char** argv) {  grpc::testing::TestEnvironment env(argc, argv);  ::testing::InitGoogleTest(&argc, argv);  const auto result = RUN_ALL_TESTS();  return result;}
 |