(Mostly) merge local and cloud logging together.
[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->lock = g_mutex_new();
90     log->cond = g_cond_new();
91     log->fs = fs;
92     log->type = LOGTYPE_UNKNOWN;
93     log->id = bluesky_cloudlog_new_id();
94     log->pointers = g_array_new(FALSE, TRUE, sizeof(BlueSkyCloudID));
95     g_atomic_int_set(&log->refcount, 1);
96
97     return log;
98 }
99
100 void bluesky_cloudlog_ref(BlueSkyCloudLog *log)
101 {
102     g_atomic_int_inc(&log->refcount);
103 }
104
105 void bluesky_cloudlog_unref(BlueSkyCloudLog *log)
106 {
107     if (g_atomic_int_dec_and_test(&log->refcount)) {
108         g_print("Cloud log refcount dropped to zero.\n");
109     }
110 }
111
112 /* Start a write of the object to the local log. */
113 void bluesky_cloudlog_sync(BlueSkyCloudLog *log)
114 {
115     bluesky_log_item_submit(log, log->fs->log);
116 }
117
118 /* Add the given entry to the global hash table containing cloud log entries.
119  * Takes ownership of the caller's reference. */
120 void bluesky_cloudlog_insert(BlueSkyCloudLog *log)
121 {
122     g_mutex_lock(log->fs->lock);
123     g_hash_table_insert(log->fs->locations, &log->id, log);
124     g_mutex_unlock(log->fs->lock);
125 }
126
127 struct log_header {
128     char magic[4];
129     uint32_t size;
130     BlueSkyCloudID id;
131     uint32_t pointer_count;
132 } __attribute__((packed));
133
134 struct logref {
135     BlueSkyCloudID id;
136     BlueSkyCloudPointer location;
137 } __attribute__((packed));
138
139 struct log_footer {
140     char refmagic[4];
141     struct logref refs[0];
142 };
143
144 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
145                                                BlueSkyCloudLogState *state)
146 {
147     if (log->location_flags & CLOUDLOG_CLOUD) {
148         return log->location;
149     }
150
151     g_print("Flushing object %s to cloud...\n",
152             bluesky_cloudlog_id_to_string(log->id));
153
154     for (int i = 0; i < log->pointers->len; i++) {
155         BlueSkyCloudID id = g_array_index(log->pointers, BlueSkyCloudID, i);
156         g_print("  ...checking reference %s...\n",
157                 bluesky_cloudlog_id_to_string(id));
158         g_mutex_lock(log->fs->lock);
159         BlueSkyCloudLog *log2
160             = (BlueSkyCloudLog *)g_hash_table_lookup(log->fs->locations, &id);
161         // TODO: refcount
162         g_mutex_unlock(log->fs->lock);
163         g_assert(log2 != NULL);
164         bluesky_cloudlog_serialize(log2, state);
165     }
166
167     g_assert(log->data != NULL);
168
169     log->location = state->location;
170     log->location.offset = state->data->len;
171     log->location.size
172         = sizeof(struct log_header) + sizeof(BlueSkyCloudID) * 0
173            + log->data->len;
174
175     struct log_header header;
176     memcpy(header.magic, "AgI ", 4);
177     header.size = GUINT32_TO_LE(log->location.size);
178     header.id = log->id;
179     header.pointer_count = GUINT32_TO_LE(0);
180
181     g_string_append_len(state->data, (const char *)&header, sizeof(header));
182     g_string_append_len(state->data, log->data->data, log->data->len);
183
184     log->location_flags |= CLOUDLOG_CLOUD;
185
186     return log->location;
187 }
188
189 static void find_inodes(gpointer key, gpointer value, gpointer user_data)
190 {
191     BlueSkyCloudLogState *state = (BlueSkyCloudLogState *)user_data;
192     BlueSkyCloudLog *item = (BlueSkyCloudLog *)value;
193
194     if (item->type != LOGTYPE_INODE)
195         return;
196
197     bluesky_cloudlog_ref(item);
198     state->inode_list = g_list_prepend(state->inode_list, item);
199 }
200
201 void bluesky_cloudlog_write_log(BlueSkyFS *fs)
202 {
203     g_print("Starting cloudlog write...\n");
204
205     BlueSkyCloudLogState *state = fs->log_state;
206     state->data = g_string_new("");
207
208     g_mutex_lock(fs->lock);
209     g_hash_table_foreach(fs->locations, find_inodes, state);
210     g_mutex_unlock(fs->lock);
211
212     while (state->inode_list != NULL) {
213         BlueSkyCloudLog *log = (BlueSkyCloudLog *)state->inode_list->data;
214         bluesky_cloudlog_serialize(log, state);
215         state->inode_list = g_list_delete_link(state->inode_list,
216                                                state->inode_list);
217     }
218
219     g_print("Serialized %zd bytes of data\n", state->data->len);
220
221     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
222     async->op = STORE_OP_PUT;
223     async->key = g_strdup_printf("log-%08d-%08d",
224                                  state->location.directory,
225                                  state->location.sequence);
226     async->data = bluesky_string_new_from_gstring(state->data);
227     bluesky_store_async_submit(async);
228     bluesky_store_async_wait(async);
229     bluesky_store_async_unref(async);
230
231     state->data = NULL;
232     state->location.sequence++;
233     state->location.offset = 0;
234 }