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 * Copyright (C) 2011 New Dream Network
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.
15 #include "cross_process_sem.h"
16 #include "include/rados/librados.h"
17 #include "st_rados_create_pool.h"
18 #include "systest_runnable.h"
19 #include "systest_settings.h"
29 using std::ostringstream;
31 std::string StRadosCreatePool::
32 get_random_buf(int sz)
35 int size = rand() % sz; // yep, it's not very random
36 for (int i = 0; i < size; ++i) {
43 StRadosCreatePool(int argc, const char **argv,
44 CrossProcessSem *setup_sem,
45 CrossProcessSem *pool_setup_sem,
46 CrossProcessSem *close_create_pool,
47 const std::string &pool_name,
49 const std::string &suffix)
50 : SysTestRunnable(argc, argv),
51 m_setup_sem(setup_sem),
52 m_pool_setup_sem(pool_setup_sem),
53 m_close_create_pool(close_create_pool),
54 m_pool_name(pool_name),
55 m_num_objects(num_objects),
65 int StRadosCreatePool::
70 RETURN1_IF_NONZERO(rados_create(&cl, NULL));
71 rados_conf_parse_argv(cl, m_argc, m_argv);
72 rados_conf_parse_argv(cl, m_argc, m_argv);
73 RETURN1_IF_NONZERO(rados_conf_read_file(cl, NULL));
74 std::string log_name = SysTestSettings::inst().get_log_name(get_id_str());
75 if (!log_name.empty())
76 rados_conf_set(cl, "log_file", log_name.c_str());
77 rados_conf_parse_env(cl, NULL);
84 RETURN1_IF_NONZERO(rados_connect(cl));
86 printf("%s: creating pool %s\n", get_id_str(), m_pool_name.c_str());
87 rados_pool_create(cl, m_pool_name.c_str());
89 RETURN1_IF_NONZERO(rados_ioctx_create(cl, m_pool_name.c_str(), &io_ctx));
91 for (int i = 0; i < m_num_objects; ++i) {
93 snprintf(oid, sizeof(oid), "%d%s", i, m_suffix.c_str());
94 std::string buf(get_random_buf(256));
95 int ret = rados_write(io_ctx, oid, buf.c_str(), buf.size(), 0);
97 printf("%s: rados_write(%s) failed with error: %d\n",
98 get_id_str(), oid, ret);
102 if (((i % 25) == 0) || (i == m_num_objects - 1)) {
103 printf("%s: created object %d...\n", get_id_str(), i);
108 printf("%s: finishing.\n", get_id_str());
109 if (m_pool_setup_sem)
110 m_pool_setup_sem->post();
111 if (m_close_create_pool)
112 m_close_create_pool->wait();
113 rados_ioctx_destroy(io_ctx);
118 std::string get_temp_pool_name(const char* prefix)
123 ret = gethostname(hostname, sizeof(hostname));
126 ret = snprintf(poolname, sizeof(poolname),
127 "%s.%s-%d", prefix, hostname, getpid());
129 assert((unsigned int)ret < sizeof(poolname));