Skip to content

Commit f43ec18

Browse files
authored
dynamic_modules: add is_validation_mode ABI callback for module optimizaion (#44417)
## Description This PR adds a new `envoy_dynamic_module_callback_is_validation_mode()` to the dynamic modules common ABI so that any module like Bootstrap, HTTP filter, network filter, etc. could detect when Envoy is running in the `--mode validate`. This lets modules optimize by only parsing/validating their config without performing expensive operations like provider lookups or loading external resources when we are in the non-production mode. --- **Commit Message**: dynamic_modules: add is_validation_mode ABI callback for module optimizaion **Additional Description:** Added a new `envoy_dynamic_module_callback_is_validation_mode()` to the dynamic modules common ABI. **Risk Level**: Low **Testing**: Added Tests **Docs Changes**: Added **Release Notes:** Added Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
1 parent 5a363b3 commit f43ec18

6 files changed

Lines changed: 72 additions & 0 deletions

File tree

changelogs/current.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ removed_config_or_runtime:
350350
and legacy code path.
351351
352352
new_features:
353+
- area: dynamic_modules
354+
change: |
355+
Added ``envoy_dynamic_module_callback_is_validation_mode`` ABI callback that allows dynamic
356+
modules to check if the server is running in config validation mode.
353357
- area: ratelimit
354358
change: |
355359
Added ``is_negative_hits`` boolean to the ``hits_addend``

source/extensions/dynamic_modules/abi/abi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,19 @@ bool envoy_dynamic_module_callback_log_enabled(envoy_dynamic_module_type_log_lev
495495
*/
496496
uint32_t envoy_dynamic_module_callback_get_concurrency();
497497

498+
// ----------------------------- Server Mode -----------------------------------
499+
500+
/**
501+
* envoy_dynamic_module_callback_is_validation_mode may be called by the dynamic
502+
* module to check if the server is running in config validation mode (--mode validate).
503+
* This allows modules to optimize by only parsing and validating their config without
504+
* performing expensive operations such as provider lookups or loading external resources.
505+
* NOTE: This function must be called on the main thread.
506+
*
507+
* @return true if the server is in validation mode, false otherwise.
508+
*/
509+
bool envoy_dynamic_module_callback_is_validation_mode();
510+
498511
// ----------------------------- Function Registry -----------------------------
499512

500513
/**

source/extensions/dynamic_modules/abi_impl.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ uint32_t envoy_dynamic_module_callback_get_concurrency() {
7474
return context->options().concurrency();
7575
}
7676

77+
bool envoy_dynamic_module_callback_is_validation_mode() {
78+
using namespace Envoy;
79+
ASSERT_IS_MAIN_OR_TEST_THREAD();
80+
auto context = Server::Configuration::ServerFactoryContextInstance::getExisting();
81+
return context->options().mode() == Server::Mode::Validate;
82+
}
83+
7784
// ---------------------- Function registry callbacks --------------------------------
7885

7986
bool envoy_dynamic_module_callback_register_function(envoy_dynamic_module_type_module_buffer key,

source/extensions/dynamic_modules/sdk/rust/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ pub unsafe fn get_server_concurrency() -> u32 {
134134
unsafe { abi::envoy_dynamic_module_callback_get_concurrency() }
135135
}
136136

137+
/// Check if the server is running in config validation mode (`--mode validate`).
138+
///
139+
/// This allows modules to optimize by only parsing and validating their config without
140+
/// performing expensive operations such as provider lookups or loading external resources.
141+
///
142+
/// # Safety
143+
///
144+
/// This function must be called on the main thread.
145+
pub unsafe fn is_validation_mode() -> bool {
146+
unsafe { abi::envoy_dynamic_module_callback_is_validation_mode() }
147+
}
148+
137149
/// Register a function pointer under a name in the process-wide function registry.
138150
///
139151
/// This allows modules loaded in the same process to expose functions that other modules can

test/extensions/dynamic_modules/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ envoy_cc_test(
4848
srcs = ["abi_impl_test.cc"],
4949
deps = [
5050
"//source/extensions/dynamic_modules:abi_impl",
51+
"//test/mocks/server:server_factory_context_mocks",
5152
"//test/test_common:utility_lib",
5253
],
5354
)

test/extensions/dynamic_modules/abi_impl_test.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
#include "source/extensions/dynamic_modules/abi/abi.h"
22

3+
#include "test/mocks/server/server_factory_context.h"
34
#include "test/test_common/utility.h"
45

56
#include "gtest/gtest.h"
67

8+
using testing::Return;
9+
710
namespace Envoy {
811
namespace Extensions {
912
namespace DynamicModules {
1013
namespace {
1114

15+
// =============================================================================
16+
// Validation Mode Tests
17+
// =============================================================================
18+
19+
TEST(CommonAbiImplTest, IsValidationModeReturnsTrueInValidateMode) {
20+
testing::NiceMock<Server::Configuration::MockServerFactoryContext> context;
21+
ON_CALL(context.options_, mode()).WillByDefault(Return(Server::Mode::Validate));
22+
23+
ScopedThreadLocalServerContextSetter setter(context);
24+
EXPECT_TRUE(envoy_dynamic_module_callback_is_validation_mode());
25+
}
26+
27+
TEST(CommonAbiImplTest, IsValidationModeReturnsFalseInServeMode) {
28+
testing::NiceMock<Server::Configuration::MockServerFactoryContext> context;
29+
ON_CALL(context.options_, mode()).WillByDefault(Return(Server::Mode::Serve));
30+
31+
ScopedThreadLocalServerContextSetter setter(context);
32+
EXPECT_FALSE(envoy_dynamic_module_callback_is_validation_mode());
33+
}
34+
35+
TEST(CommonAbiImplTest, IsValidationModeReturnsFalseInInitOnlyMode) {
36+
testing::NiceMock<Server::Configuration::MockServerFactoryContext> context;
37+
ON_CALL(context.options_, mode()).WillByDefault(Return(Server::Mode::InitOnly));
38+
39+
ScopedThreadLocalServerContextSetter setter(context);
40+
EXPECT_FALSE(envoy_dynamic_module_callback_is_validation_mode());
41+
}
42+
43+
// =============================================================================
44+
// Function Registry Tests
45+
// =============================================================================
46+
1247
// Test registering and retrieving a function.
1348
TEST(CommonAbiImplTest, FunctionRegistryRegisterAndGet) {
1449
auto fn = [](int x) { return x + 1; };

0 commit comments

Comments
 (0)