55
66#include " include/pika_binlog.h"
77
8- #include < sys/time.h>
9- #include < glog/logging.h>
108#include < fcntl.h>
11-
9+ #include < glog/logging.h>
10+ #include < sys/time.h>
1211
1312#include " include/pika_binlog_transverter.h"
1413
@@ -24,10 +23,7 @@ std::string NewFileName(const std::string name, const uint32_t current) {
2423 * Version
2524 */
2625Version::Version (slash::RWFile *save)
27- : pro_num_(0 ),
28- pro_offset_(0 ),
29- logic_id_(0 ),
30- save_(save) {
26+ : pro_num_(0 ), pro_offset_(0 ), logic_id_(0 ), save_(save) {
3127 assert (save_ != NULL );
3228
3329 pthread_rwlock_init (&rwlock_, NULL );
@@ -66,21 +62,15 @@ Status Version::Init() {
6662/*
6763 * Binlog
6864 */
69- Binlog::Binlog (const std::string& binlog_path, const int file_size) :
70- opened_(false ),
71- version_(NULL ),
72- queue_(NULL ),
73- versionfile_(NULL ),
74- pro_num_(0 ),
75- pool_(NULL ),
76- exit_all_consume_(false ),
77- binlog_path_(binlog_path),
78- file_size_(file_size),
79- binlog_io_error_(false ) {
65+ Binlog::Binlog (const std::string &binlog_path, const int file_size)
66+ : opened_(false ), version_(NULL ), queue_(NULL ), versionfile_(NULL ),
67+ pro_num_(0 ), pool_(NULL ), exit_all_consume_(false ),
68+ binlog_path_(binlog_path), file_size_(file_size),
69+ binlog_io_error_(false ) {
8070
8171 // To intergrate with old version, we don't set mmap file size to 100M;
82- // slash::SetMmapBoundSize(file_size);
83- // slash::kMmapBoundSize = 1024 * 1024 * 100;
72+ // slash::SetMmapBoundSize(file_size);
73+ // slash::kMmapBoundSize = 1024 * 1024 * 100;
8474
8575 Status s;
8676
@@ -99,7 +89,6 @@ Binlog::Binlog(const std::string& binlog_path, const int file_size) :
9989 LOG (FATAL ) << " Binlog: new " << filename_ << " " << s.ToString ();
10090 }
10191
102-
10392 s = slash::NewRWFile (manifest, &versionfile_);
10493 if (!s.ok ()) {
10594 LOG (FATAL ) << " Binlog: new versionfile error " << s.ToString ();
@@ -117,7 +106,7 @@ Binlog::Binlog(const std::string& binlog_path, const int file_size) :
117106 pro_num_ = version_->pro_num_ ;
118107
119108 // Debug
120- // version_->debug();
109+ // version_->debug();
121110 } else {
122111 LOG (FATAL ) << " Binlog: open versionfile error" ;
123112 }
@@ -126,7 +115,8 @@ Binlog::Binlog(const std::string& binlog_path, const int file_size) :
126115 DLOG (INFO ) << " Binlog: open profile " << profile;
127116 s = slash::AppendWritableFile (profile, &queue_, version_->pro_offset_ );
128117 if (!s.ok ()) {
129- LOG (FATAL ) << " Binlog: Open file " << profile << " error " << s.ToString ();
118+ LOG (FATAL ) << " Binlog: Open file " << profile << " error "
119+ << s.ToString ();
130120 }
131121
132122 uint64_t filesize = queue_->Filesize ();
@@ -161,8 +151,8 @@ void Binlog::InitLogFile() {
161151 opened_.store (true );
162152}
163153
164- Status Binlog::GetProducerStatus (uint32_t * filenum, uint64_t * pro_offset,
165- uint32_t * term, uint64_t * logic_id) {
154+ Status Binlog::GetProducerStatus (uint32_t * filenum, uint64_t * pro_offset,
155+ uint32_t * term, uint64_t * logic_id) {
166156 if (!opened_.load ()) {
167157 return Status::Busy (" Binlog is not open yet" );
168158 }
@@ -194,19 +184,22 @@ Status Binlog::Put(const std::string &item) {
194184}
195185
196186// Note: mutex lock should be held
197- Status Binlog::Put (const char * item, int len) {
187+ Status Binlog::Put (const char * item, int len) {
198188 Status s;
199189
200190 /* Check to roll log file */
201191 uint64_t filesize = queue_->Filesize ();
202192 if (filesize > file_size_) {
193+ slash::WritableFile * queue = nullptr ;
194+ std::string profile = NewFileName (filename_, pro_num_ + 1 );
195+ s = slash::NewWritableFile (profile, &queue);
196+ if (!s.ok ()) {
197+ LOG (ERROR ) << " Binlog: new " << filename_ << " " << s.ToString ();
198+ return s;
199+ }
203200 delete queue_;
204- queue_ = NULL ;
205-
201+ queue_ = queue;
206202 pro_num_++;
207- std::string profile = NewFileName (filename_, pro_num_);
208- slash::NewWritableFile (profile, &queue_);
209-
210203 {
211204 slash::RWLock l (&(version_->rwlock_ ), true );
212205 version_->pro_offset_ = 0 ;
@@ -227,38 +220,39 @@ Status Binlog::Put(const char* item, int len) {
227220
228221 return s;
229222}
230-
231- Status Binlog::EmitPhysicalRecord (RecordType t, const char *ptr, size_t n, int *temp_pro_offset) {
232- Status s;
233- assert (n <= 0xffffff );
234- assert (block_offset_ + kHeaderSize + n <= kBlockSize );
235-
236- char buf[kHeaderSize ];
237-
238- uint64_t now;
239- struct timeval tv;
240- gettimeofday (&tv, NULL );
241- now = tv.tv_sec ;
242- buf[0 ] = static_cast <char >(n & 0xff );
243- buf[1 ] = static_cast <char >((n & 0xff00 ) >> 8 );
244- buf[2 ] = static_cast <char >(n >> 16 );
245- buf[3 ] = static_cast <char >(now & 0xff );
246- buf[4 ] = static_cast <char >((now & 0xff00 ) >> 8 );
247- buf[5 ] = static_cast <char >((now & 0xff0000 ) >> 16 );
248- buf[6 ] = static_cast <char >((now & 0xff000000 ) >> 24 );
249- buf[7 ] = static_cast <char >(t);
250-
251- s = queue_->Append (Slice (buf, kHeaderSize ));
223+
224+ Status Binlog::EmitPhysicalRecord (RecordType t, const char *ptr, size_t n,
225+ int *temp_pro_offset) {
226+ Status s;
227+ assert (n <= 0xffffff );
228+ assert (block_offset_ + kHeaderSize + n <= kBlockSize );
229+
230+ char buf[kHeaderSize ];
231+
232+ uint64_t now;
233+ struct timeval tv;
234+ gettimeofday (&tv, NULL );
235+ now = tv.tv_sec ;
236+ buf[0 ] = static_cast <char >(n & 0xff );
237+ buf[1 ] = static_cast <char >((n & 0xff00 ) >> 8 );
238+ buf[2 ] = static_cast <char >(n >> 16 );
239+ buf[3 ] = static_cast <char >(now & 0xff );
240+ buf[4 ] = static_cast <char >((now & 0xff00 ) >> 8 );
241+ buf[5 ] = static_cast <char >((now & 0xff0000 ) >> 16 );
242+ buf[6 ] = static_cast <char >((now & 0xff000000 ) >> 24 );
243+ buf[7 ] = static_cast <char >(t);
244+
245+ s = queue_->Append (Slice (buf, kHeaderSize ));
246+ if (s.ok ()) {
247+ s = queue_->Append (Slice (ptr, n));
252248 if (s.ok ()) {
253- s = queue_->Append (Slice (ptr, n));
254- if (s.ok ()) {
255- s = queue_->Flush ();
256- }
249+ s = queue_->Flush ();
257250 }
258- block_offset_ += static_cast <int >(kHeaderSize + n);
251+ }
252+ block_offset_ += static_cast <int >(kHeaderSize + n);
259253
260- *temp_pro_offset += kHeaderSize + n;
261- return s;
254+ *temp_pro_offset += kHeaderSize + n;
255+ return s;
262256}
263257
264258Status Binlog::Produce (const Slice &item, int *temp_pro_offset) {
@@ -304,8 +298,8 @@ Status Binlog::Produce(const Slice &item, int *temp_pro_offset) {
304298
305299 return s;
306300}
307-
308- Status Binlog::AppendPadding (slash::WritableFile* file, uint64_t * len) {
301+
302+ Status Binlog::AppendPadding (slash::WritableFile * file, uint64_t * len) {
309303 if (*len < kHeaderSize ) {
310304 return Status::OK ();
311305 }
@@ -346,12 +340,14 @@ Status Binlog::AppendPadding(slash::WritableFile* file, uint64_t* len) {
346340 }
347341 *len -= left;
348342 if (left != 0 ) {
349- LOG (WARNING ) << " AppendPadding left bytes: " << left << " is less then kHeaderSize" ;
343+ LOG (WARNING ) << " AppendPadding left bytes: " << left
344+ << " is less then kHeaderSize" ;
350345 }
351346 return s;
352347}
353348
354- Status Binlog::SetProducerStatus (uint32_t pro_num, uint64_t pro_offset, uint32_t term, uint64_t index) {
349+ Status Binlog::SetProducerStatus (uint32_t pro_num, uint64_t pro_offset,
350+ uint32_t term, uint64_t index) {
355351 if (!opened_.load ()) {
356352 return Status::Busy (" Binlog is not open yet" );
357353 }
0 commit comments