Add a "multi" storage backend which doubles all GET requests.
[bluesky.git] / bluesky / store-multi.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 /* A stacked storage implementation which tries to improve performance by
10  * duplicating GET requests.  Create using a name like "multi:s3" and each GET
11  * request will be translated into two GET requests to the "s3" backend.  The
12  * first to complete will have its results returned. */
13
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <glib.h>
17 #include <string.h>
18
19 #include "bluesky-private.h"
20
21 struct MultiRequest {
22 };
23
24 static gpointer multistore_new(const gchar *path)
25 {
26     BlueSkyStore *base = bluesky_store_new(path);
27     if (base == NULL) {
28         g_warning("Unable to create base store %s for multirequest stacking.",
29                   path);
30     }
31
32     return base;
33 }
34
35 static void multistore_destroy(gpointer store)
36 {
37     bluesky_store_free(store);
38 }
39
40 static void multistore_completion_handler(BlueSkyStoreAsync *async,
41                                           BlueSkyStoreAsync *top_async)
42 {
43     g_mutex_lock(top_async->lock);
44
45     g_print("Completed a subrequest for %s\n", async->key);
46
47     /* This might be the second request to finish; in that case we don't do
48      * anything. */
49     if (top_async->status == ASYNC_RUNNING) {
50         if (top_async->op == STORE_OP_GET) {
51             bluesky_string_unref(top_async->data);
52             top_async->data = async->data;
53             bluesky_string_ref(top_async->data);
54         }
55         top_async->result = async->result;
56         bluesky_store_async_mark_complete(top_async);
57     }
58
59     g_mutex_unlock(top_async->lock);
60     bluesky_store_async_unref(top_async);
61 }
62
63 static void multistore_submit(gpointer store, BlueSkyStoreAsync *async)
64 {
65     BlueSkyStore *base = (BlueSkyStore *)store;
66
67     g_return_if_fail(async->status == ASYNC_NEW);
68     g_return_if_fail(async->op != STORE_OP_NONE);
69
70     switch (async->op) {
71     case STORE_OP_GET:
72         g_print("Received multirequest for %s\n", async->key);
73         async->status = ASYNC_RUNNING;
74         async->exec_time = bluesky_now_hires();
75         for (int i = 0; i < 2; i++) {
76             BlueSkyStoreAsync *a = bluesky_store_async_new(base);
77             a->op = STORE_OP_GET;
78             a->key = g_strdup(async->key);
79             bluesky_store_async_ref(async);
80             bluesky_store_async_add_notifier(a, (GFunc)multistore_completion_handler, async);
81             bluesky_store_async_submit(a);
82             bluesky_store_async_unref(a);
83         }
84         break;
85
86     case STORE_OP_PUT:
87     {
88         async->status = ASYNC_RUNNING;
89         async->exec_time = bluesky_now_hires();
90
91         bluesky_store_async_ref(async);
92         BlueSkyStoreAsync *a = bluesky_store_async_new(base);
93         a->op = STORE_OP_PUT;
94         a->key = g_strdup(async->key);
95         a->data = async->data;
96         bluesky_string_ref(a->data);
97         bluesky_store_async_add_notifier(a, (GFunc)multistore_completion_handler, async);
98         bluesky_store_async_submit(a);
99         bluesky_store_async_unref(a);
100         break;
101     }
102
103     default:
104         g_warning("Uknown operation type for multistore: %d\n", async->op);
105         bluesky_store_async_mark_complete(async);
106         break;
107     }
108 }
109
110 static void multistore_cleanup(gpointer store, BlueSkyStoreAsync *async)
111 {
112 }
113
114 static BlueSkyStoreImplementation store_impl = {
115     .create = multistore_new,
116     .destroy = multistore_destroy,
117     .submit = multistore_submit,
118     .cleanup = multistore_cleanup,
119 };
120
121 void bluesky_store_init_multi(void)
122 {
123     bluesky_store_register(&store_impl, "multi");
124 }