Skip to content

Commit dfbb553

Browse files
authored
Add component pieces for rotation (#1055)
### What does this PR do? This commit places the last few bits needed for file rotation. At this point I intend to add a list of static names to index into and then after that, expand State::advance_time to perform rotations.
1 parent 8cb9955 commit dfbb553

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

lading/src/bin/logrotate_fs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ fn main() -> Result<(), Error> {
300300

301301
let state = model::State::new(
302302
args.bytes_per_second.get_bytes() as u64, // Adjust units accordingly
303+
5, // TODO make an argument
304+
1_000_000, // 1MiB
303305
block_cache,
304306
);
305307

lading/src/generator/file_gen/model.rs

+71-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ pub struct File {
4444

4545
/// The number of bytes that accumulate in this `File` per tick.
4646
bytes_per_tick: u64,
47+
48+
/// Whether the file is read-only -- that is, no more "writes" will ever
49+
/// happen -- or not.
50+
read_only: bool,
51+
52+
/// The ordinal number of this File. If the file is foo.log the ordinal
53+
/// number is 0, if foo.log.1 then 1 etc.
54+
ordinal: u8,
4755
}
4856

4957
impl File {
@@ -70,9 +78,10 @@ impl File {
7078
/// This function runs the clock forward to `now`, updating `modified_tick`
7179
/// and `status_tick` as bytes are continuously "written" to the `File`.
7280
///
73-
/// Will have no result if `now` <= `modified_tick`.
81+
/// Will have no result if `now` <= `modified_tick`. Will have no result if
82+
/// the file is read-only.
7483
fn advance_time(&mut self, now: Tick) {
75-
if now <= self.modified_tick {
84+
if now <= self.modified_tick || self.read_only {
7685
return;
7786
}
7887

@@ -83,6 +92,39 @@ impl File {
8392
self.modified_tick = now;
8493
self.status_tick = now;
8594
}
95+
96+
/// Set this file to read-only
97+
///
98+
/// This function flips the internal bool on this `File` stopping any future
99+
/// byte accumulations.
100+
pub fn set_read_only(&mut self) {
101+
self.read_only = true;
102+
}
103+
104+
/// Return whether the file is read-only or not
105+
#[must_use]
106+
pub fn read_only(&self) -> bool {
107+
self.read_only
108+
}
109+
110+
/// Return the ordinal number of this File
111+
#[must_use]
112+
pub fn ordinal(&self) -> u8 {
113+
self.ordinal
114+
}
115+
116+
/// Increment the ordinal number of this File
117+
pub fn incr_ordinal(&mut self) {
118+
self.ordinal = self.ordinal.saturating_add(1);
119+
}
120+
121+
/// Returns the current size in bytes of the File
122+
///
123+
/// This function does not advance time.
124+
#[must_use]
125+
pub fn size(&self) -> u64 {
126+
self.bytes_written
127+
}
86128
}
87129

88130
/// Model representation of a `Directory`. Contains children are `Directory`
@@ -133,6 +175,7 @@ pub struct State {
133175
root_inode: Inode,
134176
now: Tick,
135177
block_cache: block::Cache,
178+
max_bytes_per_file: u64,
136179
}
137180

138181
/// The attributes of a `Node`.
@@ -164,7 +207,12 @@ pub enum NodeType {
164207
impl State {
165208
/// Create a new instance of `State`.
166209
#[tracing::instrument(skip(block_cache))]
167-
pub fn new(bytes_per_tick: u64, block_cache: block::Cache) -> State {
210+
pub fn new(
211+
bytes_per_tick: u64,
212+
max_rotations: u8,
213+
max_bytes_per_file: u64,
214+
block_cache: block::Cache,
215+
) -> State {
168216
let root_inode: Inode = 1; // `/`
169217
let logs_inode: Inode = 2; // `/logs`
170218
let foo_log_inode: Inode = 3; // `/logs/foo.log`
@@ -210,6 +258,9 @@ impl State {
210258
status_tick: 0,
211259

212260
bytes_per_tick,
261+
262+
read_only: false,
263+
ordinal: 0,
213264
};
214265
nodes.insert(
215266
foo_log_inode,
@@ -229,6 +280,7 @@ impl State {
229280
root_inode,
230281
now: 0,
231282
block_cache,
283+
max_bytes_per_file,
232284
}
233285
}
234286

@@ -256,7 +308,22 @@ impl State {
256308
match node {
257309
Node::File { file, .. } => {
258310
file.advance_time(now);
259-
// TODO add rotation logic here
311+
if file.read_only() {
312+
continue;
313+
};
314+
if file.size() >= self.max_bytes_per_file {
315+
file.set_read_only();
316+
// Add rotation logic here. What I want to do is add a
317+
// new File with the name of the current `File` bump the
318+
// ordinal number of `file` and make the new File have
319+
// `file` as its peer. I need to adjust `self.nodes` to
320+
// include the new File and also modify the name of the
321+
// current `file`. Or I should just make a pre-defined
322+
// array of names and index into them with the ordinal.
323+
//
324+
// If the ordinal of the `file` is past
325+
// self.max_rotations we delete it.
326+
}
260327
}
261328
Node::Directory { .. } => {
262329
// directories are immutable though time flows around them

0 commit comments

Comments
 (0)