1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: BUSL-1.1
//! Write-batch coalescing: amortizes redb fsync across consecutive PointPut tasks.
use tracing::debug;
use crate::bridge::envelope::{ErrorCode, PhysicalPlan};
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::handlers::point::apply_put::PointPutParams;
use crate::data::executor::task::ExecutionTask;
use nodedb_physical::physical_plan::DocumentOp;
impl CoreLoop {
/// Batch-coalesce consecutive PointPut tasks from the front of the task queue.
///
/// Opens ONE redb WriteTransaction, executes all PointPuts within it,
/// commits once, and sends individual responses. This amortizes the
/// fsync cost across N writes instead of paying it per-write.
///
/// Returns the number of tasks processed (0 if the front of the queue
/// is not a batchable PointPut, in which case the caller should fall
/// back to `poll_one`).
pub fn poll_write_batch(&mut self) -> usize {
// Check if the front of the queue is a non-expired PointPut.
let front_is_put = self.task_queue.front().is_some_and(|t| {
matches!(
t.plan(),
PhysicalPlan::Document(DocumentOp::PointPut { .. })
) && !t.is_expired()
});
if !front_is_put {
return 0;
}
// Collect consecutive non-expired PointPuts (max 64).
let mut batch: Vec<ExecutionTask> = Vec::with_capacity(64);
while batch.len() < 64 {
let is_put = self.task_queue.front().is_some_and(|t| {
matches!(
t.plan(),
PhysicalPlan::Document(DocumentOp::PointPut { .. })
) && !t.is_expired()
});
if !is_put {
break;
}
if let Some(task) = self.task_queue.pop_front() {
batch.push(task);
} else {
break;
}
}
// Single write: no batching benefit, let poll_one handle it
// (poll_one also handles idempotency cache and other bookkeeping).
if batch.len() <= 1 {
for t in batch.into_iter().rev() {
self.task_queue.push_front(t);
}
return 0;
}
// Open ONE transaction for the entire batch.
let txn = match self.sparse.begin_write() {
Ok(t) => t,
Err(_) => {
// Can't open txn — put tasks back, let poll_one handle individually.
for t in batch.into_iter().rev() {
self.task_queue.push_front(t);
}
return 0;
}
};
// Execute each PointPut within the shared transaction.
// Track per-task success/failure for individual responses, and
// capture the prior stored bytes per row so the Event Plane emit
// below can resolve Insert vs Update from the actual mutation.
let mut results: Vec<Result<Option<Vec<u8>>, crate::bridge::envelope::Response>> =
Vec::with_capacity(batch.len());
for task in &batch {
let PhysicalPlan::Document(DocumentOp::PointPut {
collection,
document_id: _,
value,
surrogate,
pk_bytes: _,
}) = task.plan()
else {
unreachable!("batch only contains PointPut");
};
let tid = task.request.tenant_id.as_u64();
let db_id = task.request.database_id.as_u64();
let row_key = crate::engine::document::store::surrogate_to_doc_id(*surrogate);
results.push(
self.apply_point_put(
&txn,
PointPutParams {
database_id: db_id,
tid,
collection,
document_id: &row_key,
surrogate: *surrogate,
value,
},
)
.map_err(|e| {
self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
)
}),
);
}
// If ANY write failed hard (document put error), abort the batch.
let any_hard_failure = results.iter().any(|r| r.is_err());
if any_hard_failure {
// Don't commit — transaction is dropped (implicit rollback).
// Send error responses for failed tasks, put successful ones back.
let count = batch.len();
for (task, result) in batch.into_iter().zip(results) {
let response = match result {
Err(err_response) => err_response,
Ok(_) => self.response_error(
&task,
ErrorCode::Internal {
detail: "batch aborted due to sibling failure".into(),
},
),
};
let _ = self
.response_tx
.try_push(crate::bridge::dispatch::BridgeResponse { inner: response });
}
return count;
}
// Commit once for all writes.
let commit_result = txn.commit();
let count = batch.len();
for (task, result) in batch.iter().zip(results.iter()) {
let response = match &commit_result {
Ok(()) => {
// Emit write event for each successful batched PointPut.
// The Insert vs Update tag is derived from the prior
// bytes captured per row above.
if let PhysicalPlan::Document(DocumentOp::PointPut {
collection,
document_id,
value,
..
}) = task.plan()
{
let tid = task.request.tenant_id.as_u64();
let prior = match result {
Ok(p) => p.as_deref(),
Err(_) => None,
};
self.emit_put_event(task, tid, collection, document_id, value, prior);
}
self.response_ok(task)
}
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: format!("batch commit: {e}"),
},
),
};
// Record idempotency key.
if let Some(key) = task.request.idempotency_key {
let succeeded = response.status == crate::bridge::envelope::Status::Ok;
if self.idempotency_cache.len() >= 16_384
&& let Some(oldest) = self.idempotency_order.pop_front()
{
self.idempotency_cache.remove(&oldest);
}
self.idempotency_cache.insert(key, succeeded);
self.idempotency_order.push_back(key);
}
let _ = self
.response_tx
.try_push(crate::bridge::dispatch::BridgeResponse { inner: response });
}
if commit_result.is_ok() {
debug!(core = self.core_id, count, "write batch committed");
}
count
}
}