Skip to content

Commit 15a484c

Browse files
BagritsevichStepanromange
authored andcommitted
chore: Limit expiry value to 32 bits.
We use adaptive precision, where we keep "millis" precision if we can, and switch to seconds precision if the deadline is too large. This actually was before but now I reduced the cut-off to 4B ms, or 49 days. Also, the ttl in seconds is now limited to 4B sec, with larger values quietly rounded down to this limit. Signed-off-by: Roman Gershman <[email protected]>
1 parent b4a5c87 commit 15a484c

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/core/expire_period.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
namespace dfly {
1010

11+
// ExpirePeriod encapsulates the expiration period of a key.
12+
// It can represent periods in milliseconds up to ~49 days and in seconds up to ~136 years.
13+
// If value in milliseconds is too high, it switches to seconds precision.
14+
// And if it's still too high, it silently clamps to the max value.
1115
class ExpirePeriod {
1216
public:
13-
static constexpr size_t kMaxGenId = 15;
17+
static constexpr uint32_t kMaxGenId = 15;
1418

1519
ExpirePeriod() : val_(0), gen_(0), precision_(0) {
1620
static_assert(sizeof(ExpirePeriod) == 8); // TODO
@@ -35,28 +39,30 @@ class ExpirePeriod {
3539

3640
void Set(uint64_t ms);
3741

38-
bool is_second_precision() { return precision_ == 1;}
42+
bool is_second_precision() const {
43+
return precision_ == 1;
44+
}
3945

4046
private:
41-
uint64_t val_ : 59;
42-
uint64_t gen_ : 4;
43-
uint64_t precision_ : 1; // 0 - ms, 1 - sec.
47+
uint32_t val_;
48+
uint8_t gen_ : 2; // generation id.
49+
uint8_t precision_ : 1; // 0 - ms, 1 - sec.
4450
};
4551

4652
inline void ExpirePeriod::Set(uint64_t ms) {
47-
constexpr uint64_t kBarrier = (1ULL << 48);
48-
49-
if (ms < kBarrier) {
50-
val_ = ms;
51-
precision_ = 0; // ms
53+
if (ms < UINT32_MAX) {
54+
val_ = ms; // about 49 days in ms.
55+
precision_ = 0; // ms
5256
return;
5357
}
5458

55-
precision_ = 1;
56-
if (ms < kBarrier << 10) {
57-
ms = (ms + 500) / 1000; // seconds
59+
precision_ = 1; // seconds
60+
if (ms < UINT64_MAX / 2) {
61+
ms = (ms + 500) / 1000;
62+
val_ = ms >= UINT32_MAX ? UINT32_MAX - 1 : ms;
63+
} else {
64+
val_ = UINT32_MAX - 1;
5865
}
59-
val_ = ms >= kBarrier ? kBarrier - 1 : ms;
6066
}
6167

6268
} // namespace dfly

src/server/server_family.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,7 @@ void ServerFamily::FlushAll(Namespace* ns) {
12841284
boost::intrusive_ptr<Transaction> flush_trans(new Transaction{cid});
12851285
flush_trans->InitByArgs(ns, 0, {});
12861286
VLOG(1) << "Performing flush";
1287-
error_code ec = Drakarys(flush_trans.get(), DbSlice::kDbAll);
1288-
if (ec) {
1289-
LOG(ERROR) << "Error flushing db " << ec.message();
1290-
}
1287+
Drakarys(flush_trans.get(), DbSlice::kDbAll);
12911288
}
12921289

12931290
// Load starts as many fibers as there are files to load each one separately.
@@ -2201,7 +2198,7 @@ bool ServerFamily::TEST_IsSaving() const {
22012198
return is_saving.load(std::memory_order_relaxed);
22022199
}
22032200

2204-
error_code ServerFamily::Drakarys(Transaction* transaction, DbIndex db_ind) {
2201+
void ServerFamily::Drakarys(Transaction* transaction, DbIndex db_ind) {
22052202
VLOG(1) << "Drakarys";
22062203

22072204
transaction->Execute(
@@ -2210,8 +2207,6 @@ error_code ServerFamily::Drakarys(Transaction* transaction, DbIndex db_ind) {
22102207
return OpStatus::OK;
22112208
},
22122209
true);
2213-
2214-
return error_code{};
22152210
}
22162211

22172212
SaveInfoData ServerFamily::GetLastSaveInfo() const {

src/server/server_family.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class ServerFamily {
250250

251251
// Burns down and destroy all the data from the database.
252252
// if kDbAll is passed, burns all the databases to the ground.
253-
std::error_code Drakarys(Transaction* transaction, DbIndex db_ind);
253+
void Drakarys(Transaction* transaction, DbIndex db_ind);
254254

255255
SaveInfoData GetLastSaveInfo() const;
256256

0 commit comments

Comments
 (0)