forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_binlog.cc
411 lines (340 loc) · 10.6 KB
/
pika_binlog.cc
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "pika_binlog.h"
#include <iostream>
#include <string>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <glog/logging.h>
#include "slash/include/slash_mutex.h"
using slash::RWLock;
std::string NewFileName(const std::string name, const uint32_t current) {
char buf[256];
snprintf(buf, sizeof(buf), "%s%u", name.c_str(), current);
return std::string(buf);
}
/*
* Version
*/
Version::Version(slash::RWFile *save)
: pro_offset_(0),
pro_num_(0),
save_(save) {
assert(save_ != NULL);
pthread_rwlock_init(&rwlock_, NULL);
}
Version::~Version() {
StableSave();
pthread_rwlock_destroy(&rwlock_);
}
Status Version::StableSave() {
char *p = save_->GetData();
memcpy(p, &pro_offset_, sizeof(uint64_t));
p += 20;
//memcpy(p, &con_offset_, sizeof(uint64_t));
//p += 8;
//memcpy(p, &item_num_, sizeof(uint32_t));
//p += 4;
memcpy(p, &pro_num_, sizeof(uint32_t));
//p += 4;
//memcpy(p, &con_num_, sizeof(uint32_t));
//p += 4;
return Status::OK();
}
Status Version::Init() {
Status s;
if (save_->GetData() != NULL) {
memcpy((char*)(&pro_offset_), save_->GetData(), sizeof(uint64_t));
//memcpy((char*)(&con_offset_), save_->GetData() + 8, sizeof(uint64_t));
memcpy((char*)(&item_num_), save_->GetData() + 16, sizeof(uint32_t));
memcpy((char*)(&pro_num_), save_->GetData() + 20, sizeof(uint32_t));
//memcpy((char*)(&con_num_), save_->GetData() + 24, sizeof(uint32_t));
// DLOG(INFO) << "Version Init pro_offset "<< pro_offset_ << " itemnum " << item_num << " pro_num " << pro_num_ << " con_num " << con_num_;
return Status::OK();
} else {
return Status::Corruption("version init error");
}
}
/*
* Binlog
*/
Binlog::Binlog(const std::string& binlog_path, const int file_size) :
consumer_num_(0),
item_num_(0),
version_(NULL),
queue_(NULL),
versionfile_(NULL),
pro_num_(0),
pool_(NULL),
exit_all_consume_(false),
binlog_path_(binlog_path),
file_size_(file_size) {
// To intergrate with old version, we don't set mmap file size to 100M;
//slash::SetMmapBoundSize(file_size);
//slash::kMmapBoundSize = 1024 * 1024 * 100;
Status s;
slash::CreateDir(binlog_path_);
filename = binlog_path_ + kBinlogPrefix;
const std::string manifest = binlog_path_ + kManifest;
std::string profile;
if (!slash::FileExists(manifest)) {
LOG(INFO) << "Binlog: Manifest file not exist, we create a new one.";
profile = NewFileName(filename, pro_num_);
s = slash::NewWritableFile(profile, &queue_);
if (!s.ok()) {
LOG(FATAL) << "Binlog: new " << filename << " " << s.ToString();
}
s = slash::NewRWFile(manifest, &versionfile_);
if (!s.ok()) {
LOG(FATAL) << "Binlog: new versionfile error " << s.ToString();
}
version_ = new Version(versionfile_);
version_->StableSave();
} else {
LOG(INFO) << "Binlog: Find the exist file.";
s = slash::NewRWFile(manifest, &versionfile_);
if (s.ok()) {
version_ = new Version(versionfile_);
version_->Init();
pro_num_ = version_->pro_num_;
// Debug
//version_->debug();
} else {
LOG(FATAL) << "Binlog: open versionfile error";
}
profile = NewFileName(filename, pro_num_);
DLOG(INFO) << "Binlog: open profile " << profile;
s = slash::AppendWritableFile(profile, &queue_, version_->pro_offset_);
if (!s.ok()) {
LOG(FATAL) << "Binlog: Open file " << profile << " error " << s.ToString();
}
uint64_t filesize = queue_->Filesize();
DLOG(INFO) << "Binlog: filesize is " << filesize;
}
InitLogFile();
}
Binlog::~Binlog() {
delete version_;
delete versionfile_;
delete queue_;
}
void Binlog::InitLogFile() {
assert(queue_ != NULL);
uint64_t filesize = queue_->Filesize();
block_offset_ = filesize % kBlockSize;
}
Status Binlog::GetProducerStatus(uint32_t* filenum, uint64_t* pro_offset) {
slash::RWLock(&(version_->rwlock_), false);
*filenum = version_->pro_num_;
*pro_offset = version_->pro_offset_;
//*filenum = version_->pro_num();
//*pro_offset = version_->pro_offset();
return Status::OK();
}
// Note: mutex lock should be held
Status Binlog::Put(const std::string &item) {
Status s;
/* Check to roll log file */
uint64_t filesize = queue_->Filesize();
if (filesize > file_size_) {
delete queue_;
queue_ = NULL;
pro_num_++;
std::string profile = NewFileName(filename, pro_num_);
slash::NewWritableFile(profile, &queue_);
{
slash::RWLock(&(version_->rwlock_), true);
version_->pro_offset_ = 0;
version_->pro_num_ = pro_num_;
//version_->set_pro_offset(0);
//version_->set_pro_num(pro_num_);
version_->StableSave();
//version_->debug();
}
InitLogFile();
}
int pro_offset;
s = Produce(Slice(item.data(), item.size()), &pro_offset);
if (s.ok()) {
slash::RWLock(&(version_->rwlock_), true);
//version_->plus_item_num();
version_->pro_offset_ = pro_offset;
//version_->set_pro_offset(pro_offset);
version_->StableSave();
}
return s;
}
// Note: mutex lock should be held
Status Binlog::Put(const char* item, int len) {
Status s;
/* Check to roll log file */
uint64_t filesize = queue_->Filesize();
if (filesize > file_size_) {
delete queue_;
queue_ = NULL;
pro_num_++;
std::string profile = NewFileName(filename, pro_num_);
slash::NewWritableFile(profile, &queue_);
{
slash::RWLock(&(version_->rwlock_), true);
version_->pro_offset_ = 0;
version_->pro_num_ = pro_num_;
//version_->set_pro_offset(0);
//version_->set_pro_num(pro_num_);
version_->StableSave();
}
//version_->debug();
InitLogFile();
}
int pro_offset;
s = Produce(Slice(item, len), &pro_offset);
if (s.ok()) {
slash::RWLock(&(version_->rwlock_), true);
version_->pro_offset_ = pro_offset;
//version_->plus_item_num();
//version_->set_pro_offset(pro_offset);
version_->StableSave();
}
return s;
}
Status Binlog::EmitPhysicalRecord(RecordType t, const char *ptr, size_t n, int *temp_pro_offset) {
Status s;
assert(n <= 0xffffff);
assert(block_offset_ + kHeaderSize + n <= kBlockSize);
char buf[kHeaderSize];
uint64_t now;
struct timeval tv;
gettimeofday(&tv, NULL);
now = tv.tv_sec;
buf[0] = static_cast<char>(n & 0xff);
buf[1] = static_cast<char>((n & 0xff00) >> 8);
buf[2] = static_cast<char>(n >> 16);
buf[3] = static_cast<char>(now & 0xff);
buf[4] = static_cast<char>((now & 0xff00) >> 8);
buf[5] = static_cast<char>((now & 0xff0000) >> 16);
buf[6] = static_cast<char>((now & 0xff000000) >> 24);
buf[7] = static_cast<char>(t);
s = queue_->Append(Slice(buf, kHeaderSize));
if (s.ok()) {
s = queue_->Append(Slice(ptr, n));
if (s.ok()) {
s = queue_->Flush();
}
}
block_offset_ += static_cast<int>(kHeaderSize + n);
// log_info("block_offset %d", (kHeaderSize + n));
*temp_pro_offset += kHeaderSize + n;
//version_->rise_pro_offset((uint64_t)(kHeaderSize + n));
//version_->StableSave();
return s;
}
Status Binlog::Produce(const Slice &item, int *temp_pro_offset) {
Status s;
const char *ptr = item.data();
size_t left = item.size();
bool begin = true;
*temp_pro_offset = version_->pro_offset_;
do {
const int leftover = static_cast<int>(kBlockSize) - block_offset_;
assert(leftover >= 0);
if (static_cast<size_t>(leftover) < kHeaderSize) {
if (leftover > 0) {
queue_->Append(Slice("\x00\x00\x00\x00\x00\x00\x00", leftover));
//version_->rise_pro_offset(leftover);
*temp_pro_offset += leftover;
//version_->StableSave();
}
block_offset_ = 0;
}
const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
const size_t fragment_length = (left < avail) ? left : avail;
RecordType type;
const bool end = (left == fragment_length);
if (begin && end) {
type = kFullType;
} else if (begin) {
type = kFirstType;
} else if (end) {
type = kLastType;
} else {
type = kMiddleType;
}
s = EmitPhysicalRecord(type, ptr, fragment_length, temp_pro_offset);
ptr += fragment_length;
left -= fragment_length;
begin = false;
} while (s.ok() && left > 0);
return s;
}
Status Binlog::AppendBlank(slash::WritableFile *file, uint64_t len) {
if (len < kHeaderSize) {
return Status::OK();
}
uint64_t pos = 0;
std::string blank(kBlockSize, ' ');
for (; pos + kBlockSize < len; pos += kBlockSize) {
file->Append(Slice(blank.data(), blank.size()));
}
// Append a msg which occupy the remain part of the last block
// We simply increase the remain length to kHeaderSize when remain part < kHeaderSize
uint32_t n;
if (len % kBlockSize < kHeaderSize) {
n = 0;
} else {
n = (uint32_t) ((len % kBlockSize) - kHeaderSize);
}
char buf[kBlockSize];
uint64_t now;
struct timeval tv;
gettimeofday(&tv, NULL);
now = tv.tv_sec;
buf[0] = static_cast<char>(n & 0xff);
buf[1] = static_cast<char>((n & 0xff00) >> 8);
buf[2] = static_cast<char>(n >> 16);
buf[3] = static_cast<char>(now & 0xff);
buf[4] = static_cast<char>((now & 0xff00) >> 8);
buf[5] = static_cast<char>((now & 0xff0000) >> 16);
buf[6] = static_cast<char>((now & 0xff000000) >> 24);
buf[7] = static_cast<char>(kFullType);
Status s = file->Append(Slice(buf, kHeaderSize));
if (s.ok()) {
s = file->Append(Slice(blank.data(), n));
if (s.ok()) {
s = file->Flush();
}
}
return s;
}
Status Binlog::SetProducerStatus(uint32_t pro_num, uint64_t pro_offset) {
slash::MutexLock l(&mutex_);
// offset smaller than the first header
if (pro_offset < 4) {
pro_offset = 0;
}
delete queue_;
std::string init_profile = NewFileName(filename, 0);
if (slash::FileExists(init_profile)) {
slash::DeleteFile(init_profile);
}
std::string profile = NewFileName(filename, pro_num);
if (slash::FileExists(profile)) {
slash::DeleteFile(profile);
}
slash::NewWritableFile(profile, &queue_);
Binlog::AppendBlank(queue_, pro_offset);
pro_num_ = pro_num;
{
slash::RWLock(&(version_->rwlock_), true);
version_->pro_num_ = pro_num;
version_->pro_offset_ = pro_offset;
//version_->set_pro_num(pro_num);
//version_->set_pro_offset(pro_offset);
version_->StableSave();
}
InitLogFile();
return Status::OK();
}