]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include "cross_process_sem.h" | |
f67539c2 | 16 | #include "include/ceph_assert.h" |
7c673cae FG |
17 | #include "include/rados/librados.h" |
18 | #include "st_rados_create_pool.h" | |
19 | #include "systest_runnable.h" | |
20 | #include "systest_settings.h" | |
21 | ||
22 | #include <errno.h> | |
23 | #include <stdarg.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <sstream> | |
28 | #include <string> | |
29 | ||
30 | using std::ostringstream; | |
31 | ||
32 | std::string StRadosCreatePool:: | |
33 | get_random_buf(int sz) | |
34 | { | |
35 | ostringstream oss; | |
36 | int size = rand() % sz; // yep, it's not very random | |
37 | for (int i = 0; i < size; ++i) { | |
38 | oss << "."; | |
39 | } | |
40 | return oss.str(); | |
41 | } | |
42 | ||
43 | StRadosCreatePool:: | |
44 | StRadosCreatePool(int argc, const char **argv, | |
45 | CrossProcessSem *setup_sem, | |
46 | CrossProcessSem *pool_setup_sem, | |
47 | CrossProcessSem *close_create_pool, | |
48 | const std::string &pool_name, | |
49 | int num_objects, | |
50 | const std::string &suffix) | |
51 | : SysTestRunnable(argc, argv), | |
52 | m_setup_sem(setup_sem), | |
53 | m_pool_setup_sem(pool_setup_sem), | |
54 | m_close_create_pool(close_create_pool), | |
55 | m_pool_name(pool_name), | |
56 | m_num_objects(num_objects), | |
57 | m_suffix(suffix) | |
58 | { | |
59 | } | |
60 | ||
61 | StRadosCreatePool:: | |
62 | ~StRadosCreatePool() | |
63 | { | |
64 | } | |
65 | ||
66 | int StRadosCreatePool:: | |
67 | run() | |
68 | { | |
69 | int ret_val = 0; | |
70 | rados_t cl; | |
71 | RETURN1_IF_NONZERO(rados_create(&cl, NULL)); | |
72 | rados_conf_parse_argv(cl, m_argc, m_argv); | |
73 | rados_conf_parse_argv(cl, m_argc, m_argv); | |
74 | RETURN1_IF_NONZERO(rados_conf_read_file(cl, NULL)); | |
75 | std::string log_name = SysTestSettings::inst().get_log_name(get_id_str()); | |
76 | if (!log_name.empty()) | |
77 | rados_conf_set(cl, "log_file", log_name.c_str()); | |
78 | rados_conf_parse_env(cl, NULL); | |
79 | ||
80 | if (m_setup_sem) { | |
81 | m_setup_sem->wait(); | |
82 | m_setup_sem->post(); | |
83 | } | |
84 | ||
85 | RETURN1_IF_NONZERO(rados_connect(cl)); | |
86 | ||
87 | printf("%s: creating pool %s\n", get_id_str(), m_pool_name.c_str()); | |
88 | rados_pool_create(cl, m_pool_name.c_str()); | |
89 | rados_ioctx_t io_ctx; | |
90 | RETURN1_IF_NONZERO(rados_ioctx_create(cl, m_pool_name.c_str(), &io_ctx)); | |
91 | ||
92 | for (int i = 0; i < m_num_objects; ++i) { | |
93 | char oid[128]; | |
94 | snprintf(oid, sizeof(oid), "%d%s", i, m_suffix.c_str()); | |
95 | std::string buf(get_random_buf(256)); | |
96 | int ret = rados_write(io_ctx, oid, buf.c_str(), buf.size(), 0); | |
97 | if (ret != 0) { | |
98 | printf("%s: rados_write(%s) failed with error: %d\n", | |
99 | get_id_str(), oid, ret); | |
100 | ret_val = ret; | |
101 | goto out; | |
102 | } | |
103 | if (((i % 25) == 0) || (i == m_num_objects - 1)) { | |
104 | printf("%s: created object %d...\n", get_id_str(), i); | |
105 | } | |
106 | } | |
107 | ||
108 | out: | |
109 | printf("%s: finishing.\n", get_id_str()); | |
110 | if (m_pool_setup_sem) | |
111 | m_pool_setup_sem->post(); | |
112 | if (m_close_create_pool) | |
113 | m_close_create_pool->wait(); | |
114 | rados_ioctx_destroy(io_ctx); | |
115 | rados_shutdown(cl); | |
116 | return ret_val; | |
117 | } | |
118 | ||
119 | std::string get_temp_pool_name(const char* prefix) | |
120 | { | |
11fdf7f2 | 121 | ceph_assert(prefix); |
7c673cae FG |
122 | char hostname[80]; |
123 | int ret = 0; | |
124 | ret = gethostname(hostname, sizeof(hostname)); | |
11fdf7f2 | 125 | ceph_assert(!ret); |
7c673cae FG |
126 | char poolname[256]; |
127 | ret = snprintf(poolname, sizeof(poolname), | |
128 | "%s.%s-%d", prefix, hostname, getpid()); | |
11fdf7f2 TL |
129 | ceph_assert(ret > 0); |
130 | ceph_assert((unsigned int)ret < sizeof(poolname)); | |
7c673cae FG |
131 | return poolname; |
132 | } |