From 55b0f7c08735cc4d1473524114be73d09d8a6a16 Mon Sep 17 00:00:00 2001 From: VivekSubr Date: Fri, 24 Jan 2025 21:08:50 +0530 Subject: [PATCH] added custom commands to redis proxy --- .../network/redis_proxy/v3/redis_proxy.proto | 3 ++ .../network/common/redis/client_impl.cc | 5 +++ .../common/redis/redis_command_stats.cc | 2 ++ .../network/common/redis/supported_commands.h | 32 +++++++++++++++---- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/api/envoy/extensions/filters/network/redis_proxy/v3/redis_proxy.proto b/api/envoy/extensions/filters/network/redis_proxy/v3/redis_proxy.proto index 19ab3b45325e..538205074c69 100644 --- a/api/envoy/extensions/filters/network/redis_proxy/v3/redis_proxy.proto +++ b/api/envoy/extensions/filters/network/redis_proxy/v3/redis_proxy.proto @@ -133,6 +133,9 @@ message RedisProxy { // storm to busy redis server. This config is a protection to rate limit reconnection rate. // If not set, there will be no rate limiting on the reconnection. ConnectionRateLimit connection_rate_limit = 10; + + + repeated string custom_commands = 11; } message PrefixRoutes { diff --git a/source/extensions/filters/network/common/redis/client_impl.cc b/source/extensions/filters/network/common/redis/client_impl.cc index 53cde2c8ac67..0aee3e48b440 100644 --- a/source/extensions/filters/network/common/redis/client_impl.cc +++ b/source/extensions/filters/network/common/redis/client_impl.cc @@ -1,6 +1,7 @@ #include "source/extensions/filters/network/common/redis/client_impl.h" #include "envoy/extensions/filters/network/redis_proxy/v3/redis_proxy.pb.h" +#include "source/extensions/filters/network/common/redis/supported_commands.h" namespace Envoy { namespace Extensions { @@ -57,6 +58,10 @@ ConfigImpl::ConfigImpl( connection_rate_limit_enabled_ = false; connection_rate_limit_per_sec_ = 100; } + + for (int i=0; i < config.custom_commands_size(); i++) { + Extensions::NetworkFilters::Common::Redis::SupportedCommands::addCustomCommand(config.custom_commands(i)); + } } ClientPtr ClientImpl::create(Upstream::HostConstSharedPtr host, Event::Dispatcher& dispatcher, diff --git a/source/extensions/filters/network/common/redis/redis_command_stats.cc b/source/extensions/filters/network/common/redis/redis_command_stats.cc index 9e67ab1e2ce5..0ab214b2a6cb 100644 --- a/source/extensions/filters/network/common/redis/redis_command_stats.cc +++ b/source/extensions/filters/network/common/redis/redis_command_stats.cc @@ -10,6 +10,8 @@ namespace NetworkFilters { namespace Common { namespace Redis { +absl::flat_hash_set SupportedCommands::simpleCmdHashSet; + RedisCommandStats::RedisCommandStats(Stats::SymbolTable& symbol_table, const std::string& prefix) : symbol_table_(symbol_table), stat_name_set_(symbol_table_.makeSet("Redis")), prefix_(stat_name_set_->add(prefix)), diff --git a/source/extensions/filters/network/common/redis/supported_commands.h b/source/extensions/filters/network/common/redis/supported_commands.h index 52fd1e53af3e..da8e4b3be3b5 100644 --- a/source/extensions/filters/network/common/redis/supported_commands.h +++ b/source/extensions/filters/network/common/redis/supported_commands.h @@ -15,12 +15,11 @@ namespace Common { namespace Redis { struct SupportedCommands { - /** - * @return commands which hash to a single server - */ - static const absl::flat_hash_set& simpleCommands() { - CONSTRUCT_ON_FIRST_USE( - absl::flat_hash_set, "append", "bf.add", "bf.card", "bf.exists", "bf.info", + + SupportedCommands() + { + simpleCmdHashSet = { //A list of redis commands + "append", "bf.add", "bf.card", "bf.exists", "bf.info", "bf.insert", "bf.loadchunk", "bf.madd", "bf.mexists", "bf.reserve", "bf.scandump", "bitcount", "bitfield", "bitpos", "decr", "decrby", "dump", "expire", "expireat", "geoadd", "geodist", "geohash", "geopos", "georadius_ro", "georadiusbymember_ro", "get", "getbit", @@ -34,7 +33,15 @@ struct SupportedCommands { "xautoclaim", "xclaim", "xdel", "xlen", "xpending", "xrange", "xrevrange", "xtrim", "zadd", "zcard", "zcount", "zincrby", "zlexcount", "zpopmin", "zpopmax", "zrange", "zrangebylex", "zrangebyscore", "zrank", "zrem", "zremrangebylex", "zremrangebyrank", "zremrangebyscore", - "zrevrange", "zrevrangebylex", "zrevrangebyscore", "zrevrank", "zscan", "zscore"); + "zrevrange", "zrevrangebylex", "zrevrangebyscore", "zrevrank", "zscan", "zscore" + }; + } + + /** + * @return commands which hash to a single server + */ + static const absl::flat_hash_set& simpleCommands() { + return simpleCmdHashSet; } /** @@ -112,6 +119,14 @@ struct SupportedCommands { */ static const std::string& select() { CONSTRUCT_ON_FIRST_USE(std::string, "select"); } + /** + * adds custom commands to list of redis commands + */ + static void addCustomCommand(const std::string& cmd) + { + simpleCmdHashSet.insert(cmd); + } + /** * @return commands which alters the state of redis */ @@ -138,6 +153,9 @@ struct SupportedCommands { mget() == command || mset() == command || keys() == command || ping() == command || time() == command || quit() == command || select() == command); } + +private: + static absl::flat_hash_set simpleCmdHashSet; }; } // namespace Redis