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. */
21 const BlueSkyStoreImplementation *impl;
26 int pending; /* Count of operations not yet complete. */
28 struct bluesky_stats *stats_get, *stats_put;
31 GHashTable *store_implementations;
33 /* Thread pool for calling notifier functions when an operation completes.
34 * These are called in a separate thread for locking reasons: we want to call
35 * the notifiers without the lock on the async object held, but completion
36 * occurs when the lock is held--so we need some way to defer the call. This
37 * isn't really optimal from a cache-locality standpoint. */
38 static GThreadPool *notifier_thread_pool;
40 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
43 g_hash_table_insert(store_implementations, g_strdup(name), (gpointer)impl);
46 BlueSkyStore *bluesky_store_new(const gchar *type)
48 const BlueSkyStoreImplementation *impl;
51 scheme = g_strdup(type);
52 path = strchr(scheme, ':');
58 impl = g_hash_table_lookup(store_implementations, scheme);
64 gpointer handle = impl->create(path);
70 BlueSkyStore *store = g_new(BlueSkyStore, 1);
72 store->handle = handle;
73 store->lock = g_mutex_new();
74 store->cond_idle = g_cond_new();
76 store->stats_get = bluesky_stats_new(g_strdup_printf("Store[%s]: GETS",
78 store->stats_put = bluesky_stats_new(g_strdup_printf("Store[%s]: PUTS",
84 void bluesky_store_free(BlueSkyStore *store)
86 store->impl->destroy(store->handle);
90 char *bluesky_store_lookup_last(BlueSkyStore *store, const char *prefix)
92 return store->impl->lookup_last(store->handle, prefix);
95 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
97 BlueSkyStoreAsync *async;
99 async = g_new(BlueSkyStoreAsync, 1);
100 async->store = store;
101 async->lock = g_mutex_new();
102 async->completion_cond = g_cond_new();
104 async->status = ASYNC_NEW;
105 async->op = STORE_OP_NONE;
108 async->start = async->len = 0;
109 async->range_done = FALSE;
111 async->notifiers = NULL;
112 async->notifier_count = 0;
113 async->barrier = NULL;
114 async->store_private = NULL;
119 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async)
121 return async->store->handle;
124 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
129 g_return_if_fail(g_atomic_int_get(&async->refcount) > 0);
131 g_atomic_int_inc(&async->refcount);
134 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
139 if (g_atomic_int_dec_and_test(&async->refcount)) {
140 async->store->impl->cleanup(async->store->handle, async);
141 g_mutex_free(async->lock);
142 g_cond_free(async->completion_cond);
144 bluesky_string_unref(async->data);
149 /* Block until the given operation has completed. */
150 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
152 g_return_if_fail(async != NULL);
153 g_mutex_lock(async->lock);
155 if (async->status == ASYNC_NEW) {
156 g_error("bluesky_store_async_wait on a new async object!\n");
157 g_mutex_unlock(async->lock);
161 while (async->status != ASYNC_COMPLETE
162 || g_atomic_int_get(&async->notifier_count) > 0) {
163 g_cond_wait(async->completion_cond, async->lock);
166 g_mutex_unlock(async->lock);
169 /* Add a notifier function to be called when the operation completes. */
170 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
171 GFunc func, gpointer user_data)
173 struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
174 g_mutex_lock(async->lock);
175 nl->next = async->notifiers;
177 nl->async = async; bluesky_store_async_ref(async);
178 nl->user_data = user_data;
179 g_atomic_int_inc(&async->notifier_count);
180 if (async->status == ASYNC_COMPLETE) {
181 g_thread_pool_push(notifier_thread_pool, nl, NULL);
183 async->notifiers = nl;
185 g_mutex_unlock(async->lock);
188 static void op_complete(gpointer a, gpointer b)
190 BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
192 bluesky_store_async_ref(barrier);
193 g_mutex_lock(barrier->lock);
194 barrier->store_private
195 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) - 1);
196 if (GPOINTER_TO_INT(barrier->store_private) == 0
197 && barrier->status != ASYNC_NEW) {
198 bluesky_store_async_mark_complete(barrier);
200 g_mutex_unlock(barrier->lock);
201 bluesky_store_async_unref(barrier);
204 /* Mark an asynchronous operation as complete. This should only be called by
205 * the store implementations. The lock should be held when calling this
206 * function. Any notifier functions will be called, but in a separate thread
207 * and without the lock held. */
208 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
210 g_return_if_fail(async->status != ASYNC_COMPLETE);
212 bluesky_time_hires elapsed = bluesky_now_hires() - async->start_time;
213 bluesky_time_hires latency = bluesky_now_hires() - async->exec_time;
215 if (async->op != STORE_OP_BARRIER) {
216 g_mutex_lock(async->store->lock);
217 async->store->pending--;
218 if (async->store->pending == 0)
219 g_cond_broadcast(async->store->cond_idle);
220 g_mutex_unlock(async->store->lock);
223 /* If the request was a range request but the backend read the entire
224 * object, select out the appropriate bytes. */
225 if (async->op == STORE_OP_GET
226 && !async->range_done
227 && async->result == 0
228 && async->data != NULL) {
229 if (async->start != 0 || async->len != 0) {
230 /* If the caller requesteda read outside the object, return an
232 if (async->start + async->len > async->data->len) {
233 g_warning("Range request outside object boundaries!\n");
237 async->len = async->data->len - async->start;
238 BlueSkyRCStr *newstr = bluesky_string_new(g_memdup(&async->data->data[async->start], async->len), async->len);
239 bluesky_string_unref(async->data);
240 async->data = newstr;
241 async->range_done = TRUE;
246 async->status = ASYNC_COMPLETE;
247 g_cond_broadcast(async->completion_cond);
249 if (async->barrier != NULL && async->notifiers == NULL)
250 op_complete(async, async->barrier);
252 while (async->notifiers != NULL) {
253 struct BlueSkyNotifierList *nl = async->notifiers;
254 async->notifiers = nl->next;
255 g_thread_pool_push(notifier_thread_pool, nl, NULL);
258 if (bluesky_verbose) {
259 g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
260 "[%p] complete: elapsed = %"PRIi64" ns, latency = %"PRIi64" ns",
261 async, elapsed, latency);
265 if (async->op == STORE_OP_GET) {
266 bluesky_stats_add(async->store->stats_get, async->data->len);
267 } else if (async->op == STORE_OP_PUT) {
268 bluesky_stats_add(async->store->stats_put, async->data->len);
273 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
275 BlueSkyStore *store = async->store;
277 async->start_time = bluesky_now_hires();
279 // Backends should fill this in with a better estimate of the actual time
280 // processing was started, if there could be a delay from submission time.
281 async->exec_time = bluesky_now_hires();
283 if (bluesky_verbose) {
284 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "[%p] submit: %s %s",
286 async->op == STORE_OP_GET ? "GET"
287 : async->op == STORE_OP_PUT ? "PUT"
288 : async->op == STORE_OP_DELETE ? "DELETE"
289 : async->op == STORE_OP_BARRIER ? "BARRIER" : "???",
293 /* Barriers are handled specially, and not handed down the storage
294 * implementation layer. */
295 if (async->op == STORE_OP_BARRIER) {
296 async->status = ASYNC_RUNNING;
297 if (GPOINTER_TO_INT(async->store_private) == 0)
298 bluesky_store_async_mark_complete(async);
302 g_mutex_lock(async->store->lock);
303 async->store->pending++;
304 g_mutex_unlock(async->store->lock);
306 store->impl->submit(store->handle, async);
308 if (bluesky_options.synchronous_stores)
309 bluesky_store_async_wait(async);
312 /* Add the given operation to the barrier. The barrier will not complete until
313 * all operations added to it have completed. */
314 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
315 BlueSkyStoreAsync *async)
317 g_return_if_fail(barrier->op == STORE_OP_BARRIER);
319 g_mutex_lock(barrier->lock);
320 barrier->store_private
321 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
322 g_mutex_unlock(barrier->lock);
324 g_mutex_lock(async->lock);
325 if (async->barrier == NULL && async->status != ASYNC_COMPLETE) {
326 async->barrier = barrier;
327 g_mutex_unlock(async->lock);
329 if (async->barrier != NULL)
330 g_warning("Adding async to more than one barrier!\n");
331 g_mutex_unlock(async->lock);
332 bluesky_store_async_add_notifier(async, op_complete, barrier);
336 static void notifier_task(gpointer n, gpointer s)
338 struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
340 notifier->func(notifier->async, notifier->user_data);
341 if (g_atomic_int_dec_and_test(¬ifier->async->notifier_count)) {
342 g_mutex_lock(notifier->async->lock);
343 if (notifier->async->barrier != NULL)
344 op_complete(notifier->async, notifier->async->barrier);
345 g_cond_broadcast(notifier->async->completion_cond);
346 g_mutex_unlock(notifier->async->lock);
348 bluesky_store_async_unref(notifier->async);
352 void bluesky_store_sync(BlueSkyStore *store)
354 g_mutex_lock(store->lock);
355 if (bluesky_verbose) {
356 g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
357 "Waiting for pending store operations to complete...");
359 while (store->pending > 0) {
360 g_cond_wait(store->cond_idle, store->lock);
362 g_mutex_unlock(store->lock);
363 if (bluesky_verbose) {
364 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "Operations are complete.");
368 /* Convenience wrappers that perform a single operation synchronously. */
369 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
371 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
372 async->op = STORE_OP_GET;
373 async->key = g_strdup(key);
374 bluesky_store_async_submit(async);
376 bluesky_store_async_wait(async);
378 BlueSkyRCStr *data = async->data;
379 bluesky_string_ref(data);
380 bluesky_store_async_unref(async);
384 void bluesky_store_put(BlueSkyStore *store,
385 const gchar *key, BlueSkyRCStr *val)
387 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
388 async->op = STORE_OP_PUT;
389 async->key = g_strdup(key);
390 bluesky_string_ref(val);
392 bluesky_store_async_submit(async);
394 bluesky_store_async_wait(async);
395 bluesky_store_async_unref(async);
398 /* Simple in-memory data store for test purposes. */
402 /* TODO: A hashtable isn't optimal for list queries... */
406 static gpointer memstore_create(const gchar *path)
408 MemStore *store = g_new(MemStore, 1);
409 store->lock = g_mutex_new();
410 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
412 (GDestroyNotify)bluesky_string_unref);
414 return (gpointer)store;
417 static void memstore_destroy(gpointer store)
422 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
424 MemStore *store = (MemStore *)st;
425 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
427 bluesky_string_ref(s);
431 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
433 MemStore *store = (MemStore *)s;
434 bluesky_string_ref(val);
435 g_hash_table_insert(store->store, g_strdup(key), val);
438 static void memstore_submit(gpointer s, BlueSkyStoreAsync *async)
440 g_return_if_fail(async->status == ASYNC_NEW);
441 g_return_if_fail(async->op != STORE_OP_NONE);
445 async->data = memstore_get(s, async->key);
449 memstore_put(s, async->key, async->data);
453 g_warning("Uknown operation type for MemStore: %d\n", async->op);
457 bluesky_store_async_mark_complete(async);
460 static void memstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
464 static BlueSkyStoreImplementation memstore_impl = {
465 .create = memstore_create,
466 .destroy = memstore_destroy,
467 .submit = memstore_submit,
468 .cleanup = memstore_cleanup,
471 /* Store implementation which writes data as files to disk. */
472 static gpointer filestore_create(const gchar *path)
474 return GINT_TO_POINTER(1);
477 static void filestore_destroy()
481 static BlueSkyRCStr *filestore_get(const gchar *key)
483 gchar *contents = NULL;
485 GError *error = NULL;
487 g_file_get_contents(key, &contents, &length, &error);
488 if (contents == NULL)
491 return bluesky_string_new(contents, length);
494 static void filestore_put(const gchar *key, BlueSkyRCStr *val)
496 g_file_set_contents(key, val->data, val->len, NULL);
499 static void filestore_submit(gpointer s, BlueSkyStoreAsync *async)
501 g_return_if_fail(async->status == ASYNC_NEW);
502 g_return_if_fail(async->op != STORE_OP_NONE);
506 async->data = filestore_get(async->key);
511 filestore_put(async->key, async->data);
516 g_warning("Uknown operation type for FileStore: %d\n", async->op);
520 bluesky_store_async_mark_complete(async);
523 static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
527 static char *filestore_lookup_last(gpointer store, const char *prefix)
530 GDir *dir = g_dir_open(".", 0, NULL);
532 g_warning("Unable to open directory for listing");
537 while ((file = g_dir_read_name(dir)) != NULL) {
538 if (strncmp(file, prefix, strlen(prefix)) == 0) {
539 if (last == NULL || strcmp(file, last) > 0) {
541 last = g_strdup(file);
550 static BlueSkyStoreImplementation filestore_impl = {
551 .create = filestore_create,
552 .destroy = filestore_destroy,
553 .submit = filestore_submit,
554 .cleanup = filestore_cleanup,
555 .lookup_last = filestore_lookup_last,
558 /* A store implementation which simply discards all data, for testing. */
559 static gpointer nullstore_create(const gchar *path)
561 return (gpointer)nullstore_create;
564 static void nullstore_destroy(gpointer store)
568 static void nullstore_submit(gpointer s, BlueSkyStoreAsync *async)
570 bluesky_store_async_mark_complete(async);
573 static void nullstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
577 static BlueSkyStoreImplementation nullstore_impl = {
578 .create = nullstore_create,
579 .destroy = nullstore_destroy,
580 .submit = nullstore_submit,
581 .cleanup = nullstore_cleanup,
584 void bluesky_store_init()
586 store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
587 notifier_thread_pool = g_thread_pool_new(notifier_task, NULL,
588 bluesky_max_threads, FALSE, NULL);
589 bluesky_store_register(&memstore_impl, "mem");
590 bluesky_store_register(&filestore_impl, "file");
591 bluesky_store_register(&nullstore_impl, "null");