1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
10 * Copyright 2013 Inktank
13 // install the librados-dev package to get this
14 #include <rados/librados.h>
18 int main(int argc, const char **argv)
22 // we will use all of these below
23 const char *pool_name = "hello_world_pool";
24 const char* hello = "hello world!";
25 const char* object_name = "hello_object";
26 rados_ioctx_t io_ctx = NULL;
29 // first, we create a Rados object and initialize it
32 ret = rados_create(&rados, "admin"); // just use the client.admin keyring
33 if (ret < 0) { // let's handle any error that might have come back
34 printf("couldn't initialize rados! error %d\n", ret);
38 printf("we just set up a rados cluster object\n");
43 * Now we need to get the rados object its config info. It can
44 * parse argv for us to find the id, monitors, etc, so let's just
48 ret = rados_conf_parse_argv(rados, argc, argv);
50 // This really can't happen, but we need to check to be a good citizen.
51 printf("failed to parse config options! error %d\n", ret);
55 printf("we just parsed our config options\n");
56 // We also want to apply the config file if the user specified
57 // one, and conf_parse_argv won't do that for us.
59 for (i = 0; i < argc; ++i) {
60 if ((strcmp(argv[i], "-c") == 0) || (strcmp(argv[i], "--conf") == 0)) {
61 ret = rados_conf_read_file(rados, argv[i+1]);
63 // This could fail if the config file is malformed, but it'd be hard.
64 printf("failed to parse config file %s! error %d\n", argv[i+1], ret);
75 * next, we actually connect to the cluster
78 ret = rados_connect(rados);
80 printf("couldn't connect to cluster! error %d\n", ret);
84 printf("we just connected to the rados cluster\n");
89 * let's create our own pool instead of scribbling over real data.
90 * Note that this command creates pools with default PG counts specified
91 * by the monitors, which may not be appropriate for real use -- it's fine
92 * for testing, though.
95 ret = rados_pool_create(rados, pool_name);
97 printf("couldn't create pool! error %d\n", ret);
100 printf("we just created a new pool named %s\n", pool_name);
106 * create an "IoCtx" which is used to do IO to a pool
109 ret = rados_ioctx_create(rados, pool_name, &io_ctx);
111 printf("couldn't set up ioctx! error %d\n", ret);
115 printf("we just created an ioctx for our pool\n");
120 * now let's do some IO to the pool! We'll write "hello world!" to a
125 * now that we have the data to write, let's send it to an object.
126 * We'll use the synchronous interface for simplicity.
128 ret = rados_write_full(io_ctx, object_name, hello, strlen(hello));
130 printf("couldn't write object! error %d\n", ret);
134 printf("we just wrote new object %s, with contents '%s'\n", object_name, hello);
139 * now let's read that object back! Just for fun, we'll do it using
140 * async IO instead of synchronous. (This would be more useful if we
141 * wanted to send off multiple reads at once; see
142 * http://docs.ceph.com/docs/master/rados/api/librados/#asychronous-io )
145 int read_len = 4194304; // this is way more than we need
146 char* read_buf = malloc(read_len + 1); // add one for the terminating 0 we'll add later
148 printf("couldn't allocate read buffer\n");
152 // allocate the completion from librados
153 rados_completion_t read_completion;
154 ret = rados_aio_create_completion(NULL, NULL, NULL, &read_completion);
156 printf("couldn't create completion! error %d\n", ret);
161 printf("we just created a new completion\n");
163 // send off the request.
164 ret = rados_aio_read(io_ctx, object_name, read_completion, read_buf, read_len, 0);
166 printf("couldn't start read object! error %d\n", ret);
169 rados_aio_release(read_completion);
172 // wait for the request to complete, and check that it succeeded.
173 rados_aio_wait_for_complete(read_completion);
174 ret = rados_aio_get_return_value(read_completion);
176 printf("couldn't read object! error %d\n", ret);
179 rados_aio_release(read_completion);
182 read_buf[ret] = 0; // null-terminate the string
183 printf("we read our object %s, and got back %d bytes with contents\n%s\n", object_name, ret, read_buf);
187 rados_aio_release(read_completion);
191 * We can also use xattrs that go alongside the object.
194 const char* version = "1";
195 ret = rados_setxattr(io_ctx, object_name, "version", version, strlen(version));
197 printf("failed to set xattr version entry! error %d\n", ret);
201 printf("we set the xattr 'version' on our object!\n");
206 * And if we want to be really cool, we can do multiple things in a single
207 * atomic operation. For instance, we can update the contents of our object
208 * and set the version at the same time.
211 const char* content = "v2";
212 rados_write_op_t write_op = rados_create_write_op();
214 printf("failed to allocate write op\n");
218 rados_write_op_write_full(write_op, content, strlen(content));
219 const char* version = "2";
220 rados_write_op_setxattr(write_op, "version", version, strlen(version));
221 ret = rados_write_op_operate(write_op, io_ctx, object_name, NULL, 0);
223 printf("failed to do compound write! error %d\n", ret);
225 rados_release_write_op(write_op);
228 printf("we overwrote our object %s with contents\n%s\n", object_name, content);
230 rados_release_write_op(write_op);
234 * And to be even cooler, we can make sure that the object looks the
235 * way we expect before doing the write! Notice how this attempt fails
236 * because the xattr differs.
239 rados_write_op_t failed_write_op = rados_create_write_op();
240 if (!failed_write_op) {
241 printf("failed to allocate write op\n");
245 const char* content = "v2";
246 const char* version = "2";
247 const char* old_version = "1";
248 rados_write_op_cmpxattr(failed_write_op, "version", LIBRADOS_CMPXATTR_OP_EQ, old_version, strlen(old_version));
249 rados_write_op_write_full(failed_write_op, content, strlen(content));
250 rados_write_op_setxattr(failed_write_op, "version", version, strlen(version));
251 ret = rados_write_op_operate(failed_write_op, io_ctx, object_name, NULL, 0);
253 printf("we just failed a write because the xattr wasn't as specified\n");
255 printf("we succeeded on writing despite an xattr comparison mismatch!\n");
257 rados_release_write_op(failed_write_op);
260 rados_release_write_op(failed_write_op);
263 * Now let's do the update with the correct xattr values so it
264 * actually goes through
269 rados_write_op_t update_op = rados_create_write_op();
270 if (!failed_write_op) {
271 printf("failed to allocate write op\n");
275 rados_write_op_cmpxattr(update_op, "version", LIBRADOS_CMPXATTR_OP_EQ, old_version, strlen(old_version));
276 rados_write_op_write_full(update_op, content, strlen(content));
277 rados_write_op_setxattr(update_op, "version", version, strlen(version));
278 ret = rados_write_op_operate(update_op, io_ctx, object_name, NULL, 0);
280 printf("failed to do a compound write update! error %d\n", ret);
282 rados_release_write_op(update_op);
285 printf("we overwrote our object %s following an xattr test with contents\n%s\n", object_name, content);
287 rados_release_write_op(update_op);
294 rados_ioctx_destroy(io_ctx);
299 * And now we're done, so let's remove our pool and then
300 * shut down the connection gracefully.
302 int delete_ret = rados_pool_delete(rados, pool_name);
303 if (delete_ret < 0) {
305 printf("We failed to delete our test pool!\n");
310 rados_shutdown(rados);