A few bugfixes to the storage barrier support.
[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     impl = g_hash_table_lookup(store_implementations, type);
49     if (impl == NULL)
50         return NULL;
51
52     gpointer handle = impl->create();
53     if (handle == NULL)
54         return NULL;
55
56     BlueSkyStore *store = g_new(BlueSkyStore, 1);
57     store->impl = impl;
58     store->handle = handle;
59     store->lock = g_mutex_new();
60     store->cond_idle = g_cond_new();
61     store->pending = 0;
62     return store;
63 }
64
65 void bluesky_store_free(BlueSkyStore *store)
66 {
67     store->impl->destroy(store->handle);
68     g_free(store);
69 }
70
71 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
72 {
73     BlueSkyStoreAsync *async;
74
75     async = g_new(BlueSkyStoreAsync, 1);
76     async->store = store;
77     async->lock = g_mutex_new();
78     async->completion_cond = g_cond_new();
79     async->refcount = 1;
80     async->status = ASYNC_NEW;
81     async->op = STORE_OP_NONE;
82     async->key = NULL;
83     async->data = NULL;
84     async->result = -1;
85     async->notifiers = NULL;
86     async->store_private = NULL;
87
88     return async;
89 }
90
91 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
92 {
93     if (async == NULL)
94         return;
95
96     g_return_if_fail(g_atomic_int_get(&async->refcount) > 0);
97
98     g_atomic_int_inc(&async->refcount);
99 }
100
101 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
102 {
103     if (async == NULL)
104         return;
105
106     if (g_atomic_int_dec_and_test(&async->refcount)) {
107         async->store->impl->cleanup(async->store->handle, async);
108         g_mutex_free(async->lock);
109         g_cond_free(async->completion_cond);
110         g_free(async->key);
111         bluesky_string_unref(async->data);
112         g_free(async);
113         g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
114               "freeing async");
115         g_print("Freed async\n");
116     }
117 }
118
119 /* Block until the given operation has completed. */
120 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
121 {
122     g_return_if_fail(async != NULL);
123     g_mutex_lock(async->lock);
124
125     if (async->status == ASYNC_NEW) {
126         g_error("bluesky_store_async_wait on a new async object!\n");
127         g_mutex_unlock(async->lock);
128         return;
129     }
130
131     while (async->status != ASYNC_COMPLETE) {
132         g_cond_wait(async->completion_cond, async->lock);
133     }
134
135     g_mutex_unlock(async->lock);
136 }
137
138 /* Add a notifier function to be called when the operation completes. */
139 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
140                                       GFunc func, gpointer user_data)
141 {
142     struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
143     nl->next = async->notifiers;
144     nl->func = func;
145     nl->async = async; bluesky_store_async_ref(async);
146     nl->user_data = user_data;
147     if (async->status == ASYNC_COMPLETE) {
148         g_thread_pool_push(notifier_thread_pool, nl, NULL);
149     } else {
150         async->notifiers = nl;
151     }
152 }
153
154 /* Mark an asynchronous operation as complete.  This should only be called by
155  * the store implementations.  The lock should be held when calling this
156  * function.  Any notifier functions will be called, but in a separate thread
157  * and without the lock held. */
158 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
159 {
160     g_return_if_fail(async->status != ASYNC_COMPLETE);
161
162     g_mutex_lock(async->store->lock);
163     async->store->pending--;
164     if (async->store->pending == 0)
165         g_cond_broadcast(async->store->cond_idle);
166     g_mutex_unlock(async->store->lock);
167
168     async->status = ASYNC_COMPLETE;
169     g_cond_broadcast(async->completion_cond);
170
171     while (async->notifiers != NULL) {
172         struct BlueSkyNotifierList *nl = async->notifiers;
173         async->notifiers = nl->next;
174         g_thread_pool_push(notifier_thread_pool, nl, NULL);
175     }
176 }
177
178 static void test_notifier(gpointer a, gpointer u)
179 {
180     g_print("Notifier called!\n");
181 }
182
183 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
184 {
185     BlueSkyStore *store = async->store;
186
187     /* Barriers are handled specially, and not handed down the storage
188      * implementation layer. */
189     if (async->op == STORE_OP_BARRIER) {
190         async->status = ASYNC_RUNNING;
191         if (GPOINTER_TO_INT(async->store_private) == 0)
192             bluesky_store_async_mark_complete(async);
193         return;
194     }
195
196     g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "submit: %s %s",
197           async->op == STORE_OP_GET ? "GET"
198             : async->op == STORE_OP_PUT ? "PUT"
199             : async->op == STORE_OP_DELETE ? "DELETE" : "???",
200           async->key);
201
202     g_mutex_lock(async->store->lock);
203     async->store->pending++;
204     g_mutex_unlock(async->store->lock);
205
206     bluesky_store_async_add_notifier(async, test_notifier, NULL);
207     store->impl->submit(store->handle, async);
208
209     if (bluesky_options.synchronous_stores)
210         bluesky_store_async_wait(async);
211 }
212
213 static void op_complete(gpointer a, gpointer b)
214 {
215     BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
216     BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
217
218     g_mutex_lock(barrier->lock);
219     barrier->store_private
220         = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) - 1);
221     if (GPOINTER_TO_INT(barrier->store_private) == 0
222             && barrier->status != ASYNC_NEW) {
223         bluesky_store_async_mark_complete(barrier);
224     }
225     g_mutex_unlock(barrier->lock);
226 }
227
228 /* Add the given operation to the barrier.  The barrier will not complete until
229  * all operations added to it have completed. */
230 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
231                                BlueSkyStoreAsync *async)
232 {
233     g_return_if_fail(barrier->op == STORE_OP_BARRIER);
234     barrier->store_private
235         = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
236     bluesky_store_async_add_notifier(async, op_complete, barrier);
237 }
238
239 static void notifier_task(gpointer n, gpointer s)
240 {
241     struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
242
243     notifier->func(notifier->async, notifier->user_data);
244     bluesky_store_async_unref(notifier->async);
245     g_free(notifier);
246 }
247
248 void bluesky_store_sync(BlueSkyStore *store)
249 {
250     g_mutex_lock(store->lock);
251     g_print("Waiting for pending store operations to complete...\n");
252     while (store->pending > 0) {
253         g_cond_wait(store->cond_idle, store->lock);
254     }
255     g_mutex_unlock(store->lock);
256     g_print("Operations are complete.\n");
257 }
258
259 /* Convenience wrappers that perform a single operation synchronously. */
260 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
261 {
262     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
263     async->op = STORE_OP_GET;
264     async->key = g_strdup(key);
265     bluesky_store_async_submit(async);
266
267     bluesky_store_async_wait(async);
268
269     BlueSkyRCStr *data = async->data;
270     bluesky_string_ref(data);
271     bluesky_store_async_unref(async);
272     return data;
273 }
274
275 void bluesky_store_put(BlueSkyStore *store,
276                        const gchar *key, BlueSkyRCStr *val)
277 {
278     BlueSkyStoreAsync *async = bluesky_store_async_new(store);
279     async->op = STORE_OP_PUT;
280     async->key = g_strdup(key);
281     bluesky_string_ref(val);
282     async->data = val;
283     bluesky_store_async_submit(async);
284
285     bluesky_store_async_wait(async);
286     bluesky_store_async_unref(async);
287 }
288
289 #if 0
290 /* Simple in-memory data store for test purposes. */
291 typedef struct {
292     GMutex *lock;
293
294     /* TODO: A hashtable isn't optimal for list queries... */
295     GHashTable *store;
296 } MemStore;
297
298 static gpointer memstore_create()
299 {
300     MemStore *store = g_new(MemStore, 1);
301     store->lock = g_mutex_new();
302     store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
303                                          g_free,
304                                          (GDestroyNotify)bluesky_string_unref);
305
306     return (gpointer)store;
307 }
308
309 static void memstore_destroy(gpointer store)
310 {
311     /* TODO */
312 }
313
314 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
315 {
316     MemStore *store = (MemStore *)st;
317     BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
318     if (s != NULL)
319         bluesky_string_ref(s);
320     return s;
321 }
322
323 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
324 {
325     MemStore *store = (MemStore *)s;
326     bluesky_string_ref(val);
327     g_hash_table_insert(store->store, g_strdup(key), val);
328 }
329
330 static BlueSkyStoreImplementation memstore_impl = {
331     .create = memstore_create,
332     .destroy = memstore_destroy,
333     .get = memstore_get,
334     .put = memstore_put,
335 };
336
337 /* Store implementation which writes data as files to disk. */
338 static gpointer filestore_create()
339 {
340     return GINT_TO_POINTER(1);
341 }
342
343 static void filestore_destroy()
344 {
345 }
346
347 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
348 {
349     gchar *contents = NULL;
350     gsize length;
351     GError *error = NULL;
352
353     g_file_get_contents(key, &contents, &length, &error);
354     if (contents == NULL)
355         return NULL;
356
357     return bluesky_string_new(contents, length);
358 }
359
360 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
361 {
362     g_file_set_contents(key, val->data, val->len, NULL);
363 }
364
365 static BlueSkyStoreImplementation filestore_impl = {
366     .create = filestore_create,
367     .destroy = filestore_destroy,
368     .get = filestore_get,
369     .put = filestore_put,
370 };
371 #endif
372
373 void bluesky_store_init()
374 {
375     store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
376     notifier_thread_pool = g_thread_pool_new(notifier_task, NULL, -1, FALSE,
377                                              NULL);
378     //bluesky_store_register(&memstore_impl, "mem");
379     //bluesky_store_register(&filestore_impl, "file");
380 }