Some initial work on logging gathering data into cloud log segments.
[bluesky.git] / bluesky / cloudlog.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 #include <stdio.h>
10 #include <stdint.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky-private.h"
15
16 /* The locations hash table in the file system is used to map objects to their locations.  Objects are named using 128- */
17
18 typedef struct {
19     BlueSkyCloudID id;
20
21     BlueSkyCloudPointer *cloud_loc;
22 } BlueSkyLocationEntry;
23
24 BlueSkyCloudID bluesky_cloudlog_new_id()
25 {
26     BlueSkyCloudID id;
27     bluesky_crypt_random_bytes((uint8_t *)&id.bytes, sizeof(id));
28     return id;
29 }
30
31 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id)
32 {
33     char buf[sizeof(BlueSkyCloudID) * 2 + 1];
34     buf[0] = '\0';
35
36     for (int i = 0; i < sizeof(BlueSkyCloudID); i++) {
37         sprintf(&buf[2*i], "%02x", (uint8_t)(id.bytes[i]));
38     }
39
40     return g_strdup(buf);
41 }
42
43 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr)
44 {
45     BlueSkyCloudID id;
46     memset(&id, 0, sizeof(id));
47     for (int i = 0; i < 2*sizeof(BlueSkyCloudID); i++) {
48         char c = idstr[i];
49         if (c == '\0') {
50             g_warning("Short cloud id: %s\n", idstr);
51             break;
52         }
53         int val = 0;
54         if (c >= '0' && c <= '9')
55             val = c - '0';
56         else if (c >= 'a' && c <= 'f')
57             val = c - 'a' + 10;
58         else
59             g_warning("Bad character in cloud id: %s\n", idstr);
60         id.bytes[i / 2] += val << (i % 2 ? 0 : 4);
61     }
62     return id;
63 }
64
65 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b)
66 {
67     BlueSkyCloudID *id1 = (BlueSkyCloudID *)a, *id2 = (BlueSkyCloudID *)b;
68
69     return memcmp(id1, id2, sizeof(BlueSkyCloudID)) == 0;
70 }
71
72 guint bluesky_cloudlog_hash(gconstpointer a)
73 {
74     BlueSkyCloudID *id = (BlueSkyCloudID *)a;
75
76     // Assume that bits in the ID are randomly chosen so that any subset of the
77     // bits can be used as a hash key.
78     return *(guint *)(&id->bytes);
79 }
80
81 /* Formatting of cloud log segments.  This handles grouping items together
82  * before writing a batch to the cloud, handling indirection through items like
83  * the inode map, etc. */
84
85 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs)
86 {
87     BlueSkyCloudLog *log = g_new0(BlueSkyCloudLog, 1);
88
89     log->fs = fs;
90     log->type = LOGTYPE_UNKNOWN;
91     log->id = bluesky_cloudlog_new_id();
92     log->pointers = g_array_new(FALSE, TRUE, sizeof(BlueSkyCloudID));
93     g_atomic_int_set(&log->refcount, 1);
94
95     return log;
96 }
97
98 void bluesky_cloudlog_ref(BlueSkyCloudLog *log)
99 {
100     g_atomic_int_inc(&log->refcount);
101 }
102
103 void bluesky_cloudlog_unref(BlueSkyCloudLog *log)
104 {
105     if (g_atomic_int_dec_and_test(&log->refcount)) {
106         g_print("Cloud log refcount dropped to zero.\n");
107     }
108 }
109
110 /* Start a write of the object to the local log. */
111 BlueSkyLogItem *bluesky_cloudlog_sync(BlueSkyCloudLog *log)
112 {
113     BlueSkyLogItem *log_item = bluesky_log_item_new();
114     log_item->key = bluesky_cloudlog_id_to_string(log->id);
115     log_item->data = log->data;
116     bluesky_string_ref(log->data);
117     bluesky_log_item_submit(log_item, log->fs->log);
118     return log_item;
119 }
120
121 /* Add the given entry to the global hash table containing cloud log entries.
122  * Takes ownership of the caller's reference. */
123 void bluesky_cloudlog_insert(BlueSkyCloudLog *log)
124 {
125     g_mutex_lock(log->fs->lock);
126     g_hash_table_insert(log->fs->locations, &log->id, log);
127     g_mutex_unlock(log->fs->lock);
128 }
129
130 /* Serialize objects into a log segment to be written to the cloud. */
131 struct log_state {
132     GString *data;
133     BlueSkyCloudPointer location;
134     GList *inode_list;
135 };
136
137 struct log_header {
138     char magic[4];
139     uint32_t size;
140     BlueSkyCloudID id;
141     uint32_t pointer_count;
142 } __attribute__((packed));
143
144 struct logref {
145     BlueSkyCloudID id;
146     BlueSkyCloudPointer location;
147 } __attribute__((packed));
148
149 struct log_footer {
150     char refmagic[4];
151     struct logref refs[0];
152 };
153
154 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
155                                                struct log_state *state)
156 {
157     if (log->location_flags & CLOUDLOG_CLOUD) {
158         return log->location;
159     }
160
161     g_print("Flushing object %s to cloud...\n",
162             bluesky_cloudlog_id_to_string(log->id));
163
164     for (int i = 0; i < log->pointers->len; i++) {
165         BlueSkyCloudID id = g_array_index(log->pointers, BlueSkyCloudID, i);
166         g_print("  ...checking reference %s...\n",
167                 bluesky_cloudlog_id_to_string(id));
168         g_mutex_lock(log->fs->lock);
169         BlueSkyCloudLog *log2
170             = (BlueSkyCloudLog *)g_hash_table_lookup(log->fs->locations, &id);
171         // TODO: refcount
172         g_mutex_unlock(log->fs->lock);
173         g_assert(log2 != NULL);
174         bluesky_cloudlog_serialize(log2, state);
175     }
176
177     g_assert(log->data != NULL);
178
179     log->location = state->location;
180     log->location.offset = state->data->len;
181     log->location.size
182         = sizeof(struct log_header) + sizeof(BlueSkyCloudID) * 0
183            + log->data->len;
184
185     struct log_header header;
186     memcpy(header.magic, "AgI ", 4);
187     header.size = GUINT32_TO_LE(log->location.size);
188     header.id = log->id;
189     header.pointer_count = GUINT32_TO_LE(0);
190
191     g_string_append_len(state->data, (const char *)&header, sizeof(header));
192     g_string_append_len(state->data, log->data->data, log->data->len);
193
194     log->location_flags |= CLOUDLOG_CLOUD;
195
196     return log->location;
197 }
198
199 static void find_inodes(gpointer key, gpointer value, gpointer user_data)
200 {
201     struct log_state *state = (struct log_state *)user_data;
202     BlueSkyCloudLog *item = (BlueSkyCloudLog *)value;
203
204     if (item->type != LOGTYPE_INODE)
205         return;
206
207     bluesky_cloudlog_ref(item);
208     state->inode_list = g_list_prepend(state->inode_list, item);
209 }
210
211 void bluesky_cloudlog_write_log(BlueSkyFS *fs)
212 {
213     g_print("Starting cloudlog write...\n");
214
215     struct log_state state;
216     state.data = g_string_new("");
217     state.location.directory = 0;
218     state.location.sequence = 0;
219     state.location.offset = 0;
220     state.location.size = 0;
221     state.inode_list = NULL;
222
223     g_mutex_lock(fs->lock);
224     g_hash_table_foreach(fs->locations, find_inodes, &state);
225     g_mutex_unlock(fs->lock);
226
227     while (state.inode_list != NULL) {
228         BlueSkyCloudLog *log = (BlueSkyCloudLog *)state.inode_list->data;
229         bluesky_cloudlog_serialize(log, &state);
230         state.inode_list = g_list_delete_link(state.inode_list,
231                                               state.inode_list);
232     }
233
234     g_print("Serialized %zd bytes of data\n", state.data->len);
235
236     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
237     async->op = STORE_OP_PUT;
238     async->key = g_strdup_printf("log-%08d-%08d",
239                                  state.location.directory,
240                                  state.location.sequence);
241     async->data = bluesky_string_new_from_gstring(state.data);
242     bluesky_store_async_submit(async);
243     bluesky_store_async_wait(async);
244     bluesky_store_async_unref(async);
245 }