Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_period_history.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef RGW_PERIOD_HISTORY_H
5 #define RGW_PERIOD_HISTORY_H
6
7 #include <deque>
8 #include <mutex>
9 #include <system_error>
10 #include <boost/intrusive/avl_set.hpp>
11 #include "include/assert.h"
12 #include "include/types.h"
13
14 namespace bi = boost::intrusive;
15
16 class RGWPeriod;
17
18 /**
19  * RGWPeriodHistory tracks the relative history of all inserted periods,
20  * coordinates the pulling of missing intermediate periods, and provides a
21  * Cursor object for traversing through the connected history.
22  */
23 class RGWPeriodHistory final {
24  private:
25   /// an ordered history of consecutive periods
26   class History;
27
28   // comparisons for avl_set ordering
29   friend bool operator<(const History& lhs, const History& rhs);
30   friend struct NewestEpochLess;
31
32   class Impl;
33   std::unique_ptr<Impl> impl;
34
35  public:
36   /**
37    * Puller is a synchronous interface for pulling periods from the master
38    * zone. The abstraction exists mainly to support unit testing.
39    */
40   class Puller {
41    public:
42     virtual ~Puller() = default;
43
44     virtual int pull(const std::string& period_id, RGWPeriod& period) = 0;
45   };
46
47   RGWPeriodHistory(CephContext* cct, Puller* puller,
48                    const RGWPeriod& current_period);
49   ~RGWPeriodHistory();
50
51   /**
52    * Cursor tracks a position in the period history and allows forward and
53    * backward traversal. Only periods that are fully connected to the
54    * current_period are reachable via a Cursor, because other histories are
55    * temporary and can be merged away. Cursors to periods in disjoint
56    * histories, as provided by insert() or lookup(), are therefore invalid and
57    * their operator bool() will return false.
58    */
59   class Cursor final {
60    public:
61     Cursor() = default;
62     explicit Cursor(int error) : error(error) {}
63
64     int get_error() const { return error; }
65
66     /// return false for a default-constructed or error Cursor
67     operator bool() const { return history != nullptr; }
68
69     epoch_t get_epoch() const { return epoch; }
70     const RGWPeriod& get_period() const;
71
72     bool has_prev() const;
73     bool has_next() const;
74
75     void prev() { epoch--; }
76     void next() { epoch++; }
77
78     friend bool operator==(const Cursor& lhs, const Cursor& rhs);
79     friend bool operator!=(const Cursor& lhs, const Cursor& rhs);
80
81    private:
82     // private constructors for RGWPeriodHistory
83     friend class RGWPeriodHistory::Impl;
84
85     Cursor(const History* history, std::mutex* mutex, epoch_t epoch)
86       : history(history), mutex(mutex), epoch(epoch) {}
87
88     int error{0};
89     const History* history{nullptr};
90     std::mutex* mutex{nullptr};
91     epoch_t epoch{0}; //< realm epoch of cursor position
92   };
93
94   /// return a cursor to the current period
95   Cursor get_current() const;
96
97   /// build up a connected period history that covers the span between
98   /// current_period and the given period, reading predecessor periods or
99   /// fetching them from the master as necessary. returns a cursor at the
100   /// given period that can be used to traverse the current_history
101   Cursor attach(RGWPeriod&& period);
102
103   /// insert the given period into an existing history, or create a new
104   /// unconnected history. similar to attach(), but it doesn't try to fetch
105   /// missing periods. returns a cursor to the inserted period iff it's in
106   /// the current_history
107   Cursor insert(RGWPeriod&& period);
108
109   /// search for a period by realm epoch, returning a valid Cursor iff it's in
110   /// the current_history
111   Cursor lookup(epoch_t realm_epoch);
112 };
113
114 #endif // RGW_PERIOD_HISTORY_H