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

dekaf: Expire Reads after 5m #1736

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
48 changes: 35 additions & 13 deletions crates/dekaf/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum SessionDataPreviewState {

pub struct Session {
app: Arc<App>,
reads: HashMap<(TopicName, i32), PendingRead>,
reads: HashMap<(TopicName, i32), (PendingRead, std::time::Instant)>,
secret: String,
auth: Option<Authenticated>,
data_preview_state: SessionDataPreviewState,
Expand Down Expand Up @@ -380,7 +380,7 @@ impl Session {
..
} = request;

let client = self
let mut client = self
.auth
.as_mut()
.ok_or(anyhow::anyhow!("Session not authenticated"))?
Expand Down Expand Up @@ -454,16 +454,34 @@ impl Session {
}
};

if matches!(self.reads.get(&key), Some(pending) if pending.offset == fetch_offset) {
metrics::counter!(
"dekaf_fetch_requests",
"topic_name" => key.0.to_string(),
"partition_index" => key.1.to_string(),
"state" => "read_pending"
)
.increment(1);
continue; // Common case: fetch is at the pending offset.
match self.reads.get(&key) {
Some((_, started_at))
if started_at.elapsed() > std::time::Duration::from_secs(60 * 5) =>
{
metrics::counter!(
"dekaf_fetch_requests",
"topic_name" => key.0.to_string(),
"partition_index" => key.1.to_string(),
"state" => "read_expired"
)
.increment(1);
tracing::debug!(lifetime=?started_at.elapsed(), topic_name=?key.0,partition_index=?key.1, "Restarting expired Read");
self.reads.remove(&key);
client = client.with_fresh_gazette_client();
}
Some(_) => {
metrics::counter!(
"dekaf_fetch_requests",
"topic_name" => key.0.to_string(),
"partition_index" => key.1.to_string(),
"state" => "read_pending"
)
.increment(1);
continue; // Common case: fetch is at the pending offset.
}
_ => {}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the mechanism by which this session will grab an entirely new gazette::Router (e.g. new underlying connection) for the next started read?

Copy link
Contributor Author

@jshearer jshearer Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that the call to authenticated_client() always created a new gazette client, but it turns out it only does when refreshing the access token, reducing the effective Read timeout to however long access tokens live for (an hour I think).

I updated the fetch() handler to explicitly call client.with_fresh_gazette_client() upon Read timeout. We could also go nuclear and have authenticated_client() always return a client with fresh router, but that seemed like it might be unnecessary. Thoughts?

let Some(collection) = Collection::new(&client, &key.0).await? else {
metrics::counter!(
"dekaf_fetch_requests",
Expand Down Expand Up @@ -566,12 +584,16 @@ impl Session {
"started read",
);

if let Some(old) = self.reads.insert(key.clone(), pending) {
if let Some((old, started_at)) = self
.reads
.insert(key.clone(), (pending, std::time::Instant::now()))
{
tracing::warn!(
topic = topic_request.topic.as_str(),
partition = partition_request.partition,
old_offset = old.offset,
new_offset = fetch_offset,
read_lifetime = ?started_at.elapsed(),
"discarding pending read due to offset jump",
);
}
Expand All @@ -588,7 +610,7 @@ impl Session {
for partition_request in &topic_request.partitions {
key.1 = partition_request.partition;

let Some(pending) = self.reads.get_mut(&key) else {
let Some((pending, _)) = self.reads.get_mut(&key) else {
partition_responses.push(
PartitionData::default()
.with_partition_index(partition_request.partition)
Expand Down
Loading