Finish up loading of checkpoints from cloud logs.
[bluesky.git] / bluesky / store.c
index 50d7a21..af25ca3 100644 (file)
@@ -24,6 +24,8 @@ struct _BlueSkyStore {
     GMutex *lock;
     GCond *cond_idle;
     int pending;                /* Count of operations not yet complete. */
+
+    struct bluesky_stats *stats_get, *stats_put;
 };
 
 GHashTable *store_implementations;
@@ -71,6 +73,10 @@ BlueSkyStore *bluesky_store_new(const gchar *type)
     store->lock = g_mutex_new();
     store->cond_idle = g_cond_new();
     store->pending = 0;
+    store->stats_get = bluesky_stats_new(g_strdup_printf("Store[%s]: GETS",
+                                                         type));
+    store->stats_put = bluesky_stats_new(g_strdup_printf("Store[%s]: PUTS",
+                                                         type));
     g_free(scheme);
     return store;
 }
@@ -81,6 +87,11 @@ void bluesky_store_free(BlueSkyStore *store)
     g_free(store);
 }
 
+char *bluesky_store_lookup_last(BlueSkyStore *store, const char *prefix)
+{
+    return store->impl->lookup_last(store->handle, prefix);
+}
+
 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
 {
     BlueSkyStoreAsync *async;
@@ -199,11 +210,13 @@ void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
     bluesky_time_hires elapsed = bluesky_now_hires() - async->start_time;
     bluesky_time_hires latency = bluesky_now_hires() - async->exec_time;
 
-    g_mutex_lock(async->store->lock);
-    async->store->pending--;
-    if (async->store->pending == 0)
-        g_cond_broadcast(async->store->cond_idle);
-    g_mutex_unlock(async->store->lock);
+    if (async->op != STORE_OP_BARRIER) {
+        g_mutex_lock(async->store->lock);
+        async->store->pending--;
+        if (async->store->pending == 0)
+            g_cond_broadcast(async->store->cond_idle);
+        g_mutex_unlock(async->store->lock);
+    }
 
     async->status = ASYNC_COMPLETE;
     g_cond_broadcast(async->completion_cond);
@@ -217,9 +230,19 @@ void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
         g_thread_pool_push(notifier_thread_pool, nl, NULL);
     }
 
-    g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
-          "[%p] complete: elapsed = %"PRIi64" ns, latency = %"PRIi64" ns",
-          async, elapsed, latency);
+    if (bluesky_verbose) {
+        g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
+              "[%p] complete: elapsed = %"PRIi64" ns, latency = %"PRIi64" ns",
+              async, elapsed, latency);
+    }
+
+    if (async->data) {
+        if (async->op == STORE_OP_GET) {
+            bluesky_stats_add(async->store->stats_get, async->data->len);
+        } else if (async->op == STORE_OP_PUT) {
+            bluesky_stats_add(async->store->stats_put, async->data->len);
+        }
+    }
 }
 
 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
@@ -232,13 +255,15 @@ void bluesky_store_async_submit(BlueSkyStoreAsync *async)
     // processing was started, if there could be a delay from submission time.
     async->exec_time = bluesky_now_hires();
 
-    g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "[%p] submit: %s %s",
-          async,
-          async->op == STORE_OP_GET ? "GET"
-            : async->op == STORE_OP_PUT ? "PUT"
-            : async->op == STORE_OP_DELETE ? "DELETE"
-            : async->op == STORE_OP_BARRIER ? "BARRIER" : "???",
-          async->key);
+    if (bluesky_verbose) {
+        g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "[%p] submit: %s %s",
+              async,
+              async->op == STORE_OP_GET ? "GET"
+                : async->op == STORE_OP_PUT ? "PUT"
+                : async->op == STORE_OP_DELETE ? "DELETE"
+                : async->op == STORE_OP_BARRIER ? "BARRIER" : "???",
+              async->key);
+    }
 
     /* Barriers are handled specially, and not handed down the storage
      * implementation layer. */
@@ -272,13 +297,15 @@ void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
     g_mutex_unlock(barrier->lock);
 
     g_mutex_lock(async->lock);
-    if (async->barrier == NULL) {
+    if (async->barrier == NULL && async->status != ASYNC_COMPLETE) {
         async->barrier = barrier;
+        g_mutex_unlock(async->lock);
     } else {
-        g_warning("Adding async to more than one barrier!\n");
+        if (async->barrier != NULL)
+            g_warning("Adding async to more than one barrier!\n");
+        g_mutex_unlock(async->lock);
         bluesky_store_async_add_notifier(async, op_complete, barrier);
     }
-    g_mutex_unlock(async->lock);
 }
 
 static void notifier_task(gpointer n, gpointer s)
@@ -300,12 +327,17 @@ static void notifier_task(gpointer n, gpointer s)
 void bluesky_store_sync(BlueSkyStore *store)
 {
     g_mutex_lock(store->lock);
-    g_print("Waiting for pending store operations to complete...\n");
+    if (bluesky_verbose) {
+        g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
+              "Waiting for pending store operations to complete...");
+    }
     while (store->pending > 0) {
         g_cond_wait(store->cond_idle, store->lock);
     }
     g_mutex_unlock(store->lock);
-    g_print("Operations are complete.\n");
+    if (bluesky_verbose) {
+        g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "Operations are complete.");
+    }
 }
 
 /* Convenience wrappers that perform a single operation synchronously. */
@@ -467,11 +499,61 @@ static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
 {
 }
 
+static char *filestore_lookup_last(gpointer store, const char *prefix)
+{
+    char *last = NULL;
+    GDir *dir = g_dir_open(".", 0, NULL);
+    if (dir == NULL) {
+        g_warning("Unable to open directory for listing");
+        return NULL;
+    }
+
+    const gchar *file;
+    while ((file = g_dir_read_name(dir)) != NULL) {
+        if (strncmp(file, prefix, strlen(prefix)) == 0) {
+            if (last == NULL || strcmp(file, last) > 0) {
+                g_free(last);
+                last = g_strdup(file);
+            }
+        }
+    }
+    g_dir_close(dir);
+
+    return last;
+}
+
 static BlueSkyStoreImplementation filestore_impl = {
     .create = filestore_create,
     .destroy = filestore_destroy,
     .submit = filestore_submit,
     .cleanup = filestore_cleanup,
+    .lookup_last = filestore_lookup_last,
+};
+
+/* A store implementation which simply discards all data, for testing. */
+static gpointer nullstore_create(const gchar *path)
+{
+    return (gpointer)nullstore_create;
+}
+
+static void nullstore_destroy(gpointer store)
+{
+}
+
+static void nullstore_submit(gpointer s, BlueSkyStoreAsync *async)
+{
+    bluesky_store_async_mark_complete(async);
+}
+
+static void nullstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
+{
+}
+
+static BlueSkyStoreImplementation nullstore_impl = {
+    .create = nullstore_create,
+    .destroy = nullstore_destroy,
+    .submit = nullstore_submit,
+    .cleanup = nullstore_cleanup,
 };
 
 void bluesky_store_init()
@@ -481,4 +563,5 @@ void bluesky_store_init()
                                              bluesky_max_threads, FALSE, NULL);
     bluesky_store_register(&memstore_impl, "mem");
     bluesky_store_register(&filestore_impl, "file");
+    bluesky_store_register(&nullstore_impl, "null");
 }