logger.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DTC_LOGGER_H
  17. #define __DTC_LOGGER_H
  18. #include <stdint.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include "buffer.h"
  22. #include "log/log.h"
  23. #include "journal_id.h"
  24. #define MAX_PATH_NAME_LEN 256
  25. /*
  26. * DTC binlog base class(file)
  27. */
  28. class LogBase {
  29. public:
  30. LogBase();
  31. virtual ~LogBase();
  32. protected:
  33. int set_path(const char *path, const char *prefix);
  34. void file_name(char *s, int len, uint32_t serail);
  35. int open_file(uint32_t serial, int read);
  36. void close_file();
  37. int scan_serial(uint32_t *min, uint32_t *max);
  38. int stat_size(off_t *);
  39. int delete_file(uint32_t serial);
  40. private:
  41. LogBase(const LogBase &);
  42. protected:
  43. int _fd;
  44. private:
  45. char _path[MAX_PATH_NAME_LEN]; //日志集所在目录
  46. char _prefix[MAX_PATH_NAME_LEN]; //日志集的文件前缀
  47. };
  48. class LogWriter : public LogBase {
  49. public:
  50. int open(const char *path, const char *prefix, off_t max_size,
  51. uint64_t total_size);
  52. int write(const void *buf, size_t size);
  53. JournalID query();
  54. public:
  55. LogWriter();
  56. virtual ~LogWriter();
  57. private:
  58. int shift_file();
  59. private:
  60. off_t _cur_size; //当前日志文件的大小
  61. off_t _max_size; //单个日志文件允许的最大大小
  62. uint64_t _total_size; //日志集允许的最大大小
  63. uint32_t _cur_max_serial; //当前日志文件最大编号
  64. uint32_t _cur_min_serial; //当前日志文件最大编号
  65. };
  66. class LogReader : public LogBase {
  67. public:
  68. int open(const char *path, const char *prefix);
  69. int read(void *buf, size_t size);
  70. int seek(const JournalID &);
  71. JournalID query();
  72. public:
  73. LogReader();
  74. virtual ~LogReader();
  75. private:
  76. void refresh();
  77. private:
  78. uint32_t _min_serial; //日志集的最小文件编号
  79. uint32_t _max_serial; //日志集的最大文件编号
  80. uint32_t _cur_serial; //当前日志文件编号
  81. off_t _cur_offset; //当前日志文件偏移量
  82. };
  83. /////////////////////////////////////////////////////////////////////
  84. /*
  85. * generic binlog header
  86. */
  87. typedef struct binlog_header {
  88. uint32_t length; //长度
  89. uint8_t version; //版本
  90. uint8_t type; //类型: bitmap, dtc, other
  91. uint8_t operater; //操作: insert,select,upate ...
  92. uint8_t reserve[5]; //保留
  93. uint32_t timestamp; //时间戳
  94. uint32_t recordcount; //子记录个数
  95. uint8_t endof[0];
  96. } __attribute__((__aligned__(1))) binlog_header_t;
  97. /*
  98. * binlog type
  99. * t
  100. */
  101. typedef enum binlog_type {
  102. BINLOG_LRU = 1,
  103. BINLOG_INSERT = 2,
  104. BINLOG_UPDATE = 4,
  105. BINLOG_PRUGE = 8,
  106. } BINLOG_TYPE;
  107. /*
  108. * binlog class
  109. */
  110. #define BINLOG_MAX_SIZE (100 * (1U << 20)) //100M, 默认单个日志文件大小
  111. #define BINLOG_MAX_TOTAL_SIZE (3ULL << 30) //3G, 默认最大日志文件编号
  112. #define BINLOG_DEFAULT_VERSION 0x02
  113. class BinlogWriter {
  114. public:
  115. int init(const char *path, const char *prefix,
  116. uint64_t total_size = BINLOG_MAX_TOTAL_SIZE,
  117. off_t max_size = BINLOG_MAX_SIZE);
  118. int insert_header(uint8_t type, uint8_t operater, uint32_t recordcount);
  119. int append_body(const void *buf, size_t size);
  120. int Commit();
  121. int Abort();
  122. JournalID query_id();
  123. public:
  124. BinlogWriter();
  125. virtual ~BinlogWriter();
  126. private:
  127. BinlogWriter(const BinlogWriter &);
  128. private:
  129. LogWriter _log_writer; //写者
  130. buffer _codec_buffer; //编码缓冲区
  131. };
  132. class BinlogReader {
  133. public:
  134. int init(const char *path, const char *prefix);
  135. int Read(); //顺序读,每次读出一条binlog记录
  136. int Seek(const JournalID &);
  137. JournalID query_id();
  138. uint8_t binlog_type();
  139. uint8_t binlog_operator();
  140. uint32_t record_count();
  141. char *record_pointer(int id = 0);
  142. size_t record_length(int id = 0);
  143. public:
  144. BinlogReader();
  145. virtual ~BinlogReader();
  146. private:
  147. BinlogReader(const BinlogReader &);
  148. private:
  149. LogReader _log_reader; //读者
  150. buffer _codec_buffer; //编码缓冲区
  151. };
  152. #endif