Skip to content
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
5 changes: 3 additions & 2 deletions utilities/ttl/db_ttl_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,9 @@ Status DBWithTTLImpl::SanityCheckTimestamp(const Slice& str) {
}
// Checks that TS is not lesser than kMinTimestamp
// Gaurds against corruption & normal database opened incorrectly in ttl mode
int32_t timestamp_value = DecodeFixed32(str.data() + str.size() - kTSLength);
if (timestamp_value < kMinTimestamp) {
// The timestamp is stored as an unsigned 32-bit value (valid until 2106).
uint32_t timestamp_value = DecodeFixed32(str.data() + str.size() - kTSLength);
if (timestamp_value < static_cast<uint32_t>(kMinTimestamp)) {
return Status::Corruption("Error: Timestamp < ttl feature release time!\n");
}
return Status::OK();
Expand Down
56 changes: 56 additions & 0 deletions utilities/ttl/ttl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,62 @@ TEST_F(TtlTest, TtlFiftenYears) {
CloseTtl();
}

// Values written after 2038-01-19T03:14:07Z, when the time no longer fits in
// int32_t, must stay readable.
TEST_F(TtlTest, ReadAfterYear2038) {
int64_t now = 0;
ASSERT_OK(env_->GetCurrentTime(&now));
const int64_t kYear2038 = int64_t{1} << 31; // 2038-01-19T03:14:08Z
OpenTtl(3600);
for (int64_t t : {kYear2038 - 1, kYear2038, kYear2038 + 86400 * 365}) {
SCOPED_TRACE("t=" + std::to_string(t));
env_->Sleep(t - now);
now = t;
const std::string key = "key" + std::to_string(t);
ASSERT_OK(db_ttl_->Put(WriteOptions(), key, "value"));

std::string value;
ASSERT_OK(db_ttl_->Get(ReadOptions(), key, &value));
ASSERT_EQ(value, "value");

std::vector<std::string> values;
std::vector<Status> statuses =
db_ttl_->MultiGet(ReadOptions(), {Slice(key)}, &values);
ASSERT_OK(statuses[0]);
ASSERT_EQ(values[0], "value");

bool value_found = false;
ASSERT_TRUE(db_ttl_->KeyMayExist(ReadOptions(), key, &value, &value_found));

std::unique_ptr<Iterator> it(db_ttl_->NewIterator(ReadOptions()));
it->Seek(key);
ASSERT_TRUE(it->Valid());
ASSERT_EQ(it->value().ToString(), "value");
ASSERT_OK(it->status());
}
CloseTtl();
}

// After 2038, compaction keeps fresh values and drops expired ones.
TEST_F(TtlTest, CompactionAfterYear2038) {
int64_t now = 0;
ASSERT_OK(env_->GetCurrentTime(&now));
env_->Sleep((int64_t{1} << 31) + 1000 - now);
OpenTtl(100);
ASSERT_OK(db_ttl_->Put(WriteOptions(), "key1", "value"));
ASSERT_OK(db_ttl_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
std::string value;
ASSERT_OK(db_ttl_->Get(ReadOptions(), "key1", &value));
ASSERT_EQ(value, "value");

env_->Sleep(200);
ASSERT_OK(db_ttl_->Put(WriteOptions(), "key2", "value"));
ASSERT_OK(db_ttl_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
ASSERT_TRUE(db_ttl_->Get(ReadOptions(), "key1", &value).IsNotFound());
ASSERT_OK(db_ttl_->Get(ReadOptions(), "key2", &value));
CloseTtl();
}

TEST_F(TtlTest, ColumnFamiliesTest) {
std::unique_ptr<DB> db;
Options options;
Expand Down