Disable most debugging print messages; should help with performance.
[bluesky.git] / bluesky / store.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <glib.h>
11 #include <string.h>
12
13 #include "bluesky-private.h"
14
15 /* Interaction with cloud storage.  We expose very simple GET/PUT style
16  * interface, which different backends can implement.  Available backends
17  * (will) include Amazon S3 and a simple local store for testing purposes.
18  * Operations may be performed asynchronously. */
19
20 struct _BlueSkyStore {
21     const BlueSkyStoreImplementation *impl;
22     gpointer handle;
23
24     GMutex *lock;
25     GCond *cond_idle;
26     int pending;                /* Count of operations not yet complete. */
27 };
28
29 GHashTable *store_implementations;
30
31 /* Thread pool for calling notifier functions when an operation completes.
32  * These are called in a separate thread for locking reasons: we want to call
33  * the notifiers without the lock on the async object held, but completion
34  * occurs when the lock is held--so we need some way to defer the call.  This
35  * isn't really optimal from a cache-locality standpoint. */
36 static GThreadPool *notifier_thread_pool;
37
38 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
39                             const gchar *name)
40 {
41     g_hash_table_insert(store_implementations, g_strdup(name), (gpointer)impl);
42 }
43
44 BlueSkyStore *bluesky_store_new(const gchar *type)
45 {
46     const BlueSkyStoreImplementation *impl;
47
48     gchar *scheme, *path;
49     scheme = g_strdup(type);
50     path = strchr(scheme, ':');
51     if (path != NULL) {
52         *path = '\0';
53         path++;
54     }
55
56     impl = g_hash_table_lookup(store_implementations, scheme);
57     if (impl == NULL) {
58         g_free(scheme);
59         return NULL;
60     }
61
62     gpointer handle = impl->create(path);
63     if (handle == NULL) {
64         g_free(scheme);
65         return NULL;
66     }
67
68     BlueSkyStore *store = g_new(BlueSkyStore, 1);
69     store->impl = impl;
70     store->handle = handle;
71     store->lock = g_mutex_new();
72     store->cond_idle = g_cond_new();
73     store->pending = 0;
74     g_free(scheme);
75     return store;
76 }
77
78 void bluesky_store_free(BlueSkyStore *store)
79 {
80     store->impl->destroy(store->handle);
81     g_free(store);
82 }
83
84 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
85 {
86     BlueSkyStoreAsync *async;
87
88     async = g_new(BlueSkyStoreAsync, 1);
89     async->store = store;
90     async->lock = g_mutex_new();
91     async->completion_cond = g_cond_new();
92     async->refcount = 1;
93     async->status = ASYNC_NEW;
94     async->op = STORE_OP_NONE;
95     async->key = NULL;
96     async->data = NULL;
97     async->result = -1;
98     async->notifiers = NULL;
99     async->notifier_count = 0;
100     async->barrier = NULL;
101     async->store_private = NULL;
102
103     return async;
104 }
105
106 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async)
107 {
108     return async->store->handle;
109 }
110
111 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
112 {
113     if (async == NULL)
114         return;
115
116     g_return_if_fail(g_atomic_int_get(&async->refcount) > 0);
117
118     g_atomic_int_inc(&async->refcount);
119 }
120
121 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
122 {
123     if (async == NULL)
124         return;
125
126     if (g_atomic_int_dec_and_test(&async->refcount)) {
127         async->store->impl->cleanup(async->store->handle, async);
128         g_mutex_free(async->lock);
129         g_cond_free(async->completion_cond);
130         g_free(async->key);
131         bluesky_string_unref(async->data);
132         g_free(async);
133     }
134 }
135
136 /* Block until the given operation has completed. */
137 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
138 {
139     g_return_if_fail(async != NULL);
140     g_mutex_lock(async->lock);
141
142     if (async->status == ASYNC_NEW) {
143         g_error("bluesky_store_async_wait on a new async object!\n");
144         g_mutex_unlock(async->lock);
145         return;
146     }
147
148     while (async->status != ASYNC_COMPLETE
149            || g_atomic_int_get(&async->notifier_count) > 0) {
150         g_cond_wait(async->completion_cond, async->lock);
151     }
152
153     g_mutex_unlock(async->lock);
154 }
155
156 /* Add a notifier function to be called when the operation completes. */
157 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
158                                       GFunc func, gpointer user_data)
159 {
160     struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
161     g_mutex_lock(async->lock);
162     nl->next = async->notifiers;
163     nl->func = func;
164     nl->async = async; bluesky_store_async_ref(async);
165     nl->user_data = user_data;
166     g_atomic_int_inc(&async->notifier_count);
167     if (async->status == ASYNC_COMPLETE) {
168         g_thread_pool_push(notifier_thread_pool, nl, NULL);
169     } else {
170         async->notifiers = nl;
171     }
172     g_mutex_unlock(async->lock);
173 }
174
175 static void op_complete(gpointer a, gpointer b)
176 {
177     BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
178
179     bluesky_store_async_ref(barrier);
180     g_mutex_lock(barrier->lock);
181     barrier->store_private
182         = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) - 1);
183     if (GPOINTER_TO_INT(barrier->store_private) == 0
184             && barrier->status != ASYNC_NEW) {
185         bluesky_store_async_mark_complete(barrier);
186     }
187     g_mutex_unlock(barrier->lock);
188     bluesky_store_async_unref(barrier);
189 }
190
191 /* Mark an asynchronous operation as complete.  This should only be called by
192  * the store implementations.  The lock should be held when calling this
193  * function.  Any notifier functions will be called, but in a separate thread
194  * and without the lock held. */
195 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
196 {
197     g_return_if_fail(async->status != ASYNC_COMPLETE);
198
199     bluesky_time_hires elapsed = bluesky_now_hires() - async->start_time;
200     bluesky_time_hires latency = bluesky_now_hires() - async->exec_time;
201
202     if (async->op != STORE_OP_BARRIER) {
203         g_mutex_lock(async->store->lock);
204         async->store->pending--;
205         if (async->store->pending == 0)
206             g_cond_broadcast(async->store->cond_idle);
207         g_mutex_unlock(async->store->lock);
208     }
209
210     async->status = ASYNC_COMPLETE;
211     g_cond_broadcast(async->completion_cond);
212
213     if (async->barrier != NULL && async->notifiers == NULL)
214         op_complete(async, async->barrier);
215
216     while (async->notifiers != NULL) {
217         struct BlueSkyNotifierList *nl = async->notifiers;
218         async->notifiers = nl->next;
219         g_thread_pool_push(notifier_thread_pool, nl, NULL);
220     }
221
222     if (bluesky_verbose) {
223         g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
224               "[%p] complete: elapsed = %"PRIi64" ns, latency = %"PRIi64" ns",
225               async, elapsed, latency);
226     }
227 }
228
229 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
230 {
231     BlueSkyStore *store = async->store;
232
233     async->start_time = bluesky_now_hires();
234
235     // Backends should fill this in with a better estimate of the actual time
236     // processing was started, if there could be a delay from submission time.
237     async->exec_time = bluesky_now_hires();
238
239     if (bluesky_verbose) {
240         g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "[%p] submit: %s %s",
241               async,
242               async->op == STORE_OP_GET ? "GET"
243                 : async->op == STORE_OP_PUT ? "PUT"
244                 : async->op == STORE_OP_DELETE ? "DELETE"
245                 : async->op == STORE_OP_BARRIER ? "BARRIER" : "???",
246               async->key);
247     }
248
249     /* Barriers are handled specially, and not handed down the storage
250      * implementation layer. */
251     if (async->op == STORE_OP_BARRIER) {
252         async->status = ASYNC_RUNNING;
253         if (GPOINTER_TO_INT(async->store_private) == 0)
254             bluesky_store_async_mark_complete(async);
255         return;
256     }
257
258     g_mutex_lock(async->store->lock);
259     async->store->pending++;
260     g_mutex_unlock(async->store->lock);
261
262     store->impl->submit(store->handle, async);
263
264     if (bluesky_options.synchronous_stores)
265         bluesky_store_async_wait(async);
266 }
267
268 /* Add the given operation to the barrier.  The barrier will not complete until
269  * all operations added to it have completed. */
270 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
271                                BlueSkyStoreAsync *async)
272 {
273     g_return_if_fail(barrier->op == STORE_OP_BARRIER);
274
275     g_mutex_lock(barrier->lock);
276     barrier->store_private
277         = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
278     g_mutex_unlock(barrier->lock);
279
280     g_mutex_lock(async->lock);
281     if (async->barrier == NULL) {
282         async->barrier = barrier;
283     } else {
284         g_warning("Adding async to more than one barrier!\n");
285         bluesky_store_async_add_notifier(async, op_complete, barrier);
286     }
287     g_mutex_unlock(async->lock);
288 }
289
290 static void notifier_task(gpointer n, gpointer s)
291 {
292     struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
293
294     notifier->func(notifier->async, notifier->user_data);
295     if (g_atomic_int_dec_and_test(&notifier->async->notifier_count)) {
296         g_mutex_lock(notifier->async->lock);
297         if (notifier->async->barrier != NULL)
298             op_complete(notifier->async, notifier->async->barrier);
299         g_cond_broadcast(notifier->async->completion_cond);
300         g_mutex_unlock(notifier->async->lock);
301     }
302     bluesky_store_async_unref(notifier->async);
303     g_free(notifier);
304 }
305
306 void bluesky_store_sync(BlueSkyStore *store)
307 {
308     g_mutex_lock(store->lock);
309     if (bluesky_verbose) {
310         g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
311               "Waiting for pending store operations to complete...");
312     }
313     while (store->pending > 0) {
314         g_cond_wait(store->cond_idle, store->lock);
315     }
316     g_mutex_unlock(store->lock);
317     if (bluesky_verbose) {
318         g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "Operations are complete.");
319     }
320 }
321
322 /* Convenience wrappers that perform a single operation synchronously. */
323 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
324 {
325     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
326     async->op = STORE_OP_GET;
327     async->key = g_strdup(key);
328     bluesky_store_async_submit(async);
329
330     bluesky_store_async_wait(async);
331
332     BlueSkyRCStr *data = async->data;
333     bluesky_string_ref(data);
334     bluesky_store_async_unref(async);
335     return data;
336 }
337
338 void bluesky_store_put(BlueSkyStore *store,
339                        const gchar *key, BlueSkyRCStr *val)
340 {
341     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
342     async->op = STORE_OP_PUT;
343     async->key = g_strdup(key);
344     bluesky_string_ref(val);
345     async->data = val;
346     bluesky_store_async_submit(async);
347
348     bluesky_store_async_wait(async);
349     bluesky_store_async_unref(async);
350 }
351
352 /* Simple in-memory data store for test purposes. */
353 typedef struct {
354     GMutex *lock;
355
356     /* TODO: A hashtable isn't optimal for list queries... */
357     GHashTable *store;
358 } MemStore;
359
360 static gpointer memstore_create(const gchar *path)
361 {
362     MemStore *store = g_new(MemStore, 1);
363     store->lock = g_mutex_new();
364     store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
365                                          g_free,
366                                          (GDestroyNotify)bluesky_string_unref);
367
368     return (gpointer)store;
369 }
370
371 static void memstore_destroy(gpointer store)
372 {
373     /* TODO */
374 }
375
376 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
377 {
378     MemStore *store = (MemStore *)st;
379     BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
380     if (s != NULL)
381         bluesky_string_ref(s);
382     return s;
383 }
384
385 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
386 {
387     MemStore *store = (MemStore *)s;
388     bluesky_string_ref(val);
389     g_hash_table_insert(store->store, g_strdup(key), val);
390 }
391
392 static void memstore_submit(gpointer s, BlueSkyStoreAsync *async)
393 {
394     g_return_if_fail(async->status == ASYNC_NEW);
395     g_return_if_fail(async->op != STORE_OP_NONE);
396
397     switch (async->op) {
398     case STORE_OP_GET:
399         async->data = memstore_get(s, async->key);
400         break;
401
402     case STORE_OP_PUT:
403         memstore_put(s, async->key, async->data);
404         break;
405
406     default:
407         g_warning("Uknown operation type for MemStore: %d\n", async->op);
408         return;
409     }
410
411     bluesky_store_async_mark_complete(async);
412 }
413
414 static void memstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
415 {
416 }
417
418 static BlueSkyStoreImplementation memstore_impl = {
419     .create = memstore_create,
420     .destroy = memstore_destroy,
421     .submit = memstore_submit,
422     .cleanup = memstore_cleanup,
423 };
424
425 /* Store implementation which writes data as files to disk. */
426 static gpointer filestore_create(const gchar *path)
427 {
428     return GINT_TO_POINTER(1);
429 }
430
431 static void filestore_destroy()
432 {
433 }
434
435 static BlueSkyRCStr *filestore_get(const gchar *key)
436 {
437     gchar *contents = NULL;
438     gsize length;
439     GError *error = NULL;
440
441     g_file_get_contents(key, &contents, &length, &error);
442     if (contents == NULL)
443         return NULL;
444
445     return bluesky_string_new(contents, length);
446 }
447
448 static void filestore_put(const gchar *key, BlueSkyRCStr *val)
449 {
450     g_file_set_contents(key, val->data, val->len, NULL);
451 }
452
453 static void filestore_submit(gpointer s, BlueSkyStoreAsync *async)
454 {
455     g_return_if_fail(async->status == ASYNC_NEW);
456     g_return_if_fail(async->op != STORE_OP_NONE);
457
458     switch (async->op) {
459     case STORE_OP_GET:
460         async->data = filestore_get(async->key);
461         async->result = 0;
462         break;
463
464     case STORE_OP_PUT:
465         filestore_put(async->key, async->data);
466         async->result = 0;
467         break;
468
469     default:
470         g_warning("Uknown operation type for FileStore: %d\n", async->op);
471         return;
472     }
473
474     bluesky_store_async_mark_complete(async);
475 }
476
477 static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
478 {
479 }
480
481 static BlueSkyStoreImplementation filestore_impl = {
482     .create = filestore_create,
483     .destroy = filestore_destroy,
484     .submit = filestore_submit,
485     .cleanup = filestore_cleanup,
486 };
487
488 void bluesky_store_init()
489 {
490     store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
491     notifier_thread_pool = g_thread_pool_new(notifier_task, NULL,
492                                              bluesky_max_threads, FALSE, NULL);
493     bluesky_store_register(&memstore_impl, "mem");
494     bluesky_store_register(&filestore_impl, "file");
495 }