lbug 0.20.0

An in-process property graph database management system built for query speed and scalability
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
#include "storage/wal/wal_replayer.h"

#include <filesystem>
#include <string>

#include "common/exception/io.h"
#include "common/exception/runtime.h"
#include "common/file_system/file_info.h"
#include "common/file_system/file_system.h"
#include "common/file_system/local_file_system.h"
#include "common/file_system/virtual_file_system.h"
#include "common/serializer/buffered_file.h"
#include "common/system_message.h"
#include "common/type_utils.h"
#include "common/types/types.h"
#include "main/client_context.h"
#include "main/database_manager.h"
#include "storage/checkpointer.h"
#include "storage/file_db_id_utils.h"
#include "storage/local_storage/local_rel_table.h"
#include "storage/partition_storage_registry.h"
#include "storage/storage_manager.h"
#include "storage/storage_utils.h"
#include "storage/wal/checksum_reader.h"
#include "storage/wal/wal_record.h"
#include "transaction/transaction_context.h"
#include <format>
#ifndef _WIN32
#include <fcntl.h>
#include <unistd.h>
#endif

using namespace lbug::common;
using namespace lbug::storage;
using namespace lbug::transaction;

namespace lbug {
namespace storage {

static constexpr std::string_view checksumMismatchMessage =
    "Checksum verification failed, the WAL file is corrupted.";
static constexpr std::string_view readOnlyCheckpointInProgressMessage =
    "Cannot open database in read-only mode while checkpoint is in progress. Please retry later.";

// Partition children persist their pending updates into their own shadow files durably BEFORE
// the main database's commit point. Recovery therefore mirrors the main file's decision:
// when the WAL ends in a checkpoint record the child shadow pages are applied; otherwise they
// are discarded so WAL redo replays the lost updates against the last committed state.
static void replayPartitionChildShadowPages(main::ClientContext& clientContext) {
    auto* dbManager = main::DatabaseManager::Get(clientContext);
    if (dbManager == nullptr) {
        return;
    }
    auto* registry = dbManager->getPartitionStorageRegistry();
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    bool replayedAny = false;
    for (auto* sm : registry->getAllManagers()) {
        const auto& dbPath = sm->getDatabasePath();
        if (!vfs->fileOrPathExists(StorageUtils::getShadowFilePath(dbPath), &clientContext)) {
            continue;
        }
        // Replays through the child's already-open file handle (no second lock on the data
        // file).
        ShadowFile::replayShadowPageRecordsForStorageManager(clientContext, *sm);
        replayedAny = true;
    }
    if (replayedAny) {
        // The applied shadows may have replaced the children's on-disk headers and
        // page-manager serializations; refresh the in-memory copies.
        registry->reloadPageManagers();
    }
}

static void removePartitionChildShadowFiles(main::ClientContext& clientContext) {
    auto* dbManager = main::DatabaseManager::Get(clientContext);
    if (dbManager == nullptr) {
        return;
    }
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    for (auto* sm : dbManager->getPartitionStorageRegistry()->getAllManagers()) {
        vfs->removeFileIfExists(StorageUtils::getShadowFilePath(sm->getDatabasePath()),
            &clientContext);
    }
}

static void syncParentDirectoryForLocalPath(const std::string& path) {
#ifdef _WIN32
    (void)path;
#else
    if (!LocalFileSystem::isLocalPath(path)) {
        return;
    }
    auto parentPath = std::filesystem::path(path).parent_path();
    if (parentPath.empty()) {
        parentPath = ".";
    }
    const int dirFd = open(parentPath.c_str(), O_RDONLY | O_DIRECTORY);
    if (dirFd < 0) {
        throw IOException(std::format("Failed to open parent directory {} for sync: {}",
            parentPath.string(), posixErrMessage()));
    }
    if (fsync(dirFd) != 0) {
        const auto errorMessage = posixErrMessage();
        close(dirFd);
        throw IOException(
            std::format("Failed to sync parent directory {} after removing file {}: {}",
                parentPath.string(), path, errorMessage));
    }
    close(dirFd);
#endif
}

WALReplayer::WALReplayer(main::ClientContext& clientContext) : clientContext{clientContext} {
    walPath = StorageUtils::getWALFilePath(clientContext.getDatabasePath());
    checkpointWalPath = StorageUtils::getCheckpointWALFilePath(clientContext.getDatabasePath());
    shadowFilePath = StorageUtils::getShadowFilePath(clientContext.getDatabasePath());
}

static WALHeader readWALHeader(Deserializer& deserializer) {
    WALHeader header{};
    deserializer.deserializeValue(header.databaseID);

    // It is possible to read a value other than 0/1 when deserializing the flag
    // This causes some weird behaviours with some toolchains so we manually do the conversion here
    uint8_t enableChecksumsBytes = 0;
    deserializer.deserializeValue(enableChecksumsBytes);
    header.enableChecksums = enableChecksumsBytes != 0;

    return header;
}

static Deserializer initDeserializer(FileInfo& fileInfo, main::ClientContext& clientContext,
    bool enableChecksums) {
    if (enableChecksums) {
        return Deserializer{std::make_unique<ChecksumReader>(fileInfo,
            *MemoryManager::Get(clientContext), checksumMismatchMessage)};
    } else {
        return Deserializer{std::make_unique<BufferedFileReader>(fileInfo)};
    }
}

static void checkWALHeader(const WALHeader& header, bool enableChecksums) {
    if (enableChecksums != header.enableChecksums) {
        throw RuntimeException(std::format(
            "The database you are trying to open was serialized with enableChecksums={} but you "
            "are trying to open it with enableChecksums={}. Please open your database using the "
            "correct enableChecksums config. If you wish to change this for your database, please "
            "use the export/import functionality.",
            TypeUtils::toString(header.enableChecksums), TypeUtils::toString(enableChecksums)));
    }
}

static uint64_t getReadOffset(Deserializer& deSer, bool enableChecksums) {
    if (enableChecksums) {
        return deSer.getReader()->cast<ChecksumReader>()->getReadOffset();
    } else {
        return deSer.getReader()->cast<BufferedFileReader>()->getReadOffset();
    }
}

static std::unique_ptr<FileInfo> tryAcquireReadOnlyCheckpointLock(
    main::ClientContext& clientContext, const std::string& lockPath) {
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    if (!vfs->fileOrPathExists(lockPath, &clientContext)) {
        return nullptr;
    }
    try {
        return vfs->openFile(lockPath, FileOpenFlags(FileFlags::READ_ONLY, FileLockType::READ_LOCK),
            &clientContext);
    } catch (const IOException&) {
        throw RuntimeException(std::string(readOnlyCheckpointInProgressMessage));
    }
}

static std::unique_ptr<FileInfo> acquireReadOnlyCheckpointApplyLock(
    main::ClientContext& clientContext) {
    if (!StorageManager::Get(clientContext)->isReadOnly()) {
        return nullptr;
    }
    const auto databasePath = clientContext.getDatabasePath();
    auto intentLock = tryAcquireReadOnlyCheckpointLock(clientContext,
        StorageUtils::getCheckpointIntentLockFilePath(databasePath));
    auto applyLock = tryAcquireReadOnlyCheckpointLock(clientContext,
        StorageUtils::getCheckpointApplyLockFilePath(databasePath));
    return applyLock;
}

static void throwIfReadOnlyCheckpointState(main::ClientContext& clientContext, bool hasFrozenWAL,
    const std::string& shadowFilePath) {
    if (!StorageManager::Get(clientContext)->isReadOnly()) {
        return;
    }
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    if (hasFrozenWAL || vfs->fileOrPathExists(shadowFilePath, &clientContext)) {
        throw RuntimeException(std::string(readOnlyCheckpointInProgressMessage));
    }
}

void WALReplayer::replay(bool throwOnWalReplayFailure, bool enableChecksums) const {
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    [[maybe_unused]] auto readOnlyCheckpointApplyLock =
        acquireReadOnlyCheckpointApplyLock(clientContext);
    Checkpointer checkpointer(clientContext);
    bool hasFrozenWAL = vfs->fileOrPathExists(checkpointWalPath, &clientContext);
    bool hasActiveWAL = vfs->fileOrPathExists(walPath, &clientContext);
    throwIfReadOnlyCheckpointState(clientContext, hasFrozenWAL, shadowFilePath);

    if (!hasFrozenWAL && !hasActiveWAL) {
        removeFileAndSyncParentDirectory(shadowFilePath);
        checkpointer.readCheckpoint();
        removePartitionChildShadowFiles(clientContext);
        return;
    }

    if (hasFrozenWAL) {
        replayFrozenWAL(checkpointer, throwOnWalReplayFailure, enableChecksums);
    } else {
        checkpointer.readCheckpoint();
    }

    if (hasActiveWAL) {
        replayActiveWAL(checkpointer, throwOnWalReplayFailure, enableChecksums);
    }
}

void WALReplayer::replayFrozenWAL(Checkpointer& checkpointer, bool throwOnWalReplayFailure,
    bool enableChecksums) const {
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    auto fileInfo =
        vfs->openFile(checkpointWalPath, FileOpenFlags(FileFlags::READ_ONLY | FileFlags::WRITE));
    if (fileInfo->getFileSize() == 0) {
        fileInfo.reset();
        removeWALAndShadowFiles(checkpointWalPath);
        checkpointer.readCheckpoint();
        removePartitionChildShadowFiles(clientContext);
        return;
    }
    syncWALFile(*fileInfo);

    try {
        auto [offsetDeserialized, isLastRecordCheckpoint] =
            dryReplay(*fileInfo, throwOnWalReplayFailure, enableChecksums);
        if (isLastRecordCheckpoint) {
            throwIfReadOnlyCheckpointState(clientContext, true /* hasFrozenWAL */, shadowFilePath);
            ShadowFile::replayShadowPageRecords(clientContext);
            fileInfo.reset();
            removeWALAndShadowFiles(checkpointWalPath);
            checkpointer.readCheckpoint();
            replayPartitionChildShadowPages(clientContext);
        } else {
            removeFileAndSyncParentDirectory(shadowFilePath);
            checkpointer.readCheckpoint();
            removePartitionChildShadowFiles(clientContext);
            Deserializer deserializer = initDeserializer(*fileInfo, clientContext, enableChecksums);
            if (offsetDeserialized > 0) {
                deserializer.getReader()->onObjectBegin();
                const auto walHeader = readWALHeader(deserializer);
                FileDBIDUtils::verifyDatabaseID(*fileInfo,
                    StorageManager::Get(clientContext)->getOrInitDatabaseID(clientContext),
                    walHeader.databaseID);
                deserializer.getReader()->onObjectEnd();
            }
            while (getReadOffset(deserializer, enableChecksums) < offsetDeserialized) {
                DASSERT(!deserializer.finished());
                auto walRecord = WALRecord::deserialize(deserializer, clientContext);
                replayWALRecord(*walRecord);
            }
            fileInfo.reset();
            removeFileAndSyncParentDirectory(checkpointWalPath);
        }
    } catch (const std::exception&) {
        auto transactionContext = TransactionContext::Get(clientContext);
        if (transactionContext->hasActiveTransaction()) {
            transactionContext->rollback();
        }
        throw;
    }
}

void WALReplayer::replayActiveWAL(Checkpointer& checkpointer, bool throwOnWalReplayFailure,
    bool enableChecksums) const {
    auto fileInfo = openWALFile();
    if (fileInfo->getFileSize() == 0) {
        fileInfo.reset();
        removeWALAndShadowFiles(walPath);
        removePartitionChildShadowFiles(clientContext);
        return;
    }
    syncWALFile(*fileInfo);

    try {
        auto [offsetDeserialized, isLastRecordCheckpoint] =
            dryReplay(*fileInfo, throwOnWalReplayFailure, enableChecksums);
        if (isLastRecordCheckpoint) {
            throwIfReadOnlyCheckpointState(clientContext, true /* hasFrozenWAL */, shadowFilePath);
            ShadowFile::replayShadowPageRecords(clientContext);
            fileInfo.reset();
            removeWALAndShadowFiles(walPath);
            checkpointer.readCheckpoint();
            replayPartitionChildShadowPages(clientContext);
        } else {
            removeFileAndSyncParentDirectory(shadowFilePath);
            removePartitionChildShadowFiles(clientContext);
            Deserializer deserializer = initDeserializer(*fileInfo, clientContext, enableChecksums);
            if (offsetDeserialized > 0) {
                deserializer.getReader()->onObjectBegin();
                const auto walHeader = readWALHeader(deserializer);
                FileDBIDUtils::verifyDatabaseID(*fileInfo,
                    StorageManager::Get(clientContext)->getOrInitDatabaseID(clientContext),
                    walHeader.databaseID);
                deserializer.getReader()->onObjectEnd();
            }
            while (getReadOffset(deserializer, enableChecksums) < offsetDeserialized) {
                DASSERT(!deserializer.finished());
                auto walRecord = WALRecord::deserialize(deserializer, clientContext);
                replayWALRecord(*walRecord);
            }
            truncateWALFile(*fileInfo, offsetDeserialized);
        }
    } catch (const std::exception&) {
        auto transactionContext = TransactionContext::Get(clientContext);
        if (transactionContext->hasActiveTransaction()) {
            transactionContext->rollback();
        }
        throw;
    }
}

WALReplayer::WALReplayInfo WALReplayer::dryReplay(FileInfo& fileInfo, bool throwOnWalReplayFailure,
    bool enableChecksums) const {
    uint64_t offsetDeserialized = 0;
    bool isLastRecordCheckpoint = false;
    try {
        Deserializer deserializer = initDeserializer(fileInfo, clientContext, enableChecksums);

        // Skip the databaseID here, we'll verify it when we actually replay
        deserializer.getReader()->onObjectBegin();
        const auto walHeader = readWALHeader(deserializer);
        checkWALHeader(walHeader, enableChecksums);
        deserializer.getReader()->onObjectEnd();

        bool finishedDeserializing = deserializer.finished();
        while (!finishedDeserializing) {
            auto walRecord = WALRecord::deserialize(deserializer, clientContext);
            finishedDeserializing = deserializer.finished();
            switch (walRecord->type) {
            case WALRecordType::CHECKPOINT_RECORD: {
                DASSERT(finishedDeserializing);
                // If we reach a checkpoint record, we can stop replaying.
                isLastRecordCheckpoint = true;
                finishedDeserializing = true;
                offsetDeserialized = getReadOffset(deserializer, enableChecksums);
            } break;
            case WALRecordType::COMMIT_RECORD: {
                // Update the offset to the end of the last commit record.
                offsetDeserialized = getReadOffset(deserializer, enableChecksums);
            } break;
            default: {
                // DO NOTHING.
            }
            }
        }
    } catch (...) {
        // If we hit an exception while deserializing, we assume that the WAL file is (partially)
        // corrupted. This should only happen for records of the last transaction recorded.
        if (throwOnWalReplayFailure) {
            throw;
        }
    }
    return {offsetDeserialized, isLastRecordCheckpoint};
}

void WALReplayer::replayWALRecord(WALRecord& walRecord) const {
    switch (walRecord.type) {
    case WALRecordType::BEGIN_TRANSACTION_RECORD: {
        TransactionContext::Get(clientContext)->beginRecoveryTransaction();
    } break;
    case WALRecordType::COMMIT_RECORD: {
        TransactionContext::Get(clientContext)->commit();
    } break;
    case WALRecordType::CREATE_CATALOG_ENTRY_RECORD: {
        replayCreateCatalogEntryRecord(walRecord);
    } break;
    case WALRecordType::CREATE_INDEX_RECORD: {
        replayCreateIndexRecord(walRecord);
    } break;
    case WALRecordType::DROP_CATALOG_ENTRY_RECORD: {
        replayDropCatalogEntryRecord(walRecord);
    } break;
    case WALRecordType::ALTER_TABLE_ENTRY_RECORD: {
        replayAlterTableEntryRecord(walRecord);
    } break;
    case WALRecordType::TABLE_INSERTION_RECORD: {
        replayTableInsertionRecord(walRecord);
    } break;
    case WALRecordType::NODE_DELETION_RECORD: {
        replayNodeDeletionRecord(walRecord);
    } break;
    case WALRecordType::NODE_UPDATE_RECORD: {
        replayNodeUpdateRecord(walRecord);
    } break;
    case WALRecordType::REL_DELETION_RECORD: {
        replayRelDeletionRecord(walRecord);
    } break;
    case WALRecordType::REL_DETACH_DELETE_RECORD: {
        replayRelDetachDeletionRecord(walRecord);
    } break;
    case WALRecordType::REL_UPDATE_RECORD: {
        replayRelUpdateRecord(walRecord);
    } break;
    case WALRecordType::COPY_TABLE_RECORD: {
        replayCopyTableRecord(walRecord);
    } break;
    case WALRecordType::UPDATE_SEQUENCE_RECORD: {
        replayUpdateSequenceRecord(walRecord);
    } break;
    case WALRecordType::LOAD_EXTENSION_RECORD: {
        replayLoadExtensionRecord(walRecord);
    } break;
    case WALRecordType::CHECKPOINT_RECORD: {
        // This record should not be replayed during individual record replay.
        // CHECKPOINT records are handled by the frozen/active WAL recovery path
        // that replays shadow pages as a batch. If we reach here, the WAL file has
        // been corrupted (or checksums are disabled and a torn write happened to
        // produce type byte 254). Throw a descriptive error instead of asserting
        // so that the database can still be opened with throwOnWalReplayFailure=false.
        throw common::RuntimeException(std::format(
            "WAL replay encountered a CHECKPOINT record outside the expected recovery "
            "path. This is caused by a corrupted WAL tail. "
            "Set throwOnWalReplayFailure=false to discard the tail and open the database."));
    }
    default:
        throw common::RuntimeException(std::format(
            "WAL replay encountered an unknown record type ({}). "
            "The WAL file is corrupted. "
            "Set throwOnWalReplayFailure=false to discard the tail and open the database.",
            static_cast<int>(walRecord.type)));
    }
}

void WALReplayer::removeWALAndShadowFiles(const std::string& walFilePath) const {
    const bool walRemoved = removeFileIfExists(walFilePath);
    const bool shadowRemoved = removeFileIfExists(shadowFilePath);
    if (walRemoved || shadowRemoved) {
        syncParentDirectoryForLocalPath(walRemoved ? walFilePath : shadowFilePath);
    }
}

void WALReplayer::removeFileAndSyncParentDirectory(const std::string& path) const {
    if (removeFileIfExists(path)) {
        syncParentDirectoryForLocalPath(path);
    }
}

bool WALReplayer::removeFileIfExists(const std::string& path) const {
    if (StorageManager::Get(clientContext)->isReadOnly()) {
        return false;
    }
    auto vfs = VirtualFileSystem::GetUnsafe(clientContext);
    if (vfs->fileOrPathExists(path, &clientContext)) {
        vfs->removeFileIfExists(path);
        return true;
    }
    return false;
}

std::unique_ptr<FileInfo> WALReplayer::openWALFile() const {
    auto flag = FileFlags::READ_ONLY;
    if (!StorageManager::Get(clientContext)->isReadOnly()) {
        flag |= FileFlags::WRITE; // The write flag here is to ensure the file is opened with O_RDWR
                                  // so that we can sync it.
    }
    return VirtualFileSystem::GetUnsafe(clientContext)->openFile(walPath, FileOpenFlags(flag));
}

void WALReplayer::syncWALFile(const FileInfo& fileInfo) const {
    if (StorageManager::Get(clientContext)->isReadOnly()) {
        return;
    }
    fileInfo.syncFile();
}

void WALReplayer::truncateWALFile(FileInfo& fileInfo, uint64_t size) const {
    if (StorageManager::Get(clientContext)->isReadOnly()) {
        return;
    }
    if (fileInfo.getFileSize() > size) {
        fileInfo.truncate(size);
        fileInfo.syncFile();
    }
}

} // namespace storage
} // namespace lbug