This is an automated email from the ASF dual-hosted git repository.
binbin pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new b1afbe34 Fix RESTORE TTL is 0 with ASBTTL does not restore the key
(#1724)
b1afbe34 is described below
commit b1afbe34808caae9ef6a1f8052cf00c5cd4914d9
Author: Binbin <[email protected]>
AuthorDate: Sat Sep 2 18:14:09 2023 +0800
Fix RESTORE TTL is 0 with ASBTTL does not restore the key (#1724)
TTL is 0 mean the key is created without any expire.
ABSTTL mean the TTL should represent absolute Unix timestamp
in milliseconds in which the key will expire.
When typing TTL 0 and ABSTTL, kvrocks will not restore the
key, since we think the key is expired. However, in Redis,
ABSTTL is ignored when TTL is 0. (Although they seem to be
mutually exclusive)
---
src/commands/cmd_server.cc | 2 +-
tests/gocase/unit/restore/restore_test.go | 10 ++++++++++
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc
index 84c609c5..11a17c83 100644
--- a/src/commands/cmd_server.cc
+++ b/src/commands/cmd_server.cc
@@ -1040,7 +1040,7 @@ class CommandRestore : public Commander {
return {Status::RedisExecErr, db_status.ToString()};
}
}
- if (absttl_) {
+ if (ttl_ms_ && absttl_) {
auto now = util::GetTimeStampMS();
if (ttl_ms_ <= now) {
// return ok if the ttl is already expired
diff --git a/tests/gocase/unit/restore/restore_test.go
b/tests/gocase/unit/restore/restore_test.go
index 140d43e5..81d4a076 100644
--- a/tests/gocase/unit/restore/restore_test.go
+++ b/tests/gocase/unit/restore/restore_test.go
@@ -251,12 +251,21 @@ func TestRestoreWithTTL(t *testing.T) {
key := util.RandString(32, 64, util.Alpha)
value :=
"\x02\x05\x02ab\xc1\xd2\x04\x01a\x15012345678901234567890\x03abc\x06\x00s\xf8_\x01\xf3\xf56\xd8"
+
+ // TTL is 0, key is created without any expire.
require.NoError(t, rdb.Restore(ctx, key, 0, value).Err())
require.EqualValues(t, -1, rdb.TTL(ctx, key).Val())
require.EqualValues(t, []string{
"012345678901234567890", "1234", "a", "ab", "abc",
}, rdb.SMembers(ctx, key).Val())
+ // TTL is 0 with ABSTTL, key is created without any expire.
+ require.NoError(t, rdb.Do(ctx, "RESTORE", key, 0, value, "REPLACE",
"ABSTTL").Err())
+ require.EqualValues(t, -1, rdb.TTL(ctx, key).Val())
+ require.EqualValues(t, []string{
+ "012345678901234567890", "1234", "a", "ab", "abc",
+ }, rdb.SMembers(ctx, key).Val())
+
// Cannot restore to an existing key.
newValue :=
"\x0b\x10\x02\x00\x00\x00\x04\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x0b\x00\xc4}\x10TeTI<"
require.EqualError(t, rdb.Restore(ctx, key, 0, newValue).Err(),
@@ -284,6 +293,7 @@ func TestRestoreWithExpiredTTL(t *testing.T) {
require.EqualError(t, rdb.Do(ctx, "RESTORE", key, -1, value).Err(),
"ERR out of numeric range")
require.NoError(t, rdb.Do(ctx, "RESTORE", key, 0, value).Err())
require.Equal(t, "bar", rdb.Get(ctx, key).Val())
+ // Expired TTL with ABSTTL, will not actually restore the key.
require.NoError(t, rdb.Do(ctx, "RESTORE", key, 1111, value, "REPLACE",
"ABSTTL").Err())
require.EqualError(t, rdb.Get(ctx, key).Err(), redis.Nil.Error())
}