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;
49 scheme = g_strdup(type);
50 path = strchr(scheme, ':');
56 impl = g_hash_table_lookup(store_implementations, scheme);
62 gpointer handle = impl->create(path);
68 BlueSkyStore *store = g_new(BlueSkyStore, 1);
70 store->handle = handle;
71 store->lock = g_mutex_new();
72 store->cond_idle = g_cond_new();
78 void bluesky_store_free(BlueSkyStore *store)
80 store->impl->destroy(store->handle);
84 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
86 BlueSkyStoreAsync *async;
88 async = g_new(BlueSkyStoreAsync, 1);
90 async->lock = g_mutex_new();
91 async->completion_cond = g_cond_new();
93 async->status = ASYNC_NEW;
94 async->op = STORE_OP_NONE;
98 async->notifiers = NULL;
99 async->notifier_count = 0;
100 async->barrier = NULL;
101 async->store_private = NULL;
106 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async)
108 return async->store->handle;
111 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
116 g_return_if_fail(g_atomic_int_get(&async->refcount) > 0);
118 g_atomic_int_inc(&async->refcount);
121 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
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);
131 bluesky_string_unref(async->data);
136 /* Block until the given operation has completed. */
137 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
139 g_return_if_fail(async != NULL);
140 g_mutex_lock(async->lock);
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);
148 while (async->status != ASYNC_COMPLETE
149 || g_atomic_int_get(&async->notifier_count) > 0) {
150 g_cond_wait(async->completion_cond, async->lock);
153 g_mutex_unlock(async->lock);
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)
160 struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
161 g_mutex_lock(async->lock);
162 nl->next = async->notifiers;
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);
170 async->notifiers = nl;
172 g_mutex_unlock(async->lock);
175 static void op_complete(gpointer a, gpointer b)
177 BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
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);
187 g_mutex_unlock(barrier->lock);
188 bluesky_store_async_unref(barrier);
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)
197 g_return_if_fail(async->status != ASYNC_COMPLETE);
199 bluesky_time_hires elapsed = bluesky_now_hires() - async->start_time;
200 bluesky_time_hires latency = bluesky_now_hires() - async->exec_time;
202 g_mutex_lock(async->store->lock);
203 async->store->pending--;
204 if (async->store->pending == 0)
205 g_cond_broadcast(async->store->cond_idle);
206 g_mutex_unlock(async->store->lock);
208 async->status = ASYNC_COMPLETE;
209 g_cond_broadcast(async->completion_cond);
211 if (async->barrier != NULL && async->notifiers == NULL)
212 op_complete(async, async->barrier);
214 while (async->notifiers != NULL) {
215 struct BlueSkyNotifierList *nl = async->notifiers;
216 async->notifiers = nl->next;
217 g_thread_pool_push(notifier_thread_pool, nl, NULL);
220 g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
221 "[%p] complete: elapsed = %"PRIi64" ns, latency = %"PRIi64" ns",
222 async, elapsed, latency);
225 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
227 BlueSkyStore *store = async->store;
229 async->start_time = bluesky_now_hires();
231 // Backends should fill this in with a better estimate of the actual time
232 // processing was started, if there could be a delay from submission time.
233 async->exec_time = bluesky_now_hires();
235 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "[%p] submit: %s %s",
237 async->op == STORE_OP_GET ? "GET"
238 : async->op == STORE_OP_PUT ? "PUT"
239 : async->op == STORE_OP_DELETE ? "DELETE"
240 : async->op == STORE_OP_BARRIER ? "BARRIER" : "???",
243 /* Barriers are handled specially, and not handed down the storage
244 * implementation layer. */
245 if (async->op == STORE_OP_BARRIER) {
246 async->status = ASYNC_RUNNING;
247 if (GPOINTER_TO_INT(async->store_private) == 0)
248 bluesky_store_async_mark_complete(async);
252 g_mutex_lock(async->store->lock);
253 async->store->pending++;
254 g_mutex_unlock(async->store->lock);
256 store->impl->submit(store->handle, async);
258 if (bluesky_options.synchronous_stores)
259 bluesky_store_async_wait(async);
262 /* Add the given operation to the barrier. The barrier will not complete until
263 * all operations added to it have completed. */
264 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
265 BlueSkyStoreAsync *async)
267 g_return_if_fail(barrier->op == STORE_OP_BARRIER);
269 g_mutex_lock(barrier->lock);
270 barrier->store_private
271 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
272 g_mutex_unlock(barrier->lock);
274 g_mutex_lock(async->lock);
275 if (async->barrier == NULL) {
276 async->barrier = barrier;
278 g_warning("Adding async to more than one barrier!\n");
279 bluesky_store_async_add_notifier(async, op_complete, barrier);
281 g_mutex_unlock(async->lock);
284 static void notifier_task(gpointer n, gpointer s)
286 struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
288 notifier->func(notifier->async, notifier->user_data);
289 if (g_atomic_int_dec_and_test(¬ifier->async->notifier_count)) {
290 g_mutex_lock(notifier->async->lock);
291 if (notifier->async->barrier != NULL)
292 op_complete(notifier->async, notifier->async->barrier);
293 g_cond_broadcast(notifier->async->completion_cond);
294 g_mutex_unlock(notifier->async->lock);
296 bluesky_store_async_unref(notifier->async);
300 void bluesky_store_sync(BlueSkyStore *store)
302 g_mutex_lock(store->lock);
303 g_print("Waiting for pending store operations to complete...\n");
304 while (store->pending > 0) {
305 g_cond_wait(store->cond_idle, store->lock);
307 g_mutex_unlock(store->lock);
308 g_print("Operations are complete.\n");
311 /* Convenience wrappers that perform a single operation synchronously. */
312 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
314 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
315 async->op = STORE_OP_GET;
316 async->key = g_strdup(key);
317 bluesky_store_async_submit(async);
319 bluesky_store_async_wait(async);
321 BlueSkyRCStr *data = async->data;
322 bluesky_string_ref(data);
323 bluesky_store_async_unref(async);
327 void bluesky_store_put(BlueSkyStore *store,
328 const gchar *key, BlueSkyRCStr *val)
330 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
331 async->op = STORE_OP_PUT;
332 async->key = g_strdup(key);
333 bluesky_string_ref(val);
335 bluesky_store_async_submit(async);
337 bluesky_store_async_wait(async);
338 bluesky_store_async_unref(async);
341 /* Simple in-memory data store for test purposes. */
345 /* TODO: A hashtable isn't optimal for list queries... */
349 static gpointer memstore_create(const gchar *path)
351 MemStore *store = g_new(MemStore, 1);
352 store->lock = g_mutex_new();
353 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
355 (GDestroyNotify)bluesky_string_unref);
357 return (gpointer)store;
360 static void memstore_destroy(gpointer store)
365 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
367 MemStore *store = (MemStore *)st;
368 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
370 bluesky_string_ref(s);
374 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
376 MemStore *store = (MemStore *)s;
377 bluesky_string_ref(val);
378 g_hash_table_insert(store->store, g_strdup(key), val);
381 static void memstore_submit(gpointer s, BlueSkyStoreAsync *async)
383 g_return_if_fail(async->status == ASYNC_NEW);
384 g_return_if_fail(async->op != STORE_OP_NONE);
388 async->data = memstore_get(s, async->key);
392 memstore_put(s, async->key, async->data);
396 g_warning("Uknown operation type for MemStore: %d\n", async->op);
400 bluesky_store_async_mark_complete(async);
403 static void memstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
407 static BlueSkyStoreImplementation memstore_impl = {
408 .create = memstore_create,
409 .destroy = memstore_destroy,
410 .submit = memstore_submit,
411 .cleanup = memstore_cleanup,
414 /* Store implementation which writes data as files to disk. */
415 static gpointer filestore_create(const gchar *path)
417 return GINT_TO_POINTER(1);
420 static void filestore_destroy()
424 static BlueSkyRCStr *filestore_get(const gchar *key)
426 gchar *contents = NULL;
428 GError *error = NULL;
430 g_file_get_contents(key, &contents, &length, &error);
431 if (contents == NULL)
434 return bluesky_string_new(contents, length);
437 static void filestore_put(const gchar *key, BlueSkyRCStr *val)
439 g_file_set_contents(key, val->data, val->len, NULL);
442 static void filestore_submit(gpointer s, BlueSkyStoreAsync *async)
444 g_return_if_fail(async->status == ASYNC_NEW);
445 g_return_if_fail(async->op != STORE_OP_NONE);
449 async->data = filestore_get(async->key);
454 filestore_put(async->key, async->data);
459 g_warning("Uknown operation type for FileStore: %d\n", async->op);
463 bluesky_store_async_mark_complete(async);
466 static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
470 static BlueSkyStoreImplementation filestore_impl = {
471 .create = filestore_create,
472 .destroy = filestore_destroy,
473 .submit = filestore_submit,
474 .cleanup = filestore_cleanup,
477 void bluesky_store_init()
479 store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
480 notifier_thread_pool = g_thread_pool_new(notifier_task, NULL,
481 bluesky_max_threads, FALSE, NULL);
482 bluesky_store_register(&memstore_impl, "mem");
483 bluesky_store_register(&filestore_impl, "file");