Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ext_proc: support RouteCacheAction in the filter config #33830

Merged
merged 5 commits into from May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -250,7 +250,6 @@ message ExternalProcessor {
bool disable_clear_route_cache = 11
[(udpa.annotations.field_migrate).oneof_promotion = "clear_route_cache_type"];

// [#not-implemented-hide:]
// Specifies the action to be taken when an external processor response is
// received in response to request headers. It is recommended to set this field than set
// :ref:`disable_clear_route_cache <envoy_v3_api_field_extensions.filters.http.ext_proc.v3.ExternalProcessor.disable_clear_route_cache>`.
Expand Down
5 changes: 5 additions & 0 deletions changelogs/current.yaml
Expand Up @@ -7,6 +7,11 @@ behavior_changes:
Changes the behavior of the ``SlotImpl`` class destructor. With this change the destructor can be called on any thread.
This behavior can be reverted by setting the runtime flag ``envoy.reloadable_features.allow_slot_destroy_on_worker_threads``
to false.
- area: ext_proc
change: |
Adding support for
:ref:`route_cache_action <envoy_v3_api_field_extensions.filters.http.ext_proc.v3.ExternalProcessor.route_cache_action>`.
It specifies the route action to be taken when an external processor response is received in response to request headers.

minor_behavior_changes:
# *Changes that may cause incompatibilities for some users, but should not for most*
Expand Down
19 changes: 17 additions & 2 deletions source/extensions/filters/http/ext_proc/ext_proc.h
Expand Up @@ -155,7 +155,8 @@ class FilterConfig {
Server::Configuration::CommonFactoryContext& context)
: failure_mode_allow_(config.failure_mode_allow()),
disable_clear_route_cache_(config.disable_clear_route_cache()),
message_timeout_(message_timeout), max_message_timeout_ms_(max_message_timeout_ms),
route_cache_action_(config.route_cache_action()), message_timeout_(message_timeout),
max_message_timeout_ms_(max_message_timeout_ms),
stats_(generateStats(stats_prefix, config.stat_prefix(), scope)),
processing_mode_(config.processing_mode()),
mutation_checker_(config.mutation_rules(), context.regexEngine()),
Expand All @@ -177,7 +178,14 @@ class FilterConfig {
config.metadata_options().receiving_namespaces().untyped().end()),
expression_manager_(builder, context.localInfo(), config.request_attributes(),
config.response_attributes()),
immediate_mutation_checker_(context.regexEngine()) {}
immediate_mutation_checker_(context.regexEngine()) {
if (disable_clear_route_cache_ &&
(route_cache_action_ !=
envoy::extensions::filters::http::ext_proc::v3::ExternalProcessor::DEFAULT)) {
ExceptionUtil::throwEnvoyException("disable_clear_route_cache and route_cache_action can not "
"be set to none-default at the same time.");
}
}

bool failureModeAllow() const { return failure_mode_allow_; }

Expand All @@ -200,6 +208,11 @@ class FilterConfig {

bool disableClearRouteCache() const { return disable_clear_route_cache_; }

envoy::extensions::filters::http::ext_proc::v3::ExternalProcessor::RouteCacheAction
routeCacheAction() const {
return route_cache_action_;
}

const std::vector<Matchers::StringMatcherPtr>& allowedHeaders() const { return allowed_headers_; }
const std::vector<Matchers::StringMatcherPtr>& disallowedHeaders() const {
return disallowed_headers_;
Expand Down Expand Up @@ -235,6 +248,8 @@ class FilterConfig {
}
const bool failure_mode_allow_;
const bool disable_clear_route_cache_;
const envoy::extensions::filters::http::ext_proc::v3::ExternalProcessor::RouteCacheAction
route_cache_action_;
const std::chrono::milliseconds message_timeout_;
const uint32_t max_message_timeout_ms_;

Expand Down
34 changes: 28 additions & 6 deletions source/extensions/filters/http/ext_proc/processor_state.cc
Expand Up @@ -420,20 +420,42 @@ void DecodingProcessorState::clearWatermark() {
}

void DecodingProcessorState::clearRouteCache(const CommonResponse& common_response) {
if (!common_response.clear_route_cache()) {
return;
}
if (filter_.config().isUpstream()) {
filter_.stats().clear_route_cache_upstream_ignored_.inc();
ENVOY_LOG(debug, "NOT clearing route cache. The filter is in upstream filter chain.");
return;
}
// Only clear the route cache if there is a mutation to the header and clearing is allowed.
if (filter_.config().disableClearRouteCache()) {

yanjunxiang-google marked this conversation as resolved.
Show resolved Hide resolved
// If RouteCacheAction is configured with CLEAR.
if (filter_.config().routeCacheAction() ==
envoy::extensions::filters::http::ext_proc::v3::ExternalProcessor::CLEAR) {
if (common_response.has_header_mutation()) {
ENVOY_LOG(debug,
"Clearing route cache due to the filter is config with: RouteCacheconfig CLEAR");
decoder_callbacks_->downstreamCallbacks()->clearRouteCache();
return;
} else {
filter_.stats().clear_route_cache_ignored_.inc();
ENVOY_LOG(debug, "Filter RouteCacheAction is CLEAR. NO clearing route cache as no header "
"mutations detected");
return;
}
}

if (!common_response.clear_route_cache()) {
return;
}

if (filter_.config().routeCacheAction() ==
yanjunxiang-google marked this conversation as resolved.
Show resolved Hide resolved
envoy::extensions::filters::http::ext_proc::v3::ExternalProcessor::RETAIN ||
filter_.config().disableClearRouteCache()) {
filter_.stats().clear_route_cache_disabled_.inc();
ENVOY_LOG(debug, "NOT clearing route cache, it is disabled in the config");
ENVOY_LOG(
debug,
"NOT clearing route cache, it is disabled by the filter disable_clear_route_cache config");
return;
}

if (common_response.has_header_mutation()) {
ENVOY_LOG(debug, "clearing route cache");
decoder_callbacks_->downstreamCallbacks()->clearRouteCache();
Expand Down
109 changes: 109 additions & 0 deletions test/extensions/filters/http/ext_proc/filter_test.cc
Expand Up @@ -2809,6 +2809,115 @@ TEST_F(HttpFilterTest, ClearRouteCacheUnchanged) {
EXPECT_EQ(config_->stats().streams_closed_.value(), 1);
}

// Verify that the "disable_route_cache_clearing" and "route_cache_action" setting
// can not be set at the same time.
TEST_F(HttpFilterTest, ClearRouteCacheDisableRouteCacheActionBothSet) {
std::string yaml = R"EOF(
grpc_service:
envoy_grpc:
cluster_name: "ext_proc_server"
processing_mode:
response_body_mode: "BUFFERED"
disable_clear_route_cache: true
route_cache_action: CLEAR
)EOF";

envoy::extensions::filters::http::ext_proc::v3::ExternalProcessor proto_config{};
TestUtility::loadFromYaml(yaml, proto_config);
EXPECT_THROW_WITH_MESSAGE(
{
auto config = std::make_shared<FilterConfig>(
proto_config, 200ms, 10000, *stats_store_.rootScope(), "", false,
std::make_shared<Envoy::Extensions::Filters::Common::Expr::BuilderInstance>(
Envoy::Extensions::Filters::Common::Expr::createBuilder(nullptr)),
factory_context_);
},
EnvoyException,
"disable_clear_route_cache and route_cache_action can not be set to none-default at the same "
"time.");
}

// Verify that with header mutation in response, setting route_cache_action to CLEAR
// will clear route cache even the response does not set clear_route_cache.
TEST_F(HttpFilterTest, FilterRouteCacheActionSetToClearHeaderMutation) {
initialize(R"EOF(
grpc_service:
envoy_grpc:
cluster_name: "ext_proc_server"
route_cache_action: CLEAR
)EOF");

EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->decodeHeaders(request_headers_, true));
EXPECT_CALL(decoder_callbacks_.downstream_callbacks_, clearRouteCache());
processRequestHeaders(false, [](const HttpHeaders&, ProcessingResponse&, HeadersResponse& resp) {
auto* resp_headers_mut = resp.mutable_response()->mutable_header_mutation();
auto* resp_add = resp_headers_mut->add_set_headers();
resp_add->mutable_header()->set_key("x-new-header");
resp_add->mutable_header()->set_value("new");
});

EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->encodeHeaders(response_headers_, false));
processResponseHeaders(true, absl::nullopt);

filter_->onDestroy();

EXPECT_EQ(config_->stats().clear_route_cache_disabled_.value(), 0);
EXPECT_EQ(config_->stats().clear_route_cache_ignored_.value(), 0);
EXPECT_EQ(config_->stats().streams_started_.value(), 1);
EXPECT_EQ(config_->stats().stream_msgs_sent_.value(), 2);
EXPECT_EQ(config_->stats().stream_msgs_received_.value(), 2);
EXPECT_EQ(config_->stats().streams_closed_.value(), 1);
}

// Verify that without header mutation in response, setting route_cache_action to CLEAR
// will not clear route cache.
TEST_F(HttpFilterTest, FilterRouteCacheActionSetToClearNoHeaderMutation) {
initialize(R"EOF(
grpc_service:
envoy_grpc:
cluster_name: "ext_proc_server"
route_cache_action: CLEAR
)EOF");

EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->decodeHeaders(request_headers_, true));
processRequestHeaders(false, absl::nullopt);

EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->encodeHeaders(response_headers_, false));
processResponseHeaders(true, absl::nullopt);

filter_->onDestroy();

EXPECT_EQ(config_->stats().clear_route_cache_disabled_.value(), 0);
EXPECT_EQ(config_->stats().clear_route_cache_ignored_.value(), 1);
}

// Verify that setting route_cache_action to RETAIN will not clear route cache.
TEST_F(HttpFilterTest, FilterRouteCacheActionSetToRetainWithHeaderMutation) {
initialize(R"EOF(
grpc_service:
envoy_grpc:
cluster_name: "ext_proc_server"
route_cache_action: RETAIN
)EOF");

EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->decodeHeaders(request_headers_, true));
processRequestHeaders(false, [](const HttpHeaders&, ProcessingResponse&, HeadersResponse& resp) {
auto* resp_headers_mut = resp.mutable_response()->mutable_header_mutation();
auto* resp_add = resp_headers_mut->add_set_headers();
resp_add->mutable_header()->set_key("x-new-header");
resp_add->mutable_header()->set_value("new");
resp.mutable_response()->set_clear_route_cache(true);
});

EXPECT_EQ(FilterHeadersStatus::StopIteration, filter_->encodeHeaders(response_headers_, false));
processResponseHeaders(true, absl::nullopt);

filter_->onDestroy();

EXPECT_EQ(config_->stats().clear_route_cache_disabled_.value(), 1);
EXPECT_EQ(config_->stats().clear_route_cache_ignored_.value(), 0);
}

// Using the default configuration, turn a GET into a POST.
TEST_F(HttpFilterTest, ReplaceRequest) {
initialize(R"EOF(
Expand Down