1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
13 #include "bluesky-private.h"
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. */
20 struct _BlueSkyStore {
21 const BlueSkyStoreImplementation *impl;
26 int pending; /* Count of operations not yet complete. */
29 GHashTable *store_implementations;
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;
38 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
41 g_hash_table_insert(store_implementations, g_strdup(name), (gpointer)impl);
44 BlueSkyStore *bluesky_store_new(const gchar *type)
46 const BlueSkyStoreImplementation *impl;
48 impl = g_hash_table_lookup(store_implementations, type);
52 gpointer handle = impl->create();
56 BlueSkyStore *store = g_new(BlueSkyStore, 1);
58 store->handle = handle;
59 store->lock = g_mutex_new();
60 store->cond_idle = g_cond_new();
65 void bluesky_store_free(BlueSkyStore *store)
67 store->impl->destroy(store->handle);
71 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
73 BlueSkyStoreAsync *async;
75 async = g_new(BlueSkyStoreAsync, 1);
77 async->lock = g_mutex_new();
78 async->completion_cond = g_cond_new();
80 async->status = ASYNC_NEW;
81 async->op = STORE_OP_NONE;
85 async->notifiers = NULL;
86 async->store_private = NULL;
91 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
96 g_return_if_fail(g_atomic_int_get(&async->refcount) > 0);
98 g_atomic_int_inc(&async->refcount);
101 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
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);
111 bluesky_string_unref(async->data);
116 /* Block until the given operation has completed. */
117 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
119 g_return_if_fail(async != NULL);
120 g_mutex_lock(async->lock);
122 if (async->status == ASYNC_NEW) {
123 g_error("bluesky_store_async_wait on a new async object!\n");
124 g_mutex_unlock(async->lock);
128 while (async->status != ASYNC_COMPLETE) {
129 g_cond_wait(async->completion_cond, async->lock);
132 g_mutex_unlock(async->lock);
135 /* Add a notifier function to be called when the operation completes. */
136 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
137 GFunc func, gpointer user_data)
139 struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
140 nl->next = async->notifiers;
142 nl->async = async; bluesky_store_async_ref(async);
143 nl->user_data = user_data;
144 if (async->status == ASYNC_COMPLETE) {
145 g_thread_pool_push(notifier_thread_pool, nl, NULL);
147 async->notifiers = nl;
151 /* Mark an asynchronous operation as complete. This should only be called by
152 * the store implementations. The lock should be held when calling this
153 * function. Any notifier functions will be called, but in a separate thread
154 * and without the lock held. */
155 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
157 g_return_if_fail(async->status != ASYNC_COMPLETE);
159 g_mutex_lock(async->store->lock);
160 async->store->pending--;
161 if (async->store->pending == 0)
162 g_cond_broadcast(async->store->cond_idle);
163 g_mutex_unlock(async->store->lock);
165 async->status = ASYNC_COMPLETE;
166 g_cond_broadcast(async->completion_cond);
168 while (async->notifiers != NULL) {
169 struct BlueSkyNotifierList *nl = async->notifiers;
170 async->notifiers = nl->next;
171 g_thread_pool_push(notifier_thread_pool, nl, NULL);
175 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
177 BlueSkyStore *store = async->store;
179 /* Barriers are handled specially, and not handed down the storage
180 * implementation layer. */
181 if (async->op == STORE_OP_BARRIER) {
182 async->status = ASYNC_RUNNING;
183 if (GPOINTER_TO_INT(async->store_private) == 0)
184 bluesky_store_async_mark_complete(async);
188 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "submit: %s %s",
189 async->op == STORE_OP_GET ? "GET"
190 : async->op == STORE_OP_PUT ? "PUT"
191 : async->op == STORE_OP_DELETE ? "DELETE" : "???",
194 g_mutex_lock(async->store->lock);
195 async->store->pending++;
196 g_mutex_unlock(async->store->lock);
198 store->impl->submit(store->handle, async);
200 if (bluesky_options.synchronous_stores)
201 bluesky_store_async_wait(async);
204 static void op_complete(gpointer a, gpointer b)
206 BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
208 g_mutex_lock(barrier->lock);
209 barrier->store_private
210 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) - 1);
211 if (GPOINTER_TO_INT(barrier->store_private) == 0
212 && barrier->status != ASYNC_NEW) {
213 bluesky_store_async_mark_complete(barrier);
215 g_mutex_unlock(barrier->lock);
218 /* Add the given operation to the barrier. The barrier will not complete until
219 * all operations added to it have completed. */
220 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
221 BlueSkyStoreAsync *async)
223 g_return_if_fail(barrier->op == STORE_OP_BARRIER);
224 barrier->store_private
225 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
226 bluesky_store_async_add_notifier(async, op_complete, barrier);
229 static void notifier_task(gpointer n, gpointer s)
231 struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
233 notifier->func(notifier->async, notifier->user_data);
234 bluesky_store_async_unref(notifier->async);
238 void bluesky_store_sync(BlueSkyStore *store)
240 g_mutex_lock(store->lock);
241 g_print("Waiting for pending store operations to complete...\n");
242 while (store->pending > 0) {
243 g_cond_wait(store->cond_idle, store->lock);
245 g_mutex_unlock(store->lock);
246 g_print("Operations are complete.\n");
249 /* Convenience wrappers that perform a single operation synchronously. */
250 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
252 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
253 async->op = STORE_OP_GET;
254 async->key = g_strdup(key);
255 bluesky_store_async_submit(async);
257 bluesky_store_async_wait(async);
259 BlueSkyRCStr *data = async->data;
260 bluesky_string_ref(data);
261 bluesky_store_async_unref(async);
265 void bluesky_store_put(BlueSkyStore *store,
266 const gchar *key, BlueSkyRCStr *val)
268 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
269 async->op = STORE_OP_PUT;
270 async->key = g_strdup(key);
271 bluesky_string_ref(val);
273 bluesky_store_async_submit(async);
275 bluesky_store_async_wait(async);
276 bluesky_store_async_unref(async);
279 /* Simple in-memory data store for test purposes. */
283 /* TODO: A hashtable isn't optimal for list queries... */
287 static gpointer memstore_create()
289 MemStore *store = g_new(MemStore, 1);
290 store->lock = g_mutex_new();
291 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
293 (GDestroyNotify)bluesky_string_unref);
295 return (gpointer)store;
298 static void memstore_destroy(gpointer store)
303 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
305 MemStore *store = (MemStore *)st;
306 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
308 bluesky_string_ref(s);
312 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
314 MemStore *store = (MemStore *)s;
315 bluesky_string_ref(val);
316 g_hash_table_insert(store->store, g_strdup(key), val);
319 static void memstore_submit(gpointer s, BlueSkyStoreAsync *async)
321 g_return_if_fail(async->status == ASYNC_NEW);
322 g_return_if_fail(async->op != STORE_OP_NONE);
326 async->data = memstore_get(s, async->key);
330 memstore_put(s, async->key, async->data);
334 g_warning("Uknown operation type for MemStore: %d\n", async->op);
338 bluesky_store_async_mark_complete(async);
341 static void memstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
345 static BlueSkyStoreImplementation memstore_impl = {
346 .create = memstore_create,
347 .destroy = memstore_destroy,
348 .submit = memstore_submit,
349 .cleanup = memstore_cleanup,
352 /* Store implementation which writes data as files to disk. */
353 static gpointer filestore_create()
355 return GINT_TO_POINTER(1);
358 static void filestore_destroy()
362 static BlueSkyRCStr *filestore_get(const gchar *key)
364 gchar *contents = NULL;
366 GError *error = NULL;
368 g_file_get_contents(key, &contents, &length, &error);
369 if (contents == NULL)
372 return bluesky_string_new(contents, length);
375 static void filestore_put(const gchar *key, BlueSkyRCStr *val)
377 g_file_set_contents(key, val->data, val->len, NULL);
380 static void filestore_submit(gpointer s, BlueSkyStoreAsync *async)
382 g_return_if_fail(async->status == ASYNC_NEW);
383 g_return_if_fail(async->op != STORE_OP_NONE);
387 async->data = filestore_get(async->key);
391 filestore_put(async->key, async->data);
395 g_warning("Uknown operation type for FileStore: %d\n", async->op);
399 bluesky_store_async_mark_complete(async);
402 static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
406 static BlueSkyStoreImplementation filestore_impl = {
407 .create = filestore_create,
408 .destroy = filestore_destroy,
409 .submit = filestore_submit,
410 .cleanup = filestore_cleanup,
413 void bluesky_store_init()
415 store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
416 notifier_thread_pool = g_thread_pool_new(notifier_task, NULL, -1, FALSE,
418 bluesky_store_register(&memstore_impl, "mem");
419 bluesky_store_register(&filestore_impl, "file");