From: Michael Vrable Date: Tue, 7 Sep 2010 21:10:50 +0000 (-0700) Subject: Add partial journal replay to filesystem recovery. X-Git-Url: https://git.vrable.net/?a=commitdiff_plain;h=0a021d1c28cff9cbaa4c127a837816f96dd37a27;p=bluesky.git Add partial journal replay to filesystem recovery. After loading a cloud checkpoint, replay just the last portion of the journal that may not have been committed to the cloud. --- diff --git a/bluesky/bluesky-private.h b/bluesky/bluesky-private.h index 5bb18f9..85eb99c 100644 --- a/bluesky/bluesky-private.h +++ b/bluesky/bluesky-private.h @@ -187,6 +187,11 @@ typedef enum { LOGTYPE_INODE = 2, LOGTYPE_INODE_MAP = 3, LOGTYPE_CHECKPOINT = 4, + + /* Used only as metadata in the local journal, not loaded as a + * BlueSkyCloudLogState nor stored in the cloud */ + LOGTYPE_JOURNAL_MARKER = 16, + LOGTYPE_JOURNAL_CHECKPOINT = 17, } BlueSkyCloudLogType; /* Headers that go on items in local log segments and cloud log segments. */ @@ -344,6 +349,9 @@ struct _BlueSkyCacheFile { BlueSkyLog *bluesky_log_new(const char *log_directory); void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log); void bluesky_log_finish_all(GList *log_items); +BlueSkyCloudLog *bluesky_log_get_commit_point(BlueSkyFS *fs); +void bluesky_log_write_commit_point(BlueSkyFS *fs, BlueSkyCloudLog *marker); + BlueSkyRCStr *bluesky_log_map_object(BlueSkyFS *fs, int log_dir, int log_seq, int log_offset, int log_size); void bluesky_mmap_unref(BlueSkyCacheFile *mmap); diff --git a/bluesky/cache.c b/bluesky/cache.c index 454e06f..c27d39d 100644 --- a/bluesky/cache.c +++ b/bluesky/cache.c @@ -114,6 +114,7 @@ static void flushd_cloud(BlueSkyFS *fs) /* TODO: Locking? Since we're reading a single variable this is probably * atomic but a lock could be safer. */ + BlueSkyCloudLog *marker = bluesky_log_get_commit_point(fs); int journal_seq_start = fs->log->seq_num; while (1) { @@ -177,6 +178,8 @@ static void flushd_cloud(BlueSkyFS *fs) fs->log_state->pending_segments); } + bluesky_log_write_commit_point(fs, marker); + g_print("All segments have been flushed, journal < %d is clean\n", journal_seq_start); diff --git a/bluesky/inode.c b/bluesky/inode.c index 41227fb..e11bfeb 100644 --- a/bluesky/inode.c +++ b/bluesky/inode.c @@ -106,7 +106,7 @@ BlueSkyFS *bluesky_init_fs(gchar *name, BlueSkyStore *store) if (bluesky_checkpoint_load(fs)) { g_print("Filesystem checkpoint loaded, starting journal replay...\n"); - //bluesky_replay(fs); + bluesky_replay(fs); g_print("Journal replay complete, filesystem ready.\n"); } else { /* Initialize a fresh filesystem */ diff --git a/bluesky/log.c b/bluesky/log.c index 82db346..97b4f30 100644 --- a/bluesky/log.c +++ b/bluesky/log.c @@ -285,6 +285,46 @@ void bluesky_log_finish_all(GList *log_items) } } +/* Return a committed cloud log record that can be used as a watermark for how + * much of the journal has been written. */ +BlueSkyCloudLog *bluesky_log_get_commit_point(BlueSkyFS *fs) +{ + BlueSkyCloudLog *marker = bluesky_cloudlog_new(fs, NULL); + marker->type = LOGTYPE_JOURNAL_MARKER; + marker->data = bluesky_string_new(g_strdup(""), 0); + bluesky_cloudlog_sync(marker); + + g_mutex_lock(marker->lock); + while ((marker->pending_write & CLOUDLOG_JOURNAL)) + g_cond_wait(marker->cond, marker->lock); + g_mutex_unlock(marker->lock); + + return marker; +} + +void bluesky_log_write_commit_point(BlueSkyFS *fs, BlueSkyCloudLog *marker) +{ + BlueSkyCloudLog *commit = bluesky_cloudlog_new(fs, NULL); + commit->type = LOGTYPE_JOURNAL_CHECKPOINT; + + uint32_t seq, offset; + seq = GUINT32_TO_LE(marker->log_seq); + offset = GUINT32_TO_LE(marker->log_offset); + GString *loc = g_string_new(""); + g_string_append_len(loc, (const gchar *)&seq, sizeof(seq)); + g_string_append_len(loc, (const gchar *)&offset, sizeof(offset)); + commit->data = bluesky_string_new_from_gstring(loc); + bluesky_cloudlog_sync(commit); + + g_mutex_lock(commit->lock); + while ((commit->pending_write & CLOUDLOG_JOURNAL)) + g_cond_wait(commit->cond, commit->lock); + g_mutex_unlock(commit->lock); + + bluesky_cloudlog_unref(marker); + bluesky_cloudlog_unref(commit); +} + /* Memory-map the given log object into memory (read-only) and return a pointer * to it. */ static int page_size = 0; @@ -632,7 +672,8 @@ static gboolean validate_journal_item(const char *buf, size_t len, off_t offset) /* Scan through a journal segment to extract correctly-written items (those * that pass sanity checks and have a valid checksum). */ -static void bluesky_replay_scan_journal(const char *buf, size_t len) +static void bluesky_replay_scan_journal(const char *buf, size_t len, + uint32_t *seq, uint32_t *start_offset) { const struct log_header *header; off_t offset = 0; @@ -642,6 +683,13 @@ static void bluesky_replay_scan_journal(const char *buf, size_t len) size_t size = GUINT32_FROM_LE(header->size1) + GUINT32_FROM_LE(header->size2) + GUINT32_FROM_LE(header->size3); + + if (header->type - '0' == LOGTYPE_JOURNAL_CHECKPOINT) { + const uint32_t *data = (const uint32_t *)((const char *)header + sizeof(struct log_header)); + *seq = GUINT32_FROM_LE(data[0]); + *start_offset = GUINT32_FROM_LE(data[1]); + } + offset += sizeof(struct log_header) + size + sizeof(struct log_footer); } } @@ -690,11 +738,11 @@ static void reload_item(BlueSkyCloudLog *log_item, } static void bluesky_replay_scan_journal2(BlueSkyFS *fs, GList **objects, - int log_seq, + int log_seq, int start_offset, const char *buf, size_t len) { const struct log_header *header; - off_t offset = 0; + off_t offset = start_offset; while (validate_journal_item(buf, len, offset)) { header = (const struct log_header *)(buf + offset); @@ -765,6 +813,7 @@ void bluesky_replay(BlueSkyFS *fs) /* Scan through log files in reverse order to find the most recent commit * record. */ logfiles = g_list_reverse(logfiles); + uint32_t seq_num = 0, start_offset = 0; while (logfiles != NULL) { char *filename = g_strdup_printf("%s/%s", log->log_directory, (char *)logfiles->data); @@ -774,13 +823,16 @@ void bluesky_replay(BlueSkyFS *fs) g_warning("Mapping logfile %s failed!\n", filename); } else { bluesky_replay_scan_journal(g_mapped_file_get_contents(map), - g_mapped_file_get_length(map)); + g_mapped_file_get_length(map), + &seq_num, &start_offset); g_mapped_file_unref(map); } g_free(filename); g_free(logfiles->data); logfiles = g_list_delete_link(logfiles, logfiles); + if (seq_num != 0 || start_offset != 0) + break; } g_list_foreach(logfiles, (GFunc)g_free, NULL); g_list_free(logfiles); @@ -791,11 +843,10 @@ void bluesky_replay(BlueSkyFS *fs) * references, so that any objects which were not linked into persistent * filesystem data structures are freed. */ GList *objects = NULL; - int seq_num = 0; while (TRUE) { char *filename = g_strdup_printf("%s/journal-%08d", log->log_directory, seq_num); - g_print("Replaying file %s\n", filename); + g_print("Replaying file %s from offset %d\n", filename, start_offset); GMappedFile *map = g_mapped_file_new(filename, FALSE, NULL); g_free(filename); if (map == NULL) { @@ -803,11 +854,12 @@ void bluesky_replay(BlueSkyFS *fs) break; } - bluesky_replay_scan_journal2(fs, &objects, seq_num, + bluesky_replay_scan_journal2(fs, &objects, seq_num, start_offset, g_mapped_file_get_contents(map), g_mapped_file_get_length(map)); g_mapped_file_unref(map); seq_num++; + start_offset = 0; } while (objects != NULL) {