summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/Watch.cc
blob: 98697bc2862e146e4939b54e01ebb431925ca05b (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"
#include "include/rbd_types.h"
#include "librbd/WatchNotifyTypes.h"
#include "common/errno.h"
#include <iostream>
#include <boost/program_options.hpp>

namespace rbd {
namespace action {
namespace watch {

namespace at = argument_types;
namespace po = boost::program_options;

class RbdWatchCtx : public librados::WatchCtx2 {
public:
  RbdWatchCtx(librados::IoCtx& io_ctx, const char *image_name,
              const std::string &header_oid)
    : m_io_ctx(io_ctx), m_image_name(image_name), m_header_oid(header_oid)
  {
  }

  ~RbdWatchCtx() override {}

  void handle_notify(uint64_t notify_id,
                             uint64_t cookie,
                             uint64_t notifier_id,
                             bufferlist& bl) override {
    using namespace librbd::watch_notify;
    NotifyMessage notify_message;
    if (bl.length() == 0) {
      notify_message = NotifyMessage(new HeaderUpdatePayload());
    } else {
      try {
        auto iter = bl.cbegin();
        notify_message.decode(iter);
      } catch (const buffer::error &err) {
        std::cerr << "rbd: failed to decode image notification" << std::endl;
      }
    }
   
    std::cout << m_image_name << " received notification: notify_id="
              << notify_id << ", cookie=" << cookie << ", notifier_id="
              << notifier_id << ", bl.length=" << bl.length() << ", notify_op=" 
              << notify_message.get_notify_op()  << std::endl;
    bufferlist reply;
    m_io_ctx.notify_ack(m_header_oid, notify_id, cookie, reply);
  }

  void handle_error(uint64_t cookie, int err) override {
    std::cerr << m_image_name << " received error: cookie=" << cookie << ", "
              << "err=" << cpp_strerror(err) << std::endl;
  }
private:
  librados::IoCtx m_io_ctx;
  const char *m_image_name;
  std::string m_header_oid;
};

static int do_watch(librados::IoCtx& pp, librbd::Image &image,
                    const char *imgname)
{
  uint8_t old_format;
  int r = image.old_format(&old_format);
  if (r < 0) {
    std::cerr << "failed to query format" << std::endl;
    return r;
  }

  std::string header_oid;
  if (old_format != 0) {
    header_oid = std::string(imgname) + RBD_SUFFIX;
  } else {
    std::string id;
    r = image.get_id(&id);
    if (r < 0) {
      return r;
    }

    header_oid = RBD_HEADER_PREFIX + id;
  }

  uint64_t cookie;
  RbdWatchCtx ctx(pp, imgname, header_oid);
  r = pp.watch2(header_oid, &cookie, &ctx);
  if (r < 0) {
    std::cerr << "rbd: watch failed" << std::endl;
    return r;
  }

  std::cout << "press enter to exit..." << std::endl;
  getchar();

  r = pp.unwatch2(cookie);
  if (r < 0) {
    std::cerr << "rbd: unwatch failed" << std::endl;
    return r;
  }
  return 0;
}

void get_arguments(po::options_description *positional,
                   po::options_description *options) {
  at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
}

int execute(const po::variables_map &vm,
            const std::vector<std::string> &ceph_global_init_args) {
  size_t arg_index = 0;
  std::string pool_name;
  std::string namespace_name;
  std::string image_name;
  std::string snap_name;
  int r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
    &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
    utils::SPEC_VALIDATION_NONE);
  if (r < 0) {
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  librbd::Image image;
  r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
                                 true, &rados, &io_ctx, &image);
  if (r < 0) {
    return r;
  }

  r = do_watch(io_ctx, image, image_name.c_str());
  if (r < 0) {
    std::cerr << "rbd: watch failed: " << cpp_strerror(r) << std::endl;
    return r;
  }
  return 0;
}

Shell::Action action(
  {"watch"}, {}, "Watch events on image.", "", &get_arguments, &execute);

} // namespace watch
} // namespace action
} // namespace rbd