Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / system / st_rados_watch.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) 2011 New Dream Network
7 *
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.
12 *
13 */
14
15 #include "cross_process_sem.h"
16 #include "include/rados/librados.h"
17 #include "st_rados_watch.h"
18 #include "systest_runnable.h"
19
20 void notify_cb(uint8_t opcode, uint64_t ver, void *arg)
21 {
22   int *notifies = reinterpret_cast<int*>(arg);
23   ++(*notifies);
24 }
25
26 StRadosWatch::StRadosWatch(int argc, const char **argv,
27                            CrossProcessSem *setup_sem,
28                            CrossProcessSem *watch_sem,
29                            CrossProcessSem *notify_sem,
30                            int num_notifies,
31                            int watch_retcode,
32                            const std::string &pool_name,
33                            const std::string &obj_name)
34   : SysTestRunnable(argc, argv),
35     m_setup_sem(setup_sem),
36     m_watch_sem(watch_sem),
37     m_notify_sem(notify_sem),
38     m_num_notifies(num_notifies),
39     m_watch_retcode(watch_retcode),
40     m_pool_name(pool_name),
41     m_obj_name(obj_name)
42 {
43 }
44
45 StRadosWatch::
46 ~StRadosWatch()
47 {
48 }
49
50 #pragma GCC diagnostic ignored "-Wpragmas"
51 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
53
54 int StRadosWatch::
55 run()
56 {
57   rados_t cl;
58   RETURN1_IF_NONZERO(rados_create(&cl, NULL));
59   rados_conf_parse_argv(cl, m_argc, m_argv);
60   RETURN1_IF_NONZERO(rados_conf_read_file(cl, NULL));
61   rados_conf_parse_env(cl, NULL);
62
63   if (m_setup_sem) {
64     m_setup_sem->wait();
65     m_setup_sem->post();
66   }
67
68   rados_ioctx_t io_ctx;
69   uint64_t handle;
70   int num_notifies = 0;
71   RETURN1_IF_NONZERO(rados_connect(cl));
72   RETURN1_IF_NONZERO(rados_ioctx_create(cl, m_pool_name.c_str(), &io_ctx));
73   printf("%s: watching object %s\n", get_id_str(), m_obj_name.c_str());
74
75   RETURN1_IF_NOT_VAL(m_watch_retcode,
76     rados_watch(io_ctx, m_obj_name.c_str(), 0, &handle,
77                 reinterpret_cast<rados_watchcb_t>(notify_cb),
78                 reinterpret_cast<void*>(&num_notifies))
79     );
80   if (m_watch_sem) {
81     m_watch_sem->post();
82   }
83
84   m_notify_sem->wait();
85   m_notify_sem->post();
86
87   int r = 0;
88   if (num_notifies < m_num_notifies) {
89     printf("Received fewer notifies than expected: %d < %d\n",
90            num_notifies, m_num_notifies);
91     r = 1;
92   }
93
94   if (m_watch_retcode == 0)
95     rados_unwatch(io_ctx, m_obj_name.c_str(), handle);
96   rados_ioctx_destroy(io_ctx);
97   rados_shutdown(cl);
98
99   return r;
100 }
101
102 #pragma GCC diagnostic pop
103 #pragma GCC diagnostic warning "-Wpragmas"