Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix outdated result in type command for expired keys #1286

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/cmd_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "commander.h"
#include "commands/ttl_util.h"
#include "error_constants.h"
#include "server/redis_reply.h"
#include "server/server.h"
#include "storage/redis_db.h"
#include "time_util.h"
Expand Down
7 changes: 6 additions & 1 deletion src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "parse_util.h"
#include "rocksdb/iterator.h"
#include "server/server.h"
#include "storage/redis_metadata.h"

namespace Redis {

Expand Down Expand Up @@ -441,7 +442,11 @@ rocksdb::Status Database::Type(const Slice &user_key, RedisType *type) {

Metadata metadata(kRedisNone, false);
metadata.Decode(value);
*type = metadata.Type();
if (metadata.Expired()) {
*type = kRedisNone;
} else {
*type = metadata.Type();
}
return rocksdb::Status::OK();
}

Expand Down
10 changes: 10 additions & 0 deletions tests/gocase/unit/keyspace/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ func TestKeyspace(t *testing.T) {
require.Equal(t, prefixKeys, gotKeys)
}
})

t.Run("Type a expired key", func(t *testing.T) {
expireTime := time.Second
key := "foo"
require.NoError(t, rdb.Del(ctx, key).Err())
require.Equal(t, "OK", rdb.SetEx(ctx, key, "bar", expireTime).Val())
require.Equal(t, "string", rdb.Type(ctx, key).Val())
time.Sleep(2 * expireTime)
require.Equal(t, "none", rdb.Type(ctx, key).Val())
})
}