GRPC C++  1.39.1
config_selector.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_CONFIG_SELECTOR_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_CONFIG_SELECTOR_H
19 
21 
22 #include <functional>
23 #include <map>
24 #include <vector>
25 
26 #include "absl/strings/string_view.h"
27 
28 #include <grpc/grpc.h>
29 
37 
38 // Channel arg key for ConfigSelector.
39 #define GRPC_ARG_CONFIG_SELECTOR "grpc.internal.config_selector"
40 
41 namespace grpc_core {
42 
43 // Internal API used to allow resolver implementations to override
44 // MethodConfig and provide input to LB policies on a per-call basis.
45 class ConfigSelector : public RefCounted<ConfigSelector> {
46  public:
51  };
52 
53  struct CallConfig {
54  // Can be set to indicate the call should be failed.
56  // The per-method parsed configs that will be passed to
57  // ServiceConfigCallData.
59  // A ref to the service config that contains method_configs, held by
60  // the call to ensure that method_configs lives long enough.
62  // Call attributes that will be accessible to LB policy implementations.
63  std::map<const char*, absl::string_view> call_attributes;
64  // A callback that, if set, will be invoked when the call is
65  // committed (i.e., when we know that we will never again need to
66  // ask the picker for a subchannel for this call).
67  std::function<void()> on_call_committed;
68  };
69 
70  ~ConfigSelector() override = default;
71 
72  virtual const char* name() const = 0;
73 
74  // Will be called only if the two objects have the same name, so
75  // subclasses can be free to safely down-cast the argument.
76  virtual bool Equals(const ConfigSelector* other) const = 0;
77 
78  static bool Equals(const ConfigSelector* cs1, const ConfigSelector* cs2) {
79  if (cs1 == nullptr) return cs2 == nullptr;
80  if (cs2 == nullptr) return false;
81  if (strcmp(cs1->name(), cs2->name()) != 0) return false;
82  return cs1->Equals(cs2);
83  }
84 
85  // The channel will call this when the resolver returns a new ConfigSelector
86  // to determine what set of dynamic filters will be configured.
87  virtual std::vector<const grpc_channel_filter*> GetFilters() { return {}; }
88 
89  // Modifies channel args to be passed to the dynamic filter stack.
90  // Takes ownership of argument. Caller takes ownership of result.
92  return args;
93  }
94 
96 
97  grpc_arg MakeChannelArg() const;
99  const grpc_channel_args& args);
100 };
101 
102 // Default ConfigSelector that gets the MethodConfig from the service config.
104  public:
106  : service_config_(std::move(service_config)) {
107  // The client channel code ensures that this will never be null.
108  // If neither the resolver nor the client application provide a
109  // config, a default empty config will be used.
110  GPR_DEBUG_ASSERT(service_config_ != nullptr);
111  }
112 
113  const char* name() const override { return "default"; }
114 
115  // Only comparing the ConfigSelector itself, not the underlying
116  // service config, so we always return true.
117  bool Equals(const ConfigSelector* /*other*/) const override { return true; }
118 
120  CallConfig call_config;
121  call_config.method_configs =
122  service_config_->GetMethodParsedConfigVector(*args.path);
123  call_config.service_config = service_config_;
124  return call_config;
125  }
126 
127  private:
128  RefCountedPtr<ServiceConfig> service_config_;
129 };
130 
131 } // namespace grpc_core
132 
133 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_CONFIG_SELECTOR_H */
Definition: arena.h:44
Definition: config_selector.h:45
virtual const char * name() const =0
virtual grpc_channel_args * ModifyChannelArgs(grpc_channel_args *args)
Definition: config_selector.h:91
static RefCountedPtr< ConfigSelector > GetFromChannelArgs(const grpc_channel_args &args)
Definition: config_selector.cc:50
~ConfigSelector() override=default
virtual bool Equals(const ConfigSelector *other) const =0
grpc_arg MakeChannelArg() const
Definition: config_selector.cc:44
virtual std::vector< const grpc_channel_filter * > GetFilters()
Definition: config_selector.h:87
static bool Equals(const ConfigSelector *cs1, const ConfigSelector *cs2)
Definition: config_selector.h:78
virtual CallConfig GetCallConfig(GetCallConfigArgs args)=0
Definition: config_selector.h:103
const char * name() const override
Definition: config_selector.h:113
bool Equals(const ConfigSelector *) const override
Definition: config_selector.h:117
DefaultConfigSelector(RefCountedPtr< ServiceConfig > service_config)
Definition: config_selector.h:105
CallConfig GetCallConfig(GetCallConfigArgs args) override
Definition: config_selector.h:119
Definition: ref_counted.h:282
Definition: ref_counted_ptr.h:35
absl::InlinedVector< std::unique_ptr< ParsedConfig >, kNumPreallocatedParsers > ParsedConfigVector
Definition: service_config_parser.h:71
#define GRPC_ERROR_NONE
The following "special" errors can be propagated without allocating memory.
Definition: error.h:228
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:101
Round Robin Policy.
Definition: backend_metric.cc:26
Definition: async_unary_call.h:398
A single argument...
Definition: grpc_types.h:103
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: config_selector.h:53
const ServiceConfigParser::ParsedConfigVector * method_configs
Definition: config_selector.h:58
std::map< const char *, absl::string_view > call_attributes
Definition: config_selector.h:63
std::function< void()> on_call_committed
Definition: config_selector.h:67
RefCountedPtr< ServiceConfig > service_config
Definition: config_selector.h:61
grpc_error_handle error
Definition: config_selector.h:55
Definition: config_selector.h:47
grpc_metadata_batch * initial_metadata
Definition: config_selector.h:49
Arena * arena
Definition: config_selector.h:50
grpc_slice * path
Definition: config_selector.h:48
Definition: error_internal.h:41
Definition: metadata_batch.h:52
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60