• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2021 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_FAULT_INJECTION_FAULT_INJECTION_SERVICE_CONFIG_PARSER_H
18 #define GRPC_SRC_CORE_EXT_FILTERS_FAULT_INJECTION_FAULT_INJECTION_SERVICE_CONFIG_PARSER_H
19 
20 #include <grpc/status.h>
21 #include <grpc/support/port_platform.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include <limits>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include "absl/strings/string_view.h"
31 #include "src/core/config/core_configuration.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/service_config/service_config_parser.h"
34 #include "src/core/util/json/json.h"
35 #include "src/core/util/json/json_args.h"
36 #include "src/core/util/json/json_object_loader.h"
37 #include "src/core/util/time.h"
38 #include "src/core/util/validation_errors.h"
39 
40 // Channel arg key for enabling parsing fault injection via method config.
41 #define GRPC_ARG_PARSE_FAULT_INJECTION_METHOD_CONFIG \
42   "grpc.internal.parse_fault_injection_method_config"
43 
44 namespace grpc_core {
45 
46 class FaultInjectionMethodParsedConfig
47     : public ServiceConfigParser::ParsedConfig {
48  public:
49   struct FaultInjectionPolicy {
50     grpc_status_code abort_code = GRPC_STATUS_OK;
51     std::string abort_message = "Fault injected";
52     std::string abort_code_header;
53     std::string abort_percentage_header;
54     uint32_t abort_percentage_numerator = 0;
55     uint32_t abort_percentage_denominator = 100;
56 
57     Duration delay;
58     std::string delay_header;
59     std::string delay_percentage_header;
60     uint32_t delay_percentage_numerator = 0;
61     uint32_t delay_percentage_denominator = 100;
62 
63     // By default, the max allowed active faults are unlimited.
64     uint32_t max_faults = std::numeric_limits<uint32_t>::max();
65 
66     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
67     void JsonPostLoad(const Json& json, const JsonArgs&,
68                       ValidationErrors* errors);
69   };
70 
71   // Returns the fault injection policy at certain index.
72   // There might be multiple fault injection policies functioning at the same
73   // time. The order between the policies are stable, and an index is used to
74   // keep track of their relative positions. The FaultInjectionFilter uses this
75   // method to access the parsed fault injection policy in service config,
76   // whether it came from xDS resolver or directly from service config
fault_injection_policy(size_t index)77   const FaultInjectionPolicy* fault_injection_policy(size_t index) const {
78     if (index >= fault_injection_policies_.size()) {
79       return nullptr;
80     }
81     return &fault_injection_policies_[index];
82   }
83 
84   static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
85 
86  private:
87   std::vector<FaultInjectionPolicy> fault_injection_policies_;
88 };
89 
90 class FaultInjectionServiceConfigParser final
91     : public ServiceConfigParser::Parser {
92  public:
name()93   absl::string_view name() const override { return parser_name(); }
94   // Parses the per-method service config for fault injection filter.
95   std::unique_ptr<ServiceConfigParser::ParsedConfig> ParsePerMethodParams(
96       const ChannelArgs& args, const Json& json,
97       ValidationErrors* errors) override;
98   // Returns the parser index for FaultInjectionServiceConfigParser.
99   static size_t ParserIndex();
100   // Registers FaultInjectionServiceConfigParser to ServiceConfigParser.
101   static void Register(CoreConfiguration::Builder* builder);
102 
103  private:
parser_name()104   static absl::string_view parser_name() { return "fault_injection"; }
105 };
106 
107 }  // namespace grpc_core
108 
109 #endif  // GRPC_SRC_CORE_EXT_FILTERS_FAULT_INJECTION_FAULT_INJECTION_SERVICE_CONFIG_PARSER_H
110