1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/types.h"
#include "include/utime.h"
#include "objclass/objclass.h"
#include "cls_log_types.h"
#include "cls_log_ops.h"
#include "global/global_context.h"
#include "include/compat.h"
CLS_VER(1,0)
CLS_NAME(log)
static string log_index_prefix = "1_";
static int write_log_entry(cls_method_context_t hctx, string& index, cls_log_entry& entry)
{
bufferlist bl;
encode(entry, bl);
int ret = cls_cxx_map_set_val(hctx, index, &bl);
if (ret < 0)
return ret;
return 0;
}
static void get_index_time_prefix(utime_t& ts, string& index)
{
char buf[32];
snprintf(buf, sizeof(buf), "%010ld.%06ld_", (long)ts.sec(), (long)ts.usec());
index = log_index_prefix + buf;
}
static int read_header(cls_method_context_t hctx, cls_log_header& header)
{
bufferlist header_bl;
int ret = cls_cxx_map_read_header(hctx, &header_bl);
if (ret < 0)
return ret;
if (header_bl.length() == 0) {
header = cls_log_header();
return 0;
}
auto iter = header_bl.cbegin();
try {
decode(header, iter);
} catch (buffer::error& err) {
CLS_LOG(0, "ERROR: read_header(): failed to decode header");
}
return 0;
}
static int write_header(cls_method_context_t hctx, cls_log_header& header)
{
bufferlist header_bl;
encode(header, header_bl);
int ret = cls_cxx_map_write_header(hctx, &header_bl);
if (ret < 0)
return ret;
return 0;
}
static void get_index(cls_method_context_t hctx, utime_t& ts, string& index)
{
get_index_time_prefix(ts, index);
string unique_id;
cls_cxx_subop_version(hctx, &unique_id);
index.append(unique_id);
}
static int cls_log_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_log_add_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(1, "ERROR: cls_log_add_op(): failed to decode op");
return -EINVAL;
}
cls_log_header header;
int ret = read_header(hctx, header);
if (ret < 0)
return ret;
for (list<cls_log_entry>::iterator iter = op.entries.begin();
iter != op.entries.end(); ++iter) {
cls_log_entry& entry = *iter;
string index;
utime_t timestamp = entry.timestamp;
if (op.monotonic_inc && timestamp < header.max_time)
timestamp = header.max_time;
else if (timestamp > header.max_time)
header.max_time = timestamp;
if (entry.id.empty()) {
get_index(hctx, timestamp, index);
entry.id = index;
} else {
index = entry.id;
}
CLS_LOG(20, "storing entry at %s", index.c_str());
if (index > header.max_marker)
header.max_marker = index;
ret = write_log_entry(hctx, index, entry);
if (ret < 0)
return ret;
}
ret = write_header(hctx, header);
if (ret < 0)
return ret;
return 0;
}
static int cls_log_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_log_list_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(1, "ERROR: cls_log_list_op(): failed to decode op");
return -EINVAL;
}
map<string, bufferlist> keys;
string from_index;
string to_index;
if (op.marker.empty()) {
get_index_time_prefix(op.from_time, from_index);
} else {
from_index = op.marker;
}
bool use_time_boundary = (!op.from_time.is_zero() && (op.to_time >= op.from_time));
if (use_time_boundary)
get_index_time_prefix(op.to_time, to_index);
#define MAX_ENTRIES 1000
size_t max_entries = op.max_entries;
if (!max_entries || max_entries > MAX_ENTRIES)
max_entries = MAX_ENTRIES;
cls_log_list_ret ret;
int rc = cls_cxx_map_get_vals(hctx, from_index, log_index_prefix, max_entries, &keys, &ret.truncated);
if (rc < 0)
return rc;
list<cls_log_entry>& entries = ret.entries;
map<string, bufferlist>::iterator iter = keys.begin();
string marker;
for (; iter != keys.end(); ++iter) {
const string& index = iter->first;
marker = index;
if (use_time_boundary && index.compare(0, to_index.size(), to_index) >= 0) {
ret.truncated = false;
break;
}
bufferlist& bl = iter->second;
auto biter = bl.cbegin();
try {
cls_log_entry e;
decode(e, biter);
entries.push_back(e);
} catch (buffer::error& err) {
CLS_LOG(0, "ERROR: cls_log_list: could not decode entry, index=%s", index.c_str());
}
}
ret.marker = marker;
encode(ret, *out);
return 0;
}
static int cls_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_log_trim_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(0, "ERROR: cls_log_list_op(): failed to decode entry");
return -EINVAL;
}
map<string, bufferlist> keys;
string from_index;
string to_index;
if (op.from_marker.empty()) {
get_index_time_prefix(op.from_time, from_index);
} else {
from_index = op.from_marker;
}
if (op.to_marker.empty()) {
get_index_time_prefix(op.to_time, to_index);
} else {
to_index = op.to_marker;
}
#define MAX_TRIM_ENTRIES 1000
size_t max_entries = MAX_TRIM_ENTRIES;
bool more;
int rc = cls_cxx_map_get_vals(hctx, from_index, log_index_prefix, max_entries, &keys, &more);
if (rc < 0)
return rc;
map<string, bufferlist>::iterator iter = keys.begin();
bool removed = false;
for (; iter != keys.end(); ++iter) {
const string& index = iter->first;
CLS_LOG(20, "index=%s to_index=%s", index.c_str(), to_index.c_str());
if (index.compare(0, to_index.size(), to_index) > 0)
break;
CLS_LOG(20, "removing key: index=%s", index.c_str());
int rc = cls_cxx_map_remove_key(hctx, index);
if (rc < 0) {
CLS_LOG(1, "ERROR: cls_cxx_map_remove_key failed rc=%d", rc);
return -EINVAL;
}
removed = true;
}
if (!removed)
return -ENODATA;
return 0;
}
static int cls_log_info(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_log_info_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(1, "ERROR: cls_log_add_op(): failed to decode op");
return -EINVAL;
}
cls_log_info_ret ret;
int rc = read_header(hctx, ret.header);
if (rc < 0)
return rc;
encode(ret, *out);
return 0;
}
CLS_INIT(log)
{
CLS_LOG(1, "Loaded log class!");
cls_handle_t h_class;
cls_method_handle_t h_log_add;
cls_method_handle_t h_log_list;
cls_method_handle_t h_log_trim;
cls_method_handle_t h_log_info;
cls_register("log", &h_class);
/* log */
cls_register_cxx_method(h_class, "add", CLS_METHOD_RD | CLS_METHOD_WR, cls_log_add, &h_log_add);
cls_register_cxx_method(h_class, "list", CLS_METHOD_RD, cls_log_list, &h_log_list);
cls_register_cxx_method(h_class, "trim", CLS_METHOD_RD | CLS_METHOD_WR, cls_log_trim, &h_log_trim);
cls_register_cxx_method(h_class, "info", CLS_METHOD_RD, cls_log_info, &h_log_info);
return;
}
|