高可用,高性能,线程安全,自动数据恢复 redo log 类

在金融,电商,等高可用环境下,不允许有任何数据丢失的情况,redo log 是一个不错的低成本高可用方案,其作用有点类似mysql binlog, 下面在生产环境中事件和优化的多线程安全,自己恢复的 c++ redo log 类,在实际生产过程遇到相应问题,很多地方进行优化设计,尽量保持高性能和高可靠性。

/******************************************************
function: redo log is use for service restart, sys crash
		  and Exception to recover the data.
author: liuyi
date:2016.08.26
version:1.0
******************************************************/

#ifndef REDO_LOG_H
#define REDO_LOG_H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <pthread.h>
#include <vector>
#include <map>
#include <set>

using namespace std;

struct redo_file_pair_info
{
	int file_max_lines;
	int file_input_lines;
	int file_output_lines;
	FILE *output_log_ptr;
};

struct reload_data
{
	string file_prefix;
	string uid;
	string log_str;
};

class redo_log
{
	public:
		redo_log()
		{
			m_log_version = 0;
			m_uuid = 0;
			m_input_log_lines = 0;
			m_max_log_lines = 100000;
		 	m_rm_finish_log_time = 5;
			m_mutex = new pthread_mutex_t;
			pthread_mutex_init(m_mutex, NULL);

			sleep(1);//insure every start the log file name is not same 
			m_start_time = time(NULL);
			char tmp[64] = {0};
			sprintf(tmp, "%lu_%lld", m_start_time, m_log_version);
			m_current_input_prefix = string(tmp);
		}

		~redo_log()
		{
			if(m_mutex != NULL)
			{
				pthread_mutex_destroy(m_mutex);
				delete m_mutex;
				m_mutex = NULL;
			}
		}

		bool init(const string& redo_log_path, int max_log_lines, int rm_finish_log_time = 5)
		{
			m_path = redo_log_path;
			m_max_log_lines = max_log_lines;
		 	m_rm_finish_log_time = rm_finish_log_time;
			char file_name[1024] = {0};
			snprintf(file_name, 1023, "%s/%s.input", m_path.c_str(), m_current_input_prefix.c_str());
			m_input_log = fopen(file_name, "w");
			memset(file_name, '\0', 1024);
			snprintf(file_name, 1023, "%s/%s.output", m_path.c_str(), m_current_input_prefix.c_str());
			m_output_log = fopen(file_name, "w");

			redo_file_pair_info new_file;
			new_file.file_max_lines = m_max_log_lines;
			new_file.file_input_lines = 0;
			new_file.file_output_lines = 0;
			new_file.output_log_ptr = m_output_log;
			m_file_pair_info_map.insert(pair<string, redo_file_pair_info>(m_current_input_prefix, new_file));
			++m_log_versi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值