initial code repo
[stor4nfv.git] / src / 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   ioctx.application_enable("rados", true);
46
47   ret = ioctx.create(obj_name, false);
48   if (ret < 0) {
49     std::cerr << "create failed with " << ret << std::endl;
50     exit(1);
51   }
52
53   for (int i = 0; i < 10000; ++i) {
54     std::cerr << "Iteration " << i << std::endl;
55     uint64_t handle;
56     WatchNotifyTestCtx ctx;
57     ret = ioctx.watch(obj_name, 0, &handle, &ctx);
58     assert(!ret);
59     bufferlist bl2;
60     ret = ioctx.notify(obj_name, 0, bl2);
61     assert(!ret);
62     TestAlarm alarm;
63     sem_wait(&sem);
64     ioctx.unwatch(obj_name, handle);
65   }
66
67   ioctx.close();
68   ret = cluster.pool_delete(pool_name.c_str());
69   if (ret < 0) {
70     std::cerr << "pool_delete failed with " << ret << std::endl;
71     exit(1);
72   }
73 }
74
75 #pragma GCC diagnostic pop
76 #pragma GCC diagnostic warning "-Wpragmas"
77
78 void
79 test_replicated(Rados &cluster, std::string pool_name, std::string obj_name)
80 {
81   // May already exist
82   cluster.pool_create(pool_name.c_str());
83
84   test_loop(cluster, pool_name, obj_name);
85 }
86
87 void
88 test_erasure(Rados &cluster, std::string pool_name, std::string obj_name)
89 {
90   string outs;
91   bufferlist inbl;
92   int ret;
93   ret = cluster.mon_command(
94     "{\"prefix\": \"osd erasure-code-profile set\", \"name\": \"testprofile\", \"profile\": [ \"k=2\", \"m=1\", \"crush-failure-domain=osd\"]}",
95     inbl, NULL, &outs);
96   if (ret < 0) {
97     std::cerr << "mon_command erasure-code-profile set failed with " << ret << std::endl;
98     exit(1);
99   }
100   //std::cout << outs << std::endl;
101
102   outs.clear();
103   ret = cluster.mon_command(
104     "{\"prefix\": \"osd pool create\", \"pool\": \"" + pool_name + "\", \"pool_type\":\"erasure\", \"pg_num\":12, \"pgp_num\":12, \"erasure_code_profile\":\"testprofile\"}",
105     inbl, NULL, &outs);
106   if (ret < 0) {
107     std::cerr << outs << std::endl;
108     std::cerr << "mon_command create pool failed with " << ret << std::endl;
109     exit(1);
110   }
111   //std::cout << outs << std::endl;
112
113   cluster.wait_for_latest_osdmap();
114   test_loop(cluster, pool_name, obj_name);
115   return;
116 }
117
118 int main(int args, char **argv)
119 {
120   if (args != 3 && args != 4) {
121     std::cerr << "Error: " << argv[0] << " [ec|rep] pool_name obj_name" << std::endl;
122     return 1;
123   }
124
125   std::string pool_name, obj_name, type;
126   // For backward compatibility with unmodified teuthology version
127   if (args == 3) {
128     type = "rep";
129     pool_name = argv[1];
130     obj_name = argv[2];
131   } else {
132     type = argv[1];
133     pool_name = argv[2];
134     obj_name = argv[3];
135   }
136   std::cerr << "Test type " << type << std::endl;
137   std::cerr << "pool_name, obj_name are " << pool_name << ", " << obj_name << std::endl;
138
139   if (type != "ec" && type != "rep") {
140     std::cerr << "Error: " << argv[0] << " Invalid arg must be 'ec' or 'rep' saw " << type << std::endl;
141     return 1;
142   }
143
144   char *id = getenv("CEPH_CLIENT_ID");
145   if (id) std::cerr << "Client id is: " << id << std::endl;
146   Rados cluster;
147   int ret;
148   ret = cluster.init(id);
149   if (ret) {
150     std::cerr << "Error " << ret << " in cluster.init" << std::endl;
151     return ret;
152   }
153   ret = cluster.conf_read_file(NULL);
154   if (ret) {
155     std::cerr << "Error " << ret << " in cluster.conf_read_file" << std::endl;
156     return ret;
157   }
158   ret = cluster.conf_parse_env(NULL);
159   if (ret) {
160     std::cerr << "Error " << ret << " in cluster.conf_read_env" << std::endl;
161     return ret;
162   }
163   ret = cluster.connect();
164   if (ret) {
165     std::cerr << "Error " << ret << " in cluster.connect" << std::endl;
166     return ret;
167   }
168   if (type == "rep")
169     test_replicated(cluster, pool_name, obj_name);
170   else if (type == "ec")
171     test_erasure(cluster, pool_name, obj_name);
172
173   sem_destroy(&sem);
174   return 0;
175 }