forked from OpenAtomFoundation/pikiwidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_binlog_reader.cc
266 lines (236 loc) · 7.68 KB
/
pika_binlog_reader.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
// 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 "include/pika_binlog_reader.h"
#include <glog/logging.h>
using pstd::Status;
PikaBinlogReader::PikaBinlogReader(uint32_t cur_filenum, uint64_t cur_offset)
: cur_filenum_(cur_filenum),
cur_offset_(cur_offset),
backing_store_(std::make_unique<char[]>(kBlockSize)),
buffer_() {
last_record_offset_ = cur_offset % kBlockSize;
}
PikaBinlogReader::PikaBinlogReader() : backing_store_(std::make_unique<char[]>(kBlockSize)), buffer_() {
last_record_offset_ = 0 % kBlockSize;
}
void PikaBinlogReader::GetReaderStatus(uint32_t* cur_filenum, uint64_t* cur_offset) {
std::shared_lock l(rwlock_);
*cur_filenum = cur_filenum_;
*cur_offset = cur_offset_;
}
bool PikaBinlogReader::ReadToTheEnd() {
uint32_t pro_num;
uint64_t pro_offset;
logger_->GetProducerStatus(&pro_num, &pro_offset);
std::shared_lock l(rwlock_);
return (pro_num == cur_filenum_ && pro_offset == cur_offset_);
}
int PikaBinlogReader::Seek(const std::shared_ptr<Binlog>& logger, uint32_t filenum, uint64_t offset) {
std::string confile = NewFileName(logger->filename(), filenum);
if (!pstd::FileExists(confile)) {
LOG(WARNING) << confile << " not exits";
return -1;
}
std::unique_ptr<pstd::SequentialFile> readfile;
if (!pstd::NewSequentialFile(confile, readfile).ok()) {
LOG(WARNING) << "New swquential " << confile << " failed";
return -1;
}
if (queue_) {
queue_.reset();
}
queue_ = std::move(readfile);
logger_ = logger;
std::lock_guard l(rwlock_);
cur_filenum_ = filenum;
cur_offset_ = offset;
last_record_offset_ = cur_filenum_ % kBlockSize;
pstd::Status s;
uint64_t start_block = (cur_offset_ / kBlockSize) * kBlockSize;
s = queue_->Skip((cur_offset_ / kBlockSize) * kBlockSize);
uint64_t block_offset = cur_offset_ % kBlockSize;
uint64_t ret = 0;
uint64_t res = 0;
bool is_error = false;
while (true) {
if (res >= block_offset) {
cur_offset_ = start_block + res;
break;
}
ret = 0;
is_error = GetNext(&ret);
if (is_error) {
return -1;
}
res += ret;
}
last_record_offset_ = cur_offset_ % kBlockSize;
return 0;
}
bool PikaBinlogReader::GetNext(uint64_t* size) {
uint64_t offset = 0;
pstd::Status s;
bool is_error = false;
while (true) {
buffer_.clear();
s = queue_->Read(kHeaderSize, &buffer_, backing_store_.get());
if (!s.ok()) {
is_error = true;
return is_error;
}
const char* header = buffer_.data();
const uint32_t a = static_cast<uint32_t>(header[0]) & 0xff;
const uint32_t b = static_cast<uint32_t>(header[1]) & 0xff;
const uint32_t c = static_cast<uint32_t>(header[2]) & 0xff;
const unsigned int type = header[7];
const uint32_t length = a | (b << 8) | (c << 16);
if (length > (kBlockSize - kHeaderSize)) {
return true;
}
if (type == kFullType) {
s = queue_->Read(length, &buffer_, backing_store_.get());
offset += kHeaderSize + length;
break;
} else if (type == kFirstType) {
s = queue_->Read(length, &buffer_, backing_store_.get());
offset += kHeaderSize + length;
} else if (type == kMiddleType) {
s = queue_->Read(length, &buffer_, backing_store_.get());
offset += kHeaderSize + length;
} else if (type == kLastType) {
s = queue_->Read(length, &buffer_, backing_store_.get());
offset += kHeaderSize + length;
break;
} else if (type == kBadRecord) {
s = queue_->Read(length, &buffer_, backing_store_.get());
offset += kHeaderSize + length;
break;
} else {
is_error = true;
break;
}
}
*size = offset;
return is_error;
}
unsigned int PikaBinlogReader::ReadPhysicalRecord(pstd::Slice* result, uint32_t* filenum, uint64_t* offset) {
pstd::Status s;
if (kBlockSize - last_record_offset_ <= kHeaderSize) {
queue_->Skip(kBlockSize - last_record_offset_);
std::lock_guard l(rwlock_);
cur_offset_ += (kBlockSize - last_record_offset_);
last_record_offset_ = 0;
}
buffer_.clear();
s = queue_->Read(kHeaderSize, &buffer_, backing_store_.get());
if (s.IsEndFile()) {
return kEof;
} else if (!s.ok()) {
return kBadRecord;
}
const char* header = buffer_.data();
const uint32_t a = static_cast<uint32_t>(header[0]) & 0xff;
const uint32_t b = static_cast<uint32_t>(header[1]) & 0xff;
const uint32_t c = static_cast<uint32_t>(header[2]) & 0xff;
const unsigned int type = header[7];
const uint32_t length = a | (b << 8) | (c << 16);
if (length > (kBlockSize - kHeaderSize)) {
return kBadRecord;
}
if (type == kZeroType || length == 0) {
buffer_.clear();
return kOldRecord;
}
buffer_.clear();
s = queue_->Read(length, &buffer_, backing_store_.get());
*result = pstd::Slice(buffer_.data(), buffer_.size());
last_record_offset_ += kHeaderSize + length;
if (s.ok()) {
std::lock_guard l(rwlock_);
*filenum = cur_filenum_;
cur_offset_ += (kHeaderSize + length);
*offset = cur_offset_;
}
return type;
}
Status PikaBinlogReader::Consume(std::string* scratch, uint32_t* filenum, uint64_t* offset) {
Status s;
pstd::Slice fragment;
while (true) {
const unsigned int record_type = ReadPhysicalRecord(&fragment, filenum, offset);
switch (record_type) {
case kFullType:
*scratch = std::string(fragment.data(), fragment.size());
s = Status::OK();
break;
case kFirstType:
scratch->assign(fragment.data(), fragment.size());
s = Status::NotFound("Middle Status");
break;
case kMiddleType:
scratch->append(fragment.data(), fragment.size());
s = Status::NotFound("Middle Status");
break;
case kLastType:
scratch->append(fragment.data(), fragment.size());
s = Status::OK();
break;
case kEof:
return Status::EndFile("Eof");
case kBadRecord:
LOG(WARNING)
<< "Read BadRecord record, will decode failed, this record may dbsync padded record, not processed here";
return Status::IOError("Data Corruption");
case kOldRecord:
return Status::EndFile("Eof");
default:
return Status::IOError("Unknow reason");
}
if (s.ok()) {
break;
}
}
// DLOG(INFO) << "Binlog Sender consumer a msg: " << scratch;
return Status::OK();
}
// Get a whole message;
// Append to scratch;
// the status will be OK, IOError or Corruption, EndFile;
Status PikaBinlogReader::Get(std::string* scratch, uint32_t* filenum, uint64_t* offset) {
if (!logger_ || !queue_) {
return Status::Corruption("Not seek");
}
scratch->clear();
Status s = Status::OK();
do {
if (ReadToTheEnd()) {
return Status::EndFile("End of cur log file");
}
s = Consume(scratch, filenum, offset);
if (s.IsEndFile()) {
std::string confile = NewFileName(logger_->filename(), cur_filenum_ + 1);
// sleep 10ms wait produce thread generate the new binlog
usleep(10000);
// Roll to next file need retry;
if (pstd::FileExists(confile)) {
DLOG(INFO) << "BinlogSender roll to new binlog" << confile;
queue_.reset();
queue_ = nullptr;
pstd::NewSequentialFile(confile, queue_);
{
std::lock_guard l(rwlock_);
cur_filenum_++;
cur_offset_ = 0;
}
last_record_offset_ = 0;
} else {
return Status::IOError("File Does Not Exists");
}
} else {
break;
}
} while (s.IsEndFile());
return Status::OK();
}