Skip to content

Support setlocale via CONFIG operation. - #11059

Merged
oranagra merged 6 commits into
redis:unstablefrom
yourtree:support-config-setlocale
Aug 21, 2022
Merged

oranagra merged 6 commits into
redis:unstablefrom
yourtree:support-config-setlocale

Conversation

@yourtree

@yourtree yourtree commented Jul 29, 2022 •

Copy link
Copy Markdown
Contributor

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

Comment thread src/config.c Resolved
Comment thread src/config.c Resolved
Comment thread src/config.c Resolved
Comment thread redis.conf Resolved
@oranagra oranagra added state:major-decision Requires core team consensus interface-change contains new interface, or change to inputs or outputs of an API labels Aug 3, 2022
Comment thread src/config.c Resolved

@oranagra oranagra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redis/core-team please approve a new config.

@oranagra oranagra added release-notes indication that this issue needs to be mentioned in the release notes approval-needed Waiting for core team approval to be merged labels Aug 8, 2022
@soloestoy

Copy link
Copy Markdown
Contributor

maybe a MULTI_ARG_CONFIG is better? like CONFIG SET locale collate "", in case we support other category in future.

@soloestoy

Copy link
Copy Markdown
Contributor

we discussed it in core team meeting, and decide to make it more forward compatible, that is change the locale to a MULTI_ARG_CONFIG just like CONFIG SET locale collate "C".

@yourtree do you like to implement it? you can refer to client-output-buffer-limit.

@yourtree

Copy link
Copy Markdown
Contributor Author

we discussed it in core team meeting, and decide to make it more forward compatible, that is change the locale to a MULTI_ARG_CONFIG just like CONFIG SET locale collate "C".

@yourtree do you like to implement it? you can refer to client-output-buffer-limit.

Sure, I'll do that.

@yourtree

Copy link
Copy Markdown
Contributor Author

What do you think of the categories that we need to support? I mean, some of them are GNU extensions which are available since glibc 2.2, according to https://man7.org/linux/man-pages/man7/locale.7.html. Shall we support only what the C standards specify or just all of them? @soloestoy

@soloestoy

Copy link
Copy Markdown
Contributor

@yourtree I mean you can change the config's format as CONFIG SET <parameter> <cat1> <val1> <cat2> <val2> ... instead CONFIG SET <parameter> <value>, and we only support LC_COLLATE now, but it's easy to expand in the future without change config format, you know change a config's format would break compatibility.

@yourtree

Copy link
Copy Markdown
Contributor Author

I got the format part. What I wanna do is to check the validity of category parameters before calling setlocale(). It is legal to do like this, CONFIG SET locale COLLATE "C" CTYPE "fr_FR" , but what about this ,CONFIG SET locale PAPER "C" MEASUREMENT "fr_FR" where LC_PAPER, LC_MEASUREMENT and some other categories are in fact GNU extensions. @soloestoy

@soloestoy

Copy link
Copy Markdown
Contributor

we don't need support all categories now, we only allow CONFIG SET locale collate <value>, if users try to change others like CONFIG SET locale ctype <value>, just return an error. we can add other cats when we really need them.

@yourtree

Copy link
Copy Markdown
Contributor Author

we don't need support all categories now, we only allow CONFIG SET locale collate <value>, if users try to change others like CONFIG SET locale ctype <value>, just return an error. we can add other cats when we really need them.

Got it.

@oranagra

Copy link
Copy Markdown
Member

sorry to jump in late (i was busy elsewhere).
i think we generally want to avoid adding more MULTI_ARG_CONFIGs and we have a general understanding that the interface of client-output-buffer-limit is horrible.
@madolson IIRC you agree with that.

maybe in this case, since it's a plain list of key+value (unlike the soft,hard,time triplet in client buffers), its saner though.
on the other hand, i'm not sure making it forward compatible is a good idea either.
what do we do if the user uses a syntax of a newer version on an older one, and only some of the keys are supported? reject the whole thing? maybe it's better to let them be separate configs and just plan ahead with naming so that we can add them later and it's clear they're part of the same group?

@madolson

Copy link
Copy Markdown
Contributor

i think we generally want to avoid adding more MULTI_ARG_CONFIGs and we have a general understanding that the interface of client-output-buffer-limit is horrible.

If you have any type of infrastructure ontop of configs, adding custom ones is very annoying since almost all of them are key/values except this one. If a config really is a coherent config with multiple parts, that seems okay. I don't really think this is though. I would prefer we just have a bunch of string configs for each locale value that can be updated, similar to what Oran suggested.

@yourtree

Copy link
Copy Markdown
Contributor Author

I've actually implemented it to support multi-arguments and I'm doing some test work. It runs like this

127.0.0.1:6666> CONFIG GET locale
1) "locale"
2) "collate ''"
127.0.0.1:6666> CONFIG SET locale "collate fr_FR ctype ''"
(error) ERR CONFIG SET failed (possibly related to argument 'locale') - Invalid or duplicate category.
127.0.0.1:6666> CONFIG SET locale "collate fr_FR"
OK
127.0.0.1:6666> CONFIG GET locale
1) "locale"
2) "collate fr_FR"

Above is a reference and I agree with @oranagra though. Adding multiple string configs seems better than adding a MULTI_ARG_CONFIG. It's better to do the same thing with the same config even if the version is different. It's up to the team whether to accept it or not.

@oranagra

Copy link
Copy Markdown
Member

ok, so that means we just need to rename the configs in this PR from locale to locale-collate, and then i think we're good to go.

@soloestoy

Copy link
Copy Markdown
Contributor

I'm ok with locale-collate :)

@yourtree

Copy link
Copy Markdown
Contributor Author

Updated.

Comment thread src/server.h Outdated
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*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Local environment*/
/* Local environment */

Comment thread src/config.c Outdated
return 1;
}


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment thread src/server.c Outdated

/* 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. %s", strerror(errno));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serverLog(LL_WARNING, "Failed to configure LOCALE. %s", strerror(errno));
serverLog(LL_WARNING, "Failed to configure LOCALE: %s", strerror(errno));

@yourtree yourtree Aug 18, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I found that the message errno carries was not so proper to show here. Maybe we should replace it.
image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually, on system errors, i'll argue that it's ok.. i.e. the people looking at this will be quick to realize that's ENOENT, and that they should look setlocale man page (it's far better to have that info, than not have it).
btw, i don't see any reference to errno in the man page at all!?

however, in this case if it's a plain user typo, or attempting to use a locale that's not supported, maybe we should print something more friendly.
i suppose it should be the same message we return in CONFIG SET command for an unsupported input (in which we didn't print the errno).

so maybe we should handle ENOENT separately from other errors?
can you check if this API is at all using errno?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've browsed the source code of setlocale.c. There's indeed usage of errno but it seems that only EINVAL is used in this and other related APIs ( e.g. _nl_find_locale(), not quite sure about some implicit references, see https://code.woboq.org/userspace/glibc/locale/setlocale.c.html#setlocale), and I'm uncertain when the value of errno was assigned to ENOENT. I agree to print something more intuitive, maybe like serverLog(LL_WARNING, "Failed to configure LOCALE for invalid locale name.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see it was discussed here: https://stackoverflow.com/questions/33668695/setlocale-raising-errno-while-a-non-null-value-is-returned and the conclusion is that setlocale doesn't (or shouldn't) mess with errno.
let's drop using it and replace it with the string you suggested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll replace then.

@oranagra
oranagra merged commit ca6aead into redis:unstable Aug 21, 2022
@yymm120

yymm120 commented Sep 4, 2022 •

Copy link
Copy Markdown

I get Redis to start properly only I comment out 'locale-collate'.

× redis_6379.service - LSB: start and stop redis_6379
     Loaded: loaded (/etc/init.d/redis_6379; generated)
     Active: failed (Result: exit-code) since Sun 2022-09-04 20:28:09 CST; 3s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 105758 ExecStart=/etc/init.d/redis_6379 start (code=exited, status=1/FAILURE)
        CPU: 8ms

9月 04 20:28:09 xinhao-ubuntu systemd[1]: Starting LSB: start and stop redis_6379...
9月 04 20:28:09 xinhao-ubuntu redis_6379[105758]: Starting Redis server...
9月 04 20:28:09 xinhao-ubuntu redis_6379[105759]: *** FATAL CONFIG FILE ERROR (Redis 7.0.4) ***
9月 04 20:28:09 xinhao-ubuntu redis_6379[105759]: Reading the configuration file, at line 416
9月 04 20:28:09 xinhao-ubuntu redis_6379[105759]: >>> 'locale-collate "LC_ALL"'
9月 04 20:28:09 xinhao-ubuntu redis_6379[105759]: Bad directive or wrong number of arguments
9月 04 20:28:09 xinhao-ubuntu systemd[1]: redis_6379.service: Control process exited, code=exited, status=1/FAILURE
9月 04 20:28:09 xinhao-ubuntu systemd[1]: redis_6379.service: Failed with result 'exit-code'.
9月 04 20:28:09 xinhao-ubuntu systemd[1]: Failed to start LSB: start and stop redis_6379.```



```# locale-collate ""```

@oranagra

oranagra commented Sep 4, 2022 •

Copy link
Copy Markdown
Member

@yymm120 this feature is gonna be part of a future release (probably redis 7.2), wasn't part of 7.0.4.

Comment thread src/server.c
Comment on lines +2444 to +2448
/* 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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i found a odd issue:

if i set ulimit -Sv 100000, then it is unable to start the server (missing some logs).
with the default locale-collate value ""

[root@binblog redis]# src/redis-server redis.conf --locale-collate ""
16286:C 21 Jun 2023 11:37:07.391 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16286:C 21 Jun 2023 11:37:07.391 * Redis version=255.255.255, bits=64, commit=07c14672, modified=1, pid=16286, just started
16286:C 21 Jun 2023 11:37:07.392 * Configuration loaded
16286:M 21 Jun 2023 11:37:07.392 # Failed to configure LOCALE for invalid locale name.

and it is ok the set a another value:

[root@binblog redis]# src/redis-server redis.conf --locale-collate "C"

# is ok, in this case, using config set
127.0.0.1:6379> config get locale-collate
1) "locale-collate"
2) "C"
127.0.0.1:6379> config set locale-collate ""
(error) ERR CONFIG SET failed (possibly related to argument 'locale-collate') - Invalid locale name

Is there a better way to print errors here (i support it is somehow an OOM error),
or rather not let the server exit

enjoy-binbin pushed a commit to enjoy-binbin/redis that referenced this pull request Jul 31, 2023
Till now Redis officially supported tuning it via environment variable see redis#1074.
But we had other requests to allow changing it at runtime, see redis#799, and redis#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 <[email protected]>
Co-authored-by: Oran Agra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval-needed Waiting for core team approval to be merged interface-change contains new interface, or change to inputs or outputs of an API release-notes indication that this issue needs to be mentioned in the release notes state:major-decision Requires core team consensus

Projects

Status: Done
Archived in project

Development

Successfully merging this pull request may close these issues.

8 participants