librbd: mirroring object notification types

Initial payloads for updated mirror pool mode and image state.

Signed-off-by: Jason Dillaman <dillaman@redhat.com>
This commit is contained in:
Jason Dillaman 2016-03-28 23:45:38 -04:00
parent 3145109fd5
commit 934ce8647c
3 changed files with 239 additions and 0 deletions

View File

@ -2,9 +2,159 @@
// vim: ts=8 sw=2 smarttab
#include "librbd/mirroring_watcher/Types.h"
#include "include/assert.h"
#include "include/stringify.h"
#include "common/Formatter.h"
namespace librbd {
namespace mirroring_watcher {
namespace {
class EncodePayloadVisitor : public boost::static_visitor<void> {
public:
explicit EncodePayloadVisitor(bufferlist &bl) : m_bl(bl) {}
template <typename Payload>
inline void operator()(const Payload &payload) const {
::encode(static_cast<uint32_t>(Payload::NOTIFY_OP), m_bl);
payload.encode(m_bl);
}
private:
bufferlist &m_bl;
};
class DecodePayloadVisitor : public boost::static_visitor<void> {
public:
DecodePayloadVisitor(__u8 version, bufferlist::iterator &iter)
: m_version(version), m_iter(iter) {}
template <typename Payload>
inline void operator()(Payload &payload) const {
payload.decode(m_version, m_iter);
}
private:
__u8 m_version;
bufferlist::iterator &m_iter;
};
class DumpPayloadVisitor : public boost::static_visitor<void> {
public:
explicit DumpPayloadVisitor(Formatter *formatter) : m_formatter(formatter) {}
template <typename Payload>
inline void operator()(const Payload &payload) const {
NotifyOp notify_op = Payload::NOTIFY_OP;
m_formatter->dump_string("notify_op", stringify(notify_op));
payload.dump(m_formatter);
}
private:
ceph::Formatter *m_formatter;
};
} // anonymous namespace
void ModeUpdatedPayload::encode(bufferlist &bl) const {
::encode(static_cast<uint32_t>(mirror_mode), bl);
}
void ModeUpdatedPayload::decode(__u8 version, bufferlist::iterator &iter) {
uint32_t mirror_mode_decode;
::decode(mirror_mode_decode, iter);
mirror_mode = static_cast<cls::rbd::MirrorMode>(mirror_mode_decode);
}
void ModeUpdatedPayload::dump(Formatter *f) const {
f->dump_stream("mirror_mode") << mirror_mode;
}
void ImageUpdatedPayload::encode(bufferlist &bl) const {
::encode(static_cast<uint32_t>(mirror_image_state), bl);
::encode(image_id, bl);
::encode(global_image_id, bl);
}
void ImageUpdatedPayload::decode(__u8 version, bufferlist::iterator &iter) {
uint32_t mirror_image_state_decode;
::decode(mirror_image_state_decode, iter);
mirror_image_state = static_cast<cls::rbd::MirrorImageState>(
mirror_image_state_decode);
::decode(image_id, iter);
::decode(global_image_id, iter);
}
void ImageUpdatedPayload::dump(Formatter *f) const {
f->dump_stream("mirror_image_state") << mirror_image_state;
f->dump_string("image_id", image_id);
f->dump_string("global_image_id", global_image_id);
}
void UnknownPayload::encode(bufferlist &bl) const {
assert(false);
}
void UnknownPayload::decode(__u8 version, bufferlist::iterator &iter) {
}
void UnknownPayload::dump(Formatter *f) const {
}
void NotifyMessage::encode(bufferlist& bl) const {
ENCODE_START(1, 1, bl);
boost::apply_visitor(EncodePayloadVisitor(bl), payload);
ENCODE_FINISH(bl);
}
void NotifyMessage::decode(bufferlist::iterator& iter) {
DECODE_START(1, iter);
uint32_t notify_op;
::decode(notify_op, iter);
// select the correct payload variant based upon the encoded op
switch (notify_op) {
case NOTIFY_OP_MODE_UPDATED:
payload = ModeUpdatedPayload();
break;
case NOTIFY_OP_IMAGE_UPDATED:
payload = ImageUpdatedPayload();
break;
default:
payload = UnknownPayload();
break;
}
apply_visitor(DecodePayloadVisitor(struct_v, iter), payload);
DECODE_FINISH(iter);
}
void NotifyMessage::dump(Formatter *f) const {
apply_visitor(DumpPayloadVisitor(f), payload);
}
void NotifyMessage::generate_test_instances(std::list<NotifyMessage *> &o) {
o.push_back(new NotifyMessage(ModeUpdatedPayload(cls::rbd::MIRROR_MODE_DISABLED)));
o.push_back(new NotifyMessage(ImageUpdatedPayload(cls::rbd::MIRROR_IMAGE_STATE_DISABLING,
"image id", "global image id")));
}
std::ostream &operator<<(std::ostream &out, const NotifyOp &op) {
switch (op) {
case NOTIFY_OP_MODE_UPDATED:
out << "ModeUpdated";
break;
case NOTIFY_OP_IMAGE_UPDATED:
out << "ImageUpdated";
break;
default:
out << "Unknown (" << static_cast<uint32_t>(op) << ")";
break;
}
return out;
}
} // namespace mirroring_watcher
} // namespace librbd

View File

@ -5,11 +5,98 @@
#define CEPH_LIBRBD_MIRRORING_WATCHER_TYPES_H
#include "include/int_types.h"
#include "include/buffer_fwd.h"
#include "include/encoding.h"
#include "cls/rbd/cls_rbd_types.h"
#include <iosfwd>
#include <list>
#include <string>
#include <boost/variant.hpp>
namespace ceph { class Formatter; }
namespace librbd {
namespace mirroring_watcher {
enum NotifyOp {
NOTIFY_OP_MODE_UPDATED = 0,
NOTIFY_OP_IMAGE_UPDATED = 1
};
struct ModeUpdatedPayload {
static const NotifyOp NOTIFY_OP = NOTIFY_OP_MODE_UPDATED;
cls::rbd::MirrorMode mirror_mode = cls::rbd::MIRROR_MODE_DISABLED;
ModeUpdatedPayload() {
}
ModeUpdatedPayload(cls::rbd::MirrorMode mirror_mode)
: mirror_mode(mirror_mode) {
}
void encode(bufferlist &bl) const;
void decode(__u8 version, bufferlist::iterator &iter);
void dump(Formatter *f) const;
};
struct ImageUpdatedPayload {
static const NotifyOp NOTIFY_OP = NOTIFY_OP_IMAGE_UPDATED;
cls::rbd::MirrorImageState mirror_image_state =
cls::rbd::MIRROR_IMAGE_STATE_ENABLED;
std::string image_id;
std::string global_image_id;
ImageUpdatedPayload() {
}
ImageUpdatedPayload(cls::rbd::MirrorImageState mirror_image_state,
const std::string &image_id,
const std::string &global_image_id)
: mirror_image_state(mirror_image_state), image_id(image_id),
global_image_id(global_image_id) {
}
void encode(bufferlist &bl) const;
void decode(__u8 version, bufferlist::iterator &iter);
void dump(Formatter *f) const;
};
struct UnknownPayload {
static const NotifyOp NOTIFY_OP = static_cast<NotifyOp>(-1);
UnknownPayload() {
}
void encode(bufferlist &bl) const;
void decode(__u8 version, bufferlist::iterator &iter);
void dump(Formatter *f) const;
};
typedef boost::variant<ModeUpdatedPayload,
ImageUpdatedPayload,
UnknownPayload> Payload;
struct NotifyMessage {
NotifyMessage(const Payload &payload = UnknownPayload()) : payload(payload) {
}
Payload payload;
void encode(bufferlist& bl) const;
void decode(bufferlist::iterator& it);
void dump(Formatter *f) const;
static void generate_test_instances(std::list<NotifyMessage *> &o);
};
WRITE_CLASS_ENCODER(NotifyMessage);
std::ostream &operator<<(std::ostream &out, const NotifyOp &op);
} // namespace mirroring_watcher
} // namespace librbd
using librbd::mirroring_watcher::encode;
using librbd::mirroring_watcher::decode;
#endif // CEPH_LIBRBD_MIRRORING_WATCHER_TYPES_H

View File

@ -245,6 +245,8 @@ TYPE_FEATUREFUL(EUpdate)
TYPE(librbd::journal::EventEntry)
TYPE(librbd::journal::ClientData)
TYPE(librbd::journal::TagData)
#include "librbd/mirroring_watcher/Types.h"
TYPE(librbd::mirroring_watcher::NotifyMessage)
#include "librbd/WatchNotifyTypes.h"
TYPE(librbd::watch_notify::NotifyMessage)
TYPE(librbd::watch_notify::ResponseMessage)