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

seg: change all recent timestamps to now #482

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions src/protocol/memcache/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ impl Ttl {
pub fn new(exptime: i64, time_type: TimeType) -> Self {
// all negative values mean to expire immediately, early return
if exptime < 0 {
info!("TTL is negative, should immediately expire");
return Self {
inner: NonZeroI32::new(-1),
};
Expand All @@ -336,6 +337,7 @@ impl Ttl {
let exptime = if time_type == TimeType::Unix
|| (time_type == TimeType::Memcache && exptime > 60 * 60 * 24 * 30)
{
info!("TTL is unix timestamp, converting to relative time");
// treat it as a unix timestamp

// clamp to a valid u32
Expand All @@ -360,11 +362,13 @@ impl Ttl {

seconds as i64
} else {
info!("TTL is relative, returning");
exptime
};

// clamp long TTLs
if exptime > i32::MAX as i64 {
info!("TTL is greater than i32::MAX, clamping value");
Self {
inner: NonZeroI32::new(i32::MAX),
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/seg/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Builder {
hashtable,
segments,
ttl_buckets,
time: Instant::recent(),
time: Instant::now(),
})
}
}
2 changes: 1 addition & 1 deletion src/storage/seg/src/eviction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Eviction {
}

pub fn should_rerank(&mut self) -> bool {
let now = Instant::recent();
let now = Instant::now();
match self.policy {
Policy::None | Policy::Random | Policy::RandomFifo | Policy::Merge { .. } => false,
Policy::Fifo | Policy::Cte | Policy::Util => {
Expand Down
6 changes: 2 additions & 4 deletions src/storage/seg/src/seg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,13 @@ impl Seg {
/// assert!(cache.get(b"coffee").is_none());
/// ```
pub fn expire(&mut self) -> usize {
common::time::refresh_clock();
self.time = Instant::recent();
self.time = Instant::now();
self.ttl_buckets
.expire(&mut self.hashtable, &mut self.segments)
}

pub fn clear(&mut self) -> usize {
common::time::refresh_clock();
self.time = Instant::recent();
self.time = Instant::now();
self.ttl_buckets
.clear(&mut self.hashtable, &mut self.segments)
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/seg/src/segments/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Default for SegmentsBuilder {
}
}

impl<'a> SegmentsBuilder {
impl SegmentsBuilder {
/// Set the segment size in bytes.
///
/// # Panics
Expand Down
16 changes: 9 additions & 7 deletions src/storage/seg/src/segments/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ pub struct SegmentHeader {

impl SegmentHeader {
pub fn new(id: NonZeroU32) -> Self {
let now = Instant::now();
Self {
id,
write_offset: 0,
live_bytes: 0,
live_items: 0,
prev_seg: None,
next_seg: None,
create_at: Instant::recent(),
create_at: now,
ttl: 0,
merge_at: Instant::recent(),
merge_at: now,
accessible: false,
evictable: false,
_pad: [0; 25],
Expand All @@ -89,12 +90,13 @@ impl SegmentHeader {
assert!(!self.evictable());

self.reset();
let now = Instant::now();

self.prev_seg = None;
self.next_seg = None;
self.live_items = 0;
self.create_at = Instant::recent();
self.merge_at = Instant::recent();
self.create_at = now;
self.merge_at = now;
self.accessible = true;
}

Expand Down Expand Up @@ -249,7 +251,7 @@ impl SegmentHeader {
#[inline]
/// Update the created time
pub fn mark_created(&mut self) {
self.create_at = Instant::recent();
self.create_at = Instant::now();
}

#[inline]
Expand All @@ -261,7 +263,7 @@ impl SegmentHeader {
#[inline]
/// Update the created time
pub fn mark_merged(&mut self) {
self.merge_at = Instant::recent();
self.merge_at = Instant::now();
}

#[inline]
Expand All @@ -273,6 +275,6 @@ impl SegmentHeader {
pub fn can_evict(&self) -> bool {
self.evictable()
&& self.next_seg().is_some()
&& (self.create_at() + self.ttl()) >= (Instant::recent() + SEG_MATURE_TIME)
&& (self.create_at() + self.ttl()) >= (Instant::now() + SEG_MATURE_TIME)
}
}
6 changes: 3 additions & 3 deletions src/storage/seg/src/ttl_buckets/ttl_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TtlBucket {
}

let mut expired = 0;
let ts = Instant::recent();
let ts = Instant::now();

loop {
let seg_id = self.head;
Expand All @@ -99,7 +99,7 @@ impl TtlBucket {
self.head = None;
self.tail = None;
}
let _ = segment.clear(hashtable, true);
segment.clear(hashtable, true);
segments.push_free(seg_id);
SEGMENT_EXPIRE.increment();
expired += 1;
Expand Down Expand Up @@ -131,7 +131,7 @@ impl TtlBucket {
self.head = None;
self.tail = None;
}
let _ = segment.clear(hashtable, true);
segment.clear(hashtable, true);
segments.push_free(seg_id);
SEGMENT_CLEAR.increment();
cleared += 1;
Expand Down