1 // -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/types.h"
5 #include "cls/log/cls_log_types.h"
6 #include "cls/log/cls_log_client.h"
8 #include "include/utime.h"
9 #include "common/Clock.h"
10 #include "global/global_context.h"
12 #include "gtest/gtest.h"
13 #include "test/librados/test.h"
19 static librados::ObjectWriteOperation *new_op() {
20 return new librados::ObjectWriteOperation();
23 static librados::ObjectReadOperation *new_rop() {
24 return new librados::ObjectReadOperation();
27 static void reset_rop(librados::ObjectReadOperation **pop) {
32 static int read_bl(bufferlist& bl, int *i)
34 bufferlist::iterator iter = bl.begin();
38 } catch (buffer::error& err) {
39 std::cout << "failed to decode buffer" << std::endl;
46 void add_log(librados::ObjectWriteOperation *op, utime_t& timestamp, string& section, string&name, int i)
51 cls_log_add(*op, timestamp, section, name, bl);
55 string get_name(int i)
57 string name_prefix = "data-source";
60 snprintf(buf, sizeof(buf), "%d", i);
61 return name_prefix + buf;
64 void generate_log(librados::IoCtx& ioctx, string& oid, int max, utime_t& start_time, bool modify_time)
66 string section = "global";
68 librados::ObjectWriteOperation *op = new_op();
72 for (i = 0; i < max; i++) {
73 uint32_t secs = start_time.sec();
77 utime_t ts(secs, start_time.nsec());
78 string name = get_name(i);
80 add_log(op, ts, section, name, i);
83 ASSERT_EQ(0, ioctx.operate(oid, op));
88 utime_t get_time(utime_t& start_time, int i, bool modify_time)
90 uint32_t secs = start_time.sec();
93 return utime_t(secs, start_time.nsec());
96 void check_entry(cls_log_entry& entry, utime_t& start_time, int i, bool modified_time)
98 string section = "global";
99 string name = get_name(i);
100 utime_t ts = get_time(start_time, i, modified_time);
102 ASSERT_EQ(section, entry.section);
103 ASSERT_EQ(name, entry.name);
104 ASSERT_EQ(ts, entry.timestamp);
108 TEST(cls_rgw, test_log_add_same_time)
110 librados::Rados rados;
111 librados::IoCtx ioctx;
112 string pool_name = get_temp_pool_name();
115 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
116 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
122 ASSERT_EQ(0, ioctx.create(oid, true));
125 utime_t start_time = ceph_clock_now();
126 generate_log(ioctx, oid, 10, start_time, false);
128 librados::ObjectReadOperation *rop = new_rop();
130 list<cls_log_entry> entries;
135 utime_t to_time = get_time(start_time, 1, true);
139 cls_log_list(*rop, start_time, to_time, marker, 0, entries, &marker, &truncated);
142 ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
144 ASSERT_EQ(10, (int)entries.size());
145 ASSERT_EQ(0, (int)truncated);
147 list<cls_log_entry>::iterator iter;
149 /* need to sort returned entries, all were using the same time as key */
150 map<int, cls_log_entry> check_ents;
152 for (iter = entries.begin(); iter != entries.end(); ++iter) {
153 cls_log_entry& entry = *iter;
156 ASSERT_EQ(0, read_bl(entry.data, &num));
158 check_ents[num] = entry;
161 ASSERT_EQ(10, (int)check_ents.size());
163 map<int, cls_log_entry>::iterator ei;
165 /* verify entries are as expected */
169 for (i = 0, ei = check_ents.begin(); i < 10; i++, ++ei) {
170 cls_log_entry& entry = ei->second;
172 ASSERT_EQ(i, ei->first);
173 check_entry(entry, start_time, i, false);
178 /* check list again, now want to be truncated*/
182 cls_log_list(*rop, start_time, to_time, marker, 1, entries, &marker, &truncated);
184 ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
186 ASSERT_EQ(1, (int)entries.size());
187 ASSERT_EQ(1, (int)truncated);
192 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, rados));
195 TEST(cls_rgw, test_log_add_different_time)
197 librados::Rados rados;
198 librados::IoCtx ioctx;
199 string pool_name = get_temp_pool_name();
202 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
203 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
209 ASSERT_EQ(0, ioctx.create(oid, true));
212 utime_t start_time = ceph_clock_now();
213 generate_log(ioctx, oid, 10, start_time, true);
215 librados::ObjectReadOperation *rop = new_rop();
217 list<cls_log_entry> entries;
220 utime_t to_time = utime_t(start_time.sec() + 10, start_time.nsec());
225 cls_log_list(*rop, start_time, to_time, marker, 0, entries, &marker, &truncated);
228 ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
230 ASSERT_EQ(10, (int)entries.size());
231 ASSERT_EQ(0, (int)truncated);
233 list<cls_log_entry>::iterator iter;
235 /* returned entries should be sorted by time */
236 map<int, cls_log_entry> check_ents;
240 for (i = 0, iter = entries.begin(); iter != entries.end(); ++iter, ++i) {
241 cls_log_entry& entry = *iter;
245 ASSERT_EQ(0, read_bl(entry.data, &num));
249 check_entry(entry, start_time, i, true);
254 /* check list again with shifted time */
255 utime_t next_time = get_time(start_time, 1, true);
259 cls_log_list(*rop, next_time, to_time, marker, 0, entries, &marker, &truncated);
261 ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
263 ASSERT_EQ(9, (int)entries.size());
264 ASSERT_EQ(0, (int)truncated);
273 string old_marker = marker;
274 cls_log_list(*rop, start_time, to_time, old_marker, 1, entries, &marker, &truncated);
276 ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
277 ASSERT_NE(old_marker, marker);
278 ASSERT_EQ(1, (int)entries.size());
288 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, rados));
291 TEST(cls_rgw, test_log_trim)
293 librados::Rados rados;
294 librados::IoCtx ioctx;
295 string pool_name = get_temp_pool_name();
298 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
299 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
305 ASSERT_EQ(0, ioctx.create(oid, true));
308 utime_t start_time = ceph_clock_now();
309 generate_log(ioctx, oid, 10, start_time, true);
311 librados::ObjectReadOperation *rop = new_rop();
313 list<cls_log_entry> entries;
319 utime_t to_time = get_time(start_time, 10, true);
321 for (int i = 0; i < 10; i++) {
322 utime_t trim_time = get_time(start_time, i, true);
325 string start_marker, end_marker;
327 ASSERT_EQ(0, cls_log_trim(ioctx, oid, zero_time, trim_time, start_marker, end_marker));
331 cls_log_list(*rop, start_time, to_time, marker, 0, entries, &marker, &truncated);
334 ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
336 ASSERT_EQ(9 - i, (int)entries.size());
337 ASSERT_EQ(0, (int)truncated);
342 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, rados));