-
Notifications
You must be signed in to change notification settings - Fork 13
chore: Add doc and rename function for flushing strategy #740
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
Open
lym953
wants to merge
4
commits into
main
Choose a base branch
from
yiming.luo/flush-strategy-doc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::config::flush_strategy::{FlushStrategy, PeriodicStrategy}; | ||
use crate::config::flush_strategy::{ConcreteFlushStrategy, FlushStrategy, PeriodicStrategy}; | ||
|
||
const TWENTY_SECONDS: u64 = 20 * 1000; | ||
const LOOKBACK_COUNT: usize = 20; | ||
|
@@ -23,39 +23,63 @@ impl InvocationTimes { | |
self.head = (self.head + 1) % LOOKBACK_COUNT; | ||
} | ||
|
||
pub(crate) fn should_adapt(&self, now: u64, flush_timeout: u64) -> FlushStrategy { | ||
// If the buffer isn't full, then we haven't seen enough invocations, so we should flush. | ||
for idx in self.head..LOOKBACK_COUNT { | ||
if self.times[idx] == 0 { | ||
return FlushStrategy::End; | ||
// Translate FlushStrategy to a ConcreteFlushStrategy | ||
// For FlushStrategy::Default, evaluate based on past invocation times. Otherwise, return the | ||
// strategy as is. | ||
pub(crate) fn evaluate_concrete_strategy( | ||
&self, | ||
now: u64, | ||
flush_timeout: u64, | ||
flush_strategy: FlushStrategy, | ||
) -> ConcreteFlushStrategy { | ||
match flush_strategy { | ||
FlushStrategy::Periodically(p) => ConcreteFlushStrategy::Periodically(p), | ||
FlushStrategy::End => ConcreteFlushStrategy::End, | ||
FlushStrategy::Continuously(p) => ConcreteFlushStrategy::Continuously(p), | ||
FlushStrategy::EndPeriodically(p) => ConcreteFlushStrategy::EndPeriodically(p), | ||
FlushStrategy::Default => { | ||
// If the buffer isn't full, then we haven't seen enough invocations, so we should flush | ||
// at the end of the invocation. | ||
for idx in self.head..LOOKBACK_COUNT { | ||
if self.times[idx] == 0 { | ||
return ConcreteFlushStrategy::End; | ||
} | ||
} | ||
|
||
// Now we've seen at least 20 invocations. Possible cases: | ||
// 1. If the average time between invocations is longer than 2 minutes, stick to End strategy. | ||
// 2. If average interval is shorter than 2 minutes: | ||
// 2.1 If it's very short, use the continuous strategy to minimize delaying the next invocation. | ||
// 2.2 If it's not too short, use the periodic strategy to minimize the risk that | ||
// flushing is delayed due to the Lambda environment being frozen between invocations. | ||
// We get the average time between each invocation by taking the difference between newest (`now`) and the | ||
// oldest invocation in the buffer, then dividing by `LOOKBACK_COUNT - 1`. | ||
let oldest = self.times[self.head]; | ||
|
||
let elapsed = now - oldest; | ||
let should_adapt = | ||
(elapsed as f64 / (LOOKBACK_COUNT - 1) as f64) < ONE_TWENTY_SECONDS; | ||
if should_adapt { | ||
// Both units here are in seconds | ||
// TODO: What does this mean? | ||
if elapsed < flush_timeout { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did that answer your question (in the other thread)? |
||
return ConcreteFlushStrategy::Continuously(PeriodicStrategy { | ||
interval: TWENTY_SECONDS, | ||
}); | ||
} | ||
return ConcreteFlushStrategy::Periodically(PeriodicStrategy { | ||
interval: TWENTY_SECONDS, | ||
}); | ||
} | ||
ConcreteFlushStrategy::End | ||
} | ||
} | ||
|
||
// Now we've seen at least 20 invocations. Switch to periodic if we're invoked at least once every 2 minutes. | ||
// We get the average time between each invocation by taking the difference between newest (`now`) and the | ||
// oldest invocation in the buffer, then dividing by `LOOKBACK_COUNT - 1`. | ||
let oldest = self.times[self.head]; | ||
|
||
let elapsed = now - oldest; | ||
let should_adapt = (elapsed as f64 / (LOOKBACK_COUNT - 1) as f64) < ONE_TWENTY_SECONDS; | ||
if should_adapt { | ||
// Both units here are in seconds | ||
if elapsed < flush_timeout { | ||
return FlushStrategy::Continuously(PeriodicStrategy { | ||
interval: TWENTY_SECONDS, | ||
}); | ||
} | ||
return FlushStrategy::Periodically(PeriodicStrategy { | ||
interval: TWENTY_SECONDS, | ||
}); | ||
} | ||
FlushStrategy::End | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::config::flush_strategy::{FlushStrategy, PeriodicStrategy}; | ||
use crate::config::flush_strategy::{ConcreteFlushStrategy, FlushStrategy, PeriodicStrategy}; | ||
use crate::lifecycle::invocation_times::{self, TWENTY_SECONDS}; | ||
|
||
#[test] | ||
|
@@ -75,7 +99,10 @@ mod tests { | |
invocation_times.add(timestamp); | ||
assert_eq!(invocation_times.times[0], timestamp); | ||
assert_eq!(invocation_times.head, 1); | ||
assert_eq!(invocation_times.should_adapt(1, 60), FlushStrategy::End); | ||
assert_eq!( | ||
invocation_times.evaluate_concrete_strategy(1, 60, FlushStrategy::Default), | ||
ConcreteFlushStrategy::End | ||
); | ||
} | ||
|
||
#[test] | ||
|
@@ -88,8 +115,8 @@ mod tests { | |
assert_eq!(invocation_times.times[0], 20); | ||
assert_eq!(invocation_times.head, 1); | ||
assert_eq!( | ||
invocation_times.should_adapt(21, 60), | ||
FlushStrategy::Continuously(PeriodicStrategy { | ||
invocation_times.evaluate_concrete_strategy(21, 60, FlushStrategy::Default), | ||
ConcreteFlushStrategy::Continuously(PeriodicStrategy { | ||
interval: TWENTY_SECONDS | ||
}) | ||
); | ||
|
@@ -105,8 +132,8 @@ mod tests { | |
assert_eq!(invocation_times.times[0], 20); | ||
assert_eq!(invocation_times.head, 1); | ||
assert_eq!( | ||
invocation_times.should_adapt(21, 1), | ||
FlushStrategy::Periodically(PeriodicStrategy { | ||
invocation_times.evaluate_concrete_strategy(21, 1, FlushStrategy::Default), | ||
ConcreteFlushStrategy::Periodically(PeriodicStrategy { | ||
interval: TWENTY_SECONDS | ||
}) | ||
); | ||
|
@@ -122,7 +149,10 @@ mod tests { | |
// should wrap around | ||
assert_eq!(invocation_times.times[0], 5019); | ||
assert_eq!(invocation_times.head, 1); | ||
assert_eq!(invocation_times.should_adapt(10000, 60), FlushStrategy::End); | ||
assert_eq!( | ||
invocation_times.evaluate_concrete_strategy(10000, 60, FlushStrategy::Default), | ||
ConcreteFlushStrategy::End | ||
); | ||
} | ||
|
||
#[test] | ||
|
@@ -140,8 +170,8 @@ mod tests { | |
1901 | ||
); | ||
assert_eq!( | ||
invocation_times.should_adapt(2501, 60), | ||
FlushStrategy::Periodically(PeriodicStrategy { | ||
invocation_times.evaluate_concrete_strategy(2501, 60, FlushStrategy::Default), | ||
ConcreteFlushStrategy::Periodically(PeriodicStrategy { | ||
interval: TWENTY_SECONDS | ||
}) | ||
); | ||
|
@@ -161,6 +191,9 @@ mod tests { | |
invocation_times.times[invocation_times::LOOKBACK_COUNT - 1], | ||
2471 | ||
); | ||
assert_eq!(invocation_times.should_adapt(3251, 60), FlushStrategy::End); | ||
assert_eq!( | ||
invocation_times.evaluate_concrete_strategy(3251, 60, FlushStrategy::Default), | ||
ConcreteFlushStrategy::End | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some doc is moved from
flush_control.rs