void ReportResult()

in e2e-examples/gcs/benchmark/channel_policy.cc [235:317]


  void ReportResult(void* handle, const grpc::Status& status,
                    const grpc::ClientContext& context,
                    absl::Duration elapsed_time, int64_t bytes) override {
    absl::MutexLock l(&lock_);

    // if error, evict it right away.
    if (!status.ok()) {
      if (status.error_code() == grpc::CANCELLED ||
          status.error_code() == grpc::DEADLINE_EXCEEDED) {
        auto i = std::find_if(channels_.begin(), channels_.end(),
                              [handle](std::shared_ptr<grpc::Channel> val) {
                                return (void*)val.get() == handle;
                              });
        if (i != channels_.end()) {
          std::cout << "Evict the channel (peer=" << context.peer()
                    << ") due to error:" << status.error_code() << std::endl;
          *i = channel_creator_();
        }
        return;
      } else {
        return;
      }
    }

    // Update the state of the corresponding channel.
    auto iter = channep_state_map_.find(handle);
    if (iter != channep_state_map_.end()) {
      iter->second.count += 1;
      iter->second.total_bytes += bytes;
      iter->second.total_time += elapsed_time;
      iter->second.peer = context.peer();
    }

    // once the update counter exceeds the threadhold, it evaluates
    // the last performer to be evicted.
    score_count += 1;
    if (score_count > (int)channep_state_map_.size() * 3) {
      void* least_key = 0;
      int valid_count = 0;
      int64_t least_score = 0xffffffffffffl;
      std::string least_peer;
      int64_t best_score = 0;
      for (auto s : channep_state_map_) {
        if (s.second.count == 0) {
          continue;
        }
        valid_count += 1;
        int64_t score = s.second.total_bytes /
                        absl::ToInt64Milliseconds(s.second.total_time);
        if (score < least_score) {
          least_key = s.first;
          least_score = score;
          least_peer = s.second.peer;
        }
        if (score > best_score) {
          best_score = score;
        }
      }

      if (valid_count < int(channep_state_map_.size()) / 2) {
        std::cerr << "No quorum: " << valid_count << " of "
                  << channep_state_map_.size() / 2 << std::endl;
      } else if (least_score >= best_score / 3) {
        std::cerr << "No least to evict: least_score " << least_score
                  << ", best_score: " << best_score << std::endl;
        score_count = 0;
        InitChannelStateMap();
      } else {
        auto i = std::find_if(channels_.begin(), channels_.end(),
                              [least_key](std::shared_ptr<grpc::Channel> val) {
                                return (void*)val.get() == least_key;
                              });
        if (i != channels_.end()) {
          std::cout << "Evict the channel (peer=" << least_peer
                    << ") because it underperformed score: " << least_score
                    << ", best_score: " << best_score << std::endl;
          *i = channel_creator_();
        }
        score_count = 0;
        InitChannelStateMap();
      }
    }
  }