radixdb-storage 1.1.0

Storage contracts and physical persistence engine for RadixDB
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
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;

use crate::v6::{
    fault::reach_generation_boundary, ArtifactKind, ArtifactRef, CompleteStagingSet,
    ControlSlotIndex, FormatError, FormatResult, GenerationCrashPoint,
};

use super::plan::{GenerationPublicationPlan, PublicationMember, PublicationMemberRole};
use super::source::validate_new_artifact_identity;

const COMPARE_BUFFER_BYTES: usize = 64 * 1024;

pub(crate) fn publish_members(
    root: &Path,
    staging: &CompleteStagingSet,
    plan: &GenerationPublicationPlan,
) -> FormatResult<()> {
    validate_directory(root, "inspect database root")?;
    validate_directory(staging.path(), "inspect publication directory")?;
    validate_same_filesystem(root, staging.path())?;
    for member in plan.members() {
        publish_member(root, staging.path(), member)?;
    }
    publish_control(root, plan.target_control().slot(), plan.control_bytes())
}

/// Promote one complete set of immutable compaction outputs without changing
/// CONTROL. The artifacts remain unreachable until a following rebased
/// manifest publication commits them; a crash in between is therefore safe
/// and generation-aware GC may reclaim the orphaned files.
///
/// The caller must hold the physical publication fence across this operation
/// and the manifest/CONTROL publication that makes the artifacts reachable.
pub(crate) fn publish_prebuilt_artifacts(
    root: &Path,
    staging: &CompleteStagingSet,
    artifacts: &[ArtifactRef],
) -> FormatResult<()> {
    validate_directory(root, "inspect database root")?;
    validate_directory(staging.path(), "inspect publication directory")?;
    validate_same_filesystem(root, staging.path())?;

    let members = prebuilt_artifact_members(artifacts)?;
    for member in &members {
        publish_member(root, staging.path(), member)?;
    }
    Ok(())
}

/// Validate the exact completed prebuilt member set before entering the short
/// physical publication fence. This is the only phase that hashes complete
/// DATA/INDEX payloads; the fenced promotion and manifest rebase do not reread
/// their bodies.
pub(crate) fn validate_prebuilt_artifacts(
    root: &Path,
    staging: &CompleteStagingSet,
    artifacts: &[ArtifactRef],
) -> FormatResult<()> {
    validate_directory(root, "inspect database root")?;
    validate_directory(staging.path(), "inspect publication directory")?;
    validate_same_filesystem(root, staging.path())?;
    let members = prebuilt_artifact_members(artifacts)?;
    let member_count =
        u64::try_from(members.len()).map_err(|_| FormatError::InvalidMaintenance {
            detail: "prebuilt compaction artifact count exceeds u64",
        })?;
    staging.validate_records(member_count)?;
    for (member, reference) in members.iter().zip(artifacts_for_members(artifacts)) {
        validate_new_artifact_identity(&staging.path().join(member.relative_path()), reference)?;
    }
    Ok(())
}

fn prebuilt_artifact_members(artifacts: &[ArtifactRef]) -> FormatResult<Vec<PublicationMember>> {
    let members = artifacts_for_members(artifacts)
        .map(|reference| {
            let role = match reference.kind() {
                ArtifactKind::Data => PublicationMemberRole::Data,
                ArtifactKind::Index => PublicationMemberRole::Index,
            };
            PublicationMember::new(role, reference.relative_path())
        })
        .collect::<Vec<_>>();
    if members
        .windows(2)
        .any(|pair| pair[0].relative_path() == pair[1].relative_path())
    {
        return Err(FormatError::InvalidMaintenance {
            detail: "prebuilt compaction artifacts repeat a final locator",
        });
    }
    Ok(members)
}

fn artifacts_for_members(artifacts: &[ArtifactRef]) -> impl Iterator<Item = ArtifactRef> + '_ {
    let mut artifacts = artifacts.to_vec();
    artifacts.sort_unstable_by_key(|reference| reference.relative_path());
    artifacts.into_iter()
}

fn publish_member(root: &Path, staging: &Path, member: &PublicationMember) -> FormatResult<()> {
    let relative = member.relative_path();
    let staged = staging.join(relative);
    let final_path = root.join(relative);
    let parent = final_path.parent().ok_or(FormatError::InvalidPublication {
        detail: "publication member has no final parent",
    })?;
    create_durable_directories(root, parent)?;

    let staged_exists = path_exists(&staged, "inspect staged publication member")?;
    let final_exists = path_exists(&final_path, "inspect final publication member")?;
    match (staged_exists, final_exists) {
        (false, true) => validate_regular(&final_path)?,
        (false, false) => {
            return Err(FormatError::InvalidPublication {
                detail: "publication member is absent from staging and final location",
            });
        }
        (true, true) => {
            validate_regular(&staged)?;
            validate_regular(&final_path)?;
            let compatible = match member.role() {
                // The successor is created before publication so new commits
                // can continue while checkpoint metadata is staged. Its
                // staged snapshot must therefore be an exact prefix of the
                // live append-only WAL, not an exact-size immutable member.
                PublicationMemberRole::WalSuccessor => is_prefix(&staged, &final_path)?,
                _ => same_contents(&staged, &final_path)?,
            };
            if !compatible {
                return Err(FormatError::InvalidPublication {
                    detail: match member.role() {
                        PublicationMemberRole::WalSuccessor => {
                            "live WAL successor does not extend staged prefix"
                        }
                        _ => "immutable final locator contains different bytes",
                    },
                });
            }
        }
        (true, false) => {
            validate_regular(&staged)?;
            rename_without_replace(&staged, &final_path)?;
            if let Some(point) = after_rename_point(member.role()) {
                reach_generation_boundary(point).map_err(|error| {
                    publication_io("inject after immutable member rename", error)
                })?;
            }
        }
    }

    // Existing bytes/names may be from an interrupted attempt, not durable state.
    if final_exists {
        open_read(&final_path)?
            .sync_all()
            .map_err(|error| publication_io("sync existing publication member", error))?;
    }
    sync_directory(parent, "sync final publication directory")?;
    if let Some(point) = directory_durable_point(member.role()) {
        reach_generation_boundary(point)
            .map_err(|error| publication_io("inject after immutable member durability", error))?;
    }
    if let Some(mut staged_parent) = staged.parent() {
        // A resumed staging set may have had empty member directories removed.
        // Sync the nearest surviving parent to finish that namespace cleanup.
        while !path_exists(staged_parent, "inspect staged publication directory")? {
            staged_parent = staged_parent
                .parent()
                .filter(|parent| parent.starts_with(staging))
                .ok_or(FormatError::InvalidPublication {
                    detail: "staged publication directory disappeared",
                })?;
        }
        validate_directory(staged_parent, "inspect staged publication directory")?;
        sync_directory(staged_parent, "sync staged publication directory")?;
    }
    validate_regular(&final_path)
}

const fn after_rename_point(role: PublicationMemberRole) -> Option<GenerationCrashPoint> {
    match role {
        PublicationMemberRole::Data => {
            Some(GenerationCrashPoint::DataAfterFinalRenameBeforeDirSync)
        }
        PublicationMemberRole::Index => {
            Some(GenerationCrashPoint::IndexAfterFinalRenameBeforeDirSync)
        }
        PublicationMemberRole::TableManifest => {
            Some(GenerationCrashPoint::TableManifestAfterRenameBeforeDirSync)
        }
        PublicationMemberRole::CatalogPack => {
            Some(GenerationCrashPoint::CatalogPackAfterRenameBeforeDirSync)
        }
        PublicationMemberRole::DatabaseManifest => {
            Some(GenerationCrashPoint::DatabaseManifestAfterRenameBeforeDirSync)
        }
        PublicationMemberRole::WalSuccessor => None,
    }
}

const fn directory_durable_point(role: PublicationMemberRole) -> Option<GenerationCrashPoint> {
    match role {
        PublicationMemberRole::Data => Some(GenerationCrashPoint::DataFinalDirDurable),
        PublicationMemberRole::Index => Some(GenerationCrashPoint::IndexFinalDirDurable),
        PublicationMemberRole::TableManifest => Some(GenerationCrashPoint::TableManifestDirDurable),
        PublicationMemberRole::CatalogPack => Some(GenerationCrashPoint::CatalogPackDirDurable),
        PublicationMemberRole::DatabaseManifest => {
            Some(GenerationCrashPoint::DatabaseManifestDirDurable)
        }
        PublicationMemberRole::WalSuccessor => None,
    }
}

fn publish_control(root: &Path, slot: ControlSlotIndex, bytes: &[u8]) -> FormatResult<()> {
    let path = root.join(match slot {
        ControlSlotIndex::Zero => "CONTROL.0",
        ControlSlotIndex::One => "CONTROL.1",
    });
    if let Ok(metadata) = std::fs::symlink_metadata(&path) {
        if metadata.file_type().is_symlink() || !metadata.is_file() {
            return Err(FormatError::InvalidPublication {
                detail: "CONTROL slot is not a regular file",
            });
        }
    }

    let mut options = OpenOptions::new();
    options.write(true).create(true).truncate(true);
    set_no_follow(&mut options);
    let mut file = options
        .open(&path)
        .map_err(|error| publication_io("open inactive CONTROL", error))?;
    let split = bytes.len() / 2;
    file.write_all(&bytes[..split])
        .map_err(|error| recovery_required("write inactive CONTROL prefix", error))?;
    reach_generation_boundary(GenerationCrashPoint::ControlAfterPartialWrite)
        .map_err(|error| publication_io("inject after partial CONTROL write", error))?;
    file.write_all(&bytes[split..])
        .map_err(|error| recovery_required("write inactive CONTROL suffix", error))?;
    reach_generation_boundary(GenerationCrashPoint::ControlAfterWriteBeforeFdatasync)
        .map_err(|error| recovery_required("inject before inactive CONTROL sync", error))?;
    file.sync_all()
        .map_err(|error| recovery_required("sync inactive CONTROL", error))?;
    reach_generation_boundary(GenerationCrashPoint::ControlDurable)
        .map_err(|error| recovery_required("inject after inactive CONTROL sync", error))?;
    sync_directory(root, "sync database root after CONTROL").map_err(|error| match error {
        FormatError::PublicationIo { kind, .. } => FormatError::PublicationRecoveryRequired {
            operation: "sync database root after CONTROL",
            kind,
        },
        other => other,
    })?;
    reach_generation_boundary(GenerationCrashPoint::ControlAfterRootDirSync)
        .map_err(|error| recovery_required("inject after database-root sync", error))
}

fn create_durable_directories(root: &Path, target: &Path) -> FormatResult<()> {
    let relative = target
        .strip_prefix(root)
        .map_err(|_| FormatError::InvalidPublication {
            detail: "final publication path escapes database root",
        })?;
    let mut current = root.to_path_buf();
    for component in relative.components() {
        let previous = current.clone();
        current.push(component);
        match std::fs::create_dir(&current) {
            Ok(()) => {}
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
                validate_directory(&current, "inspect publication directory")?;
            }
            Err(error) => return Err(publication_io("create publication directory", error)),
        }
        // AlreadyExists may follow mkdir success and parent fsync failure.
        sync_directory(&previous, "sync publication directory parent")?;
    }
    Ok(())
}

fn path_exists(path: &Path, operation: &'static str) -> FormatResult<bool> {
    match std::fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(publication_io(operation, error)),
    }
}

fn validate_regular(path: &Path) -> FormatResult<()> {
    let metadata = std::fs::symlink_metadata(path)
        .map_err(|error| publication_io("inspect publication member", error))?;
    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(FormatError::InvalidPublication {
            detail: "publication member is not a regular file",
        });
    }
    Ok(())
}

fn validate_directory(path: &Path, operation: &'static str) -> FormatResult<()> {
    let metadata =
        std::fs::symlink_metadata(path).map_err(|error| publication_io(operation, error))?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(FormatError::InvalidPublication {
            detail: "required publication path is not a real directory",
        });
    }
    Ok(())
}

fn same_contents(left: &Path, right: &Path) -> FormatResult<bool> {
    let left_metadata = std::fs::symlink_metadata(left)
        .map_err(|error| publication_io("inspect staged collision", error))?;
    let right_metadata = std::fs::symlink_metadata(right)
        .map_err(|error| publication_io("inspect final collision", error))?;
    if left_metadata.len() != right_metadata.len() {
        return Ok(false);
    }
    let mut left = open_read(left)?;
    let mut right = open_read(right)?;
    let mut left_buffer = [0_u8; COMPARE_BUFFER_BYTES];
    let mut right_buffer = [0_u8; COMPARE_BUFFER_BYTES];
    loop {
        let left_count = left
            .read(&mut left_buffer)
            .map_err(|error| publication_io("read staged collision", error))?;
        let right_count = right
            .read(&mut right_buffer)
            .map_err(|error| publication_io("read final collision", error))?;
        if left_count != right_count || left_buffer[..left_count] != right_buffer[..right_count] {
            return Ok(false);
        }
        if left_count == 0 {
            return Ok(true);
        }
    }
}

fn is_prefix(prefix: &Path, complete: &Path) -> FormatResult<bool> {
    let prefix_metadata = std::fs::symlink_metadata(prefix)
        .map_err(|error| publication_io("inspect staged WAL successor", error))?;
    let complete_metadata = std::fs::symlink_metadata(complete)
        .map_err(|error| publication_io("inspect live WAL successor", error))?;
    if prefix_metadata.len() > complete_metadata.len() {
        return Ok(false);
    }
    let mut prefix = open_read(prefix)?;
    let mut complete = open_read(complete)?;
    let mut prefix_buffer = [0_u8; COMPARE_BUFFER_BYTES];
    let mut complete_buffer = [0_u8; COMPARE_BUFFER_BYTES];
    loop {
        let prefix_count = prefix
            .read(&mut prefix_buffer)
            .map_err(|error| publication_io("read staged WAL successor", error))?;
        if prefix_count == 0 {
            return Ok(true);
        }
        complete
            .read_exact(&mut complete_buffer[..prefix_count])
            .map_err(|error| publication_io("read live WAL successor", error))?;
        if prefix_buffer[..prefix_count] != complete_buffer[..prefix_count] {
            return Ok(false);
        }
    }
}

fn open_read(path: &Path) -> FormatResult<File> {
    let mut options = OpenOptions::new();
    options.read(true);
    set_no_follow(&mut options);
    options
        .open(path)
        .map_err(|error| publication_io("open publication member", error))
}

#[cfg(target_os = "linux")]
fn rename_without_replace(source: &Path, target: &Path) -> FormatResult<()> {
    use std::ffi::CString;
    use std::os::unix::ffi::OsStrExt;

    let source_path = source;
    let target_path = target;
    let source = CString::new(source.as_os_str().as_bytes()).map_err(|_| {
        FormatError::InvalidPublication {
            detail: "staged publication path contains NUL",
        }
    })?;
    let target = CString::new(target.as_os_str().as_bytes()).map_err(|_| {
        FormatError::InvalidPublication {
            detail: "final publication path contains NUL",
        }
    })?;
    let result = unsafe {
        libc::renameat2(
            libc::AT_FDCWD,
            source.as_ptr(),
            libc::AT_FDCWD,
            target.as_ptr(),
            libc::RENAME_NOREPLACE,
        )
    };
    if result == 0 {
        return Ok(());
    }
    let error = std::io::Error::last_os_error();
    if matches!(error.raw_os_error(), Some(code) if code == libc::ENOSYS || code == libc::EINVAL) {
        return link_without_replace(source_path, target_path);
    }
    Err(publication_io("rename publication member", error))
}

#[cfg(not(target_os = "linux"))]
fn rename_without_replace(source: &Path, target: &Path) -> FormatResult<()> {
    link_without_replace(source, target)
}

fn link_without_replace(source: &Path, target: &Path) -> FormatResult<()> {
    std::fs::hard_link(source, target)
        .map_err(|error| publication_io("link publication member", error))?;
    std::fs::remove_file(source)
        .map_err(|error| publication_io("retire staged publication member", error))
}

#[cfg(unix)]
fn validate_same_filesystem(left: &Path, right: &Path) -> FormatResult<()> {
    use std::os::unix::fs::MetadataExt;

    let left_device = std::fs::metadata(left)
        .map_err(|error| publication_io("inspect database filesystem", error))?
        .dev();
    let right_device = std::fs::metadata(right)
        .map_err(|error| publication_io("inspect staging filesystem", error))?
        .dev();
    if left_device != right_device {
        return Err(FormatError::InvalidPublication {
            detail: "staging and final root are on different filesystems",
        });
    }
    Ok(())
}

#[cfg(not(unix))]
fn validate_same_filesystem(_left: &Path, _right: &Path) -> FormatResult<()> {
    Ok(())
}

#[cfg(unix)]
fn set_no_follow(options: &mut OpenOptions) {
    use std::os::unix::fs::OpenOptionsExt;
    options.custom_flags(libc::O_NOFOLLOW);
}

#[cfg(not(unix))]
fn set_no_follow(_options: &mut OpenOptions) {}

fn sync_directory(path: &Path, operation: &'static str) -> FormatResult<()> {
    File::open(path)
        .and_then(|directory| directory.sync_all())
        .map_err(|error| publication_io(operation, error))
}

fn publication_io(operation: &'static str, error: std::io::Error) -> FormatError {
    FormatError::PublicationIo {
        operation,
        kind: error.kind(),
    }
}

fn recovery_required(operation: &'static str, error: std::io::Error) -> FormatError {
    FormatError::PublicationRecoveryRequired {
        operation,
        kind: error.kind(),
    }
}