xref: /aosp_15_r20/external/grpc-grpc/src/core/load_balancing/outlier_detection/outlier_detection.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CORE_LOAD_BALANCING_OUTLIER_DETECTION_OUTLIER_DETECTION_H
18 #define GRPC_SRC_CORE_LOAD_BALANCING_OUTLIER_DETECTION_OUTLIER_DETECTION_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <stdint.h>  // for uint32_t
23 
24 #include "absl/types/optional.h"
25 
26 #include "src/core/lib/gprpp/time.h"
27 #include "src/core/lib/gprpp/validation_errors.h"
28 #include "src/core/lib/json/json.h"
29 #include "src/core/lib/json/json_args.h"
30 #include "src/core/lib/json/json_object_loader.h"
31 
32 namespace grpc_core {
33 
34 struct OutlierDetectionConfig {
35   Duration interval = Duration::Seconds(10);
36   Duration base_ejection_time = Duration::Milliseconds(30000);
37   Duration max_ejection_time = Duration::Milliseconds(30000);
38   uint32_t max_ejection_percent = 10;
39   struct SuccessRateEjection {
40     uint32_t stdev_factor = 1900;
41     uint32_t enforcement_percentage = 100;
42     uint32_t minimum_hosts = 5;
43     uint32_t request_volume = 100;
44 
SuccessRateEjectionOutlierDetectionConfig::SuccessRateEjection45     SuccessRateEjection() {}
46 
47     bool operator==(const SuccessRateEjection& other) const {
48       return stdev_factor == other.stdev_factor &&
49              enforcement_percentage == other.enforcement_percentage &&
50              minimum_hosts == other.minimum_hosts &&
51              request_volume == other.request_volume;
52     }
53 
54     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
55     void JsonPostLoad(const Json&, const JsonArgs&, ValidationErrors* errors);
56   };
57   struct FailurePercentageEjection {
58     uint32_t threshold = 85;
59     uint32_t enforcement_percentage = 100;
60     uint32_t minimum_hosts = 5;
61     uint32_t request_volume = 50;
62 
FailurePercentageEjectionOutlierDetectionConfig::FailurePercentageEjection63     FailurePercentageEjection() {}
64 
65     bool operator==(const FailurePercentageEjection& other) const {
66       return threshold == other.threshold &&
67              enforcement_percentage == other.enforcement_percentage &&
68              minimum_hosts == other.minimum_hosts &&
69              request_volume == other.request_volume;
70     }
71 
72     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
73     void JsonPostLoad(const Json&, const JsonArgs&, ValidationErrors* errors);
74   };
75   absl::optional<SuccessRateEjection> success_rate_ejection;
76   absl::optional<FailurePercentageEjection> failure_percentage_ejection;
77 
78   bool operator==(const OutlierDetectionConfig& other) const {
79     return interval == other.interval &&
80            base_ejection_time == other.base_ejection_time &&
81            max_ejection_time == other.max_ejection_time &&
82            max_ejection_percent == other.max_ejection_percent &&
83            success_rate_ejection == other.success_rate_ejection &&
84            failure_percentage_ejection == other.failure_percentage_ejection;
85   }
86 
87   static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
88   void JsonPostLoad(const Json& json, const JsonArgs&,
89                     ValidationErrors* errors);
90 };
91 
92 }  // namespace grpc_core
93 
94 #endif  // GRPC_SRC_CORE_LOAD_BALANCING_OUTLIER_DETECTION_OUTLIER_DETECTION_H
95