summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_replayer/journal/EventPreprocessor.h
blob: 12f70eb9350a319d1a2b703d59377ea0f22d0042 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef RBD_MIRROR_IMAGE_REPLAYER_EVENT_PREPROCESSOR_H
#define RBD_MIRROR_IMAGE_REPLAYER_EVENT_PREPROCESSOR_H

#include "include/int_types.h"
#include "librbd/journal/Types.h"
#include "librbd/journal/TypeTraits.h"
#include <map>
#include <string>
#include <boost/variant/static_visitor.hpp>

struct Context;
namespace journal { class Journaler; }
namespace librbd {
class ImageCtx;
namespace asio { struct ContextWQ; }
} // namespace librbd

namespace rbd {
namespace mirror {
namespace image_replayer {
namespace journal {

template <typename ImageCtxT = librbd::ImageCtx>
class EventPreprocessor {
public:
  using Journaler = typename librbd::journal::TypeTraits<ImageCtxT>::Journaler;
  using EventEntry = librbd::journal::EventEntry;
  using MirrorPeerClientMeta = librbd::journal::MirrorPeerClientMeta;

  static EventPreprocessor *create(ImageCtxT &local_image_ctx,
                                   Journaler &remote_journaler,
                                   const std::string &local_mirror_uuid,
                                   MirrorPeerClientMeta *client_meta,
                                   librbd::asio::ContextWQ *work_queue) {
    return new EventPreprocessor(local_image_ctx, remote_journaler,
                                 local_mirror_uuid, client_meta, work_queue);
  }

  static void destroy(EventPreprocessor* processor) {
    delete processor;
  }

  EventPreprocessor(ImageCtxT &local_image_ctx, Journaler &remote_journaler,
                    const std::string &local_mirror_uuid,
                    MirrorPeerClientMeta *client_meta,
                    librbd::asio::ContextWQ *work_queue);
  ~EventPreprocessor();

  bool is_required(const EventEntry &event_entry);
  void preprocess(EventEntry *event_entry, Context *on_finish);

private:
  /**
   * @verbatim
   *
   * <start>
   *    |
   *    v (skip if not required)
   * REFRESH_IMAGE
   *    |
   *    v (skip if not required)
   * PREPROCESS_EVENT
   *    |
   *    v (skip if not required)
   * UPDATE_CLIENT
   *
   * @endverbatim
   */

  typedef std::map<uint64_t, uint64_t> SnapSeqs;

  class PreprocessEventVisitor : public boost::static_visitor<int> {
  public:
    EventPreprocessor *event_preprocessor;

    PreprocessEventVisitor(EventPreprocessor *event_preprocessor)
      : event_preprocessor(event_preprocessor) {
    }

    template <typename T>
    inline int operator()(T&) const {
      return 0;
    }
    inline int operator()(librbd::journal::SnapRenameEvent &event) const {
      return event_preprocessor->preprocess_snap_rename(event);
    }
  };

  ImageCtxT &m_local_image_ctx;
  Journaler &m_remote_journaler;
  std::string m_local_mirror_uuid;
  MirrorPeerClientMeta *m_client_meta;
  librbd::asio::ContextWQ *m_work_queue;

  bool m_in_progress = false;
  EventEntry *m_event_entry = nullptr;
  Context *m_on_finish = nullptr;

  SnapSeqs m_snap_seqs;
  bool m_snap_seqs_updated = false;

  bool prune_snap_map(SnapSeqs *snap_seqs);

  void refresh_image();
  void handle_refresh_image(int r);

  void preprocess_event();
  int preprocess_snap_rename(librbd::journal::SnapRenameEvent &event);

  void update_client();
  void handle_update_client(int r);

  void finish(int r);

};

} // namespace journal
} // namespace image_replayer
} // namespace mirror
} // namespace rbd

extern template class rbd::mirror::image_replayer::journal::EventPreprocessor<librbd::ImageCtx>;

#endif // RBD_MIRROR_IMAGE_REPLAYER_EVENT_PREPROCESSOR_H