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);
113 g_log("bluesky/store", G_LOG_LEVEL_DEBUG,
118 /* Block until the given operation has completed. */
119 void bluesky_store_async_wait(BlueSkyStoreAsync *async)
121 g_return_if_fail(async != NULL);
122 g_mutex_lock(async->lock);
124 if (async->status == ASYNC_NEW) {
125 g_error("bluesky_store_async_wait on a new async object!\n");
126 g_mutex_unlock(async->lock);
130 while (async->status != ASYNC_COMPLETE) {
131 g_cond_wait(async->completion_cond, async->lock);
134 g_mutex_unlock(async->lock);
137 /* Add a notifier function to be called when the operation completes. */
138 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
139 GFunc func, gpointer user_data)
141 struct BlueSkyNotifierList *nl = g_new(struct BlueSkyNotifierList, 1);
142 nl->next = async->notifiers;
144 nl->async = async; bluesky_store_async_ref(async);
145 nl->user_data = user_data;
146 if (async->status == ASYNC_COMPLETE) {
147 g_thread_pool_push(notifier_thread_pool, nl, NULL);
149 async->notifiers = nl;
153 /* Mark an asynchronous operation as complete. This should only be called by
154 * the store implementations. The lock should be held when calling this
155 * function. Any notifier functions will be called, but in a separate thread
156 * and without the lock held. */
157 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
159 g_return_if_fail(async->status != ASYNC_COMPLETE);
161 g_mutex_lock(async->store->lock);
162 async->store->pending--;
163 if (async->store->pending == 0)
164 g_cond_broadcast(async->store->cond_idle);
165 g_mutex_unlock(async->store->lock);
167 async->status = ASYNC_COMPLETE;
168 g_cond_broadcast(async->completion_cond);
170 while (async->notifiers != NULL) {
171 struct BlueSkyNotifierList *nl = async->notifiers;
172 async->notifiers = nl->next;
173 g_thread_pool_push(notifier_thread_pool, nl, NULL);
177 void bluesky_store_async_submit(BlueSkyStoreAsync *async)
179 BlueSkyStore *store = async->store;
181 /* Barriers are handled specially, and not handed down the storage
182 * implementation layer. */
183 if (async->op == STORE_OP_BARRIER) {
184 async->status = ASYNC_RUNNING;
185 if (GPOINTER_TO_INT(async->store_private) == 0)
186 bluesky_store_async_mark_complete(async);
190 g_log("bluesky/store", G_LOG_LEVEL_DEBUG, "submit: %s %s",
191 async->op == STORE_OP_GET ? "GET"
192 : async->op == STORE_OP_PUT ? "PUT"
193 : async->op == STORE_OP_DELETE ? "DELETE" : "???",
196 g_mutex_lock(async->store->lock);
197 async->store->pending++;
198 g_mutex_unlock(async->store->lock);
200 store->impl->submit(store->handle, async);
202 if (bluesky_options.synchronous_stores)
203 bluesky_store_async_wait(async);
206 static void op_complete(gpointer a, gpointer b)
208 BlueSkyStoreAsync *barrier = (BlueSkyStoreAsync *)b;
210 g_mutex_lock(barrier->lock);
211 barrier->store_private
212 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) - 1);
213 if (GPOINTER_TO_INT(barrier->store_private) == 0
214 && barrier->status != ASYNC_NEW) {
215 bluesky_store_async_mark_complete(barrier);
217 g_mutex_unlock(barrier->lock);
220 /* Add the given operation to the barrier. The barrier will not complete until
221 * all operations added to it have completed. */
222 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
223 BlueSkyStoreAsync *async)
225 g_return_if_fail(barrier->op == STORE_OP_BARRIER);
226 barrier->store_private
227 = GINT_TO_POINTER(GPOINTER_TO_INT(barrier->store_private) + 1);
228 bluesky_store_async_add_notifier(async, op_complete, barrier);
231 static void notifier_task(gpointer n, gpointer s)
233 struct BlueSkyNotifierList *notifier = (struct BlueSkyNotifierList *)n;
235 notifier->func(notifier->async, notifier->user_data);
236 bluesky_store_async_unref(notifier->async);
240 void bluesky_store_sync(BlueSkyStore *store)
242 g_mutex_lock(store->lock);
243 g_print("Waiting for pending store operations to complete...\n");
244 while (store->pending > 0) {
245 g_cond_wait(store->cond_idle, store->lock);
247 g_mutex_unlock(store->lock);
248 g_print("Operations are complete.\n");
251 /* Convenience wrappers that perform a single operation synchronously. */
252 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key)
254 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
255 async->op = STORE_OP_GET;
256 async->key = g_strdup(key);
257 bluesky_store_async_submit(async);
259 bluesky_store_async_wait(async);
261 BlueSkyRCStr *data = async->data;
262 bluesky_string_ref(data);
263 bluesky_store_async_unref(async);
267 void bluesky_store_put(BlueSkyStore *store,
268 const gchar *key, BlueSkyRCStr *val)
270 BlueSkyStoreAsync *async = bluesky_store_async_new(store);
271 async->op = STORE_OP_PUT;
272 async->key = g_strdup(key);
273 bluesky_string_ref(val);
275 bluesky_store_async_submit(async);
277 bluesky_store_async_wait(async);
278 bluesky_store_async_unref(async);
282 /* Simple in-memory data store for test purposes. */
286 /* TODO: A hashtable isn't optimal for list queries... */
290 static gpointer memstore_create()
292 MemStore *store = g_new(MemStore, 1);
293 store->lock = g_mutex_new();
294 store->store = g_hash_table_new_full(g_str_hash, g_str_equal,
296 (GDestroyNotify)bluesky_string_unref);
298 return (gpointer)store;
301 static void memstore_destroy(gpointer store)
306 static BlueSkyRCStr *memstore_get(gpointer st, const gchar *key)
308 MemStore *store = (MemStore *)st;
309 BlueSkyRCStr *s = g_hash_table_lookup(store->store, key);
311 bluesky_string_ref(s);
315 static void memstore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
317 MemStore *store = (MemStore *)s;
318 bluesky_string_ref(val);
319 g_hash_table_insert(store->store, g_strdup(key), val);
322 static BlueSkyStoreImplementation memstore_impl = {
323 .create = memstore_create,
324 .destroy = memstore_destroy,
329 /* Store implementation which writes data as files to disk. */
330 static gpointer filestore_create()
332 return GINT_TO_POINTER(1);
335 static void filestore_destroy()
339 static BlueSkyRCStr *filestore_get(gpointer s, const gchar *key)
341 gchar *contents = NULL;
343 GError *error = NULL;
345 g_file_get_contents(key, &contents, &length, &error);
346 if (contents == NULL)
349 return bluesky_string_new(contents, length);
352 static void filestore_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
354 g_file_set_contents(key, val->data, val->len, NULL);
357 static BlueSkyStoreImplementation filestore_impl = {
358 .create = filestore_create,
359 .destroy = filestore_destroy,
360 .get = filestore_get,
361 .put = filestore_put,
365 void bluesky_store_init()
367 store_implementations = g_hash_table_new(g_str_hash, g_str_equal);
368 notifier_thread_pool = g_thread_pool_new(notifier_task, NULL, -1, FALSE,
370 //bluesky_store_register(&memstore_impl, "mem");
371 //bluesky_store_register(&filestore_impl, "file");