Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / examples / librados / hello_world_c.c
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
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
11  */
12
13 // install the librados-dev package to get this
14 #include <rados/librados.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 int main(int argc, const char **argv)
19 {
20   int ret = 0;
21
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;
27   int pool_created = 0;
28
29   // first, we create a Rados object and initialize it
30   rados_t rados = NULL;
31   {
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);
35       ret = EXIT_FAILURE;
36       goto out;
37     } else {
38       printf("we just set up a rados cluster object\n");
39     }
40   }
41
42   /*
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
45    * use that.
46    */
47   {
48     ret = rados_conf_parse_argv(rados, argc, argv);
49     if (ret < 0) {
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);
52       ret = EXIT_FAILURE;
53       goto out;
54     } else {
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.
58       int i;
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]);
62           if (ret < 0) {
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);
65             ret = EXIT_FAILURE;
66             goto out;
67           }
68           break;
69         }
70       }
71     }
72   }
73
74   /*
75    * next, we actually connect to the cluster
76    */
77   {
78     ret = rados_connect(rados);
79     if (ret < 0) {
80       printf("couldn't connect to cluster! error %d\n", ret);
81       ret = EXIT_FAILURE;
82       goto out;
83     } else {
84       printf("we just connected to the rados cluster\n");
85     }
86   }
87
88   /*
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.
93    */
94   {
95     ret = rados_pool_create(rados, pool_name);
96     if (ret < 0) {
97       printf("couldn't create pool! error %d\n", ret);
98       return EXIT_FAILURE;
99     } else {
100       printf("we just created a new pool named %s\n", pool_name);
101     }
102     pool_created = 1;
103   }
104
105   /*
106    * create an "IoCtx" which is used to do IO to a pool
107    */
108   {
109     ret = rados_ioctx_create(rados, pool_name, &io_ctx);
110     if (ret < 0) {
111       printf("couldn't set up ioctx! error %d\n", ret);
112       ret = EXIT_FAILURE;
113       goto out;
114     } else {
115       printf("we just created an ioctx for our pool\n");
116     }
117   }
118
119   /*
120    * now let's do some IO to the pool! We'll write "hello world!" to a
121    * new object.
122    */
123   {
124     /*
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.
127      */
128     ret = rados_write_full(io_ctx, object_name, hello, strlen(hello));
129     if (ret < 0) {
130       printf("couldn't write object! error %d\n", ret);
131       ret = EXIT_FAILURE;
132       goto out;
133     } else {
134       printf("we just wrote new object %s, with contents '%s'\n", object_name, hello);
135     }
136   }
137
138   /*
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 )
143    */
144   {
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
147     if (!read_buf) {
148       printf("couldn't allocate read buffer\n");
149       ret = EXIT_FAILURE;
150       goto out;
151     }
152     // allocate the completion from librados
153     rados_completion_t read_completion;
154     ret = rados_aio_create_completion(NULL, NULL, NULL, &read_completion);
155     if (ret < 0) {
156       printf("couldn't create completion! error %d\n", ret);
157       ret = EXIT_FAILURE;
158       free(read_buf);
159       goto out;
160     } else {
161       printf("we just created a new completion\n");
162     }
163     // send off the request.
164     ret = rados_aio_read(io_ctx, object_name, read_completion, read_buf, read_len, 0);
165     if (ret < 0) {
166       printf("couldn't start read object! error %d\n", ret);
167       ret = EXIT_FAILURE;
168       free(read_buf);
169       rados_aio_release(read_completion);
170       goto out;
171     }
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);
175     if (ret < 0) {
176       printf("couldn't read object! error %d\n", ret);
177       ret = EXIT_FAILURE;
178       free(read_buf);
179       rados_aio_release(read_completion);
180       goto out;
181     } else {
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);
184     }
185
186     free(read_buf);
187     rados_aio_release(read_completion);
188   }
189
190   /*
191    * We can also use xattrs that go alongside the object.
192    */
193   {
194     const char* version = "1";
195     ret = rados_setxattr(io_ctx, object_name, "version", version, strlen(version));
196     if (ret < 0) {
197       printf("failed to set xattr version entry! error %d\n", ret);
198       ret = EXIT_FAILURE;
199       goto out;
200     } else {
201       printf("we set the xattr 'version' on our object!\n");
202     }
203   }
204
205   /*
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.
209    */
210   {
211     const char* content = "v2";
212     rados_write_op_t write_op = rados_create_write_op();
213     if (!write_op) {
214       printf("failed to allocate write op\n");
215       ret = EXIT_FAILURE;
216       goto out;
217     }
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);
222     if (ret < 0) {
223       printf("failed to do compound write! error %d\n", ret);
224       ret = EXIT_FAILURE;
225       rados_release_write_op(write_op);
226       goto out;
227     } else {
228       printf("we overwrote our object %s with contents\n%s\n", object_name, content);
229     }
230     rados_release_write_op(write_op);
231   }
232
233   /*
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.
237    */
238   {
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");
242       ret = EXIT_FAILURE;
243       goto out;
244     }
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);
252     if (ret < 0) {
253       printf("we just failed a write because the xattr wasn't as specified\n");
254     } else {
255       printf("we succeeded on writing despite an xattr comparison mismatch!\n");
256       ret = EXIT_FAILURE;
257       rados_release_write_op(failed_write_op);
258       goto out;
259     }
260     rados_release_write_op(failed_write_op);
261
262     /*
263      * Now let's do the update with the correct xattr values so it
264      * actually goes through
265      */
266     content = "v3";
267     old_version = "2";
268     version = "3";
269     rados_write_op_t update_op = rados_create_write_op();
270     if (!failed_write_op) {
271       printf("failed to allocate write op\n");
272       ret = EXIT_FAILURE;
273       goto out;
274     }
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);
279     if (ret < 0) {
280       printf("failed to do a compound write update! error %d\n", ret);
281       ret = EXIT_FAILURE;
282       rados_release_write_op(update_op);
283       goto out;
284     } else {
285       printf("we overwrote our object %s following an xattr test with contents\n%s\n", object_name, content);
286     }
287     rados_release_write_op(update_op);
288   }
289
290   ret = EXIT_SUCCESS;
291
292  out:
293   if (io_ctx) {
294     rados_ioctx_destroy(io_ctx);
295   }
296
297   if (pool_created) {
298     /*
299      * And now we're done, so let's remove our pool and then
300      * shut down the connection gracefully.
301      */
302     int delete_ret = rados_pool_delete(rados, pool_name);
303     if (delete_ret < 0) {
304       // be careful not to
305       printf("We failed to delete our test pool!\n");
306       ret = EXIT_FAILURE;
307     }
308   }
309
310   rados_shutdown(rados);
311
312   return ret;
313 }