Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rbd_replay / Replayer.hpp
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2014 Adam Crume <adamcrume@gmail.com>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #ifndef _INCLUDED_RBD_REPLAY_REPLAYER_HPP
16 #define _INCLUDED_RBD_REPLAY_REPLAYER_HPP
17
18 #include <boost/thread/mutex.hpp>
19 #include <boost/thread/shared_mutex.hpp>
20 #include "rbd_replay/ActionTypes.h"
21 #include "BoundedBuffer.hpp"
22 #include "ImageNameMap.hpp"
23 #include "PendingIO.hpp"
24
25 namespace rbd_replay {
26
27 class Replayer;
28
29 /**
30    Performs Actions within a single thread.
31  */
32 class Worker : public ActionCtx {
33 public:
34   explicit Worker(Replayer &replayer);
35
36   void start();
37
38   /// Should only be called by StopThreadAction
39   void stop() override;
40
41   void join();
42
43   void send(Action::ptr action);
44
45   void add_pending(PendingIO::ptr io) override;
46
47   void remove_pending(PendingIO::ptr io) override;
48
49   librbd::Image* get_image(imagectx_id_t imagectx_id) override;
50
51   void put_image(imagectx_id_t imagectx_id, librbd::Image* image) override;
52
53   void erase_image(imagectx_id_t imagectx_id) override;
54
55   librbd::RBD* rbd() override;
56
57   librados::IoCtx* ioctx() override;
58
59   void set_action_complete(action_id_t id) override;
60
61   bool readonly() const override;
62
63   rbd_loc map_image_name(std::string image_name, std::string snap_name) const override;
64
65 private:
66   void run();
67
68   Replayer &m_replayer;
69   BoundedBuffer<Action::ptr> m_buffer;
70   boost::shared_ptr<boost::thread> m_thread;
71   std::map<action_id_t, PendingIO::ptr> m_pending_ios;
72   boost::mutex m_pending_ios_mutex;
73   boost::condition m_pending_ios_empty;
74   bool m_done;
75 };
76
77
78 class Replayer {
79 public:
80   explicit Replayer(int num_action_trackers);
81
82   ~Replayer();
83
84   void run(const std::string &replay_file);
85
86   librbd::RBD* get_rbd() {
87     return m_rbd;
88   }
89
90   librados::IoCtx* get_ioctx() {
91     return m_ioctx;
92   }
93
94   librbd::Image* get_image(imagectx_id_t imagectx_id);
95
96   void put_image(imagectx_id_t imagectx_id, librbd::Image *image);
97
98   void erase_image(imagectx_id_t imagectx_id);
99
100   void set_action_complete(action_id_t id);
101
102   bool is_action_complete(action_id_t id);
103
104   void wait_for_actions(const action::Dependencies &deps);
105
106   std::string pool_name() const;
107
108   void set_pool_name(std::string pool_name);
109
110   void set_latency_multiplier(float f);
111
112   bool readonly() const;
113
114   void set_readonly(bool readonly);
115
116   void set_image_name_map(const ImageNameMap &map) {
117     m_image_name_map = map;
118   }
119
120   void set_dump_perf_counters(bool dump_perf_counters) {
121     m_dump_perf_counters = dump_perf_counters;
122   }
123
124   const ImageNameMap &image_name_map() const {
125     return m_image_name_map;
126   }
127
128 private:
129   struct action_tracker_d {
130     /// Maps an action ID to the time the action completed
131     std::map<action_id_t, boost::system_time> actions;
132     boost::shared_mutex mutex;
133     boost::condition condition;
134   };
135
136   void clear_images();
137
138   action_tracker_d &tracker_for(action_id_t id);
139
140   /// Disallow copying
141   Replayer(const Replayer& rhs);
142   /// Disallow assignment
143   const Replayer& operator=(const Replayer& rhs);
144
145   librbd::RBD* m_rbd;
146   librados::IoCtx* m_ioctx;
147   std::string m_pool_name;
148   float m_latency_multiplier;
149   bool m_readonly;
150   ImageNameMap m_image_name_map;
151   bool m_dump_perf_counters;
152
153   std::map<imagectx_id_t, librbd::Image*> m_images;
154   boost::shared_mutex m_images_mutex;
155
156   /// Actions are hashed across the trackers by ID.
157   /// Number of trackers should probably be larger than the number of cores and prime.
158   /// Should definitely be odd.
159   const int m_num_action_trackers;
160   action_tracker_d* m_action_trackers;
161 };
162
163 }
164
165 #endif