]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/neorados/list_pool.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / test / neorados / list_pool.cc
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) 2019 Red Hat <contact@redhat.com>
7 * Author: Adam C. Emerson <aemerson@redhat.com>
8 *
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
13 *
14 */
15
16 #include <iostream>
17 #include <initializer_list>
18 #include <optional>
19 #include <thread>
20 #include <tuple>
21 #include <string_view>
22 #include <vector>
23
24 #include <sys/param.h>
25
26 #include <unistd.h>
27
28 #include <boost/system/system_error.hpp>
29
30 #include <fmt/format.h>
31
32 #include "include/neorados/RADOS.hpp"
33
34 #include "include/scope_guard.h"
35
36 #include "common/async/context_pool.h"
37 #include "common/ceph_time.h"
38 #include "common/ceph_argparse.h"
39 #include "common/async/blocked_completion.h"
40
41 #include "global/global_init.h"
42
43 #include "test/neorados/common_tests.h"
44
45
46 namespace ba = boost::asio;
47 namespace bs = boost::system;
48 namespace ca = ceph::async;
49 namespace R = neorados;
50
51 std::string_view hostname() {
52 static char hostname[MAXHOSTNAMELEN] = { 0 };
53 static size_t len = 0;
54 if (!len) {
55 auto r = gethostname(hostname, sizeof(hostname));
56 if (r != 0) {
57 throw bs::system_error(
58 errno, bs::system_category());
59 }
60 len = std::strlen(hostname);
61 }
62 return {hostname, len};
63 }
64
65 std::string temp_pool_name(const std::string_view prefix)
66 {
67 using namespace std::chrono;
68 static std::uint64_t num = 1;
69 return fmt::format(
70 "{}-{}-{}-{}-{}",
71 prefix,
72 hostname(),
73 getpid(),
74 duration_cast<milliseconds>(ceph::coarse_real_clock::now()
75 .time_since_epoch()).count(),
76 num++);
77 }
78
79 bs::error_code noisy_list(R::RADOS& r, int64_t p)
80 {
81 auto b = R::Cursor::begin();
82 auto e = R::Cursor::end();
83
84 std::cout << "begin = " << b.to_str() << std::endl;
85 std::cout << "end = " << e.to_str() << std::endl;
86 try {
87 auto [v, next] = r.enumerate_objects(p, b, e, 1000, {}, ca::use_blocked,
88 R::all_nspaces);
89
90 std::cout << "Got " << v.size() << " entries." << std::endl;
91 std::cout << "next cursor = " << next.to_str() << std::endl;
92 std::cout << "next == end: " << (next == e) << std::endl;
93 std::cout << "Returned Objects: ";
94 std::cout << "[";
95 auto o = v.cbegin();
96 while (o != v.cend()) {
97 std::cout << *o;
98 if (++o != v.cend())
99 std::cout << " ";
100 }
101 std::cout << "]" << std::endl;
102 } catch (const bs::system_error& e) {
103 std::cerr << "RADOS::enumerate_objects: " << e.what() << std::endl;
104 return e.code();
105 }
106 return {};
107 }
108
109 bs::error_code create_several(R::RADOS& r, const R::IOContext& i,
110 std::initializer_list<std::string> l)
111 {
112 for (const auto& o : l) try {
113 R::WriteOp op;
114 std::cout << "Creating " << o << std::endl;
115 ceph::bufferlist bl;
116 bl.append("My bologna has no name.");
117 op.write_full(std::move(bl));
118 r.execute(o, i, std::move(op), ca::use_blocked);
119 } catch (const bs::system_error& e) {
120 std::cerr << "RADOS::execute: " << e.what() << std::endl;
121 return e.code();
122 }
123 return {};
124 }
125
126 int main(int argc, char** argv)
127 {
128 using namespace std::literals;
129
130 std::vector<const char*> args;
131 argv_to_vec(argc, const_cast<const char**>(argv), args);
132 env_to_vec(args);
133
134 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
135 CODE_ENVIRONMENT_UTILITY, 0);
136 common_init_finish(cct.get());
137
138 try {
139 ca::io_context_pool p(1);
140 auto r = R::RADOS::make_with_cct(cct.get(), p, ca::use_blocked);
141
142 auto pool_name = get_temp_pool_name("ceph_test_RADOS_list_pool"sv);
143 r.create_pool(pool_name, std::nullopt, ca::use_blocked);
144 auto pd = make_scope_guard(
145 [&pool_name, &r]() {
146 r.delete_pool(pool_name, ca::use_blocked);
147 });
148 auto pool = r.lookup_pool(pool_name, ca::use_blocked);
149 R::IOContext i(pool);
150
151 if (noisy_list(r, pool)) {
152 return 1;
153 }
154 if (create_several(r, i, {"meow", "woof", "squeak"})) {
155 return 1;
156 }
157 if (noisy_list(r, pool)) {
158 return 1;
159 }
160
161 } catch (const bs::system_error& e) {
162 std::cerr << "Error: " << e.what() << std::endl;
163 return 1;
164 }
165
166 return 0;
167 }