]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/multi_stress_watch.cc
0d1e19ca9d1d0790d0f809b417fc8de805efc1a6
[ceph.git] / ceph / src / test / multi_stress_watch.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 #include "include/rados/librados.h"
3 #include "include/rados/librados.hpp"
4 #include "test/librados/test.h"
5
6 #include <semaphore.h>
7 #include <errno.h>
8 #include <map>
9 #include <sstream>
10 #include <iostream>
11 #include <string>
12 #include <stdlib.h>
13 #include <unistd.h>
14
15 using namespace librados;
16 using std::map;
17 using std::ostringstream;
18 using std::string;
19
20 static sem_t sem;
21
22 class WatchNotifyTestCtx : public WatchCtx
23 {
24 public:
25 void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override
26 {
27 sem_post(&sem);
28 }
29 };
30
31 #pragma GCC diagnostic ignored "-Wpragmas"
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34
35 void
36 test_loop(Rados &cluster, std::string pool_name, std::string obj_name)
37 {
38 int ret;
39 IoCtx ioctx;
40 ret = cluster.ioctx_create(pool_name.c_str(), ioctx);
41 if (ret < 0) {
42 std::cerr << "ioctx_create " << pool_name << " failed with " << ret << std::endl;
43 exit(1);
44 }
45
46 ret = ioctx.create(obj_name, false);
47 if (ret < 0) {
48 std::cerr << "create failed with " << ret << std::endl;
49 exit(1);
50 }
51
52 for (int i = 0; i < 10000; ++i) {
53 std::cerr << "Iteration " << i << std::endl;
54 uint64_t handle;
55 WatchNotifyTestCtx ctx;
56 ret = ioctx.watch(obj_name, 0, &handle, &ctx);
57 assert(!ret);
58 bufferlist bl2;
59 ret = ioctx.notify(obj_name, 0, bl2);
60 assert(!ret);
61 TestAlarm alarm;
62 sem_wait(&sem);
63 ioctx.unwatch(obj_name, handle);
64 }
65
66 ioctx.close();
67 }
68
69 #pragma GCC diagnostic pop
70 #pragma GCC diagnostic warning "-Wpragmas"
71
72 void
73 test_replicated(Rados &cluster, std::string pool_name, std::string obj_name)
74 {
75 // May already exist
76 cluster.pool_create(pool_name.c_str());
77
78 test_loop(cluster, pool_name, obj_name);
79 }
80
81 void
82 test_erasure(Rados &cluster, std::string pool_name, std::string obj_name)
83 {
84 string outs;
85 bufferlist inbl;
86 int ret;
87 ret = cluster.mon_command(
88 "{\"prefix\": \"osd erasure-code-profile set\", \"name\": \"testprofile\", \"profile\": [ \"k=2\", \"m=1\", \"ruleset-failure-domain=osd\"]}",
89 inbl, NULL, &outs);
90 if (ret < 0) {
91 std::cerr << "mon_command erasure-code-profile set failed with " << ret << std::endl;
92 exit(1);
93 }
94 //std::cout << outs << std::endl;
95
96 outs.clear();
97 ret = cluster.mon_command(
98 "{\"prefix\": \"osd pool create\", \"pool\": \"" + pool_name + "\", \"pool_type\":\"erasure\", \"pg_num\":12, \"pgp_num\":12, \"erasure_code_profile\":\"testprofile\"}",
99 inbl, NULL, &outs);
100 if (ret < 0) {
101 std::cerr << outs << std::endl;
102 std::cerr << "mon_command create pool failed with " << ret << std::endl;
103 exit(1);
104 }
105 //std::cout << outs << std::endl;
106
107 cluster.wait_for_latest_osdmap();
108 test_loop(cluster, pool_name, obj_name);
109 return;
110 }
111
112 int main(int args, char **argv)
113 {
114 if (args != 3 && args != 4) {
115 std::cerr << "Error: " << argv[0] << " [ec|rep] pool_name obj_name" << std::endl;
116 return 1;
117 }
118
119 std::string pool_name, obj_name, type;
120 // For backward compatibility with unmodified teuthology version
121 if (args == 3) {
122 type = "rep";
123 pool_name = argv[1];
124 obj_name = argv[2];
125 } else {
126 type = argv[1];
127 pool_name = argv[2];
128 obj_name = argv[3];
129 }
130 std::cerr << "Test type " << type << std::endl;
131 std::cerr << "pool_name, obj_name are " << pool_name << ", " << obj_name << std::endl;
132
133 if (type != "ec" && type != "rep") {
134 std::cerr << "Error: " << argv[0] << " Invalid arg must be 'ec' or 'rep' saw " << type << std::endl;
135 return 1;
136 }
137
138 char *id = getenv("CEPH_CLIENT_ID");
139 if (id) std::cerr << "Client id is: " << id << std::endl;
140 Rados cluster;
141 int ret;
142 ret = cluster.init(id);
143 if (ret) {
144 std::cerr << "Error " << ret << " in cluster.init" << std::endl;
145 return ret;
146 }
147 ret = cluster.conf_read_file(NULL);
148 if (ret) {
149 std::cerr << "Error " << ret << " in cluster.conf_read_file" << std::endl;
150 return ret;
151 }
152 ret = cluster.conf_parse_env(NULL);
153 if (ret) {
154 std::cerr << "Error " << ret << " in cluster.conf_read_env" << std::endl;
155 return ret;
156 }
157 ret = cluster.connect();
158 if (ret) {
159 std::cerr << "Error " << ret << " in cluster.connect" << std::endl;
160 return ret;
161 }
162 if (type == "rep")
163 test_replicated(cluster, pool_name, obj_name);
164 else if (type == "ec")
165 test_erasure(cluster, pool_name, obj_name);
166
167 sem_destroy(&sem);
168 return 0;
169 }