Skip to content

Commit eae02a1

Browse files
authored
fix: Make restore accept ttl in ms (#1724)
Signed-off-by: Vladislav Oleshko <[email protected]>
1 parent 8b6de91 commit eae02a1

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

src/server/generic_family.cc

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ using VersionBuffer = std::array<char, sizeof(uint16_t)>;
4141
using CrcBuffer = std::array<char, sizeof(uint64_t)>;
4242
constexpr size_t DUMP_FOOTER_SIZE = sizeof(uint64_t) + sizeof(uint16_t); // version number and crc
4343

44-
int64_t CalculateExpirationTime(bool seconds, bool absolute, int64_t ts, int64_t now_msec) {
45-
int64_t msec = seconds ? ts * 1000 : ts;
46-
int64_t rel_msec = absolute ? msec - now_msec : msec;
47-
return rel_msec;
48-
}
49-
5044
VersionBuffer MakeRdbVersion() {
5145
VersionBuffer buf;
5246
buf[0] = RDB_SER_VERSION & 0xff;
@@ -188,12 +182,13 @@ class RestoreArgs {
188182
return replace_;
189183
}
190184

191-
constexpr int64_t ExpirationTime() const {
185+
uint64_t ExpirationTime() const {
186+
DCHECK_GE(expiration_, 0);
192187
return expiration_;
193188
}
194189

195190
[[nodiscard]] constexpr bool Expired() const {
196-
return ExpirationTime() < 0;
191+
return expiration_ < 0;
197192
}
198193

199194
[[nodiscard]] constexpr bool HasExpiration() const {
@@ -207,14 +202,11 @@ class RestoreArgs {
207202

208203
[[nodiscard]] bool RestoreArgs::UpdateExpiration(int64_t now_msec) {
209204
if (HasExpiration()) {
210-
auto new_ttl = CalculateExpirationTime(!abs_time_, abs_time_, expiration_, now_msec);
211-
if (new_ttl > kMaxExpireDeadlineSec * 1000) {
205+
int64_t ttl = abs_time_ ? expiration_ - now_msec : expiration_;
206+
if (ttl > kMaxExpireDeadlineSec * 1000)
212207
return false;
213-
}
214-
expiration_ = new_ttl;
215-
if (new_ttl > 0) {
216-
expiration_ += now_msec;
217-
}
208+
209+
expiration_ = ttl < 0 ? -1 : ttl + now_msec;
218210
}
219211
return true;
220212
}

src/server/generic_family_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,11 @@ TEST_F(GenericFamilyTest, Restore) {
474474
0x75, 0x59, 0x6d, 0x10, 0x04, 0x3f, 0x5c};
475475
auto resp = Run({"set", "exiting-key", "1234"});
476476
EXPECT_EQ(resp, "OK");
477-
// try to restore into existing key - this should failed
477+
// try to restore into existing key - this should fail
478478
ASSERT_THAT(Run({"restore", "exiting-key", "0", ToSV(STRING_DUMP_REDIS)}),
479479
ArgType(RespExpr::ERROR));
480480

481-
// Try restore while setting expiration into the pass
481+
// Try restore while setting expiration into the past
482482
// note that value for expiration is just some valid unix time stamp from the pass
483483
resp = Run(
484484
{"restore", "exiting-key", "1665476212900", ToSV(STRING_DUMP_REDIS), "ABSTTL", "REPLACE"});
@@ -518,11 +518,11 @@ TEST_F(GenericFamilyTest, Restore) {
518518
resp = Run({"dump", "string-key"});
519519
dump = resp.GetBuf();
520520
// this will change the value from "hello world" to "1234"
521-
resp = Run({"restore", "string-key", "7", ToSV(STRING_DUMP_REDIS), "REPLACE"});
521+
resp = Run({"restore", "string-key", "7000", ToSV(STRING_DUMP_REDIS), "REPLACE"});
522522
resp = Run({"get", "string-key"});
523523
EXPECT_EQ("1234", resp);
524524
// check TTL validity
525-
EXPECT_EQ(CheckedInt({"ttl", "string-key"}), 7);
525+
EXPECT_EQ(CheckedInt({"pttl", "string-key"}), 7000);
526526

527527
// Make check about ttl with abs time, restoring back to "hello world"
528528
resp = Run({"restore", "string-key", absl::StrCat(TEST_current_time_ms + 2000), ToSV(dump),

0 commit comments

Comments
 (0)