rockraft 0.1.6

A strong consistency KV service library base on Raft and Rocksdb
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use std::io;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};

use crate::raft::types::{decode, encode};
use tokio::fs::File;
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt as _;
use tracing::info;

use crate::raft::types::Snapshot;
use crate::raft::types::SnapshotMeta;

/// Construct the path to the snapshot dump file for a given snapshot ID directory.
///
/// This function takes a snapshot ID directory path and joins it with the "dump" filename,
/// returning the full path as a string. The dump file is used to store snapshot data in
/// a serialized format.
///
/// # Arguments
///
/// * `snapshot_id_dir` - A reference to the `PathBuf` representing the directory for a specific snapshot ID
///
/// # Returns
///
/// A `String` containing the full path to the dump file within the snapshot ID directory
///
/// # Panics
///
/// This function will panic if the resulting path cannot be converted to a string representation.
/// This typically occurs with paths containing invalid UTF-8 sequences.
pub fn snapshot_dump_file(snapshot_id_dir: &Path) -> String {
  snapshot_id_dir
    .join("dump")
    .as_path()
    .to_str()
    .unwrap()
    .to_string()
}

/// Construct the path to the snapshot metadata file for a given snapshot ID directory.
///
/// This function takes a snapshot ID directory path and joins it with the "meta" filename,
/// returning the full path as a string. The metadata file stores serialized snapshot metadata
/// including snapshot ID, term, membership, and other Raft-related information.
///
/// # Arguments
///
/// * `snapshot_id_dir` - A reference to the `PathBuf` representing the directory for a specific snapshot ID
///
/// # Returns
///
/// A `String` containing the full path to the metadata file within the snapshot ID directory
///
/// # Panics
///
/// This function will panic if the resulting path cannot be converted to a string representation.
/// This typically occurs with paths containing invalid UTF-8 sequences.
pub fn snapshot_meta_file(snapshot_id_dir: &Path) -> String {
  snapshot_id_dir
    .join("meta")
    .as_path()
    .to_str()
    .unwrap()
    .to_string()
}

/// Construct the path to the snapshot data file for a given snapshot ID directory.
///
/// This function takes a snapshot ID directory path and joins it with the "snapshot" filename,
/// returning the full path as a string. The snapshot data file contains the actual state machine
/// data that was captured at the time the snapshot was created.
///
/// # Arguments
///
/// * `snapshot_id_dir` - A reference to the `PathBuf` representing the directory for a specific snapshot ID
///
/// # Returns
///
/// A `String` containing the full path to the snapshot data file within the snapshot ID directory
///
/// # Panics
///
/// This function will panic if the resulting path cannot be converted to a string representation.
/// This typically occurs with paths containing invalid UTF-8 sequences.
pub fn snapshot_data_file(snapshot_id_dir: &Path) -> String {
  snapshot_id_dir
    .join("snapshot")
    .as_path()
    .to_str()
    .unwrap()
    .to_string()
}

/// Construct the path to the file storing the last snapshot ID in the snapshot directory.
///
/// This function takes the main snapshot directory path and joins it with the "last_snapshot_id"
/// filename. This file tracks the ID of the most recent snapshot, which is used to determine
/// which snapshot to recover from when the system restarts.
///
/// # Arguments
///
/// * `snapshot_dir` - A reference to the `PathBuf` representing the main snapshot directory
///
/// # Returns
///
/// A `String` containing the full path to the last snapshot ID file
///
/// # Panics
///
/// This function will panic if the resulting path cannot be converted to a string representation.
/// This typically occurs with paths containing invalid UTF-8 sequences.
pub fn snapshot_last_snapshot_id_file(snapshot_dir: &Path) -> String {
  snapshot_dir
    .join("last_snapshot_id")
    .as_path()
    .to_str()
    .unwrap()
    .to_string()
}

/// Construct the directory path for a specific snapshot ID within the snapshot directory.
///
/// This function joins the main snapshot directory with a snapshot ID to create the
/// full path to the directory that contains all files for that specific snapshot
/// (meta, snapshot data, dump, etc.).
///
/// # Arguments
///
/// * `snapshot_dir` - A reference to the `PathBuf` representing the main snapshot directory
/// * `snapshot_id` - The unique identifier for the snapshot
///
/// # Returns
///
/// A `PathBuf` representing the full path to the snapshot ID directory
pub fn snapshot_id_dir(snapshot_dir: &Path, snapshot_id: &str) -> PathBuf {
  snapshot_dir.join(snapshot_id)
}

/// Save the last snapshot ID to the snapshot directory.
///
/// This function writes the provided snapshot ID to the "last_snapshot_id" file in the
/// snapshot directory. This is used to track the most recent snapshot for recovery purposes.
/// The file is created if it doesn't exist, or overwritten if it does.
///
/// # Arguments
///
/// * `snapshot_dir` - A reference to the `PathBuf` representing the main snapshot directory
/// * `last_snapshot_id` - The snapshot ID to save as the last snapshot
///
/// # Returns
///
/// Returns `Ok(())` if the snapshot ID was successfully written and flushed to disk
/// Returns `Err(io::Error)` if file creation, writing, or flushing fails
///
/// # Errors
///
/// This function will return an error if:
/// - The snapshot directory doesn't exist and cannot be created
/// - There are insufficient permissions to create or write the file
/// - The disk is full
/// - The file system experiences an I/O error
pub async fn save_last_snapshot_id_file(
  snapshot_dir: &Path,
  last_snapshot_id: &str,
) -> io::Result<()> {
  let last_snapshot_id_file = snapshot_last_snapshot_id_file(snapshot_dir);

  let mut file = File::create(&last_snapshot_id_file).await?;
  file.write_all(last_snapshot_id.as_bytes()).await?;
  file.flush().await?;

  Ok(())
}

/// Retrieve the last snapshot ID from the snapshot directory.
///
/// This function reads and returns the snapshot ID stored in the "last_snapshot_id" file.
/// This is a crate-private function used internally to determine which snapshot is the most recent.
///
/// # Arguments
///
/// * `snapshot_dir` - A reference to the `PathBuf` representing the main snapshot directory
///
/// # Returns
///
/// Returns `Ok(String)` containing the last snapshot ID if successful
/// Returns `Err(io::Error)` if the file cannot be opened or read
///
/// # Errors
///
/// This function will return an error if:
/// - The "last_snapshot_id" file doesn't exist (ErrorKind::NotFound)
/// - There are insufficient permissions to read the file
/// - The file contains invalid UTF-8 data
/// - The file system experiences an I/O error
pub(crate) async fn get_last_snapshot_id(snapshot_dir: &Path) -> io::Result<String> {
  let last_snapshot_file = snapshot_last_snapshot_id_file(snapshot_dir);

  let mut file = File::open(&last_snapshot_file).await?;
  let mut content = String::new();
  file.read_to_string(&mut content).await?;
  Ok(content)
}

/// Save snapshot metadata to the snapshot ID directory.
///
/// This function serializes the provided snapshot metadata using encoder and writes it
/// to the "meta" file in the specified snapshot ID directory. The metadata includes information
/// such as the snapshot ID, term, membership configuration, and other Raft-related metadata.
///
/// # Arguments
///
/// * `snapshot_id_dir` - A reference to the `PathBuf` representing the directory for a specific snapshot ID
/// * `meta` - The `SnapshotMeta` object containing the snapshot metadata to save
///
/// # Returns
///
/// Returns `Ok(())` if the metadata was successfully serialized, written, and flushed to disk
/// Returns `Err(io::Error)` if serialization, file creation, writing, or flushing fails
///
/// # Errors
///
/// This function will return an error if:
/// - The snapshot metadata cannot be serialized (ErrorKind::InvalidData)
/// - The snapshot ID directory doesn't exist or cannot be created
/// - There are insufficient permissions to create or write the file
/// - The disk is full
/// - The file system experiences an I/O error
pub async fn save_snapshot_meta(snapshot_id_dir: &Path, meta: SnapshotMeta) -> io::Result<()> {
  let meta_file = snapshot_meta_file(snapshot_id_dir);

  let data = encode(&meta).map_err(|e| {
    io::Error::new(
      ErrorKind::InvalidData,
      format!("Serialize meta data error: {}", e),
    )
  })?;

  let mut file = File::create(&meta_file).await?;
  file.write_all(&data).await?;
  file.flush().await?;

  Ok(())
}

/// Retrieve and deserialize snapshot metadata from a file.
///
/// This function reads the serialized snapshot metadata from the specified file path,
/// deserializes it using encoder, and returns the `SnapshotMeta` object.
///
/// # Arguments
///
/// * `snapshot_id_dir` - A reference to the `PathBuf` pointing to the metadata file
///
/// # Returns
///
/// Returns `Ok(SnapshotMeta)` containing the deserialized snapshot metadata if successful
/// Returns `Err(io::Error)` if the file cannot be opened, read, or deserialized
///
/// # Errors
///
/// This function will return an error if:
/// - The metadata file doesn't exist (ErrorKind::NotFound)
/// - There are insufficient permissions to read the file
/// - The file contains invalid or corrupted data (ErrorKind::InvalidData)
/// - The file system experiences an I/O error
pub async fn get_snapshot_meta(snapshot_id_dir: &PathBuf) -> io::Result<SnapshotMeta> {
  let mut file = File::open(snapshot_id_dir).await?;
  let mut data = Vec::new();
  file.read_to_end(&mut data).await?;

  decode(&data)
    .map_err(|e| io::Error::new(ErrorKind::InvalidData, format!("Deserialize error: {}", e)))
}

/// Retrieve the current (most recent) snapshot from the snapshot directory.
///
/// This function attempts to load the most recent snapshot by:
/// 1. Reading the last snapshot ID from the "last_snapshot_id" file
/// 2. Loading the snapshot metadata for that snapshot ID
/// 3. Opening the snapshot data file
/// 4. Returning a complete `Snapshot` object containing both metadata and data file handle
///
/// If any component of the snapshot is missing (no snapshot ID file, no metadata, or no data file),
/// the function returns `Ok(None)` instead of an error, as this is a valid state when no snapshot exists.
///
/// # Arguments
///
/// * `snapshot_dir` - A reference to the `PathBuf` representing the main snapshot directory
///
/// # Returns
///
/// Returns `Ok(Some(Snapshot))` if the snapshot was successfully loaded with both metadata and data file
/// Returns `Ok(None)` if no snapshot exists (missing last_snapshot_id file, metadata, or data file)
/// Returns `Err(io::Error)` if there's an I/O error reading files or deserializing data
///
/// # Errors
///
/// This function will return an error if:
/// - The last_snapshot_id file exists but cannot be read
/// - The snapshot metadata file exists but cannot be deserialized
/// - The snapshot data file exists but cannot be opened due to permission issues
/// - Any unexpected I/O error occurs
///
/// Note that missing files are not considered errors and result in `Ok(None)`.
pub async fn get_current_snapshot(snapshot_dir: &Path) -> io::Result<Option<Snapshot>> {
  let snapshot_id = match get_last_snapshot_id(snapshot_dir).await {
    Ok(id) => id,
    Err(e) if e.kind() == ErrorKind::NotFound => {
      info!("No snapshot found, returning None");
      return Ok(None);
    }
    Err(e) => return Err(e),
  };

  let snapshot_id_dir = snapshot_id_dir(snapshot_dir, &snapshot_id);

  let snapshot_meta_file = snapshot_meta_file(&snapshot_id_dir);
  let snapshot_meta = match get_snapshot_meta(&PathBuf::from(snapshot_meta_file)).await {
    Ok(meta) => meta,
    Err(e) if e.kind() == ErrorKind::NotFound => {
      info!(
        "Snapshot metadata not found for snapshot_id={}, returning None",
        snapshot_id
      );
      return Ok(None);
    }
    Err(e) => return Err(e),
  };

  let snapshot_data_file = snapshot_data_file(&snapshot_id_dir);
  let res = match File::open(&snapshot_data_file).await {
    Ok(file) => file,
    Err(e) if e.kind() == ErrorKind::NotFound => {
      info!(
        "Snapshot file not found for snapshot_id={}, returning None",
        snapshot_id
      );
      return Ok(None);
    }
    Err(e) => return Err(e),
  };

  Ok(Some(Snapshot {
    meta: snapshot_meta,
    snapshot: res,
  }))
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::raft::types::TypeConfig;
  use openraft::Membership;
  use openraft::SnapshotMeta;
  use openraft::StoredMembership;
  use tempfile::tempdir;

  /// Helper function to create a complete test snapshot structure
  ///
  /// Creates a temporary directory with all necessary files for a snapshot:
  /// - last_snapshot_id file containing the snapshot ID
  /// - Snapshot ID directory with meta file (serialized SnapshotMeta)
  /// - Snapshot data file (empty file for testing)
  ///
  /// # Arguments
  ///
  /// * `snapshot_dir` - The base snapshot directory path
  /// * `snapshot_id` - The snapshot ID to use
  async fn create_test_snapshot(snapshot_dir: &PathBuf, snapshot_id: &str) {
    // Create last_snapshot_id file
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, snapshot_id)
      .await
      .unwrap();

    // Create snapshot ID directory
    let snapshot_id_dir = snapshot_id_dir(snapshot_dir, snapshot_id);
    tokio::fs::create_dir_all(&snapshot_id_dir).await.unwrap();

    // Create snapshot metadata
    let meta = SnapshotMeta::<TypeConfig> {
      snapshot_id: snapshot_id.to_string(),
      ..Default::default()
    };
    save_snapshot_meta(&snapshot_id_dir, meta).await.unwrap();

    // Create empty snapshot data file
    let data_file = snapshot_data_file(&snapshot_id_dir);
    tokio::fs::File::create(&data_file).await.unwrap();
  }

  /// Test successful retrieval of a complete snapshot
  ///
  /// Verifies that when all snapshot files exist (last_snapshot_id, meta, and data),
  /// the function successfully loads and returns the snapshot with correct metadata.
  #[tokio::test]
  async fn test_get_current_snapshot_success() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();

    // Create a complete snapshot structure
    create_test_snapshot(&snapshot_dir, "test_snapshot_001").await;

    // Retrieve the snapshot
    let result = get_current_snapshot(&snapshot_dir).await;

    // Verify the snapshot was loaded successfully
    assert!(result.is_ok());
    let snapshot = result.unwrap();
    assert!(snapshot.is_some());
    let snapshot = snapshot.unwrap();

    // Verify the snapshot metadata
    assert_eq!(snapshot.meta.snapshot_id, "test_snapshot_001");
  }

  /// Test behavior when snapshot directory doesn't exist
  ///
  /// Verifies that get_current_snapshot returns Ok(None) when the entire
  /// snapshot directory structure doesn't exist, as this is a valid state
  /// for a system that hasn't created any snapshots yet.
  #[tokio::test]
  async fn test_get_current_snapshot_no_directory() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().join("nonexistent_snapshots");

    // Attempt to get snapshot from non-existent directory
    let result = get_current_snapshot(&snapshot_dir).await;

    // Should return Ok(None) - no error, just no snapshot
    assert!(result.is_ok());
    assert!(result.unwrap().is_none());
  }

  /// Test behavior when last_snapshot_id file is missing
  ///
  /// Verifies that get_current_snapshot returns Ok(None) when the snapshot
  /// directory exists but doesn't contain a last_snapshot_id file.
  #[tokio::test]
  async fn test_get_current_snapshot_no_last_snapshot_id_file() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();

    // Create the snapshot directory but no files
    tokio::fs::create_dir_all(&snapshot_dir).await.unwrap();

    // Attempt to get snapshot
    let result = get_current_snapshot(&snapshot_dir).await;

    // Should return Ok(None) since last_snapshot_id file is missing
    assert!(result.is_ok());
    assert!(result.unwrap().is_none());
  }

  /// Test behavior when snapshot metadata file is missing
  ///
  /// Verifies that get_current_snapshot returns Ok(None) when last_snapshot_id
  /// exists but the snapshot metadata file is missing.
  #[tokio::test]
  async fn test_get_current_snapshot_no_meta_file() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();
    let snapshot_id = "test_snapshot_002";

    // Create last_snapshot_id file
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(&snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, snapshot_id)
      .await
      .unwrap();

    // Create snapshot directory but no meta file or data file
    let snapshot_id_dir = snapshot_id_dir(&snapshot_dir, snapshot_id);
    tokio::fs::create_dir_all(&snapshot_id_dir).await.unwrap();

    // Attempt to get snapshot
    let result = get_current_snapshot(&snapshot_dir).await;

    // Should return Ok(None) since meta file is missing
    assert!(result.is_ok());
    assert!(result.unwrap().is_none());
  }

  /// Test behavior when snapshot data file is missing
  ///
  /// Verifies that get_current_snapshot returns Ok(None) when last_snapshot_id
  /// and meta files exist but the snapshot data file is missing.
  #[tokio::test]
  async fn test_get_current_snapshot_no_data_file() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();
    let snapshot_id = "test_snapshot_003";

    // Create last_snapshot_id file
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(&snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, snapshot_id)
      .await
      .unwrap();

    // Create snapshot directory and meta file but no data file
    let snapshot_id_dir = snapshot_id_dir(&snapshot_dir, snapshot_id);
    tokio::fs::create_dir_all(&snapshot_id_dir).await.unwrap();

    let meta = SnapshotMeta::<TypeConfig>::default();
    save_snapshot_meta(&snapshot_id_dir, meta).await.unwrap();

    // Attempt to get snapshot
    let result = get_current_snapshot(&snapshot_dir).await;

    // Should return Ok(None) since data file is missing
    assert!(result.is_ok());
    assert!(result.unwrap().is_none());
  }

  /// Test behavior with corrupted metadata file
  ///
  /// Verifies that get_current_snapshot returns an error when the metadata
  /// file exists but contains invalid/corrupted data that cannot be deserialized.
  #[tokio::test]
  async fn test_get_current_snapshot_corrupted_meta() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();
    let snapshot_id = "test_snapshot_004";

    // Create last_snapshot_id file
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(&snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, snapshot_id)
      .await
      .unwrap();

    // Create snapshot directory with corrupted meta file
    let snapshot_id_dir = snapshot_id_dir(&snapshot_dir, snapshot_id);
    tokio::fs::create_dir_all(&snapshot_id_dir).await.unwrap();

    let meta_file = snapshot_meta_file(&snapshot_id_dir);
    tokio::fs::write(&meta_file, b"invalid corrupted data")
      .await
      .unwrap();

    // Create snapshot data file
    let data_file = snapshot_data_file(&snapshot_id_dir);
    tokio::fs::File::create(&data_file).await.unwrap();

    // Attempt to get snapshot
    let result = get_current_snapshot(&snapshot_dir).await;

    // Should return an error due to corrupted meta data
    assert!(result.is_err());
    let error = result.unwrap_err();
    assert_eq!(error.kind(), ErrorKind::InvalidData);
  }

  /// Test behavior with empty last_snapshot_id file
  ///
  /// Verifies that get_current_snapshot handles an empty last_snapshot_id file
  /// by creating a snapshot ID directory with an empty string and proceeding
  /// with normal snapshot loading logic.
  #[tokio::test]
  async fn test_get_current_snapshot_empty_snapshot_id() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();

    // Create last_snapshot_id file with empty content
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(&snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, "").await.unwrap();

    // Create snapshot directory with empty name (edge case)
    let snapshot_id_dir = snapshot_id_dir(&snapshot_dir, "");
    tokio::fs::create_dir_all(&snapshot_id_dir).await.unwrap();

    let meta = SnapshotMeta::<TypeConfig>::default();
    save_snapshot_meta(&snapshot_id_dir, meta).await.unwrap();

    let data_file = snapshot_data_file(&snapshot_id_dir);
    tokio::fs::File::create(&data_file).await.unwrap();

    // Attempt to get snapshot
    let result = get_current_snapshot(&snapshot_dir).await;

    // Should successfully load snapshot (though with empty ID)
    assert!(result.is_ok());
    let snapshot = result.unwrap();
    assert!(snapshot.is_some());
    let snapshot = snapshot.unwrap();
    assert_eq!(snapshot.meta.snapshot_id, "");
  }

  /// Test that snapshot file handle is valid
  ///
  /// Verifies that the returned snapshot contains a valid file handle
  /// to the snapshot data file that can be used for reading.
  #[tokio::test]
  async fn test_get_current_snapshot_file_handle_valid() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();

    // Create a complete snapshot structure
    create_test_snapshot(&snapshot_dir, "test_snapshot_005").await;

    // Retrieve the snapshot
    let result = get_current_snapshot(&snapshot_dir).await;
    assert!(result.is_ok());

    let snapshot = result.unwrap();
    assert!(snapshot.is_some());
    let snapshot = snapshot.unwrap();

    // Verify the snapshot data file can be accessed
    // The file handle should be valid
    let metadata = snapshot.snapshot.metadata().await;
    assert!(metadata.is_ok());
    let file_metadata = metadata.unwrap();
    assert!(file_metadata.is_file());
  }

  /// Test with multiple snapshots (verifies it only loads the latest)
  ///
  /// Verifies that get_current_snapshot loads the snapshot referenced by
  /// last_snapshot_id, even if multiple snapshot directories exist.
  #[tokio::test]
  async fn test_get_current_snapshot_multiple_snapshots() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();

    // Create multiple snapshots
    create_test_snapshot(&snapshot_dir, "snapshot_001").await;
    create_test_snapshot(&snapshot_dir, "snapshot_002").await;
    create_test_snapshot(&snapshot_dir, "snapshot_003").await;

    // Set last_snapshot_id to point to snapshot_002
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(&snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, "snapshot_002")
      .await
      .unwrap();

    // Retrieve the snapshot
    let result = get_current_snapshot(&snapshot_dir).await;
    assert!(result.is_ok());

    let snapshot = result.unwrap();
    assert!(snapshot.is_some());
    let snapshot = snapshot.unwrap();

    // Verify it loaded snapshot_002, not the others
    assert_eq!(snapshot.meta.snapshot_id, "snapshot_002");
  }

  /// Test that snapshot metadata is correctly deserialized
  ///
  /// Verifies that when loading a snapshot, the metadata is correctly
  /// deserialized and matches what was originally saved.
  #[tokio::test]
  async fn test_get_current_snapshot_metadata_integrity() {
    let temp_dir = tempdir().unwrap();
    let snapshot_dir = temp_dir.path().to_path_buf();
    let snapshot_id = "test_snapshot_006";

    // Create last_snapshot_id file
    let last_snapshot_id_file = snapshot_last_snapshot_id_file(&snapshot_dir);
    tokio::fs::write(&last_snapshot_id_file, snapshot_id)
      .await
      .unwrap();

    // Create snapshot directory
    let snapshot_id_dir = snapshot_id_dir(&snapshot_dir, snapshot_id);
    tokio::fs::create_dir_all(&snapshot_id_dir).await.unwrap();

    // Create snapshot metadata with specific values
    let membership = Membership::<TypeConfig>::default();
    let stored_membership = StoredMembership::new(None, membership);

    let meta = SnapshotMeta::<TypeConfig> {
      snapshot_id: snapshot_id.to_string(),
      last_membership: stored_membership.clone(),
      last_log_id: None,
    };
    save_snapshot_meta(&snapshot_id_dir, meta.clone())
      .await
      .unwrap();

    // Create snapshot data file
    let data_file = snapshot_data_file(&snapshot_id_dir);
    tokio::fs::File::create(&data_file).await.unwrap();

    // Retrieve the snapshot
    let result = get_current_snapshot(&snapshot_dir).await;
    assert!(result.is_ok());

    let snapshot = result.unwrap();
    assert!(snapshot.is_some());
    let loaded_snapshot = snapshot.unwrap();

    // Verify metadata integrity
    assert_eq!(loaded_snapshot.meta.snapshot_id, snapshot_id);
    assert_eq!(loaded_snapshot.meta.last_membership, stored_membership);
    assert_eq!(loaded_snapshot.meta.last_log_id, None);
  }
}