summaryrefslogtreecommitdiffstats
path: root/src/rbd_replay/actions.hpp
blob: 89e48315863838540ac29cda9043c369defdf600 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 Adam Crume <adamcrume@gmail.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#ifndef _INCLUDED_RBD_REPLAY_ACTIONS_HPP
#define _INCLUDED_RBD_REPLAY_ACTIONS_HPP

#include <boost/shared_ptr.hpp>
#include "include/rbd/librbd.hpp"
#include "common/Formatter.h"
#include "rbd_replay/ActionTypes.h"
#include "rbd_loc.hpp"
#include <iostream>

// Stupid Doxygen requires this or else the typedef docs don't appear anywhere.
/// @file rbd_replay/actions.hpp

namespace rbd_replay {

typedef uint64_t imagectx_id_t;
typedef uint64_t thread_id_t;

/// Even IDs are normal actions, odd IDs are completions.
typedef uint32_t action_id_t;

class PendingIO;

/**
   %Context through which an Action interacts with its environment.
 */
class ActionCtx {
public:
  virtual ~ActionCtx() {
  }

  /**
     Returns the image with the given ID.
     The image must have been previously tracked with put_image(imagectx_id_t,librbd::Image*).
   */
  virtual librbd::Image* get_image(imagectx_id_t imagectx_id) = 0;

  /**
     Tracks an image.
     put_image(imagectx_id_t,librbd::Image*) must not have been called previously with the same ID,
     and the image must not be NULL.
   */
  virtual void put_image(imagectx_id_t imagectx_id, librbd::Image* image) = 0;

  /**
     Stops tracking an Image and release it.
     This deletes the C++ object, not the image itself.
     The image must have been previously tracked with put_image(imagectx_id_t,librbd::Image*).
   */
  virtual void erase_image(imagectx_id_t imagectx_id) = 0;

  virtual librbd::RBD* rbd() = 0;

  virtual librados::IoCtx* ioctx() = 0;

  virtual void add_pending(boost::shared_ptr<PendingIO> io) = 0;

  virtual bool readonly() const = 0;

  virtual void remove_pending(boost::shared_ptr<PendingIO> io) = 0;

  virtual void set_action_complete(action_id_t id) = 0;

  virtual void stop() = 0;

  /**
     Maps an image name from the name in the original trace to the name that should be used when replaying.
     @param image_name name of the image in the original trace
     @param snap_name name of the snap in the original trace
     @return image name to replay against
   */
  virtual rbd_loc map_image_name(std::string image_name, std::string snap_name) const = 0;
};


/**
   Performs an %IO or a maintenance action such as starting or stopping a thread.
   Actions are read from a replay file and scheduled by Replayer.
   Corresponds to the IO class, except that Actions are executed by rbd-replay,
   and IOs are used by rbd-replay-prep for processing the raw trace.
 */
class Action {
public:
  typedef boost::shared_ptr<Action> ptr;

  virtual ~Action() {
  }

  virtual void perform(ActionCtx &ctx) = 0;

  /// Returns the ID of the completion corresponding to this action.
  action_id_t pending_io_id() {
    return id() + 1;
  }

  // There's probably a better way to do this, but oh well.
  virtual bool is_start_thread() {
    return false;
  }

  virtual action_id_t id() const = 0;
  virtual thread_id_t thread_id() const = 0;
  virtual const action::Dependencies& predecessors() const = 0;

  virtual std::ostream& dump(std::ostream& o) const = 0;

  static ptr construct(const action::ActionEntry &action_entry);
};

template <typename ActionType>
class TypedAction : public Action {
public:
  explicit TypedAction(const ActionType &action) : m_action(action) {
  }

  action_id_t id() const override {
    return m_action.id;
  }

  thread_id_t thread_id() const override {
    return m_action.thread_id;
  }

  const action::Dependencies& predecessors() const override {
    return m_action.dependencies;
  }

  std::ostream& dump(std::ostream& o) const override {
    o << get_action_name() << ": ";
    ceph::JSONFormatter formatter(false);
    formatter.open_object_section("");
    m_action.dump(&formatter);
    formatter.close_section();
    formatter.flush(o);
    return o;
  }

protected:
  const ActionType m_action;

  virtual const char *get_action_name() const = 0;
};

/// Writes human-readable debug information about the action to the stream.
/// @related Action
std::ostream& operator<<(std::ostream& o, const Action& a);

class StartThreadAction : public TypedAction<action::StartThreadAction> {
public:
  explicit StartThreadAction(const action::StartThreadAction &action)
    : TypedAction<action::StartThreadAction>(action) {
  }

  bool is_start_thread() override {
    return true;
  }
  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "StartThreadAction";
  }
};

class StopThreadAction : public TypedAction<action::StopThreadAction> {
public:
  explicit StopThreadAction(const action::StopThreadAction &action)
    : TypedAction<action::StopThreadAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "StartThreadAction";
  }
};


class AioReadAction : public TypedAction<action::AioReadAction> {
public:
  explicit AioReadAction(const action::AioReadAction &action)
    : TypedAction<action::AioReadAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "AioReadAction";
  }
};


class ReadAction : public TypedAction<action::ReadAction> {
public:
  explicit ReadAction(const action::ReadAction &action)
    : TypedAction<action::ReadAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "ReadAction";
  }
};


class AioWriteAction : public TypedAction<action::AioWriteAction> {
public:
  explicit AioWriteAction(const action::AioWriteAction &action)
    : TypedAction<action::AioWriteAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "AioWriteAction";
  }
};


class WriteAction : public TypedAction<action::WriteAction> {
public:
  explicit WriteAction(const action::WriteAction &action)
    : TypedAction<action::WriteAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "WriteAction";
  }
};


class AioDiscardAction : public TypedAction<action::AioDiscardAction> {
public:
  explicit AioDiscardAction(const action::AioDiscardAction &action)
    : TypedAction<action::AioDiscardAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "AioDiscardAction";
  }
};


class DiscardAction : public TypedAction<action::DiscardAction> {
public:
  explicit DiscardAction(const action::DiscardAction &action)
    : TypedAction<action::DiscardAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "DiscardAction";
  }
};


class OpenImageAction : public TypedAction<action::OpenImageAction> {
public:
  explicit OpenImageAction(const action::OpenImageAction &action)
    : TypedAction<action::OpenImageAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "OpenImageAction";
  }
};


class CloseImageAction : public TypedAction<action::CloseImageAction> {
public:
  explicit CloseImageAction(const action::CloseImageAction &action)
    : TypedAction<action::CloseImageAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "CloseImageAction";
  }
};

class AioOpenImageAction : public TypedAction<action::AioOpenImageAction> {
public:
  explicit AioOpenImageAction(const action::AioOpenImageAction &action)
    : TypedAction<action::AioOpenImageAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "AioOpenImageAction";
  }
};


class AioCloseImageAction : public TypedAction<action::AioCloseImageAction> {
public:
  explicit AioCloseImageAction(const action::AioCloseImageAction &action)
    : TypedAction<action::AioCloseImageAction>(action) {
  }

  void perform(ActionCtx &ctx) override;

protected:
  const char *get_action_name() const override {
    return "AioCloseImageAction";
  }
};

}

#endif