• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STATEFUL_SESSION_STATEFUL_SESSION_FILTER_H
18 #define GRPC_SRC_CORE_EXT_FILTERS_STATEFUL_SESSION_STATEFUL_SESSION_FILTER_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <stddef.h>
23 
24 #include <utility>
25 
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 
29 #include "src/core/ext/filters/stateful_session/stateful_session_service_config_parser.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/channel/channel_fwd.h"
32 #include "src/core/lib/channel/promise_based_filter.h"
33 #include "src/core/lib/gprpp/ref_counted_string.h"
34 #include "src/core/lib/gprpp/unique_type_name.h"
35 #include "src/core/lib/promise/arena_promise.h"
36 #include "src/core/lib/transport/transport.h"
37 #include "src/core/service_config/service_config_call_data.h"
38 
39 namespace grpc_core {
40 
41 // A call attribute to be passed to the xds_override_host LB policy.
42 // The StatefulSession filter will populate the cookie's address list,
43 // if set.  The xds_override_host LB policy will use that info, and then
44 // set the actual address list based on the chosen endpoint.  The
45 // StatefulSession filter will then use the actual address list to
46 // update the cookie.
47 class XdsOverrideHostAttribute
48     : public ServiceConfigCallData::CallAttributeInterface {
49  public:
50   static UniqueTypeName TypeName();
51 
XdsOverrideHostAttribute(absl::string_view cookie_address_list)52   explicit XdsOverrideHostAttribute(absl::string_view cookie_address_list)
53       : cookie_address_list_(cookie_address_list) {}
54 
cookie_address_list()55   absl::string_view cookie_address_list() const { return cookie_address_list_; }
56 
actual_address_list()57   absl::string_view actual_address_list() const {
58     return actual_address_list_.as_string_view();
59   }
set_actual_address_list(RefCountedStringValue actual_address_list)60   void set_actual_address_list(RefCountedStringValue actual_address_list) {
61     actual_address_list_ = std::move(actual_address_list);
62   }
63 
64  private:
type()65   UniqueTypeName type() const override { return TypeName(); }
66 
67   absl::string_view cookie_address_list_;
68   RefCountedStringValue actual_address_list_;
69 };
70 
71 // A filter to provide cookie-based stateful session affinity.
72 class StatefulSessionFilter
73     : public ImplementChannelFilter<StatefulSessionFilter> {
74  public:
75   static const grpc_channel_filter kFilter;
76 
77   static absl::StatusOr<StatefulSessionFilter> Create(
78       const ChannelArgs& args, ChannelFilter::Args filter_args);
79 
80   class Call {
81    public:
82     void OnClientInitialMetadata(ClientMetadata& md,
83                                  StatefulSessionFilter* filter);
84     void OnServerInitialMetadata(ServerMetadata& md);
85     void OnServerTrailingMetadata(ServerMetadata& md);
86     static const NoInterceptor OnClientToServerMessage;
87     static const NoInterceptor OnServerToClientMessage;
88     static const NoInterceptor OnFinalize;
89 
90    private:
91     const StatefulSessionMethodParsedConfig::CookieConfig* cookie_config_;
92     XdsOverrideHostAttribute* override_host_attribute_;
93     absl::string_view cluster_name_;
94     absl::string_view cookie_address_list_;
95     bool cluster_changed_;
96     bool perform_filtering_ = false;
97   };
98 
99  private:
100   explicit StatefulSessionFilter(ChannelFilter::Args filter_args);
101   // The relative index of instances of the same filter.
102   const size_t index_;
103   // Index of the service config parser.
104   const size_t service_config_parser_index_;
105 };
106 
107 }  // namespace grpc_core
108 
109 #endif  // GRPC_SRC_CORE_EXT_FILTERS_STATEFUL_SESSION_STATEFUL_SESSION_FILTER_H
110