Partial work on parallel data fetches from S3 for large reads.
[bluesky.git] / bluesky / file.c
index 09e4805..822c2c8 100644 (file)
@@ -35,7 +35,7 @@ void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
         block->data = bluesky_string_new(g_malloc0(block_len), block_len);
         break;
     case BLUESKY_BLOCK_REF:
-        bluesky_block_fetch(inode->fs, block);
+        bluesky_block_fetch(inode->fs, block, NULL);
         g_assert(block->type == BLUESKY_BLOCK_CACHED);
         /* Fall through */
     case BLUESKY_BLOCK_CACHED:
@@ -71,7 +71,8 @@ void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
                                              inode->blocks->len - 1);
 
             if (b->type != BLUESKY_BLOCK_ZERO
-                    && b->data->len < BLUESKY_BLOCK_SIZE) {
+                    && (b->type == BLUESKY_BLOCK_REF
+                        || b->data->len < BLUESKY_BLOCK_SIZE)) {
                 bluesky_block_touch(inode, inode->blocks->len - 1);
                 gsize old_size = b->data->len;
                 bluesky_string_resize(b->data, BLUESKY_BLOCK_SIZE);
@@ -168,7 +169,7 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
             memset(buf, 0, bytes);
             break;
         case BLUESKY_BLOCK_REF:
-            bluesky_block_fetch(inode->fs, b);
+            bluesky_block_fetch(inode->fs, b, NULL);
             /* Fall through */
         case BLUESKY_BLOCK_CACHED:
         case BLUESKY_BLOCK_DIRTY:
@@ -184,16 +185,32 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
 
 /* Read the given block from cloud-backed storage if the data is not already
  * cached. */
-void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
+static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
+{
+    BlueSkyBlock *block = (BlueSkyBlock *)data;
+
+    bluesky_string_unref(block->data);
+    block->data = async->data;
+    bluesky_string_ref(block->data);
+    block->type = BLUESKY_BLOCK_CACHED;
+}
+
+void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
+                         BlueSkyStoreAsync *barrier)
 {
     if (block->type != BLUESKY_BLOCK_REF)
         return;
 
-    BlueSkyRCStr *string = bluesky_store_get(fs->store, block->ref);
+    BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
+    async->op = STORE_OP_GET;
+    async->key = g_strdup(block->ref);
+    bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
+    bluesky_store_async_submit(async);
 
-    bluesky_string_unref(block->data);
-    block->data = string;
-    block->type = BLUESKY_BLOCK_CACHED;
+    if (barrier != NULL)
+        bluesky_store_add_barrier(barrier, async);
+    else
+        bluesky_store_async_wait(async);
 }
 
 /* Write the given block to cloud-backed storage and mark it clean. */
@@ -224,13 +241,9 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
     g_free(block->ref);
     block->ref = name;
 
-    /* block->type = BLUESKY_BLOCK_CACHED; */
-    bluesky_string_unref(block->data);
-    block->data = NULL;
-    block->type = BLUESKY_BLOCK_REF;
+    block->type = BLUESKY_BLOCK_CACHED;
 
     g_checksum_free(csum);
-    //bluesky_string_unref(data);
 }
 
 /* Flush all blocks in a file to stable storage. */
@@ -243,3 +256,22 @@ void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier)
         bluesky_block_flush(inode->fs, b, barrier);
     }
 }
+
+/* Drop clean data blocks for a file from cache. */
+void bluesky_file_drop_cached(BlueSkyInode *inode)
+{
+    g_return_if_fail(inode->type == BLUESKY_REGULAR);
+
+    for (int i = 0; i < inode->blocks->len; i++) {
+        BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
+        if (b->type == BLUESKY_BLOCK_CACHED) {
+            g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
+                  "Dropping block %d of inode %"PRIu64" from cache",
+                  i, inode->inum);
+
+            bluesky_string_unref(b->data);
+            b->data = NULL;
+            b->type = BLUESKY_BLOCK_REF;
+        }
+    }
+}