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. */
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 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
92 BlueSkyStoreAsync *async;
94 async = g_new(BlueSkyStoreAsync, 1);
96 async->lock = g_mutex_new();
97 async->completion_cond = g_cond_new();
99 async->status = ASYNC_NEW;
100 async->op = STORE_OP_NONE;
104 async->notifiers = NULL;
105 async->notifier_count = 0;
106 async->barrier = NULL;
107 async->store_private = NULL;
112 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async)
114 return async->store->handle;
117 void bluesky_store_async_ref(BlueSkyStoreAsync *async)
122 g_return_if_fail(g_atomic_int_get(&async->refcount) > 0);
124 g_atomic_int_inc(&async->refcount);
127 void bluesky_store_async_unref(BlueSkyStoreAsync *async)
132 if (g_atomic_int_dec_and_test(&async->refcount)) {
133 async->store->impl->cleanup(async->store->handle, async);
134 g_mutex_free(async->lock);
135 g_cond_free(async->completion_cond);
137 bluesky_string_unref(async->data);
142 /* Block until the given operation has completed. */
143 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
145 g_return_if_fail(async != NULL);
146 g_mutex_lock(async->lock);
148 if (async->status == ASYNC_NEW) {
149 g_error("bluesky_store_async_wait on a new async object!\n");
150 g_mutex_unlock(async->lock);
154 while (async->status != ASYNC_COMPLETE
155 || g_atomic_int_get(&async->notifier_count) > 0) {
156 g_cond_wait(async->completion_cond, async->lock);
159 g_mutex_unlock(async->lock);
162 /* Add a notifier function to be called when the operation completes. */
163 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
164 GFunc func, gpointer user_data)
166 struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
167 g_mutex_lock(async->lock);
168 nl->next = async->notifiers;
170 nl->async = async; bluesky_store_async_ref(async);
171 nl->user_data = user_data;
172 g_atomic_int_inc(&async->notifier_count);
173 if (async->status == ASYNC_COMPLETE) {
174 g_thread_pool_push(notifier_thread_pool, nl, NULL);
176 async->notifiers = nl;
178 g_mutex_unlock(async->lock);
181 static void op_complete(gpointer a, gpointer b)
183 BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
185 bluesky_store_async_ref(barrier);
186 g_mutex_lock(barrier->lock);
187 barrier->store_private
188 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) - 1);
189 if (GPOINTER_TO_INT(barrier->store_private) == 0
190 && barrier->status != ASYNC_NEW) {
191 bluesky_store_async_mark_complete(barrier);
193 g_mutex_unlock(barrier->lock);
194 bluesky_store_async_unref(barrier);
197 /* Mark an asynchronous operation as complete. This should only be called by
198 * the store implementations. The lock should be held when calling this
199 * function. Any notifier functions will be called, but in a separate thread
200 * and without the lock held. */
201 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
203 g_return_if_fail(async->status != ASYNC_COMPLETE);
205 bluesky_time_hires elapsed = bluesky_now_hires() - async->start_time;
206 bluesky_time_hires latency = bluesky_now_hires() - async->exec_time;
208 if (async->op != STORE_OP_BARRIER) {
209 g_mutex_lock(async->store->lock);
210 async->store->pending--;
211 if (async->store->pending == 0)
212 g_cond_broadcast(async->store->cond_idle);
213 g_mutex_unlock(async->store->lock);
216 async->status = ASYNC_COMPLETE;
217 g_cond_broadcast(async->completion_cond);
219 if (async->barrier != NULL && async->notifiers == NULL)
220 op_complete(async, async->barrier);
222 while (async->notifiers != NULL) {
223 struct BlueSkyNotifierList *nl = async->notifiers;
224 async->notifiers = nl->next;
225 g_thread_pool_push(notifier_thread_pool, nl, NULL);
228 if (bluesky_verbose) {
229 g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
230 "[%p] complete: elapsed = %"PRIi64" ns, latency = %"PRIi64" ns",
231 async, elapsed, latency);
234 if (async->op == STORE_OP_GET) {
235 bluesky_stats_add(async->store->stats_get, 1);
236 } else if (async->op == STORE_OP_PUT) {
237 bluesky_stats_add(async->store->stats_put, 1);
241 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
243 BlueSkyStore *store = async->store;
245 async->start_time = bluesky_now_hires();
247 // Backends should fill this in with a better estimate of the actual time
248 // processing was started, if there could be a delay from submission time.
249 async->exec_time = bluesky_now_hires();
251 if (bluesky_verbose) {
252 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "[%p] submit: %s %s",
254 async->op == STORE_OP_GET ? "GET"
255 : async->op == STORE_OP_PUT ? "PUT"
256 : async->op == STORE_OP_DELETE ? "DELETE"
257 : async->op == STORE_OP_BARRIER ? "BARRIER" : "???",
261 /* Barriers are handled specially, and not handed down the storage
262 * implementation layer. */
263 if (async->op == STORE_OP_BARRIER) {
264 async->status = ASYNC_RUNNING;
265 if (GPOINTER_TO_INT(async->store_private) == 0)
266 bluesky_store_async_mark_complete(async);
270 g_mutex_lock(async->store->lock);
271 async->store->pending++;
272 g_mutex_unlock(async->store->lock);
274 store->impl->submit(store->handle, async);
276 if (bluesky_options.synchronous_stores)
277 bluesky_store_async_wait(async);
280 /* Add the given operation to the barrier. The barrier will not complete until
281 * all operations added to it have completed. */
282 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
283 BlueSkyStoreAsync *async)
285 g_return_if_fail(barrier->op == STORE_OP_BARRIER);
287 g_mutex_lock(barrier->lock);
288 barrier->store_private
289 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
290 g_mutex_unlock(barrier->lock);
292 g_mutex_lock(async->lock);
293 if (async->barrier == NULL) {
294 async->barrier = barrier;
296 g_warning("Adding async to more than one barrier!\n");
297 bluesky_store_async_add_notifier(async, op_complete, barrier);
299 g_mutex_unlock(async->lock);
302 static void notifier_task(gpointer n, gpointer s)
304 struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
306 notifier->func(notifier->async, notifier->user_data);
307 if (g_atomic_int_dec_and_test(¬ifier->async->notifier_count)) {
308 g_mutex_lock(notifier->async->lock);
309 if (notifier->async->barrier != NULL)
310 op_complete(notifier->async, notifier->async->barrier);
311 g_cond_broadcast(notifier->async->completion_cond);
312 g_mutex_unlock(notifier->async->lock);
314 bluesky_store_async_unref(notifier->async);
318 void bluesky_store_sync(BlueSkyStore *store)
320 g_mutex_lock(store->lock);
321 if (bluesky_verbose) {
322 g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
323 "Waiting for pending store operations to complete...");
325 while (store->pending > 0) {
326 g_cond_wait(store->cond_idle, store->lock);
328 g_mutex_unlock(store->lock);
329 if (bluesky_verbose) {
330 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "Operations are complete.");
334 /* Convenience wrappers that perform a single operation synchronously. */
335 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
337 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
338 async->op = STORE_OP_GET;
339 async->key = g_strdup(key);
340 bluesky_store_async_submit(async);
342 bluesky_store_async_wait(async);
344 BlueSkyRCStr *data = async->data;
345 bluesky_string_ref(data);
346 bluesky_store_async_unref(async);
350 void bluesky_store_put(BlueSkyStore *store,
351 const gchar *key, BlueSkyRCStr *val)
353 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
354 async->op = STORE_OP_PUT;
355 async->key = g_strdup(key);
356 bluesky_string_ref(val);
358 bluesky_store_async_submit(async);
360 bluesky_store_async_wait(async);
361 bluesky_store_async_unref(async);
364 /* Simple in-memory data store for test purposes. */
368 /* TODO: A hashtable isn't optimal for list queries... */
372 static gpointer memstore_create(const gchar *path)
374 MemStore *store = g_new(MemStore, 1);
375 store->lock = g_mutex_new();
376 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
378 (GDestroyNotify)bluesky_string_unref);
380 return (gpointer)store;
383 static void memstore_destroy(gpointer store)
388 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
390 MemStore *store = (MemStore *)st;
391 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
393 bluesky_string_ref(s);
397 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
399 MemStore *store = (MemStore *)s;
400 bluesky_string_ref(val);
401 g_hash_table_insert(store->store, g_strdup(key), val);
404 static void memstore_submit(gpointer s, BlueSkyStoreAsync *async)
406 g_return_if_fail(async->status == ASYNC_NEW);
407 g_return_if_fail(async->op != STORE_OP_NONE);
411 async->data = memstore_get(s, async->key);
415 memstore_put(s, async->key, async->data);
419 g_warning("Uknown operation type for MemStore: %d\n", async->op);
423 bluesky_store_async_mark_complete(async);
426 static void memstore_cleanup(gpointer store, BlueSkyStoreAsync *async)
430 static BlueSkyStoreImplementation memstore_impl = {
431 .create = memstore_create,
432 .destroy = memstore_destroy,
433 .submit = memstore_submit,
434 .cleanup = memstore_cleanup,
437 /* Store implementation which writes data as files to disk. */
438 static gpointer filestore_create(const gchar *path)
440 return GINT_TO_POINTER(1);
443 static void filestore_destroy()
447 static BlueSkyRCStr *filestore_get(const gchar *key)
449 gchar *contents = NULL;
451 GError *error = NULL;
453 g_file_get_contents(key, &contents, &length, &error);
454 if (contents == NULL)
457 return bluesky_string_new(contents, length);
460 static void filestore_put(const gchar *key, BlueSkyRCStr *val)
462 g_file_set_contents(key, val->data, val->len, NULL);
465 static void filestore_submit(gpointer s, BlueSkyStoreAsync *async)
467 g_return_if_fail(async->status == ASYNC_NEW);
468 g_return_if_fail(async->op != STORE_OP_NONE);
472 async->data = filestore_get(async->key);
477 filestore_put(async->key, async->data);
482 g_warning("Uknown operation type for FileStore: %d\n", async->op);
486 bluesky_store_async_mark_complete(async);
489 static void filestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
493 static BlueSkyStoreImplementation filestore_impl = {
494 .create = filestore_create,
495 .destroy = filestore_destroy,
496 .submit = filestore_submit,
497 .cleanup = filestore_cleanup,
500 void bluesky_store_init()
502 store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
503 notifier_thread_pool = g_thread_pool_new(notifier_task, NULL,
504 bluesky_max_threads, FALSE, NULL);
505 bluesky_store_register(&memstore_impl, "mem");
506 bluesky_store_register(&filestore_impl, "file");