summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/ServiceDaemon.cc
blob: f3b549b860c10a370b97265a81dca5040febb7d8 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd_mirror/ServiceDaemon.h"
#include "include/Context.h"
#include "include/stringify.h"
#include "common/ceph_context.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/Formatter.h"
#include "common/Timer.h"
#include "tools/rbd_mirror/Threads.h"
#include <sstream>

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::ServiceDaemon: " << this << " " \
                           << __func__ << ": "

namespace rbd {
namespace mirror {

namespace {

const std::string RBD_MIRROR_AUTH_ID_PREFIX("rbd-mirror.");

struct AttributeDumpVisitor : public boost::static_visitor<void> {
  ceph::Formatter *f;
  const std::string& name;

  AttributeDumpVisitor(ceph::Formatter *f, const std::string& name)
    : f(f), name(name) {
  }

  void operator()(bool val) const {
    f->dump_bool(name.c_str(), val);
  }
  void operator()(uint64_t val) const {
    f->dump_unsigned(name.c_str(), val);
  }
  void operator()(const std::string& val) const {
    f->dump_string(name.c_str(), val);
  }
};

} // anonymous namespace

using namespace service_daemon;

template <typename I>
ServiceDaemon<I>::ServiceDaemon(CephContext *cct, RadosRef rados,
                                Threads<I>* threads)
  : m_cct(cct), m_rados(rados), m_threads(threads),
    m_lock("rbd::mirror::ServiceDaemon") {
  dout(20) << dendl;
}

template <typename I>
ServiceDaemon<I>::~ServiceDaemon() {
  dout(20) << dendl;
  Mutex::Locker timer_locker(m_threads->timer_lock);
  if (m_timer_ctx != nullptr) {
    m_threads->timer->cancel_event(m_timer_ctx);
    update_status();
  }
}

template <typename I>
int ServiceDaemon<I>::init() {
  dout(20) << dendl;

  std::string id = m_cct->_conf->name.get_id();
  if (id.find(RBD_MIRROR_AUTH_ID_PREFIX) == 0) {
    id = id.substr(RBD_MIRROR_AUTH_ID_PREFIX.size());
  }

  std::string instance_id = stringify(m_rados->get_instance_id());
  std::map<std::string, std::string> service_metadata = {
    {"id", id}, {"instance_id", instance_id}};
  int r = m_rados->service_daemon_register("rbd-mirror", instance_id,
                                           service_metadata);
  if (r < 0) {
    return r;
  }

  return 0;
}

template <typename I>
void ServiceDaemon<I>::add_pool(int64_t pool_id, const std::string& pool_name) {
  dout(20) << "pool_id=" << pool_id << ", pool_name=" << pool_name << dendl;

  {
    Mutex::Locker locker(m_lock);
    m_pools.insert({pool_id, {pool_name}});
  }
  schedule_update_status();
}

template <typename I>
void ServiceDaemon<I>::remove_pool(int64_t pool_id) {
  dout(20) << "pool_id=" << pool_id << dendl;
  {
    Mutex::Locker locker(m_lock);
    m_pools.erase(pool_id);
  }
  schedule_update_status();
}

template <typename I>
uint64_t ServiceDaemon<I>::add_or_update_callout(int64_t pool_id,
                                                 uint64_t callout_id,
                                                 CalloutLevel callout_level,
                                                 const std::string& text) {
  dout(20) << "pool_id=" << pool_id << ", "
           << "callout_id=" << callout_id << ", "
           << "callout_level=" << callout_level << ", "
           << "text=" << text << dendl;

  {
    Mutex::Locker locker(m_lock);
    auto pool_it = m_pools.find(pool_id);
    if (pool_it == m_pools.end()) {
      return CALLOUT_ID_NONE;
    }

    if (callout_id == CALLOUT_ID_NONE) {
      callout_id = ++m_callout_id;
    }
    pool_it->second.callouts[callout_id] = {callout_level, text};
  }

  schedule_update_status();
  return callout_id;
}

template <typename I>
void ServiceDaemon<I>::remove_callout(int64_t pool_id, uint64_t callout_id) {
  dout(20) << "pool_id=" << pool_id << ", "
           << "callout_id=" << callout_id << dendl;

  {
    Mutex::Locker locker(m_lock);
    auto pool_it = m_pools.find(pool_id);
    if (pool_it == m_pools.end()) {
      return;
    }
    pool_it->second.callouts.erase(callout_id);
  }

  schedule_update_status();
}

template <typename I>
void ServiceDaemon<I>::add_or_update_attribute(int64_t pool_id,
                                               const std::string& key,
                                               const AttributeValue& value) {
  dout(20) << "pool_id=" << pool_id << ", "
           << "key=" << key << ", "
           << "value=" << value << dendl;

  {
    Mutex::Locker locker(m_lock);
    auto pool_it = m_pools.find(pool_id);
    if (pool_it == m_pools.end()) {
      return;
    }
    pool_it->second.attributes[key] = value;
  }

  schedule_update_status();
}

template <typename I>
void ServiceDaemon<I>::remove_attribute(int64_t pool_id,
                                        const std::string& key) {
  dout(20) << "pool_id=" << pool_id << ", "
           << "key=" << key << dendl;

  {
    Mutex::Locker locker(m_lock);
    auto pool_it = m_pools.find(pool_id);
    if (pool_it == m_pools.end()) {
      return;
    }
    pool_it->second.attributes.erase(key);
  }

  schedule_update_status();
}

template <typename I>
void ServiceDaemon<I>::schedule_update_status() {
  Mutex::Locker timer_locker(m_threads->timer_lock);
  if (m_timer_ctx != nullptr) {
    return;
  }

  m_timer_ctx = new FunctionContext([this](int) {
      m_timer_ctx = nullptr;
      update_status();
    });
  m_threads->timer->add_event_after(1, m_timer_ctx);
}

template <typename I>
void ServiceDaemon<I>::update_status() {
  dout(20) << dendl;
  ceph_assert(m_threads->timer_lock.is_locked());

  ceph::JSONFormatter f;
  {
    Mutex::Locker locker(m_lock);
    f.open_object_section("pools");
    for (auto& pool_pair : m_pools) {
      f.open_object_section(stringify(pool_pair.first).c_str());
      f.dump_string("name", pool_pair.second.name);
      f.open_object_section("callouts");
      for (auto& callout : pool_pair.second.callouts) {
        f.open_object_section(stringify(callout.first).c_str());
        f.dump_string("level", stringify(callout.second.level).c_str());
        f.dump_string("text", callout.second.text.c_str());
        f.close_section();
      }
      f.close_section(); // callouts

      for (auto& attribute : pool_pair.second.attributes) {
        AttributeDumpVisitor attribute_dump_visitor(&f, attribute.first);
        boost::apply_visitor(attribute_dump_visitor, attribute.second);
      }
      f.close_section(); // pool
    }
    f.close_section(); // pools
  }

  std::stringstream ss;
  f.flush(ss);

  int r = m_rados->service_daemon_update_status({{"json", ss.str()}});
  if (r < 0) {
    derr << "failed to update service daemon status: " << cpp_strerror(r)
         << dendl;
  }
}

} // namespace mirror
} // namespace rbd

template class rbd::mirror::ServiceDaemon<librbd::ImageCtx>;