diff options
author | yourtree <56780191+yourtree@users.noreply.github.com> | 2022-08-21 22:55:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-21 17:55:45 +0300 |
commit | ca6aeadfbef8a0da8817f9f74a7bde1f5df004f7 (patch) | |
tree | cfef91d7fff74cad6898cd2418e55b9f7cbba2ba | |
parent | 31ef410e88dabb3f0d475dd3cae9edc00ba55aa7 (diff) | |
download | redis-ca6aeadfbef8a0da8817f9f74a7bde1f5df004f7.tar.gz |
Support setlocale via CONFIG operation. (#11059)
Till now Redis officially supported tuning it via environment variable see #1074.
But we had other requests to allow changing it at runtime, see #799, and #11041.
Note that `strcoll()` is used as Lua comparison function and also for comparison of
certain string objects in Redis, which leads to a problem that, in different regions,
for some characters, the result may be different. Below is an example.
```
127.0.0.1:6333> SORT test alpha
1) "<"
2) ">"
3) ","
4) "*"
127.0.0.1:6333> CONFIG GET locale-collate
1) "locale-collate"
2) ""
127.0.0.1:6333> CONFIG SET locale-collate 1
(error) ERR CONFIG SET failed (possibly related to argument 'locale')
127.0.0.1:6333> CONFIG SET locale-collate C
OK
127.0.0.1:6333> SORT test alpha
1) "*"
2) ","
3) "<"
4) ">"
```
That will cause accidental code compatibility issues for Lua scripts and some
Redis commands. This commit creates a new config parameter to control the
local environment which only affects `Collate` category. Above shows how it
affects `SORT` command, and below shows the influence on Lua scripts.
```
127.0.0.1:6333> CONFIG GET locale-collate
1) " locale-collate"
2) "C"
127.0.0.1:6333> EVAL "return ',' < '*'" 0
(nil)
127.0.0.1:6333> CONFIG SET locale-collate ""
OK
127.0.0.1:6333> EVAL "return ',' < '*'" 0
(integer) 1
```
Co-authored-by: calvincjli <calvincjli@tencent.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
-rw-r--r-- | redis.conf | 5 | ||||
-rw-r--r-- | src/config.c | 11 | ||||
-rw-r--r-- | src/server.c | 7 | ||||
-rw-r--r-- | src/server.h | 2 |
4 files changed, 24 insertions, 1 deletions
diff --git a/redis.conf b/redis.conf index 13b29c428..3bb52e76d 100644 --- a/redis.conf +++ b/redis.conf @@ -409,6 +409,11 @@ set-proc-title yes # proc-title-template "{title} {listen-addr} {server-mode}" +# Set the local environment which is used for string comparison operations, and +# also affect the performance of Lua scripts. Empty String indicates the locale +# is derived from the environment variables. +locale-collate "" + ################################ SNAPSHOTTING ################################ # Save the DB to disk. diff --git a/src/config.c b/src/config.c index 0ffe65a61..f98225cbe 100644 --- a/src/config.c +++ b/src/config.c @@ -35,6 +35,7 @@ #include <sys/stat.h> #include <glob.h> #include <string.h> +#include <locale.h> /*----------------------------------------------------------------------------- * Config file name-value maps. @@ -2400,6 +2401,15 @@ static int isValidProcTitleTemplate(char *val, const char **err) { return 1; } +static int updateLocaleCollate(const char **err) { + const char *s = setlocale(LC_COLLATE, server.locale_collate); + if (s == NULL) { + *err = "Invalid locale name"; + return 0; + } + return 1; +} + static int updateProcTitleTemplate(const char **err) { if (redisSetProcTitle(NULL) == C_ERR) { *err = "failed to set process title"; @@ -2995,6 +3005,7 @@ standardConfig static_configs[] = { createStringConfig("proc-title-template", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.proc_title_template, CONFIG_DEFAULT_PROC_TITLE_TEMPLATE, isValidProcTitleTemplate, updateProcTitleTemplate), createStringConfig("bind-source-addr", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.bind_source_addr, NULL, NULL, NULL), createStringConfig("logfile", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.logfile, "", NULL, NULL), + createStringConfig("locale-collate", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.locale_collate, "", NULL, updateLocaleCollate), /* SDS Configs */ createSDSConfig("masterauth", NULL, MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, server.masterauth, NULL, NULL, NULL), diff --git a/src/server.c b/src/server.c index 3456f06c9..b96d95b75 100644 --- a/src/server.c +++ b/src/server.c @@ -2440,6 +2440,12 @@ void initServer(void) { server.reply_buffer_resizing_enabled = 1; resetReplicationBuffer(); + /* Make sure the locale is set on startup based on the config file. */ + if (setlocale(LC_COLLATE,server.locale_collate) == NULL) { + serverLog(LL_WARNING, "Failed to configure LOCALE for invalid locale name."); + exit(1); + } + if ((server.tls_port || server.tls_replication || server.tls_cluster) && tlsConfigure(&server.tls_ctx_config) == C_ERR) { serverLog(LL_WARNING, "Failed to configure TLS. Check logs for more info."); @@ -6910,7 +6916,6 @@ int main(int argc, char **argv) { #ifdef INIT_SETPROCTITLE_REPLACEMENT spt_init(argc, argv); #endif - setlocale(LC_COLLATE,""); tzset(); /* Populates 'timezone' global. */ zmalloc_set_oom_handler(redisOutOfMemoryHandler); diff --git a/src/server.h b/src/server.h index 685768803..c0f68c73f 100644 --- a/src/server.h +++ b/src/server.h @@ -1944,6 +1944,8 @@ struct redisServer { is down, doesn't affect pubsub global. */ long reply_buffer_peak_reset_time; /* The amount of time (in milliseconds) to wait between reply buffer peak resets */ int reply_buffer_resizing_enabled; /* Is reply buffer resizing enabled (1 by default) */ + /* Local environment */ + char *locale_collate; }; #define MAX_KEYS_BUFFER 256 |