Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 11, 2024
1 parent 4feefda commit 119b039
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 95 deletions.
28 changes: 14 additions & 14 deletions src/append/rolling_file/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jiff::Timestamp;
use jiff::Zoned;

#[derive(Debug)]
pub enum Clock {
Expand All @@ -22,18 +22,18 @@ pub enum Clock {
}

impl Clock {
pub fn now(&self) -> Timestamp {
pub fn now(&self) -> Zoned {
match self {
Clock::DefaultClock => Timestamp::now(),
Clock::DefaultClock => Zoned::now(),
#[cfg(test)]
Clock::ManualClock(clock) => clock.now(),
}
}

#[cfg(test)]
pub fn set_now(&mut self, new_time: Timestamp) {
pub fn set_now(&mut self, now: Zoned) {
if let Clock::ManualClock(clock) = self {
clock.set_now(new_time);
clock.set_now(now);
}
}
}
Expand All @@ -42,20 +42,20 @@ impl Clock {
#[derive(Debug)]
#[cfg(test)]
pub struct ManualClock {
now: Timestamp,
now: Zoned,
}

#[cfg(test)]
impl ManualClock {
pub fn new(now: Timestamp) -> ManualClock {
pub fn new(now: Zoned) -> ManualClock {
ManualClock { now }
}

fn now(&self) -> Timestamp {
self.now
fn now(&self) -> Zoned {
self.now.clone()
}

pub fn set_now(&mut self, now: Timestamp) {
pub fn set_now(&mut self, now: Zoned) {
self.now = now;
}
}
Expand All @@ -68,12 +68,12 @@ mod tests {

#[test]
fn test_manual_clock_adjusting() {
let now = Timestamp::from_str("2023-01-01T12:00:00Z").unwrap();
let mut clock = ManualClock { now };
let now = Zoned::from_str("2024-08-10T17:12:52+08[+08]").unwrap();
let mut clock = ManualClock { now: now.clone() };
assert_eq!(clock.now(), now);

let now = Timestamp::from_str("2024-01-01T12:00:00Z").unwrap();
clock.set_now(now);
let now = Zoned::from_str("2024-01-01T12:00:00+08[+08]").unwrap();
clock.set_now(now.clone());
assert_eq!(clock.now(), now);
}
}
82 changes: 43 additions & 39 deletions src/append/rolling_file/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::path::Path;
use std::path::PathBuf;

use anyhow::Context;
use jiff::Timestamp;
use jiff::Zoned;
use parking_lot::RwLock;

use crate::append::rolling_file::clock::Clock;
Expand All @@ -45,13 +45,13 @@ impl Write for RollingFileWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let now = self.state.clock.now();
let writer = self.writer.get_mut();
if self.state.should_rollover_on_date(now) {
self.state.advance_date(now);
self.state.refresh_writer(now, 0, writer);
if self.state.should_rollover_on_date(&now) {
self.state.advance_date(&now);
self.state.refresh_writer(&now, 0, writer);
}
if self.state.should_rollover_on_size() {
let cnt = self.state.advance_cnt();
self.state.refresh_writer(now, cnt, writer);
self.state.refresh_writer(&now, cnt, writer);
}

writer.write(buf).map(|n| {
Expand Down Expand Up @@ -206,12 +206,12 @@ impl State {
clock,
};

let file = state.create_log_writer(now, 0)?;
let file = state.create_log_writer(&now, 0)?;
let writer = RwLock::new(file);
Ok((state, writer))
}

fn join_date(&self, date: &Timestamp, cnt: usize) -> String {
fn join_date(&self, date: &Zoned, cnt: usize) -> String {
let date = date.strftime(self.date_format);
match (
&self.rotation,
Expand All @@ -230,9 +230,9 @@ impl State {
}
}

fn create_log_writer(&self, now: Timestamp, cnt: usize) -> anyhow::Result<File> {
fn create_log_writer(&self, now: &Zoned, cnt: usize) -> anyhow::Result<File> {
fs::create_dir_all(&self.log_dir).context("failed to create log directory")?;
let filename = self.join_date(&now, cnt);
let filename = self.join_date(now, cnt);
if let Some(max_files) = self.max_files {
if let Err(err) = self.delete_oldest_logs(max_files) {
eprintln!("failed to delete oldest logs: {err}");
Expand Down Expand Up @@ -303,7 +303,7 @@ impl State {
Ok(())
}

fn refresh_writer(&self, now: Timestamp, cnt: usize, file: &mut File) {
fn refresh_writer(&self, now: &Zoned, cnt: usize, file: &mut File) {
match self.create_log_writer(now, cnt) {
Ok(new_file) => {
if let Err(err) = file.flush() {
Expand All @@ -315,9 +315,9 @@ impl State {
}
}

fn should_rollover_on_date(&self, date: Timestamp) -> bool {
fn should_rollover_on_date(&self, date: &Zoned) -> bool {
self.next_date_timestamp
.is_some_and(|ts| date.as_millisecond() as usize >= ts)
.is_some_and(|ts| date.timestamp().as_millisecond() as usize >= ts)
}

fn should_rollover_on_size(&self) -> bool {
Expand All @@ -330,10 +330,10 @@ impl State {
self.current_count
}

fn advance_date(&mut self, now: Timestamp) {
fn advance_date(&mut self, now: &Zoned) {
self.current_count = 0;
self.current_filesize = 0;
self.next_date_timestamp = self.rotation.next_date_timestamp(&now);
self.next_date_timestamp = self.rotation.next_date_timestamp(now);
}
}

Expand All @@ -343,7 +343,10 @@ mod tests {
use std::fs;
use std::io::Write;
use std::ops::Add;
use std::time::Duration;
use std::str::FromStr;

use jiff::Span;
use jiff::Zoned;
use rand::distributions::Alphanumeric;
use rand::Rng;
use tempfile::TempDir;
Expand Down Expand Up @@ -395,37 +398,37 @@ mod tests {
fn test_file_rolling_via_time_rotation() {
test_file_rolling_for_specific_time_rotation(
Rotation::Minutely,
Duration::minutes(1),
Duration::seconds(1),
Span::new().minutes(1),
Span::new().seconds(1),
);
test_file_rolling_for_specific_time_rotation(
Rotation::Hourly,
Duration::hours(1),
Duration::minutes(1),
Span::new().hours(1),
Span::new().minutes(1),
);
test_file_rolling_for_specific_time_rotation(
Rotation::Daily,
Duration::days(1),
Duration::hours(1),
Span::new().days(1),
Span::new().hours(1),
);
}

fn test_file_rolling_for_specific_time_rotation(
rotation: Rotation,
rotation_duration: Duration,
write_interval: Duration,
rotation_duration: Span,
write_interval: Span,
) {
let temp_dir = TempDir::new().expect("failed to create a temporary directory");
let max_files = 10;

let start_time = datetime!(2024-08-10 00:00:00 +0);
let start_time = Zoned::from_str("2024-08-10T00:00:00[UTC]").unwrap();
let mut writer = RollingFileWriterBuilder::new()
.rotation(rotation)
.filename_prefix("test_prefix")
.filename_suffix("log")
.max_log_files(max_files)
.max_file_size(usize::MAX)
.clock(Clock::ManualClock(ManualClock::new(start_time)))
.clock(Clock::ManualClock(ManualClock::new(start_time.clone())))
.build(&temp_dir)
.unwrap();

Expand All @@ -435,11 +438,12 @@ mod tests {
let mut expected_file_size = 0;
let end_time = cur_time.add(rotation_duration);
while cur_time < end_time {
writer.state.clock.set_now(cur_time);
writer.state.clock.set_now(cur_time.clone());

let rand_str = generate_random_string();
expected_file_size += rand_str.len();

println!("i: {i}, cur_time: {cur_time}, end_time: {end_time}, expected_file_size: {expected_file_size}");
assert_eq!(writer.write(rand_str.as_bytes()).unwrap(), rand_str.len());
assert_eq!(writer.state.current_filesize, expected_file_size);

Expand All @@ -458,41 +462,41 @@ mod tests {
fn test_file_rolling_via_file_size_and_time_rotation() {
test_file_size_and_time_rotation_for_specific_time_rotation(
Rotation::Minutely,
Duration::minutes(1),
Duration::seconds(1),
Span::new().minutes(1),
Span::new().seconds(1),
);
test_file_size_and_time_rotation_for_specific_time_rotation(
Rotation::Hourly,
Duration::hours(1),
Duration::minutes(1),
Span::new().hours(1),
Span::new().minutes(1),
);
test_file_size_and_time_rotation_for_specific_time_rotation(
Rotation::Daily,
Duration::days(1),
Duration::hours(1),
Span::new().days(1),
Span::new().hours(1),
);
}

fn test_file_size_and_time_rotation_for_specific_time_rotation(
rotation: Rotation,
rotation_duration: Duration,
write_interval: Duration,
rotation_duration: Span,
write_interval: Span,
) {
let temp_dir = TempDir::new().expect("failed to create a temporary directory");
let max_files = 10;
// Small file size and too many files to ensure both of file size and time rotation can
be // triggered.
// Small file size and too many files to ensure both of file size and time rotation can be
// triggered.
let total_files = 100;
let file_size = 500;

let start_time = datetime!(2024-08-10 00:00:00 +0);
let start_time = Zoned::from_str("2024-08-10T00:00:00[UTC]").unwrap();
let mut writer = RollingFileWriterBuilder::new()
.rotation(rotation)
.filename_prefix("test_prefix")
.filename_suffix("log")
.max_log_files(max_files)
.max_file_size(file_size)
.clock(Clock::ManualClock(ManualClock::new(start_time)))
.clock(Clock::ManualClock(ManualClock::new(start_time.clone())))
.build(&temp_dir)
.unwrap();

Expand All @@ -504,7 +508,7 @@ be // triggered.
for i in 1..=total_files {
let mut expected_file_size = 0;
loop {
writer.state.clock.set_now(cur_time);
writer.state.clock.set_now(cur_time.clone());

let rand_str = generate_random_string();
expected_file_size += rand_str.len();
Expand Down
Loading

0 comments on commit 119b039

Please sign in to comment.