reifydb-transaction 0.4.13

Transaction management and concurrency control for ReifyDB
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

// This file includes and modifies code from the skipdb project (https://github.com/al8n/skipdb),
// originally licensed under the Apache License, Version 2.0.
// Original copyright:
//   Copyright (c) 2024 Al Liu
//
// The original Apache License can be found at:
//   http://www.apache.org/licenses/LICENSE-2.0

use std::{
	fmt,
	fmt::Debug,
	sync::{
		Arc,
		atomic::{AtomicU64, Ordering},
	},
	time::Duration,
};

use reifydb_core::{actors::watermark::WatermarkMessage, common::CommitVersion};
use reifydb_runtime::{
	actor::{mailbox::ActorRef, system::ActorSystem},
	sync::waiter::WaiterHandle,
};
use tracing::instrument;

use super::actor::{WatermarkActor, WatermarkShared};

/// WaterMark is used to keep track of the minimum un-finished index. Typically,
/// an index k becomes finished or "done" according to a WaterMark once
/// `done(k)` has been called
///  1. as many times as `begin(k)` has, AND
///  2. a positive number of times.
pub struct WaterMark {
	actor: ActorRef<WatermarkMessage>,
	shared: Arc<WatermarkShared>,
}

impl Debug for WaterMark {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("WaterMark")
			.field("done_until", &self.shared.done_until.load(Ordering::Relaxed))
			.field("last_index", &self.shared.last_index.load(Ordering::Relaxed))
			.finish()
	}
}

impl WaterMark {
	/// Create a new WaterMark with given name and actor system.
	#[instrument(name = "transaction::watermark::new", level = "debug", skip(system), fields(task_name = %task_name))]
	pub fn new(task_name: String, system: &ActorSystem) -> Self {
		let shared = Arc::new(WatermarkShared {
			done_until: AtomicU64::new(0),
			last_index: AtomicU64::new(0),
		});

		let actor = WatermarkActor {
			shared: shared.clone(),
		};
		let actor_ref = system.spawn(&task_name, actor).actor_ref().clone();

		Self {
			actor: actor_ref,
			shared,
		}
	}

	/// Register `version` as in-flight. Must be paired with a later
	/// `mark_finished(version)` call so `done_until` can advance past it.
	#[instrument(name = "transaction::watermark::register_in_flight", level = "trace", skip(self), fields(version = version.0))]
	pub fn register_in_flight(&self, version: CommitVersion) {
		self.shared.last_index.fetch_max(version.0, Ordering::SeqCst);

		let _ = self.actor.send(WatermarkMessage::Begin {
			version: version.0,
		});
	}

	/// Mark `version` as finished. Pairs with an earlier
	/// `register_in_flight(version)`. `done_until` advances when every
	/// version up through some `V` has been marked finished.
	#[instrument(name = "transaction::watermark::mark_finished", level = "trace", skip(self), fields(index = version.0))]
	pub fn mark_finished(&self, version: CommitVersion) {
		let _ = self.actor.send(WatermarkMessage::Done {
			version: version.0,
		});
	}

	/// Returns the maximum index that has the property that all indices
	/// less than or equal to it are done.
	pub fn done_until(&self) -> CommitVersion {
		CommitVersion(self.shared.done_until.load(Ordering::SeqCst))
	}

	/// Returns the last index that was begun.
	pub fn last_index(&self) -> CommitVersion {
		CommitVersion(self.shared.last_index.load(Ordering::SeqCst))
	}

	/// Advance the watermark to the given version for replica replication.
	///
	/// Directly sets `done_until` and `last_index` atomically, bypassing the
	/// gap-based watermark logic. This is correct for replicas because they
	/// apply entries sequentially with no concurrent transactions, and the
	/// primary's version space is independent from the replica's.
	pub fn advance_to(&self, version: CommitVersion) {
		self.shared.last_index.fetch_max(version.0, Ordering::SeqCst);
		self.shared.done_until.fetch_max(version.0, Ordering::SeqCst);
	}

	/// Waits until the given index is marked as done with a default
	/// timeout.
	pub fn wait_for_mark(&self, index: u64) {
		self.wait_for_mark_timeout(CommitVersion(index), Duration::from_secs(30));
	}

	/// Waits until the given index is marked as done with a specified
	/// timeout.
	// #[cfg(feature = "native")]
	pub fn wait_for_mark_timeout(&self, index: CommitVersion, timeout: Duration) -> bool {
		let current_done = self.shared.done_until.load(Ordering::SeqCst);
		if current_done >= index.0 {
			return true;
		}

		let waiter = Arc::new(WaiterHandle::new());

		if self.actor
			.send(WatermarkMessage::WaitFor {
				version: index.0,
				waiter: waiter.clone(),
			})
			.is_err()
		{
			// Actor stopped
			return false;
		}

		// Wait with timeout using condvar

		waiter.wait_timeout(timeout)
	}
}

#[cfg(test)]
pub mod tests {
	use std::{sync::atomic::AtomicUsize, thread, thread::sleep, time::Duration};

	use reifydb_runtime::{actor::system::ActorSystem, context::clock::Clock, pool::Pools};

	use super::*;
	use crate::multi::watermark::OLD_VERSION_THRESHOLD;

	#[test]
	fn test_basic() {
		init_and_close(|_| {});
	}

	#[test]
	fn test_begin_done() {
		init_and_close(|watermark| {
			watermark.register_in_flight(CommitVersion(1));
			watermark.register_in_flight(CommitVersion(2));
			watermark.register_in_flight(CommitVersion(3));

			watermark.mark_finished(CommitVersion(1));
			watermark.mark_finished(CommitVersion(2));
			watermark.mark_finished(CommitVersion(3));
		});
	}

	#[test]
	fn test_wait_for_mark() {
		init_and_close(|watermark| {
			watermark.register_in_flight(CommitVersion(1));
			watermark.register_in_flight(CommitVersion(2));
			watermark.register_in_flight(CommitVersion(3));

			watermark.mark_finished(CommitVersion(2));
			watermark.mark_finished(CommitVersion(3));

			assert_eq!(watermark.done_until(), 0);

			watermark.mark_finished(CommitVersion(1));
			watermark.wait_for_mark(1);
			watermark.wait_for_mark(3);
			assert_eq!(watermark.done_until(), 3);
		});
	}

	#[test]
	fn test_done_until() {
		init_and_close(|watermark| {
			watermark.shared.done_until.store(1, Ordering::SeqCst);
			assert_eq!(watermark.done_until(), 1);
		});
	}

	#[test]
	fn test_high_concurrency() {
		let system = ActorSystem::new(Pools::default(), Clock::Real);
		let watermark = Arc::new(WaterMark::new("concurrent".into(), &system));

		const NUM_TASKS: usize = 50;
		const OPS_PER_TASK: usize = 100;

		let mut handles = vec![];

		// Spawn tasks that perform concurrent begin/done operations
		for task_id in 0..NUM_TASKS {
			let wm = watermark.clone();
			let handle = thread::spawn(move || {
				for i in 0..OPS_PER_TASK {
					let version = CommitVersion((task_id * OPS_PER_TASK + i) as u64 + 1);
					wm.register_in_flight(version);
					wm.mark_finished(version);
				}
			});
			handles.push(handle);
		}

		for handle in handles {
			handle.join().unwrap();
		}

		sleep(Duration::from_millis(100));

		// Verify the watermark progressed
		let final_done = watermark.done_until();
		assert!(final_done.0 > 0, "Watermark should have progressed");

		system.shutdown();
		sleep(Duration::from_millis(150)); // Wait for actor to stop
	}

	#[test]
	fn test_concurrent_wait_for_mark() {
		let system = ActorSystem::new(Pools::default(), Clock::Real);
		let watermark = Arc::new(WaterMark::new("wait_concurrent".into(), &system));
		let success_count = Arc::new(AtomicUsize::new(0));

		// Start some versions
		for i in 1..=10 {
			watermark.register_in_flight(CommitVersion(i));
		}

		let mut handles = vec![];

		// Spawn tasks that wait for marks
		for version in 1..=10 {
			let wm = watermark.clone();
			let counter = success_count.clone();
			let handle = thread::spawn(move || {
				// Use timeout to avoid hanging if something goes wrong
				if wm.wait_for_mark_timeout(CommitVersion(version), Duration::from_secs(5)) {
					counter.fetch_add(1, Ordering::Relaxed);
				}
			});
			handles.push(handle);
		}

		// Give tasks time to start waiting
		sleep(Duration::from_millis(50));

		// Complete the versions
		for i in 1..=10 {
			watermark.mark_finished(CommitVersion(i));
		}

		for handle in handles {
			handle.join().unwrap();
		}

		// All waits should have succeeded
		assert_eq!(success_count.load(Ordering::Relaxed), 10);

		system.shutdown();
		sleep(Duration::from_millis(150)); // Wait for actor to stop
	}

	#[test]
	fn test_old_version_rejection() {
		init_and_close(|watermark| {
			// Advance done_until significantly
			for i in 1..=100 {
				watermark.register_in_flight(CommitVersion(i));
				watermark.mark_finished(CommitVersion(i));
			}

			let reached = watermark.wait_for_mark_timeout(CommitVersion(100), Duration::from_secs(5));
			assert!(reached, "Should have processed all 100 versions");
			let done_until = watermark.done_until();

			// Try to wait for a very old version (should return immediately)
			let very_old = done_until.0.saturating_sub(OLD_VERSION_THRESHOLD + 10);
			let clock = Clock::Real;
			let start = clock.instant();
			watermark.wait_for_mark(very_old);
			let elapsed = start.elapsed();

			// Should return almost immediately (< 10ms)
			assert!(elapsed.as_millis() < 10, "Old version wait should return immediately");
		});
	}

	#[test]
	fn test_timeout_behavior() {
		init_and_close(|watermark| {
			// Begin but don't complete a version
			watermark.register_in_flight(CommitVersion(1));

			// Wait with short timeout
			let clock = Clock::Real;
			let start = clock.instant();
			let result = watermark.wait_for_mark_timeout(CommitVersion(1), Duration::from_millis(100));
			let elapsed = start.elapsed();

			// Should timeout and return false
			assert!(!result, "Should timeout waiting for uncompleted version");
			assert!(
				elapsed.as_millis() >= 100 && elapsed.as_millis() < 200,
				"Should respect timeout duration"
			);
		});
	}

	#[test]
	fn test_out_of_order_begin() {
		// Test that begin() calls can arrive out of order with gap-tolerant processing
		init_and_close(|watermark| {
			// Begin versions out of order
			watermark.register_in_flight(CommitVersion(3));
			watermark.register_in_flight(CommitVersion(1));
			watermark.register_in_flight(CommitVersion(2));

			// Complete in order
			watermark.mark_finished(CommitVersion(1));
			watermark.mark_finished(CommitVersion(2));
			watermark.mark_finished(CommitVersion(3));

			let reached = watermark.wait_for_mark_timeout(CommitVersion(3), Duration::from_secs(5));
			assert!(reached, "Timed out waiting for watermark to advance to 3");
			let done = watermark.done_until();
			assert_eq!(done.0, 3, "Watermark should advance to 3, got {}", done.0);
		});
	}

	#[test]
	fn test_orphaned_done_before_begin() {
		// Test that done() arriving before begin() is handled correctly
		init_and_close(|watermark| {
			// done() arrives before begin() - this is an "orphaned" done
			watermark.mark_finished(CommitVersion(1));

			// Wait a bit for processing
			sleep(Duration::from_millis(20));

			// Watermark should NOT advance yet (begin hasn't arrived)
			assert_eq!(watermark.done_until().0, 0);

			// Now begin() arrives
			watermark.register_in_flight(CommitVersion(1));

			// Wait for processing
			sleep(Duration::from_millis(50));

			// Now watermark should advance
			let done = watermark.done_until();
			assert_eq!(done.0, 1, "Watermark should advance to 1 after begin, got {}", done.0);
		});
	}

	#[test]
	fn test_mixed_out_of_order() {
		// Test complex out-of-order scenario
		init_and_close(|watermark| {
			// Interleaved begin/done in various orders
			watermark.register_in_flight(CommitVersion(2));
			watermark.mark_finished(CommitVersion(3)); // orphaned
			watermark.register_in_flight(CommitVersion(1));
			watermark.mark_finished(CommitVersion(1));
			watermark.register_in_flight(CommitVersion(3));
			watermark.mark_finished(CommitVersion(2));

			let reached = watermark.wait_for_mark_timeout(CommitVersion(3), Duration::from_secs(5));
			assert!(reached, "Timed out waiting for watermark to advance to 3");
			let done = watermark.done_until();
			assert_eq!(done.0, 3, "Watermark should advance to 3, got {}", done.0);
		});
	}

	fn init_and_close<F>(f: F)
	where
		F: FnOnce(Arc<WaterMark>),
	{
		let system = ActorSystem::new(Pools::default(), Clock::Real);
		let watermark = Arc::new(WaterMark::new("watermark".into(), &system));

		f(watermark);

		sleep(Duration::from_millis(10));
		system.shutdown();
		sleep(Duration::from_millis(150)); // Wait for actor to stop
	}
}