Skip to content

Commit a2d9525

Browse files
committed
feat: auto-compaction for distributed 2PC durable log
Adds set_auto_compact_threshold(N) to DistributedTransactionManager. When N > 0, the manager fires txn_log_->compact() after every Nth successful transaction completion, keeping the WAL file bounded without operator intervention. Threshold 0 (default) preserves the existing manual-compaction behavior. Compaction only fires in maybe_log_complete(), i.e. after a fully successful phase 2. In-doubt transactions (phase-2 failure) are untouched and still recoverable on startup.
1 parent c1bda13 commit a2d9525

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

include/sql_engine/distributed_txn.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ class DistributedTransactionManager : public TransactionManager {
7878
phase_statement_timeout_ms_ = ms;
7979
}
8080

81+
// Auto-compact the durable log every N successful completions. When > 0,
82+
// the manager calls txn_log_->compact() after every Nth completed txn.
83+
// 0 (default) = disabled. Callers should pick a value balancing compaction
84+
// cost vs. log growth (e.g., 100-10000 depending on txn rate).
85+
void set_auto_compact_threshold(uint32_t n) {
86+
auto_compact_threshold_ = n;
87+
}
88+
8189
bool begin() override {
8290
txn_id_ = generate_txn_id();
8391
participants_.clear();
@@ -277,6 +285,10 @@ class DistributedTransactionManager : public TransactionManager {
277285
bool require_durable_log_ = false;
278286
uint32_t phase_statement_timeout_ms_ = 0;
279287

288+
// Auto-compaction: count completions, fire compact when counter hits threshold.
289+
uint32_t auto_compact_threshold_ = 0;
290+
uint32_t completions_since_compact_ = 0;
291+
280292
// Best-effort: set a per-session statement timeout on a backend before
281293
// issuing a phase-1 or phase-2 SQL. Returns true if the SET succeeded
282294
// OR if no timeout is configured; false only if the SET itself fails
@@ -326,7 +338,15 @@ class DistributedTransactionManager : public TransactionManager {
326338
}
327339

328340
void maybe_log_complete() {
329-
if (txn_log_) txn_log_->log_complete(txn_id_);
341+
if (!txn_log_) return;
342+
txn_log_->log_complete(txn_id_);
343+
if (auto_compact_threshold_ > 0) {
344+
++completions_since_compact_;
345+
if (completions_since_compact_ >= auto_compact_threshold_) {
346+
completions_since_compact_ = 0;
347+
txn_log_->compact();
348+
}
349+
}
330350
}
331351

332352
// Generate a unique transaction ID.

0 commit comments

Comments
 (0)