# binlogconvert **Repository Path**: ecnu_-dase_-idds/binlogconvert ## Basic Information - **Project Name**: binlogconvert - **Description**: No description available - **Primary Language**: C++ - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-18 - **Last Updated**: 2025-02-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Build 1. 下载 FlatBuffers 库 ```bash git clone https://github.com/google/flatbuffers.git cd flatbuffers mkdir build && cd build cmake .. make -j$(nproc) make install ``` 2. 编译项目得到 so 文件 ```bash 1. Clone the repository git clone git@gitee.com:ecnu_-dase_-idds/binlogconvert.git git submodule update --init --recursive # Make sure the google test framework is downloaded # 2. The default compilation parameters are not to enable debug and test mode ./build.sh # 3. libsql2bl.so in the ./build/lib directory find ./build/lib/ -name '*.so' # 4. Confirm that the so dynamic library file can expose the ELF symbol table readelf -s build/lib/libsql2bl.so | grep SetBinlogPath readelf -s build/lib/libsql2bl.so | grep ConvertFlatBufferToBinlog readelf -s build/lib/libsql2bl.so | grep GetLastScnAndSeq ``` # 测试 ## 单元测试 ```bash # 1. 目录下./build.sh 编译脚本,修改编译选项 -DLOFT_TESTING=YES # 2. 查看测试结果 ./build/test/event_test # --> event 级别测试,构造 DDL | DML 的 binlog 文件内的 event 存储格式正确 mysqlbinlog -vv --base64-output=decode-rows --hexdump "event_file_name" ./build/test/fbs_test # DDL_TEST, DML_TEST # --> 读取 flatbuffer 内容无误 # SQL_TEST # --> 转换一条 DDL | DML,连续转换多条 DDL | DML,并通过 mysqlbinlog 回放工具证明无误 mysqlbinlog "binlog_file_name" | mysql -u -p -P 3306 -h ``` ## 集成测试 ```bash cd bin g++ main.cpp -o test -std=c++17 -pthread -ldl ./test ``` # Main API 1. 设置 binlog 文件写入的目录路径 ```c++ @param[in] bashPathBytes 目录路径的字符数组 @param[in] length bashPathBytes 的长度 @param[in] maxSize binlog 文件的大小 @param[in] threadNum 转换工作的线程数 @param[in] capacity binlog 文件的总大小 @param[in] expirationTime binlog 文件的过期时间 RC SetBinlogPath(char *bashPathBytes, int length, long maxSize, int threadNum, long capacity, int expirationTime); ``` 2. 把 FlatBuffer 日志格式 转换成 binlog 日志格式 ```c++ @param[in] fbStr 待转换的 flatbuffer 二进制数据 @param[in] length fbStr 的长度 @param[in] is_ddl 是否是 DDL 语句, true 表示 DDL, false 表示 DML std::future ConvertFlatBufferToBinlog(char *fbStr, int length, bool is_ddl); ``` 3. 获取转换的进度 checkpoint ```c++ @param[out] scn scn @param[out] seq seq @param[out] ckp checkpoint 记录在 controlinfo 文件中的第一行, 格式为 trxSeq-seq-scn RC GetLastScnAndSeq(long *scn, long *seq, char **ckp); ``` # How To Use ```c++ // 1. 创建一个 LogFileManager 对象 auto logFileManager = std::make_unique(); // 2. 设置写入binlog文件的目录,binlog文件的前缀名,默认是'teledb-bin', binlog文件的大小,默认是 20MB // "/home/yincong/collectBin" 的字符数组 char byteArray[] = "\x2f\x68\x6f\x6d\x65\x2f\x79\x69\x6e\x63\x6f\x6e\x67\x2f\x63\x6f\x6c\x6c\x65\x63\x74\x42\x69\x6e\x2f"; logFileManager->SetBinlogPath(byteArray, 25, DEFAULT_BINLOG_FILE_SIZE, 4, DEFAULT_BINLOG_FILE_SIZE * 20, 3600); // 3. 异步投放任务 std::vector> futures; // 存储所有的future for (auto& buf: buffers) { // 待转换的 中间数据 // true 表示 ddl,false 表示 dml futures.push_back(logFileManager->ConvertFlatBufferToBinlog(std::move(buf.data()), sql_len, true)); futures.push_back(logFileManager->ConvertFlatBufferToBinlog(std::move(buf.data()), sql_len, false)); } for (auto& future : futures) { RC result = future.get(); if (result != RC::SUCCESS) { LOG_ERROR("Transform task failed"); } } // 4. 查询转换进度 long scn = 0; long seq = 0; std::string ckp = ""; GetLastScnAndSeq(scn, seq, ckp); // 5. 程序退出,自动析构所有资源对象 ``` # Refs - Some helper wheels in include/common are refer from: https://github.com/oceanbase/miniob/blob/main/src/common/