Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / cls / replica_log / cls_replica_log_types.h
1 /*
2  * Ceph - scalable distributed file system
3  *
4  * This is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License version 2.1, as published by the Free Software
7  * Foundation.  See file COPYING.
8  *
9  * Copyright 2013 Inktank
10  */
11
12 #ifndef CLS_REPLICA_LOG_TYPES_H_
13 #define CLS_REPLICA_LOG_TYPES_H_
14
15 #include "include/utime.h"
16 #include "include/encoding.h"
17 #include "include/types.h"
18 #include <errno.h>
19
20 class JSONObj;
21
22 struct cls_replica_log_item_marker {
23   string item_name; // the name of the item we're marking
24   utime_t item_timestamp; // the time stamp at which the item was outdated
25
26   cls_replica_log_item_marker() {}
27   cls_replica_log_item_marker(const string& name, const utime_t& time) :
28     item_name(name), item_timestamp(time) {}
29
30   void encode(bufferlist& bl) const {
31     ENCODE_START(1, 1, bl);
32     ::encode(item_name, bl);
33     ::encode(item_timestamp, bl);
34     ENCODE_FINISH(bl);
35   }
36
37   void decode(bufferlist::iterator& bl) {
38     DECODE_START(1, bl);
39     ::decode(item_name, bl);
40     ::decode(item_timestamp, bl);
41     DECODE_FINISH(bl);
42   }
43
44   void dump(Formatter *f) const;
45   void decode_json(JSONObj *obj);
46   static void generate_test_instances(std::list<cls_replica_log_item_marker*>& ls);
47 };
48 WRITE_CLASS_ENCODER(cls_replica_log_item_marker)
49
50 struct cls_replica_log_progress_marker {
51   string entity_id; // the name of the entity setting the progress marker
52   string position_marker; // represents a log listing position on the master
53   utime_t position_time; // the timestamp associated with the position marker
54   std::list<cls_replica_log_item_marker> items; /* any items not caught up
55                                                    to the position marker*/
56
57   cls_replica_log_progress_marker() {}
58   cls_replica_log_progress_marker(const string& entity, const string& marker,
59                                   const utime_t& time ) :
60                                     entity_id(entity), position_marker(marker),
61                                     position_time(time) {}
62   cls_replica_log_progress_marker(const string& entity, const string& marker,
63                                   const utime_t& time,
64                                   const std::list<cls_replica_log_item_marker>& b) :
65                                     entity_id(entity), position_marker(marker),
66                                     position_time(time),
67                                     items(b) {}
68
69   void encode(bufferlist& bl) const {
70     ENCODE_START(1, 1, bl);
71     ::encode(entity_id, bl);
72     ::encode(position_marker, bl);
73     ::encode(position_time, bl);
74     ::encode(items, bl);
75     ENCODE_FINISH(bl);
76   }
77
78   void decode(bufferlist::iterator& bl) {
79     DECODE_START(1, bl);
80     ::decode(entity_id, bl);
81     ::decode(position_marker, bl);
82     ::decode(position_time, bl);
83     ::decode(items, bl);
84     DECODE_FINISH(bl);
85   }
86
87   void dump(Formatter *f) const;
88   void decode_json(JSONObj *obj);
89   static void generate_test_instances(std::list<cls_replica_log_progress_marker*>& ls);
90 };
91 WRITE_CLASS_ENCODER(cls_replica_log_progress_marker)
92
93 class cls_replica_log_bound {
94   /**
95    * Right now, we are lazy and only support a single marker at a time. In the
96    * future, we might support more than one, so the interface is designed to
97    * let that work.
98    */
99   string position_marker; // represents a log listing position on the master
100   utime_t position_time; // the timestamp associated with the position marker
101   bool marker_exists; // has the marker been set?
102   cls_replica_log_progress_marker marker; // the status of the current locker
103
104 public:
105   cls_replica_log_bound() : marker_exists(false) {}
106
107   int update_marker(const cls_replica_log_progress_marker& new_mark) {
108     // only one marker at a time right now
109     if (marker_exists && (marker.entity_id != new_mark.entity_id)) {
110       return -EEXIST;
111     }
112     // can't go backwards with our one marker!
113     if (marker_exists && (marker.position_time > new_mark.position_time)) {
114       return -EINVAL;
115     }
116
117     marker = new_mark;
118     position_marker = new_mark.position_marker;
119     position_time = new_mark.position_time;
120     marker_exists = true;
121     // hey look, updating is idempotent; did you notice that?
122     return 0;
123   }
124
125   int delete_marker(const string& entity_id) {
126     if (marker_exists) {
127       // ENOENT if our marker doesn't match the passed ID
128       if (marker.entity_id != entity_id) {
129         return -ENOENT;
130       }
131       // you can't delete it if there are unclean entries
132       if (!marker.items.empty()) {
133         return -ENOTEMPTY;
134       }
135     }
136
137     marker_exists = false;
138     marker = cls_replica_log_progress_marker();
139     // hey look, deletion is idempotent! Hurray.
140     return 0;
141   }
142
143   std::string get_lowest_marker_bound() {
144     return position_marker;
145   }
146
147   utime_t get_lowest_time_bound() {
148     return position_time;
149   }
150
151   utime_t get_oldest_time() {
152     utime_t oldest = position_time;
153     list<cls_replica_log_item_marker>::const_iterator i;
154     for ( i = marker.items.begin(); i != marker.items.end(); ++i) {
155       if (i->item_timestamp < oldest)
156         oldest = i->item_timestamp;
157     }
158     return oldest;
159   }
160
161   void get_markers(list<cls_replica_log_progress_marker>& ls) {
162     if (marker_exists) {
163       ls.push_back(marker);
164     }
165   }
166
167   void encode(bufferlist& bl) const {
168     ENCODE_START(1, 1, bl);
169     ::encode(position_marker, bl);
170     ::encode(position_time, bl);
171     ::encode(marker_exists, bl);
172     if (marker_exists) {
173       ::encode(marker, bl);
174     }
175     ENCODE_FINISH(bl);
176   }
177   void decode(bufferlist::iterator& bl) {
178     DECODE_START(1, bl);
179     ::decode(position_marker, bl);
180     ::decode(position_time, bl);
181     ::decode(marker_exists, bl);
182     if (marker_exists) {
183       ::decode(marker, bl);
184     }
185     DECODE_FINISH(bl);
186   }
187
188   void dump(Formatter *f) const;
189   void decode_json(JSONObj *obj);
190   static void generate_test_instances(std::list<cls_replica_log_bound*>& ls);
191 };
192 WRITE_CLASS_ENCODER(cls_replica_log_bound)
193
194 #endif /* CLS_REPLICA_LOG_TYPES_H_ */