Skip to content

Commit b0b0f65

Browse files
committed
Fix stale is_deleting flag blocking recreation, serialize delete vs create/update
Root cause of the Quest distributed CI failure: a node that doesn't run the background deletion job itself (an ingestor) never clears its resident stream's is_deleting flag on its own -- it only self-heals on the next sync_all_streams tick. Recreating a stream shortly after deleting it (a pattern several Quest tests use) landed in the window before that tick, so create_update_stream kept rejecting the recreate with 409 even though the tombstone was already gone. It now re-checks the durable tombstone before trusting the in-memory flag, and clears the flag itself when the tombstone turns out to already be cleared. Also addresses two new CodeRabbit findings on the delete/create race: mark_deleting() plus the tombstone write could interleave with a concurrent create_update_stream on the same node, since only the query node's put_stream held CREATE_STREAM_LOCK and DELETE never did. Standalone and ingestor put_stream/delete had no lock at all. All three now hold the same lock across the is_deleting()-check-through- tombstone-write window.
1 parent eba77ac commit b0b0f65

5 files changed

Lines changed: 144 additions & 73 deletions

File tree

‎src/handlers/http/logstream.rs‎

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,52 +50,72 @@ use itertools::Itertools;
5050
use serde_json::{Value, json};
5151
use std::fs;
5252
use std::sync::Arc;
53+
use tokio::sync::Mutex;
5354
use tracing::{Instrument, warn};
5455

56+
// Shared between put_stream and delete: without it, a concurrent create/
57+
// update could read is_deleting()=false right before delete() flips it and
58+
// writes the tombstone, then go on to write fresh stream data that the
59+
// background deletion job -- already committed to running by that point --
60+
// would sweep up from underneath it.
61+
pub static CREATE_STREAM_LOCK: Mutex<()> = Mutex::const_new(());
62+
5563
pub async fn delete(
5664
req: HttpRequest,
5765
logstream: Path<String>,
5866
) -> Result<impl Responder, StreamError> {
5967
let stream_name = logstream.into_inner();
6068
// Error out if stream doesn't exist in memory, or in the case of query node, in storage as well
6169
let tenant_id = get_tenant_id_from_request(&req);
62-
if !PARSEABLE
63-
.check_or_load_stream(&stream_name, &tenant_id)
64-
.await
65-
{
66-
return Err(StreamNotFound(stream_name).into());
67-
}
6870

6971
// Fetched once, up front: every step below this point is either
7072
// infallible or best-effort, so nothing after this line can bail out
7173
// with "stream not found" partway through an already-durably-started
7274
// deletion.
73-
let stream = PARSEABLE.get_stream(&stream_name, &tenant_id)?;
75+
let stream = {
76+
// Scoped to just this check-through-tombstone-write window:
77+
// everything after is already guarded by is_deleting()/
78+
// is_tombstoned() checks on the read/write paths.
79+
let _guard = CREATE_STREAM_LOCK.lock().await;
80+
81+
if !PARSEABLE
82+
.check_or_load_stream(&stream_name, &tenant_id)
83+
.await
84+
{
85+
return Err(StreamNotFound(stream_name).into());
86+
}
7487

75-
// Flip the in-memory guard before any `.await` point: check_or_load_stream's
76-
// resident-stream fast path doesn't itself consult is_tombstoned, so a
77-
// concurrent request on this node could otherwise slip through in the
78-
// window between the tombstone becoming durable and this flag being set.
79-
stream.mark_deleting();
88+
let stream = PARSEABLE.get_stream(&stream_name, &tenant_id)?;
89+
90+
// Flip the in-memory guard before any `.await` point: check_or_load_stream's
91+
// resident-stream fast path doesn't itself consult is_tombstoned, so a
92+
// concurrent request on this node could otherwise slip through in the
93+
// window between the tombstone becoming durable and this flag being set.
94+
stream.mark_deleting();
95+
96+
let objectstore = PARSEABLE.storage.get_object_store();
97+
98+
// Durable marker first: if the process crashes anywhere after this
99+
// point, restart-recovery resumes the deletion instead of silently
100+
// leaving the stream half-deleted with no record of it.
101+
if let Err(e) = objectstore
102+
.put_object(
103+
&tombstone_path(&stream_name, &tenant_id),
104+
to_bytes(&()),
105+
&tenant_id,
106+
)
107+
.await
108+
{
109+
// Nothing durable happened -- undo the in-memory flag so the stream
110+
// isn't left permanently blocked by a transient write failure.
111+
stream.clear_deleting();
112+
return Err(e.into());
113+
}
80114

81-
let objectstore = PARSEABLE.storage.get_object_store();
115+
stream
116+
};
82117

83-
// Durable marker first: if the process crashes anywhere after this
84-
// point, restart-recovery resumes the deletion instead of silently
85-
// leaving the stream half-deleted with no record of it.
86-
if let Err(e) = objectstore
87-
.put_object(
88-
&tombstone_path(&stream_name, &tenant_id),
89-
to_bytes(&()),
90-
&tenant_id,
91-
)
92-
.await
93-
{
94-
// Nothing durable happened -- undo the in-memory flag so the stream
95-
// isn't left permanently blocked by a transient write failure.
96-
stream.clear_deleting();
97-
return Err(e.into());
98-
}
118+
let objectstore = PARSEABLE.storage.get_object_store();
99119

100120
// Best-effort: makes the stream vanish from listings almost
101121
// immediately. Not fatal if it fails -- is_deleting()/is_tombstoned()
@@ -259,6 +279,7 @@ pub async fn put_stream(
259279
let stream_name = logstream.into_inner();
260280
let tenant_id = get_tenant_id_from_request(&req);
261281

282+
let _guard = CREATE_STREAM_LOCK.lock().await;
262283
PARSEABLE
263284
.create_update_stream(req.headers(), &body, &stream_name, &tenant_id)
264285
.await?;

‎src/handlers/http/modal/ingest/ingestor_logstream.rs‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use actix_web::{
2424
web::{Json, Path},
2525
};
2626
use bytes::Bytes;
27+
use tokio::sync::Mutex;
2728
use tracing::warn;
2829

2930
use crate::option::Mode;
@@ -34,6 +35,12 @@ use crate::{
3435
utils::get_tenant_id_from_request,
3536
};
3637

38+
// Shared between put_stream and delete: without it, a concurrent create/
39+
// update could read is_deleting()=false right before delete() flips it,
40+
// then go on to write fresh stream data into a directory delete() is about
41+
// to (or just did) remove.
42+
static CREATE_STREAM_LOCK: Mutex<()> = Mutex::const_new(());
43+
3744
pub async fn retention_cleanup(
3845
req: HttpRequest,
3946
stream_name: Path<String>,
@@ -75,9 +82,14 @@ pub async fn delete(
7582
) -> Result<impl Responder, StreamError> {
7683
let stream_name = stream_name.into_inner();
7784
let tenant_id = get_tenant_id_from_request(&req);
78-
// Delete from staging
79-
let stream_dir = PARSEABLE.get_stream(&stream_name, &tenant_id)?;
80-
stream_dir.mark_deleting();
85+
86+
let stream_dir = {
87+
let _guard = CREATE_STREAM_LOCK.lock().await;
88+
// Delete from staging
89+
let stream_dir = PARSEABLE.get_stream(&stream_name, &tenant_id)?;
90+
stream_dir.mark_deleting();
91+
stream_dir
92+
};
8193

8294
// delete staging only for ingest server or standalone server
8395
// else skip
@@ -110,6 +122,7 @@ pub async fn put_stream(
110122
) -> Result<impl Responder, StreamError> {
111123
let stream_name = stream_name.into_inner();
112124
let tenant_id = get_tenant_id_from_request(&req);
125+
let _guard = CREATE_STREAM_LOCK.lock().await;
113126
PARSEABLE
114127
.create_update_stream(req.headers(), &body, &stream_name, &tenant_id)
115128
.await?;

‎src/handlers/http/modal/query/querier_logstream.rs‎

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,48 +63,65 @@ pub async fn delete(
6363
) -> Result<impl Responder, StreamError> {
6464
let stream_name = stream_name.into_inner();
6565
let tenant_id = get_tenant_id_from_request(&req);
66-
// if the stream not found in memory map,
67-
//check if it exists in the storage
68-
//create stream and schema from storage
69-
if !PARSEABLE.streams.contains(&stream_name, &tenant_id)
70-
&& !PARSEABLE
71-
.create_stream_and_schema_from_storage(&stream_name, &tenant_id)
72-
.await
73-
.unwrap_or(false)
74-
{
75-
return Err(StreamNotFound(stream_name.clone()).into());
76-
}
7766

7867
// Fetched once, up front: every step below this point is either
7968
// infallible or best-effort, so nothing after this line can bail out
8069
// with "stream not found" partway through an already-durably-started
8170
// deletion.
82-
let stream = PARSEABLE.get_stream(&stream_name, &tenant_id)?;
71+
let stream = {
72+
// Shared with put_stream: without it, a concurrent create/update on
73+
// this node could read is_deleting()=false right before this
74+
// handler flips it and writes the tombstone, then go on to write
75+
// fresh stream data that the background deletion job -- already
76+
// committed to running by that point -- would sweep up from
77+
// underneath it. Scoped to just this check-through-tombstone-write
78+
// window: everything after is already guarded by is_deleting()/
79+
// is_tombstoned() checks on the read/write paths.
80+
let _guard = CREATE_STREAM_LOCK.lock().await;
81+
82+
// if the stream not found in memory map,
83+
//check if it exists in the storage
84+
//create stream and schema from storage
85+
if !PARSEABLE.streams.contains(&stream_name, &tenant_id)
86+
&& !PARSEABLE
87+
.create_stream_and_schema_from_storage(&stream_name, &tenant_id)
88+
.await
89+
.unwrap_or(false)
90+
{
91+
return Err(StreamNotFound(stream_name.clone()).into());
92+
}
8393

84-
// Flip the in-memory guard before any `.await` point: check_or_load_stream's
85-
// resident-stream fast path doesn't itself consult is_tombstoned, so a
86-
// concurrent request on this node could otherwise slip through in the
87-
// window between the tombstone becoming durable and this flag being set.
88-
stream.mark_deleting();
94+
let stream = PARSEABLE.get_stream(&stream_name, &tenant_id)?;
8995

90-
let objectstore = PARSEABLE.storage.get_object_store();
96+
// Flip the in-memory guard before any `.await` point: check_or_load_stream's
97+
// resident-stream fast path doesn't itself consult is_tombstoned, so a
98+
// concurrent request on this node could otherwise slip through in the
99+
// window between the tombstone becoming durable and this flag being set.
100+
stream.mark_deleting();
91101

92-
// Durable marker first: if the process crashes anywhere after this
93-
// point, restart-recovery resumes the deletion instead of silently
94-
// leaving the stream half-deleted with no record of it.
95-
if let Err(e) = objectstore
96-
.put_object(
97-
&tombstone_path(&stream_name, &tenant_id),
98-
to_bytes(&()),
99-
&tenant_id,
100-
)
101-
.await
102-
{
103-
// Nothing durable happened -- undo the in-memory flag so the stream
104-
// isn't left permanently blocked by a transient write failure.
105-
stream.clear_deleting();
106-
return Err(e.into());
107-
}
102+
let objectstore = PARSEABLE.storage.get_object_store();
103+
104+
// Durable marker first: if the process crashes anywhere after this
105+
// point, restart-recovery resumes the deletion instead of silently
106+
// leaving the stream half-deleted with no record of it.
107+
if let Err(e) = objectstore
108+
.put_object(
109+
&tombstone_path(&stream_name, &tenant_id),
110+
to_bytes(&()),
111+
&tenant_id,
112+
)
113+
.await
114+
{
115+
// Nothing durable happened -- undo the in-memory flag so the stream
116+
// isn't left permanently blocked by a transient write failure.
117+
stream.clear_deleting();
118+
return Err(e.into());
119+
}
120+
121+
stream
122+
};
123+
124+
let objectstore = PARSEABLE.storage.get_object_store();
108125

109126
// Best-effort: makes the stream vanish from listings almost
110127
// immediately, without touching every listing endpoint individually.

‎src/parseable/mod.rs‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,27 @@ impl Parseable {
804804
&& let Ok(stream) = self.get_stream(stream_name, tenant_id)
805805
&& stream.is_deleting()
806806
{
807-
return Err(StreamError::Custom {
808-
msg: format!("Logstream {stream_name} is being deleted, please retry shortly"),
809-
status: StatusCode::CONFLICT,
810-
});
807+
// The flag can be stale on a node that never runs the
808+
// background deletion job itself (e.g. an ingestor: see its
809+
// `delete()` handler) -- it only self-heals once
810+
// `sync_all_streams` next notices the tombstone is gone, which
811+
// can lag well behind a client recreating the stream right
812+
// away. Re-check the durable tombstone before trusting the
813+
// in-memory flag, and clear it here instead of blocking
814+
// creation for up to a full sync interval.
815+
if is_tombstoned(
816+
self.storage.get_object_store().as_ref(),
817+
stream_name,
818+
tenant_id,
819+
)
820+
.await?
821+
{
822+
return Err(StreamError::Custom {
823+
msg: format!("Logstream {stream_name} is being deleted, please retry shortly"),
824+
status: StatusCode::CONFLICT,
825+
});
826+
}
827+
stream.clear_deleting();
811828
}
812829

813830
// A tombstoned-but-not-yet-purged stream must be rejected the same

‎src/parseable/streams.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,10 +1632,13 @@ impl Stream {
16321632
self.metadata.write().expect(LOCK_EXPECT).deleting = true;
16331633
}
16341634

1635-
/// Undoes a `mark_deleting()` call whose tombstone write then failed.
1636-
/// Safe only because nothing durable was ever created -- unlike
1635+
/// Undoes a `mark_deleting()` call, in one of two cases: the tombstone
1636+
/// write that would have made it durable just failed, or the caller has
1637+
/// independently confirmed (via `is_tombstoned()`) that the tombstone is
1638+
/// already gone and this flag is just a stale leftover on a node that
1639+
/// doesn't run the background deletion job itself. Unlike
16371640
/// `mark_deleting()`, this is not part of the monotonic contract, and
1638-
/// must never be called once the tombstone actually exists in storage.
1641+
/// must never be called while the tombstone still exists in storage.
16391642
pub fn clear_deleting(&self) {
16401643
self.metadata.write().expect(LOCK_EXPECT).deleting = false;
16411644
}

0 commit comments

Comments
 (0)