summaryrefslogtreecommitdiffstats
path: root/src/test/system/st_rados_create_pool.cc
blob: a802d9da4f4a0af073924b9008f4c6c2e8407d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation.  See file COPYING.
*
*/

#include "cross_process_sem.h"
#include "include/ceph_assert.h"
#include "include/rados/librados.h"
#include "st_rados_create_pool.h"
#include "systest_runnable.h"
#include "systest_settings.h"

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sstream>
#include <string>

using std::ostringstream;

std::string StRadosCreatePool::
get_random_buf(int sz)
{
  ostringstream oss;
  int size = rand() % sz; // yep, it's not very random
  for (int i = 0; i < size; ++i) {
    oss << ".";
  }
  return oss.str();
}

StRadosCreatePool::
StRadosCreatePool(int argc, const char **argv,
		  CrossProcessSem *setup_sem,
		  CrossProcessSem *pool_setup_sem,
		  CrossProcessSem *close_create_pool,
		  const std::string &pool_name,
		  int num_objects,
		  const std::string &suffix)
  : SysTestRunnable(argc, argv),
    m_setup_sem(setup_sem),
    m_pool_setup_sem(pool_setup_sem),
    m_close_create_pool(close_create_pool),
    m_pool_name(pool_name),
    m_num_objects(num_objects),
    m_suffix(suffix)
{
}

StRadosCreatePool::
~StRadosCreatePool()
{
}

int StRadosCreatePool::
run()
{
  int ret_val = 0;
  rados_t cl;
  RETURN1_IF_NONZERO(rados_create(&cl, NULL));
  rados_conf_parse_argv(cl, m_argc, m_argv);
  rados_conf_parse_argv(cl, m_argc, m_argv);
  RETURN1_IF_NONZERO(rados_conf_read_file(cl, NULL));
  std::string log_name = SysTestSettings::inst().get_log_name(get_id_str());
  if (!log_name.empty())
    rados_conf_set(cl, "log_file", log_name.c_str());
  rados_conf_parse_env(cl, NULL);

  if (m_setup_sem) {
    m_setup_sem->wait();
    m_setup_sem->post();
  }

  RETURN1_IF_NONZERO(rados_connect(cl));

  printf("%s: creating pool %s\n", get_id_str(), m_pool_name.c_str());
  rados_pool_create(cl, m_pool_name.c_str());
  rados_ioctx_t io_ctx;
  RETURN1_IF_NONZERO(rados_ioctx_create(cl, m_pool_name.c_str(), &io_ctx));

  for (int i = 0; i < m_num_objects; ++i) {
    char oid[128];
    snprintf(oid, sizeof(oid), "%d%s", i, m_suffix.c_str());
    std::string buf(get_random_buf(256));
    int ret = rados_write(io_ctx, oid, buf.c_str(), buf.size(), 0);
    if (ret != 0) {
      printf("%s: rados_write(%s) failed with error: %d\n",
	     get_id_str(), oid, ret);
      ret_val = ret;
      goto out;
    }
    if (((i % 25) == 0) || (i == m_num_objects - 1)) {
      printf("%s: created object %d...\n", get_id_str(), i);
    }
  }

out:
  printf("%s: finishing.\n", get_id_str());
  if (m_pool_setup_sem)
    m_pool_setup_sem->post();
  if (m_close_create_pool)
    m_close_create_pool->wait();
  rados_ioctx_destroy(io_ctx);
  rados_shutdown(cl);
  return ret_val;
}

std::string get_temp_pool_name(const char* prefix)
{
  ceph_assert(prefix);
  char hostname[80];
  int ret = 0;
  ret = gethostname(hostname, sizeof(hostname));
  ceph_assert(!ret);
  char poolname[256];
  ret = snprintf(poolname, sizeof(poolname),
                 "%s.%s-%d", prefix, hostname, getpid());
  ceph_assert(ret > 0);
  ceph_assert((unsigned int)ret < sizeof(poolname));
  return poolname;
}