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_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_OUTLIER_DETECTION_OUTLIER_DETECTION_H
18 #define GRPC_SRC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_OUTLIER_DETECTION_OUTLIER_DETECTION_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <stdint.h>  // for uint32_t
23 
24 #include <memory>
25 #include <string>
26 
27 #include "absl/types/optional.h"
28 
29 #include "src/core/lib/gprpp/time.h"
30 #include "src/core/lib/gprpp/validation_errors.h"
31 #include "src/core/lib/json/json.h"
32 #include "src/core/lib/json/json_args.h"
33 #include "src/core/lib/json/json_object_loader.h"
34 #include "src/core/lib/resolver/server_address.h"
35 
36 namespace grpc_core {
37 
38 struct OutlierDetectionConfig {
39   Duration interval = Duration::Seconds(10);
40   Duration base_ejection_time = Duration::Milliseconds(30000);
41   Duration max_ejection_time = Duration::Milliseconds(30000);
42   uint32_t max_ejection_percent = 10;
43   struct SuccessRateEjection {
44     uint32_t stdev_factor = 1900;
45     uint32_t enforcement_percentage = 100;
46     uint32_t minimum_hosts = 5;
47     uint32_t request_volume = 100;
48 
SuccessRateEjectionOutlierDetectionConfig::SuccessRateEjection49     SuccessRateEjection() {}
50 
51     bool operator==(const SuccessRateEjection& other) const {
52       return stdev_factor == other.stdev_factor &&
53              enforcement_percentage == other.enforcement_percentage &&
54              minimum_hosts == other.minimum_hosts &&
55              request_volume == other.request_volume;
56     }
57 
58     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
59     void JsonPostLoad(const Json&, const JsonArgs&, ValidationErrors* errors);
60   };
61   struct FailurePercentageEjection {
62     uint32_t threshold = 85;
63     uint32_t enforcement_percentage = 100;
64     uint32_t minimum_hosts = 5;
65     uint32_t request_volume = 50;
66 
FailurePercentageEjectionOutlierDetectionConfig::FailurePercentageEjection67     FailurePercentageEjection() {}
68 
69     bool operator==(const FailurePercentageEjection& other) const {
70       return threshold == other.threshold &&
71              enforcement_percentage == other.enforcement_percentage &&
72              minimum_hosts == other.minimum_hosts &&
73              request_volume == other.request_volume;
74     }
75 
76     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
77     void JsonPostLoad(const Json&, const JsonArgs&, ValidationErrors* errors);
78   };
79   absl::optional<SuccessRateEjection> success_rate_ejection;
80   absl::optional<FailurePercentageEjection> failure_percentage_ejection;
81 
82   bool operator==(const OutlierDetectionConfig& other) const {
83     return interval == other.interval &&
84            base_ejection_time == other.base_ejection_time &&
85            max_ejection_time == other.max_ejection_time &&
86            max_ejection_percent == other.max_ejection_percent &&
87            success_rate_ejection == other.success_rate_ejection &&
88            failure_percentage_ejection == other.failure_percentage_ejection;
89   }
90 
91   static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
92   void JsonPostLoad(const Json& json, const JsonArgs&,
93                     ValidationErrors* errors);
94 };
95 
96 // TODO(roth): This is a horrible hack used to disable outlier detection
97 // when used with the pick_first policy.  Remove this as part of
98 // implementing the dualstack backend design.
99 class DisableOutlierDetectionAttribute
100     : public ServerAddress::AttributeInterface {
101  public:
102   static const char* kName;
103 
Copy()104   std::unique_ptr<AttributeInterface> Copy() const override {
105     return std::make_unique<DisableOutlierDetectionAttribute>();
106   }
107 
Cmp(const AttributeInterface *)108   int Cmp(const AttributeInterface*) const override { return true; }
109 
ToString()110   std::string ToString() const override { return "true"; }
111 };
112 
113 }  // namespace grpc_core
114 
115 #endif  // GRPC_SRC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_OUTLIER_DETECTION_OUTLIER_DETECTION_H
116