You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Redis "skip sorting" then store in SORT actually sorts stored results #2079
I would expect to see testb have keys in the same order as those returned by sort testa by nosort, where the nosort key doesn't exist, but the documentation doesn't say anything either way.
I may be misunderstanding things here, but if the two options are exclusive then the documentation could be cleaned up, or an error raised when they're seen.
/* For the STORE option, or when SORT is called from a Lua script, * we want to force a specific ordering even when no explicit ordering * was asked (SORT BY nosort). This guarantees that replication / AOF * is deterministic. * * However in the case 'dontsort' is true, but the type to sort is a * sorted set, we don't need to do anything as ordering is guaranteed * in this special case. */
So, SORT + NOSORT + STORE = stored, but sorted.
The problem here is: SORT takes one of three argument types (list, set, zset). Of those three types, the SET type has no ordering, so we must sort before storing (or else when the SORT+STORE happens on the replica, the replica would have a different value).
As for the other two types: ZSET has a native ordering (sorted by score) and LIST has a native ordering (ordered by list position).
I believe we can change the check from "force sort everything when SORT+NOSORT+STORE" to "sort only SET when SORT+NOSORT+STORE"
We only need to actually sort if we are using a SET. LIST and ZSET are fine unsorted, since they will be stable across calls/servers in a fixed order.
This changes the code to (*nb: also need to update the comment above this code):
diff --git a/src/sort.c b/src/sort.c
index fedf0cf..46e7851 100644
--- a/src/sort.c+++ b/src/sort.c@@ -293,8 +293,7 @@ void sortCommand(redisClient *c) {
* However in the case 'dontsort' is true, but the type to sort is a
* sorted set, we don't need to do anything as ordering is guaranteed
* in this special case. */
- if ((storekey || c->flags & REDIS_LUA_CLIENT) &&- (dontsort && sortval->type != REDIS_ZSET))+ if ((dontsort && sortval->type == REDIS_SET))
{
/* Force ALPHA sorting */
dontsort = 0;
The change says "force sorting if the user asked for NOSORT and the source is a SET," otherwise we continue to do exactly what the user requested.
What about the REDIS_LUA_CLIENT check? I don't think that's needed anymore since the only sort order we care about is forcing a SET to have a known ordering. The other two types (LIST, ZSET) will always have the same nosort order because they are stored by position or score.
Also, with the change above, we can now:
127.0.0.1:9999> sort testa by nosort limit 03 store testb
(integer) 3127.0.0.1:9999> lrange testb 0-11) "5"2) "3"3) "4"
All tests pass with the change above. If there's any more logic behind not changing it this way (maybe around replication problems or reproducible script problems), I'd love to hear it.
Alternatively, we could remove all of that "special sort exception" code and return an error if the user asks to sort a SET with NOSORT (since the return contents won't be stable across calls/replication/scripting).
The deny-all-set-nosort fix is also simple, but it breaks existing tests since we check for "SET automatically sorts even when we request no sorting:"
diff --git a/src/sort.c b/src/sort.c
index fedf0cf..ce4d14c 100644
--- a/src/sort.c+++ b/src/sort.c@@ -249,7 +249,13 @@ void sortCommand(redisClient *c) {
/* If the BY pattern does not contain '*', i.e. it is constant,
* we don't need to sort nor to lookup the weight keys. */
if (strchr(c->argv[j+1]->ptr,'*') == NULL) {
- dontsort = 1;+ if (sortval->type == REDIS_SET) {+ addReplyError(c,"Must specify sort order for SET source.");+ syntax_error++;+ break;+ } else {+ dontsort = 1;+ }
} else {
/* If BY is specified with a real patter, we can't accept
* it in cluster mode. */
anyone asking for ordering on it can't reasonably complain at anything they get back as long as it contains all the keys...
Great point :)
So, the first option remains the best unless anybody can think of a reason why removing restrictions from the force-sort condition will break replication or scripting consistency.
Activity
mattsta commentedon Oct 22, 2014
A code comment mentions:
So, SORT + NOSORT + STORE = stored, but sorted.
The problem here is: SORT takes one of three argument types (list, set, zset). Of those three types, the SET type has no ordering, so we must sort before storing (or else when the SORT+STORE happens on the replica, the replica would have a different value).
As for the other two types: ZSET has a native ordering (sorted by score) and LIST has a native ordering (ordered by list position).
I believe we can change the check from "force sort everything when SORT+NOSORT+STORE" to "sort only SET when SORT+NOSORT+STORE"
We only need to actually sort if we are using a SET. LIST and ZSET are fine unsorted, since they will be stable across calls/servers in a fixed order.
This changes the code to (*nb: also need to update the comment above this code):
The change says "force sorting if the user asked for NOSORT and the source is a SET," otherwise we continue to do exactly what the user requested.
What about the REDIS_LUA_CLIENT check? I don't think that's needed anymore since the only sort order we care about is forcing a SET to have a known ordering. The other two types (LIST, ZSET) will always have the same nosort order because they are stored by position or score.
Also, with the change above, we can now:
All tests pass with the change above. If there's any more logic behind not changing it this way (maybe around replication problems or reproducible script problems), I'd love to hear it.
This is also the exact same problem and the same fix for https://github.com/antirez/redis/issues/545
mattsta commentedon Oct 22, 2014
Alternatively, we could remove all of that "special sort exception" code and return an error if the user asks to sort a SET with NOSORT (since the return contents won't be stable across calls/replication/scripting).
The deny-all-set-nosort fix is also simple, but it breaks existing tests since we check for "SET automatically sorts even when we request no sorting:"
mattsta commentedon Oct 22, 2014
Great point :)
So, the first option remains the best unless anybody can think of a reason why removing restrictions from the force-sort condition will break replication or scripting consistency.
Fix zero-ordering SORT when called against lists
Fix zero-ordering SORT when called against lists
Update RedisTimeSeries to version 8.11.90 (#15969)