This is an automated email from the ASF dual-hosted git repository.

hulk 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 7611d73f fix(string): should reset the value of expired key for INCRBY 
cmd  (#2667)
7611d73f is described below

commit 7611d73f940886bbe7b77598cdd760c8e8371427
Author: FishYoung <[email protected]>
AuthorDate: Mon Nov 18 19:18:38 2024 +0800

    fix(string): should reset the value of expired key for INCRBY cmd  (#2667)
---
 src/types/redis_string.cc                |  2 ++
 tests/gocase/unit/type/incr/incr_test.go | 15 +++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/src/types/redis_string.cc b/src/types/redis_string.cc
index 7d7f476d..85a3a95d 100644
--- a/src/types/redis_string.cc
+++ b/src/types/redis_string.cc
@@ -329,6 +329,7 @@ rocksdb::Status String::IncrBy(engine::Context &ctx, const 
std::string &user_key
   if (!s.ok() && !s.IsNotFound()) return s;
   if (s.IsNotFound()) {
     Metadata metadata(kRedisString, false);
+    raw_value.clear();
     metadata.Encode(&raw_value);
   }
 
@@ -367,6 +368,7 @@ rocksdb::Status String::IncrByFloat(engine::Context &ctx, 
const std::string &use
 
   if (s.IsNotFound()) {
     Metadata metadata(kRedisString, false);
+    raw_value.clear();
     metadata.Encode(&raw_value);
   }
   size_t offset = Metadata::GetOffsetAfterExpire(raw_value[0]);
diff --git a/tests/gocase/unit/type/incr/incr_test.go 
b/tests/gocase/unit/type/incr/incr_test.go
index f8a1785c..3b507ff5 100644
--- a/tests/gocase/unit/type/incr/incr_test.go
+++ b/tests/gocase/unit/type/incr/incr_test.go
@@ -22,6 +22,7 @@ package incr
 import (
        "context"
        "testing"
+       "time"
 
        "github.com/apache/kvrocks/tests/gocase/util"
        "github.com/stretchr/testify/require"
@@ -75,6 +76,14 @@ func testIncr(t *testing.T, configs 
util.KvrocksServerConfigs) {
                require.EqualValues(t, 34359738368, rdb.IncrBy(ctx, "novar", 
17179869184).Val())
        })
 
+       t.Run("INCR over an expired key", func(t *testing.T) {
+               require.NoError(t, rdb.SetEx(ctx, "expired-foo", "1024", 
2*time.Second).Err())
+               require.NoError(t, rdb.SetEx(ctx, "expired-str", "value", 
2*time.Second).Err())
+               time.Sleep(3 * time.Second)
+               require.EqualValues(t, 1, rdb.IncrBy(ctx, "expired-foo", 
1).Val())
+               require.EqualValues(t, 1, rdb.IncrBy(ctx, "expired-str", 
1).Val())
+       })
+
        t.Run("INCR fails against key with spaces (left)", func(t *testing.T) {
                require.NoError(t, rdb.Set(ctx, "novar", "    11", 0).Err())
                util.ErrorRegexp(t, rdb.Incr(ctx, "novar").Err(), "ERR.*")
@@ -133,6 +142,12 @@ func testIncr(t *testing.T, configs 
util.KvrocksServerConfigs) {
                require.EqualValues(t, 34359738368, rdb.IncrByFloat(ctx, 
"novar", 17179869184).Val())
        })
 
+       t.Run("INCRBYFLOAT over an expired key", func(t *testing.T) {
+               require.NoError(t, rdb.SetEx(ctx, "expired-foo", "10.24", 
2*time.Second).Err())
+               time.Sleep(3 * time.Second)
+               require.EqualValues(t, 1, rdb.IncrBy(ctx, "expired-foo", 
1.0).Val())
+       })
+
        t.Run("INCRBYFLOAT fails against key with spaces (left)", func(t 
*testing.T) {
                require.NoError(t, rdb.Set(ctx, "novar", "    11", 0).Err())
                util.ErrorRegexp(t, rdb.IncrByFloat(ctx, "novar", 1.0).Err(), 
"ERR.*valid.*")

Reply via email to