d-engine-server 0.2.3

Production-ready Raft consensus engine server and runtime
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
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Integration tests for ReadIndex Batching (Ticket #236)
//!
//! These tests verify that batching multiple linearizable read requests
//! improves throughput by sharing a single verify_leadership() call.
//!
//! Uses embedded mode with EmbeddedEngine for production-ready testing.

use std::time::Duration;

use bytes::Bytes;
use d_engine_server::EmbeddedEngine;
use tempfile::TempDir;
use tokio::time::Instant;

use tracing_test::traced_test;

use crate::common::get_available_ports;

/// Helper to create a test EmbeddedEngine with batching config
async fn create_engine_with_batching(
    test_name: &str,
    size_threshold: usize,
) -> (EmbeddedEngine, TempDir) {
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join(test_name);

    let config_path = temp_dir.path().join("d-engine.toml");
    let mut port_guard = get_available_ports(1).await;
    port_guard.release_listeners();
    let port = port_guard.as_slice()[0];
    let config_content = format!(
        r#"
[cluster]
listen_address = "127.0.0.1:{}"
db_root_dir = "{}"
single_node = true

[raft.read_consistency]
state_machine_sync_timeout_ms = 5000

[raft.read_consistency.read_batching]
size_threshold = {}
"#,
        port,
        db_path.display(),
        size_threshold,
    );
    std::fs::write(&config_path, config_content).expect("Failed to write config");

    let engine = EmbeddedEngine::start_with(config_path.to_str().unwrap())
        .await
        .expect("Failed to start engine");

    engine.wait_ready(Duration::from_secs(5)).await.expect("Engine not ready");

    (engine, temp_dir)
}

/// Test 2.1: Linearizability verification with batched reads
///
/// # Test Scenario
/// Verifies that batched linearizable reads return correct committed data
/// and maintain linearizability guarantees.
///
/// # Given
/// - Single-node cluster with batching enabled (size_threshold=50)
/// - Keys key1=v1, key2=v2 written and committed
///
/// # When
/// - Send 100 concurrent linearizable read requests (50 read key1, 50 read key2)
/// - Requests are batched together (sharing single verify_leadership call)
///
/// # Then
/// - All 100 requests return correct values (v1 or v2)
/// - No requests return empty/stale data
/// - All requests complete successfully
#[traced_test]
#[tokio::test]
async fn test_batching_preserves_linearizability() {
    let (engine, _temp_dir) = create_engine_with_batching("test_linearizability", 50).await;

    // Write test data
    let client = engine.client();
    client
        .put(Bytes::from("key1"), Bytes::from("v1"))
        .await
        .expect("Failed to write key1");
    client
        .put(Bytes::from("key2"), Bytes::from("v2"))
        .await
        .expect("Failed to write key2");

    // Wait for commit
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Send 100 concurrent linearizable reads
    let mut handles = vec![];
    let base_client = engine.client();
    for i in 0..100 {
        let client = base_client.clone();
        let key = if i < 50 {
            Bytes::from("key1")
        } else {
            Bytes::from("key2")
        };
        let expected = if i < 50 {
            Bytes::from("v1")
        } else {
            Bytes::from("v2")
        };

        let handle = tokio::spawn(async move {
            let result = client
                .get_linearizable(&key)
                .await
                .expect("Read failed")
                .expect("Key should exist");
            assert_eq!(result, expected, "Should return correct value");
        });
        handles.push(handle);
    }

    // Wait for all reads to complete
    for handle in handles {
        handle.await.expect("Task failed");
    }
}

/// Test 2.2: Concurrent write-read verification
///
/// # Test Scenario
/// Verifies that batched reads after a write always see the committed value,
/// ensuring commitIndex synchronization works correctly.
///
/// # Given
/// - Single-node cluster with batching enabled
///
/// # When
/// - Write key1=v1 and wait for commit
/// - Immediately send 50 concurrent linearizable reads for key1
///
/// # Then
/// - All 50 requests return v1 (not empty/stale)
/// - Requests complete within reasonable time (<500ms)
#[traced_test]
#[tokio::test]
async fn test_concurrent_write_and_read() {
    let (engine, _temp_dir) = create_engine_with_batching("test_concurrent_write", 50).await;

    let client = engine.client();

    // Write key1=v1
    client
        .put(Bytes::from("key1"), Bytes::from("v1"))
        .await
        .expect("Failed to write key1");

    // Wait for commit
    tokio::time::sleep(Duration::from_millis(100)).await;

    let start = Instant::now();

    // Immediately send 50 concurrent reads
    let mut handles = vec![];
    let base_client = engine.client();
    for _i in 0..100 {
        let client = base_client.clone();
        let handle = tokio::spawn(async move {
            let result = client
                .get_linearizable(b"key1")
                .await
                .expect("Read failed")
                .expect("Key should exist");
            assert_eq!(
                result,
                Bytes::from("v1"),
                "Should read latest committed value"
            );
        });
        handles.push(handle);
    }

    // Wait for all reads
    for handle in handles {
        handle.await.expect("Task failed");
    }

    let elapsed = start.elapsed();
    assert!(
        elapsed < Duration::from_millis(500),
        "Batched reads should complete quickly, took {elapsed:?}"
    );

    // Cleanup
    engine.stop().await.expect("Failed to stop engine");
}

/// Test 2.3: Single request timeout trigger (CRITICAL BOUNDARY CASE)
///
/// # Test Scenario
/// Verifies that a single linearizable read request completes successfully
/// in a single-node cluster, ensuring batching doesn't block requests.
///
/// # Given
/// - Single-node cluster with batching (size_threshold=50, time_threshold=10ms)
///
/// # When
/// - Send 1 linearizable read request (far below size threshold)
///
/// # Then
/// - Request completes quickly (single-node fast path: quorum=1)
/// - Request does NOT hang forever
/// - Returns correct value
///
/// # Note
/// Single-node clusters have optimized quorum verification (only leader confirms),
/// so requests complete in microseconds, not milliseconds. This is correct behavior.
#[traced_test]
#[tokio::test]
async fn test_single_request_timeout_trigger() {
    let (engine, _temp_dir) = create_engine_with_batching("test_timeout", 50).await;

    let client = engine.client();

    // Write test data
    client
        .put(Bytes::from("lonely_key"), Bytes::from("lonely_value"))
        .await
        .expect("Failed to write");

    tokio::time::sleep(Duration::from_millis(100)).await;

    let start = Instant::now();

    // Send single request
    let result = client
        .get_linearizable(b"lonely_key")
        .await
        .expect("Read failed")
        .expect("Key should exist");

    let elapsed = start.elapsed();

    // Verify result
    assert_eq!(result, Bytes::from("lonely_value"));

    // Verify fast completion (single-node optimization)
    // Single-node clusters don't need to wait for followers, so they complete quickly
    assert!(
        elapsed < Duration::from_millis(100),
        "Should not hang, took {elapsed:?}"
    );

    println!("Single request completed in {elapsed:?} (single-node fast path verified)");

    // Cleanup
    engine.stop().await.expect("Failed to stop engine");
}

/// Test 2.4: Size threshold immediate flush (HIGH CONCURRENCY CASE)
///
/// # Test Scenario
/// Verifies that reaching size_threshold triggers immediate flush
/// without waiting for timeout.
///
/// # Given
/// - Single-node cluster with batching (size_threshold=50, time_threshold=10ms)
///
/// # When
/// - Rapidly send 50 concurrent linearizable reads
///
/// # Then
/// - 50th request triggers immediate flush
/// - Flush latency < 5ms (proves size trigger, not time trigger)
/// - All 50 requests succeed
#[traced_test]
#[tokio::test]
async fn test_size_threshold_immediate_flush() {
    let (engine, _temp_dir) = create_engine_with_batching("test_performance", 100).await;

    let client = engine.client();

    // Write test data
    for i in 0..50 {
        client
            .put(Bytes::from(format!("key{i}")), Bytes::from(format!("v{i}")))
            .await
            .expect("Failed to write");
    }

    tokio::time::sleep(Duration::from_millis(100)).await;

    let start = Instant::now();

    // Send 50 concurrent reads
    let mut handles = vec![];
    let base_client = engine.client();
    for i in 0..20 {
        let client = base_client.clone();
        let key = format!("key{i}");
        let handle = tokio::spawn(async move {
            client.get_linearizable(key.as_bytes()).await.expect("Read failed")
        });
        handles.push(handle);
    }

    // Wait for all reads
    for handle in handles {
        handle.await.expect("Task failed");
    }

    let elapsed = start.elapsed();

    // Size trigger should flush immediately (< 5ms)
    // Time trigger would wait 10ms
    assert!(
        elapsed < Duration::from_millis(50),
        "Size threshold should trigger immediate flush, took {elapsed:?}"
    );

    println!("50 batched reads completed in {elapsed:?} (size threshold verified)");
}

/// Test 2.8: Throughput regression test (batching doesn't degrade performance)
///
/// # Test Scenario
/// Verifies that batching doesn't degrade performance compared to baseline.
/// In single-node embedded mode, batching advantage is limited since
/// verify_leadership() has no network overhead.
///
/// # Given
/// - Two test runs with same workload (1000 concurrent reads)
/// - Run 1: batching disabled (baseline)
/// - Run 2: batching enabled
///
/// # Then
/// - Run 2 throughput >= Run 1 throughput (no regression)
/// - Log actual improvement for monitoring
#[traced_test]
#[tokio::test]
async fn test_batching_throughput_improvement() {
    // Skip benchmark in non-CI environments to save time
    if std::env::var("CI").is_ok() {
        println!("Skipping benchmark (set CI=1 to run)");
        return;
    }

    const NUM_REQUESTS: usize = 1000;

    // Run 1: Batching disabled (set size_threshold very high)
    let (node1, _temp_dir1) = create_engine_with_batching("test_throughput_off", 10000).await;

    let client1 = node1.client();
    client1
        .put(Bytes::from("bench_key"), Bytes::from("bench_value"))
        .await
        .expect("Failed to write");
    tokio::time::sleep(Duration::from_millis(100)).await;

    let start1 = Instant::now();
    let mut handles1 = vec![];
    let base_client1 = node1.client();
    for _ in 0..NUM_REQUESTS {
        let client = base_client1.clone();
        let handle = tokio::spawn(async move { client.get_linearizable(b"bench_key").await });
        handles1.push(handle);
    }
    for handle in handles1 {
        let _ = handle.await;
    }
    let elapsed1 = start1.elapsed();
    let throughput1 = NUM_REQUESTS as f64 / elapsed1.as_secs_f64();

    node1.stop().await.expect("Failed to stop node1");

    // Run 2: Batching enabled
    let (node2, _temp_dir2) = create_engine_with_batching("test_throughput_on", 50).await;

    let client2 = node2.client();
    client2
        .put(Bytes::from("bench_key"), Bytes::from("bench_value"))
        .await
        .expect("Failed to write");
    tokio::time::sleep(Duration::from_millis(100)).await;

    let start2 = Instant::now();
    let mut handles2 = vec![];
    let base_client2 = node2.client();
    for _ in 0..NUM_REQUESTS {
        let client = base_client2.clone();
        let handle = tokio::spawn(async move { client.get_linearizable(b"bench_key").await });
        handles2.push(handle);
    }
    for handle in handles2 {
        let _ = handle.await;
    }
    let elapsed2 = start2.elapsed();
    let throughput2 = NUM_REQUESTS as f64 / elapsed2.as_secs_f64();

    node2.stop().await.expect("Failed to stop node2");

    println!("\n=== Throughput Regression Test ===");
    println!("Baseline (batching OFF): {throughput1:.0} ops/sec (took {elapsed1:?})");
    println!("Batching ON:             {throughput2:.0} ops/sec (took {elapsed2:?})");
    println!("Performance change:      {:.2}x", throughput2 / throughput1);

    // Regression check: batching should not degrade performance
    assert!(
        throughput2 >= throughput1,
        "Batching should not degrade performance: {throughput2:.0} ops/sec < {throughput1:.0} ops/sec"
    );

    println!("\n✓ No performance regression detected");
    if throughput2 > throughput1 * 1.5 {
        println!(
            "  Bonus: {:.2}x improvement observed",
            throughput2 / throughput1
        );
    }
}