From: Michael Vrable Date: Wed, 28 Jul 2010 19:05:05 +0000 (-0700) Subject: Preparatory work before implementing proper cloud writing. X-Git-Url: http://git.vrable.net/?a=commitdiff_plain;h=db4915b4f7fc8f74958c4d1891dc69b76cbbe383;p=bluesky.git Preparatory work before implementing proper cloud writing. --- diff --git a/bluesky/bluesky-private.h b/bluesky/bluesky-private.h index 822e029..3cabcd1 100644 --- a/bluesky/bluesky-private.h +++ b/bluesky/bluesky-private.h @@ -236,6 +236,13 @@ typedef struct { BlueSkyRCStr *data; } BlueSkyCloudLog; +/* Serialize objects into a log segment to be written to the cloud. */ +struct _BlueSkyCloudLogState { + GString *data; + BlueSkyCloudPointer location; + GList *inode_list; +}; + gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b); guint bluesky_cloudlog_hash(gconstpointer a); BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs); diff --git a/bluesky/bluesky.h b/bluesky/bluesky.h index 9543d9c..f72c6d0 100644 --- a/bluesky/bluesky.h +++ b/bluesky/bluesky.h @@ -96,6 +96,9 @@ typedef struct _BlueSkyStore BlueSkyStore; struct _BlueSkyLog; typedef struct _BlueSkyLog BlueSkyLog; +struct _BlueSkyCloudLogState; +typedef struct _BlueSkyCloudLogState BlueSkyCloudLogState; + void bluesky_store_init(); BlueSkyStore *bluesky_store_new(const gchar *type); void bluesky_store_free(BlueSkyStore *store); @@ -130,6 +133,7 @@ typedef struct { BlueSkyStore *store; BlueSkyLog *log; + BlueSkyCloudLogState *log_state; /* Accounting for memory used for caches. Space is measured in blocks, not * bytes. We track both total data in the caches and dirty data (total @@ -142,7 +146,9 @@ typedef struct { * avoid deadlock do not attempt to take any other locks while the FS lock * is held for list editing purposes. Items at the head of the list are * most recently accessed/modified. */ - GList dirty_list, accessed_list; + GList unlogged_list; // Changes not yet synced to journal + GList dirty_list; // Not yet written to cloud storage + GList accessed_list; // All in-memory inodes /* Mutex for the flush daemon, to prevent concurrent execution. */ GMutex *flushd_lock; @@ -218,7 +224,7 @@ typedef struct { * dirty linked lists. We re-use the GList structure, using ->next to * point to the head of the list and ->prev to point to the tail. The data * element is unused. */ - GList *accessed_list, *dirty_list; + GList *unlogged_list, *accessed_list, *dirty_list; int64_t atime; /* Microseconds since the Unix epoch */ int64_t ctime; diff --git a/bluesky/cache.c b/bluesky/cache.c index 6a91873..441e6c4 100644 --- a/bluesky/cache.c +++ b/bluesky/cache.c @@ -15,7 +15,12 @@ #define WRITEBACK_DELAY (20 * 1000000) -/* Filesystem caching and cache coherency. */ +/* Filesystem caching and cache coherency. There are actually a couple of + * different tasks that are performed here: + * - Forcing data to the log if needed to reclaim memory or simply if the + * data has been dirty in memory long enough. + * - Writing batches of data to the cloud. + */ static void writeback_complete(gpointer a, gpointer i) { @@ -42,86 +47,6 @@ static void writeback_complete(gpointer a, gpointer i) g_mutex_unlock(inode->lock); } -#if 0 -static void flushd_inode(gpointer value, gpointer user_data) -{ - BlueSkyFS *fs = (BlueSkyFS *)user_data; - - BlueSkyInode *inode = (BlueSkyInode *)value; - - g_mutex_lock(inode->lock); - - if (inode->change_count == inode->change_commit) { - uint64_t delay = bluesky_get_current_time() - inode->access_time; - if (delay >= CACHE_CLEAN_DELAY) { - drop_caches(inode); - - /* If the only references are the one we hold and the one in the - * filesystem inum->inode hash table... First check the refcount - * without the lock for speed, but if the check looks good verify - * it after taking the filesystem lock. */ - if (inode->refcount == 2) { - g_mutex_lock(fs->lock); - if (inode->refcount == 2) { - g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG, - "Trying to drop inode %"PRIu64" from cache", - inode->inum); - if (g_hash_table_remove(fs->inodes, &inode->inum)) - bluesky_inode_unref(inode); - } - bluesky_list_unlink(&inode->fs->accessed_list, - inode->accessed_list); - inode->accessed_list = NULL; - bluesky_list_unlink(&inode->fs->dirty_list, - inode->dirty_list); - inode->dirty_list = NULL; - g_mutex_unlock(fs->lock); - } - } - - g_mutex_unlock(inode->lock); - bluesky_inode_unref(inode); - return; - } - - if (inode->change_pending) { - /* Waiting for an earlier writeback to finish, so don't start a new - * writeback yet. */ - g_mutex_unlock(inode->lock); - bluesky_inode_unref(inode); - return; - } - - uint64_t elapsed = bluesky_get_current_time() - inode->change_time; - if (elapsed < WRITEBACK_DELAY) { - /* Give a bit more time before starting writeback. */ - g_mutex_unlock(inode->lock); - bluesky_inode_unref(inode); - return; - } - - inode->change_pending = inode->change_count; - - g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG, - "Starting flush of inode %"PRIu64, inode->inum); - - /* Create a store barrier. All operations part of the writeback will be - * added to this barrier, so when the barrier completes we know that the - * writeback is finished. */ - BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store); - barrier->op = STORE_OP_BARRIER; - - bluesky_inode_start_sync(inode, barrier); - - bluesky_store_async_add_notifier(barrier, writeback_complete, inode); - bluesky_store_async_submit(barrier); - bluesky_store_async_unref(barrier); - - g_mutex_unlock(inode->lock); - bluesky_inode_unref(inode); -} -#endif - static void flushd_dirty_inode(BlueSkyInode *inode) { BlueSkyFS *fs = inode->fs; @@ -269,8 +194,8 @@ static gpointer flushd_task(BlueSkyFS *fs) if (!g_mutex_trylock(fs->flushd_lock)) return NULL; flushd_dirty(fs); - flushd_clean(fs); bluesky_cloudlog_write_log(fs); + flushd_clean(fs); g_mutex_unlock(fs->flushd_lock); return NULL; diff --git a/bluesky/cloudlog.c b/bluesky/cloudlog.c index 84e63d8..e1fd44a 100644 --- a/bluesky/cloudlog.c +++ b/bluesky/cloudlog.c @@ -127,13 +127,6 @@ void bluesky_cloudlog_insert(BlueSkyCloudLog *log) g_mutex_unlock(log->fs->lock); } -/* Serialize objects into a log segment to be written to the cloud. */ -struct log_state { - GString *data; - BlueSkyCloudPointer location; - GList *inode_list; -}; - struct log_header { char magic[4]; uint32_t size; @@ -152,7 +145,7 @@ struct log_footer { }; BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log, - struct log_state *state) + BlueSkyCloudLogState *state) { if (log->location_flags & CLOUDLOG_CLOUD) { return log->location; @@ -198,7 +191,7 @@ BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log, static void find_inodes(gpointer key, gpointer value, gpointer user_data) { - struct log_state *state = (struct log_state *)user_data; + BlueSkyCloudLogState *state = (BlueSkyCloudLogState *)user_data; BlueSkyCloudLog *item = (BlueSkyCloudLog *)value; if (item->type != LOGTYPE_INODE) @@ -212,34 +205,33 @@ void bluesky_cloudlog_write_log(BlueSkyFS *fs) { g_print("Starting cloudlog write...\n"); - struct log_state state; - state.data = g_string_new(""); - state.location.directory = 0; - state.location.sequence = 0; - state.location.offset = 0; - state.location.size = 0; - state.inode_list = NULL; + BlueSkyCloudLogState *state = fs->log_state; + state->data = g_string_new(""); g_mutex_lock(fs->lock); - g_hash_table_foreach(fs->locations, find_inodes, &state); + g_hash_table_foreach(fs->locations, find_inodes, state); g_mutex_unlock(fs->lock); - while (state.inode_list != NULL) { - BlueSkyCloudLog *log = (BlueSkyCloudLog *)state.inode_list->data; - bluesky_cloudlog_serialize(log, &state); - state.inode_list = g_list_delete_link(state.inode_list, - state.inode_list); + while (state->inode_list != NULL) { + BlueSkyCloudLog *log = (BlueSkyCloudLog *)state->inode_list->data; + bluesky_cloudlog_serialize(log, state); + state->inode_list = g_list_delete_link(state->inode_list, + state->inode_list); } - g_print("Serialized %zd bytes of data\n", state.data->len); + g_print("Serialized %zd bytes of data\n", state->data->len); BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store); async->op = STORE_OP_PUT; async->key = g_strdup_printf("log-%08d-%08d", - state.location.directory, - state.location.sequence); - async->data = bluesky_string_new_from_gstring(state.data); + state->location.directory, + state->location.sequence); + async->data = bluesky_string_new_from_gstring(state->data); bluesky_store_async_submit(async); bluesky_store_async_wait(async); bluesky_store_async_unref(async); + + state->data = NULL; + state->location.sequence++; + state->location.offset = 0; } diff --git a/bluesky/debug.c b/bluesky/debug.c index cfb6a81..ca09868 100644 --- a/bluesky/debug.c +++ b/bluesky/debug.c @@ -57,6 +57,11 @@ void bluesky_debug_dump(BlueSkyFS *fs) g_hash_table_size(fs->inodes), fs->next_inum); GList *item; + g_print("Unsynced inode list:"); + for (item = fs->unlogged_list.next; item != NULL; item = item->next) { + g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum); + } + g_print("\n"); g_print("Dirty inode LRU list:"); for (item = fs->dirty_list.next; item != NULL; item = item->next) { g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum); diff --git a/bluesky/inode.c b/bluesky/inode.c index 2780253..745d81b 100644 --- a/bluesky/inode.c +++ b/bluesky/inode.c @@ -48,6 +48,8 @@ void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime) #endif g_mutex_lock(inode->fs->lock); + bluesky_list_unlink(&inode->fs->unlogged_list, inode->unlogged_list); + inode->unlogged_list = bluesky_list_prepend(&inode->fs->unlogged_list, inode); bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list); inode->dirty_list = bluesky_list_prepend(&inode->fs->dirty_list, inode); bluesky_list_unlink(&inode->fs->accessed_list, inode->accessed_list); @@ -89,6 +91,9 @@ BlueSkyFS *bluesky_new_fs(gchar *name) fs->locations = g_hash_table_new(bluesky_cloudlog_hash, bluesky_cloudlog_equal); + fs->log_state = g_new0(BlueSkyCloudLogState, 1); + fs->log_state->data = g_string_new(""); + return fs; } @@ -144,6 +149,7 @@ void bluesky_inode_unref(BlueSkyInode *inode) /* Sanity check: Is the inode clean? */ if (inode->change_commit < inode->change_count || inode->accessed_list != NULL + || inode->unlogged_list != NULL || inode->dirty_list != NULL) { g_warning("Dropping inode which is not clean (commit %"PRIi64" < change %"PRIi64"; accessed_list = %p; dirty_list = %p)\n", inode->change_commit, inode->change_count, inode->accessed_list, inode->dirty_list); } @@ -154,6 +160,7 @@ void bluesky_inode_unref(BlueSkyInode *inode) g_mutex_lock(inode->fs->lock); bluesky_list_unlink(&inode->fs->accessed_list, inode->accessed_list); bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list); + bluesky_list_unlink(&inode->fs->unlogged_list, inode->unlogged_list); g_mutex_unlock(inode->fs->lock); /* Free file type specific data. It should be an error for there to be