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>
9 /* Interface to the simple BlueSky test storage server. */
16 #include <sys/types.h>
17 #include <sys/socket.h>
21 #include "bluesky-private.h"
23 #define MAX_IDLE_CONNECTIONS 8
26 GThreadPool *thread_pool;
27 struct sockaddr_in server_addr;
29 /* A pool of open file connections to the server which are not currently in
35 static int get_connection(SimpleStore *store)
39 g_mutex_lock(store->fd_pool_lock);
40 if (!g_queue_is_empty(store->fd_pool)) {
41 fd = GPOINTER_TO_INT(g_queue_pop_head(store->fd_pool));
43 g_mutex_unlock(store->fd_pool_lock);
47 fd = socket(PF_INET, SOCK_STREAM, 0);
49 g_warning("Error creating simplestore socket: %m");
53 if (connect(fd, (struct sockaddr *)&store->server_addr,
54 sizeof(store->server_addr)) < 0) {
55 g_warning("Error connecting to simplestore server: %m");
62 static void put_connection(SimpleStore *store, int fd)
64 g_mutex_lock(store->fd_pool_lock);
65 g_queue_push_head(store->fd_pool, GINT_TO_POINTER(fd));
66 while (g_queue_get_length(store->fd_pool) > MAX_IDLE_CONNECTIONS) {
67 fd = GPOINTER_TO_INT(g_queue_pop_tail(store->fd_pool));
70 g_mutex_unlock(store->fd_pool_lock);
73 static gboolean write_data(int fd, const char *buf, size_t len)
76 ssize_t bytes = write(fd, buf, len);
88 static ssize_t read_line(int fd, char *buf, size_t maxlen)
92 /* Leave room for a null byte at the end */
95 while (buflen == 0 || memchr(buf, '\n', buflen) == NULL) {
98 if (buflen == maxlen) {
102 bytes = read(fd, buf + buflen, maxlen - buflen);
112 g_assert(bytes <= maxlen - buflen);
120 static gboolean read_bytes(int fd, char *buf, size_t len)
125 bytes = read(fd, buf, len);
135 g_assert(bytes <= len);
143 /* TODO: Re-use a connection for multiple requests, and support pipelining. */
144 static void simplestore_task(gpointer a, gpointer b)
146 BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
147 SimpleStore *server = (SimpleStore *)bluesky_store_async_get_handle(async);
149 async->status = ASYNC_RUNNING;
151 int fd = get_connection(server);
153 bluesky_store_async_mark_complete(async);
154 bluesky_store_async_unref(async);
162 char *cmd = g_strdup_printf("GET %s %zd %zd\n",
163 async->key, async->start, async->len);
164 char result_buf[256];
166 if (!write_data(fd, cmd, strlen(cmd))) {
173 int bytes = read_line(fd, result_buf, sizeof(result_buf));
176 int result = atoi(result_buf);
180 char *data = g_malloc(result);
181 if (strchr(result_buf, '\n') != NULL) {
182 int header_size = strchr(result_buf, '\n') - result_buf + 1;
183 int data_bytes = bytes - header_size;
184 if (data_bytes > result)
186 memcpy(data, result_buf + header_size, data_bytes);
187 if (!read_bytes(fd, data + data_bytes, result - data_bytes))
190 if (!read_bytes(fd, data, result))
194 async->data = bluesky_string_new(data, result);
196 async->range_done = TRUE;
203 char *cmd = g_strdup_printf("PUT %s %zd\n",
204 async->key, async->data->len);
205 char result_buf[256];
207 if (!write_data(fd, cmd, strlen(cmd))) {
213 if (!write_data(fd, async->data->data, async->data->len)) {
217 if (read_line(fd, result_buf, sizeof(result_buf)) < 0)
219 if (atoi(result_buf) != 0)
230 int success = (async->result == 0);
231 bluesky_store_async_mark_complete(async);
232 bluesky_store_async_unref(async);
235 put_connection(server, fd);
241 static char *simplestore_lookup_last(gpointer s, const char *prefix)
246 static gpointer simplestore_new(const gchar *path)
248 SimpleStore *store = g_new0(SimpleStore, 1);
250 /* TODO: Right now we leak this memory. We should probably clean up in
251 * simplestore_destroy, but it's not a big deal. */
252 const gchar *host = "127.0.0.1", *port = "9541";
254 gchar **target = g_strsplit(path, ":", 0);
255 if (target[0] != NULL) {
257 if (target[1] != NULL) {
263 g_print("simplestore: %s port %s\n", host, port);
265 struct addrinfo hints;
266 struct addrinfo *lookup_result = NULL;
267 memset(&hints, 0, sizeof(hints));
268 hints.ai_family = AF_INET;
269 hints.ai_socktype = SOCK_STREAM;
270 int res = getaddrinfo(host, port, &hints, &lookup_result);
272 fprintf(stderr, "simplestore: cannot resolve target name: %s\n",
276 for (struct addrinfo *ai = lookup_result; ai != NULL; ai = ai->ai_next) {
277 printf("flags=%d family=%d socktype=%d proto=%d\n",
282 if (ai->ai_addrlen == sizeof(struct sockaddr_in)) {
283 memcpy(&store->server_addr, ai->ai_addr,
284 sizeof(struct sockaddr_in));
286 fprintf(stderr, "Warning: Bad address record size!\n");
289 freeaddrinfo(lookup_result);
291 store->fd_pool = g_queue_new();
292 store->fd_pool_lock = g_mutex_new();
294 store->thread_pool = g_thread_pool_new(simplestore_task, NULL,
295 bluesky_max_threads, FALSE, NULL);
300 static void simplestore_destroy(gpointer store)
304 static void simplestore_submit(gpointer store, BlueSkyStoreAsync *async)
306 SimpleStore *server = (SimpleStore *)store;
308 g_return_if_fail(async->status == ASYNC_NEW);
309 g_return_if_fail(async->op != STORE_OP_NONE);
314 async->status = ASYNC_PENDING;
315 bluesky_store_async_ref(async);
316 g_thread_pool_push(server->thread_pool, async, NULL);
320 g_warning("Uknown operation type for simplestore: %d\n", async->op);
321 bluesky_store_async_mark_complete(async);
326 static void simplestore_cleanup(gpointer store, BlueSkyStoreAsync *async)
330 static BlueSkyStoreImplementation store_impl = {
331 .create = simplestore_new,
332 .destroy = simplestore_destroy,
333 .submit = simplestore_submit,
334 .cleanup = simplestore_cleanup,
335 .lookup_last = simplestore_lookup_last,
338 void bluesky_store_init_simple(void)
340 bluesky_store_register(&store_impl, "simple");