Skip to content

Allow setting locale in lua script. - #11041

Closed
yourtree wants to merge 1 commit into
redis:unstablefrom
yourtree:feature-support-setlocale
Closed

yourtree wants to merge 1 commit into
redis:unstablefrom
yourtree:feature-support-setlocale

Conversation

@yourtree

@yourtree yourtree commented Jul 26, 2022 •

Copy link
Copy Markdown
Contributor

It is noted that Lua basically uses strcoll() as comparison function, which leads to a problem that, in different regions, for some characters, the comparison result may be different. Below is an example.
16578518801507(1)
This will cause accidental code compatibility issues for Lua scripts. And here provides a solution.
This commit allows the use of setlocale() function of Lua os library. To avoid hidden systematic risk, the parameter category is set to collate and is unchangeable. The packaged function accepts only one parameter and the other functions of os library are unreachable. Allow setting locale offers an opportunity to uniform standards. Below shows how it works.

127.0.0.1:6379> EVAL "return os.setlocale('C')" 0
"C"
127.0.0.1:6379> EVAL "return os.clock()" 0
(error) ERR user_script:1: attempt to call field 'clock' (a nil value) script: ea58cfad299460ea863f576834104a675e48c28c, on @user_script:1.
127.0.0.1:6379> EVAL "return os.setlocale('C', 'numeric')" 0
(error) ERR wrong number of arguments script: 4393e830ac85509505f24894333f199e9a5e3eb6, on @user_script:1.
127.0.0.1:6379> EVAL "return os.setlocale('C')" 0
"C"
127.0.0.1:6379> EVAL "return '*' < ','" 0
(integer) 1
127.0.0.1:6379> EVAL "return '*' > ','" 0
(nil)
127.0.0.1:6379> EVAL "return os.setlocale('fr_FR')" 0
"fr_FR"
127.0.0.1:6379> EVAL "return '*' > ','" 0
(integer) 1
127.0.0.1:6379> EVAL "return '*' < ','" 0
(nil)

Comment thread src/script_lua.c
"math",
"table",
"struct",
"os",

@sundb sundb Jul 26, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

import os library would be dangerous, e.x os.execute, os.rename
Pardon my misunderstanding (on small screen phone), but maybe we shouldn't call it os.

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.

Yup, it could be replaced with "ros" or something else. The initial intention to name it 'os' is to conform to the concept of daily use.

Comment thread src/roslib.c
return lua_error(L);
}
const char *l = luaL_optstring(L, 1, NULL);
lua_pushstring(L, setlocale(LC_COLLATE, l));

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 don't think we wanna change the system (global) locale in a script, it'll have implications outside that script.
maybe instead we should expose a CONFIG directive to control it?
i.e. currently redis relies on environment variable for that.
see #1074 and #799

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.

CONFIG is indeed better cuz the environment is shared. I'll make changes in later commits.

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 think controlling locale through environment is more standard, having a configuration parameter that overrides that might be very counter intuitive in some cases. If a Lua script can change the locale programmatically, and that change only affects that script - I think that's much better.

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'm not sure it's at all possible to have it only affect Lua.
indeed it's standard to control the locale via env var, but that's actually not very convenient.
i think that (despite being non-standard), it's much more useful to set such things via config file and config command).
same as we handle other global server configs like rdbcompression and ziplist threshold.
although granted, these do not affect behavior, just optimizations.

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.

Since it seems necessary to uniform the output format of SORT command globally (eg. the same business deployed in different regions), it may be better to control it from the upper level, i.e. CONFIG command.

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.

The title of this pr is easily misunderstood and I've created a new pr #11059

@oranagra

oranagra commented Aug 3, 2022

Copy link
Copy Markdown
Member

closing in favor of #11059 (to be controlled by a config, since we can't make sure it only affects Lua)

@oranagra oranagra closed this Aug 3, 2022
oranagra added a commit that referenced this pull request Aug 21, 2022
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 <[email protected]>
Co-authored-by: Oran Agra <[email protected]>
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants