|
| 1 | +#include "adaptive_load/step_controller_impl.h" |
| 2 | + |
| 3 | +#include <memory> |
| 4 | + |
| 5 | +#include "external/envoy/source/common/protobuf/protobuf.h" |
| 6 | + |
| 7 | +#include "api/adaptive_load/adaptive_load.pb.h" |
| 8 | +#include "api/adaptive_load/benchmark_result.pb.h" |
| 9 | +#include "api/adaptive_load/metric_spec.pb.h" |
| 10 | +#include "api/adaptive_load/step_controller_impl.pb.h" |
| 11 | + |
| 12 | +#include "adaptive_load/input_variable_setter_impl.h" |
| 13 | +#include "adaptive_load/plugin_loader.h" |
| 14 | + |
| 15 | +namespace Nighthawk { |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +using ::nighthawk::adaptive_load::BenchmarkResult; |
| 20 | +using ::nighthawk::adaptive_load::ExponentialSearchStepControllerConfig; |
| 21 | +using ::nighthawk::adaptive_load::MetricEvaluation; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks if any non-informational metrics (weight > 0) were outside thresholds (score < 0). |
| 25 | + * |
| 26 | + * @param benchmark_result Metrics from the latest Nighthawk benchmark session. |
| 27 | + * |
| 28 | + * @return double -1.0 if any metric was outside its threshold or 1.0 if all metrics were within |
| 29 | + * thresholds. |
| 30 | + */ |
| 31 | +double TotalScore(const BenchmarkResult& benchmark_result) { |
| 32 | + for (const MetricEvaluation& evaluation : benchmark_result.metric_evaluations()) { |
| 33 | + if (evaluation.weight() > 0.0 && evaluation.threshold_score() < 0.0) { |
| 34 | + return -1.0; |
| 35 | + } |
| 36 | + } |
| 37 | + return 1.0; |
| 38 | +} |
| 39 | + |
| 40 | +} // namespace |
| 41 | + |
| 42 | +Envoy::ProtobufTypes::MessagePtr |
| 43 | +ExponentialSearchStepControllerConfigFactory::createEmptyConfigProto() { |
| 44 | + return std::make_unique<ExponentialSearchStepControllerConfig>(); |
| 45 | +} |
| 46 | + |
| 47 | +std::string ExponentialSearchStepControllerConfigFactory::name() const { |
| 48 | + return "nighthawk.exponential_search"; |
| 49 | +} |
| 50 | + |
| 51 | +absl::Status ExponentialSearchStepControllerConfigFactory::ValidateConfig( |
| 52 | + const Envoy::Protobuf::Message& message) const { |
| 53 | + const auto& any = dynamic_cast<const Envoy::ProtobufWkt::Any&>(message); |
| 54 | + ExponentialSearchStepControllerConfig config; |
| 55 | + Envoy::MessageUtil::unpackTo(any, config); |
| 56 | + if (config.has_input_variable_setter()) { |
| 57 | + return LoadInputVariableSetterPlugin(config.input_variable_setter()).status(); |
| 58 | + } |
| 59 | + return absl::OkStatus(); |
| 60 | +} |
| 61 | + |
| 62 | +StepControllerPtr ExponentialSearchStepControllerConfigFactory::createStepController( |
| 63 | + const Envoy::Protobuf::Message& message, |
| 64 | + const nighthawk::client::CommandLineOptions& command_line_options_template) { |
| 65 | + const auto& any = dynamic_cast<const Envoy::ProtobufWkt::Any&>(message); |
| 66 | + ExponentialSearchStepControllerConfig config; |
| 67 | + Envoy::MessageUtil::unpackTo(any, config); |
| 68 | + return std::make_unique<ExponentialSearchStepController>(config, command_line_options_template); |
| 69 | +} |
| 70 | + |
| 71 | +REGISTER_FACTORY(ExponentialSearchStepControllerConfigFactory, StepControllerConfigFactory); |
| 72 | + |
| 73 | +ExponentialSearchStepController::ExponentialSearchStepController( |
| 74 | + const ExponentialSearchStepControllerConfig& config, |
| 75 | + nighthawk::client::CommandLineOptions command_line_options_template) |
| 76 | + : command_line_options_template_{std::move(command_line_options_template)}, |
| 77 | + exponential_factor_{config.exponential_factor() > 0.0 ? config.exponential_factor() : 2.0}, |
| 78 | + current_load_value_{config.initial_value()} { |
| 79 | + doom_reason_ = ""; |
| 80 | + if (config.has_input_variable_setter()) { |
| 81 | + absl::StatusOr<InputVariableSetterPtr> input_variable_setter_or = |
| 82 | + LoadInputVariableSetterPlugin(config.input_variable_setter()); |
| 83 | + RELEASE_ASSERT(input_variable_setter_or.ok(), |
| 84 | + absl::StrCat("InputVariableSetter plugin loading error should have been caught " |
| 85 | + "during input validation: ", |
| 86 | + input_variable_setter_or.status().message())); |
| 87 | + input_variable_setter_ = std::move(input_variable_setter_or.value()); |
| 88 | + } else { |
| 89 | + input_variable_setter_ = std::make_unique<RequestsPerSecondInputVariableSetter>( |
| 90 | + nighthawk::adaptive_load::RequestsPerSecondInputVariableSetterConfig()); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +absl::StatusOr<nighthawk::client::CommandLineOptions> |
| 95 | +ExponentialSearchStepController::GetCurrentCommandLineOptions() const { |
| 96 | + nighthawk::client::CommandLineOptions options = command_line_options_template_; |
| 97 | + absl::Status status = input_variable_setter_->SetInputVariable(options, current_load_value_); |
| 98 | + if (!status.ok()) { |
| 99 | + return status; |
| 100 | + } |
| 101 | + return options; |
| 102 | +} |
| 103 | + |
| 104 | +bool ExponentialSearchStepController::IsConverged() const { |
| 105 | + // Binary search has brought successive input values within 1% of each other. |
| 106 | + return doom_reason_.empty() && !is_range_finding_phase_ && |
| 107 | + abs(current_load_value_ / previous_load_value_ - 1.0) < 0.01; |
| 108 | +} |
| 109 | + |
| 110 | +bool ExponentialSearchStepController::IsDoomed(std::string& doom_reason) const { |
| 111 | + if (doom_reason_.empty()) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + doom_reason = doom_reason_; |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +void ExponentialSearchStepController::UpdateAndRecompute(const BenchmarkResult& benchmark_result) { |
| 119 | + if (benchmark_result.status().code()) { |
| 120 | + doom_reason_ = "Nighthawk Service returned an error."; |
| 121 | + return; |
| 122 | + } |
| 123 | + const double score = TotalScore(benchmark_result); |
| 124 | + if (is_range_finding_phase_) { |
| 125 | + IterateRangeFindingPhase(score); |
| 126 | + } else { |
| 127 | + IterateBinarySearchPhase(score); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Updates state variables based on the latest score. Exponentially increases the load in each step. |
| 133 | + * Transitions to the binary search phase when the load has caused metrics to go outside thresholds. |
| 134 | + */ |
| 135 | +void ExponentialSearchStepController::IterateRangeFindingPhase(double score) { |
| 136 | + if (score > 0.0) { |
| 137 | + // Have not reached the threshold yet; continue increasing the load exponentially. |
| 138 | + previous_load_value_ = current_load_value_; |
| 139 | + current_load_value_ *= exponential_factor_; |
| 140 | + } else { |
| 141 | + // We have found a value that exceeded the threshold. |
| 142 | + // Prepare for the binary search phase. |
| 143 | + if (std::isnan(previous_load_value_)) { |
| 144 | + doom_reason_ = |
| 145 | + "ExponentialSearchStepController cannot continue if the metrics values already exceed " |
| 146 | + "metric thresholds with the initial load. Check the initial load value in the " |
| 147 | + "ExponentialSearchStepControllerConfig, requested metrics, and thresholds."; |
| 148 | + return; |
| 149 | + } |
| 150 | + is_range_finding_phase_ = false; |
| 151 | + // Binary search is between previous load (ok) and current load (too high). |
| 152 | + bottom_load_value_ = previous_load_value_; |
| 153 | + top_load_value_ = current_load_value_; |
| 154 | + |
| 155 | + previous_load_value_ = current_load_value_; |
| 156 | + current_load_value_ = (bottom_load_value_ + top_load_value_) / 2; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Updates state variables based on the latest score. Performs one step of a binary search. |
| 162 | + */ |
| 163 | +void ExponentialSearchStepController::IterateBinarySearchPhase(double score) { |
| 164 | + if (score > 0.0) { |
| 165 | + // Within threshold, go higher. |
| 166 | + bottom_load_value_ = current_load_value_; |
| 167 | + } else { |
| 168 | + // Outside threshold, go lower. |
| 169 | + top_load_value_ = current_load_value_; |
| 170 | + } |
| 171 | + previous_load_value_ = current_load_value_; |
| 172 | + current_load_value_ = (bottom_load_value_ + top_load_value_) / 2; |
| 173 | +} |
| 174 | + |
| 175 | +} // namespace Nighthawk |
0 commit comments