Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / cls / user / cls_user_types.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 CEPH_CLS_USER_TYPES_H
5 #define CEPH_CLS_USER_TYPES_H
6
7 #include "include/encoding.h"
8 #include "include/types.h"
9 #include "include/utime.h"
10 #include "common/ceph_time.h"
11
12 /*
13  * this needs to be compatible with rgw_bucket, as it replaces it
14  */
15 struct cls_user_bucket {
16   std::string name;
17   std::string marker;
18   std::string bucket_id;
19   std::string placement_id;
20   struct {
21     std::string data_pool;
22     std::string index_pool;
23     std::string data_extra_pool;
24   } explicit_placement;
25
26   void encode(bufferlist& bl) const {
27     /* since new version of this structure is not backward compatible,
28      * we have older rgw running against newer osd if we encode it
29      * in the new way. Only encode newer version if placement_id is
30      * not empty, otherwise keep handling it as before
31      */
32     if (!placement_id.empty()) {
33       ENCODE_START(9, 8, bl);
34       ::encode(name, bl);
35       ::encode(marker, bl);
36       ::encode(bucket_id, bl);
37       ::encode(placement_id, bl);
38       ENCODE_FINISH(bl);
39     } else {
40       ENCODE_START(7, 3, bl);
41       ::encode(name, bl);
42       ::encode(explicit_placement.data_pool, bl);
43       ::encode(marker, bl);
44       ::encode(bucket_id, bl);
45       ::encode(explicit_placement.index_pool, bl);
46       ::encode(explicit_placement.data_extra_pool, bl);
47       ENCODE_FINISH(bl);
48     }
49   }
50   void decode(bufferlist::iterator& bl) {
51     DECODE_START_LEGACY_COMPAT_LEN(8, 3, 3, bl);
52     ::decode(name, bl);
53     if (struct_v < 8) {
54       ::decode(explicit_placement.data_pool, bl);
55     }
56     if (struct_v >= 2) {
57       ::decode(marker, bl);
58       if (struct_v <= 3) {
59         uint64_t id;
60         ::decode(id, bl);
61         char buf[16];
62         snprintf(buf, sizeof(buf), "%llu", (long long)id);
63         bucket_id = buf;
64       } else {
65         ::decode(bucket_id, bl);
66       }
67     }
68     if (struct_v < 8) {
69       if (struct_v >= 5) {
70         ::decode(explicit_placement.index_pool, bl);
71       } else {
72         explicit_placement.index_pool = explicit_placement.data_pool;
73       }
74       if (struct_v >= 7) {
75         ::decode(explicit_placement.data_extra_pool, bl);
76       }
77     } else {
78       ::decode(placement_id, bl);
79       if (struct_v == 8 && placement_id.empty()) {
80         ::decode(explicit_placement.data_pool, bl);
81         ::decode(explicit_placement.index_pool, bl);
82         ::decode(explicit_placement.data_extra_pool, bl);
83       }
84     }
85     DECODE_FINISH(bl);
86   }
87
88   bool operator<(const cls_user_bucket& b) const {
89     return name.compare(b.name) < 0;
90   }
91
92   void dump(Formatter *f) const;
93   static void generate_test_instances(list<cls_user_bucket*>& ls);
94 };
95 WRITE_CLASS_ENCODER(cls_user_bucket)
96
97 /*
98  * this structure overrides RGWBucketEnt
99  */
100 struct cls_user_bucket_entry {
101   cls_user_bucket bucket;
102   size_t size;
103   size_t size_rounded;
104   ceph::real_time creation_time;
105   uint64_t count;
106   bool user_stats_sync;
107
108   cls_user_bucket_entry() : size(0), size_rounded(0), count(0), user_stats_sync(false) {}
109
110   void encode(bufferlist& bl) const {
111     ENCODE_START(9, 5, bl);
112     uint64_t s = size;
113     __u32 mt = ceph::real_clock::to_time_t(creation_time);
114     string empty_str;  // originally had the bucket name here, but we encode bucket later
115     ::encode(empty_str, bl);
116     ::encode(s, bl);
117     ::encode(mt, bl);
118     ::encode(count, bl);
119     ::encode(bucket, bl);
120     s = size_rounded;
121     ::encode(s, bl);
122     ::encode(user_stats_sync, bl);
123     ::encode(creation_time, bl);
124     //::encode(placement_rule, bl); removed in v9
125     ENCODE_FINISH(bl);
126   }
127   void decode(bufferlist::iterator& bl) {
128     DECODE_START_LEGACY_COMPAT_LEN(9, 5, 5, bl);
129     __u32 mt;
130     uint64_t s;
131     string empty_str;  // backward compatibility
132     ::decode(empty_str, bl);
133     ::decode(s, bl);
134     ::decode(mt, bl);
135     size = s;
136     if (struct_v < 7) {
137       creation_time = ceph::real_clock::from_time_t(mt);
138     }
139     if (struct_v >= 2)
140       ::decode(count, bl);
141     if (struct_v >= 3)
142       ::decode(bucket, bl);
143     if (struct_v >= 4)
144       ::decode(s, bl);
145     size_rounded = s;
146     if (struct_v >= 6)
147       ::decode(user_stats_sync, bl);
148     if (struct_v >= 7)
149       ::decode(creation_time, bl);
150     if (struct_v == 8) { // added in v8, removed in v9
151       std::string placement_rule;
152       ::decode(placement_rule, bl);
153     }
154     DECODE_FINISH(bl);
155   }
156   void dump(Formatter *f) const;
157   static void generate_test_instances(list<cls_user_bucket_entry*>& ls);
158 };
159 WRITE_CLASS_ENCODER(cls_user_bucket_entry)
160
161 struct cls_user_stats {
162   uint64_t total_entries;
163   uint64_t total_bytes;
164   uint64_t total_bytes_rounded;
165
166   cls_user_stats()
167     : total_entries(0),
168       total_bytes(0),
169       total_bytes_rounded(0) {}
170
171   void encode(bufferlist& bl) const {
172      ENCODE_START(1, 1, bl);
173     ::encode(total_entries, bl);
174     ::encode(total_bytes, bl);
175     ::encode(total_bytes_rounded, bl);
176     ENCODE_FINISH(bl);
177   }
178   void decode(bufferlist::iterator& bl) {
179     DECODE_START(1, bl);
180     ::decode(total_entries, bl);
181     ::decode(total_bytes, bl);
182     ::decode(total_bytes_rounded, bl);
183     DECODE_FINISH(bl);
184   }
185
186   void dump(Formatter *f) const;
187   static void generate_test_instances(list<cls_user_stats*>& ls);
188 };
189 WRITE_CLASS_ENCODER(cls_user_stats)
190
191 /*
192  * this needs to be compatible with rgw_bucket, as it replaces it
193  */
194 struct cls_user_header {
195   cls_user_stats stats;
196   ceph::real_time last_stats_sync;     /* last time a full stats sync completed */
197   ceph::real_time last_stats_update;   /* last time a stats update was done */
198
199   void encode(bufferlist& bl) const {
200      ENCODE_START(1, 1, bl);
201     ::encode(stats, bl);
202     ::encode(last_stats_sync, bl);
203     ::encode(last_stats_update, bl);
204     ENCODE_FINISH(bl);
205   }
206   void decode(bufferlist::iterator& bl) {
207     DECODE_START(1, bl);
208     ::decode(stats, bl);
209     ::decode(last_stats_sync, bl);
210     ::decode(last_stats_update, bl);
211     DECODE_FINISH(bl);
212   }
213
214   void dump(Formatter *f) const;
215   static void generate_test_instances(list<cls_user_header*>& ls);
216 };
217 WRITE_CLASS_ENCODER(cls_user_header)
218
219 void cls_user_gen_test_bucket(cls_user_bucket *bucket, int i);
220 void cls_user_gen_test_bucket_entry(cls_user_bucket_entry *entry, int i);
221 void cls_user_gen_test_stats(cls_user_stats *stats);
222 void cls_user_gen_test_header(cls_user_header *h);
223
224 #endif
225
226