redis源码(八)redis-check-aof.c
/*
* Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/ #include "fmacros.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "config.h" #define ERROR(...) { \
char __buf[]; \
sprintf(__buf, __VA_ARGS__); \
sprintf(error, "0x%16llx: %s", (long long)epos, __buf); \
} static char error[];
static off_t epos; int consumeNewline(char *buf) {
if (strncmp(buf,"\r\n",) != ) {
ERROR("Expected \\r\\n, got: %02x%02x",buf[],buf[]);
return ;
}
return ;
} int readLong(FILE *fp, char prefix, long *target) {
char buf[], *eptr;
epos = ftello(fp);
if (fgets(buf,sizeof(buf),fp) == NULL) {
return ;
}
if (buf[] != prefix) {
ERROR("Expected prefix '%c', got: '%c'",buf[],prefix);
return ;
}
*target = strtol(buf+,&eptr,);
return consumeNewline(eptr);
} int readBytes(FILE *fp, char *target, long length) {
long real;
epos = ftello(fp);
real = fread(target,,length,fp);
if (real != length) {
ERROR("Expected to read %ld bytes, got %ld bytes",length,real);
return ;
}
return ;
} int readString(FILE *fp, char** target) {
long len;
*target = NULL;
if (!readLong(fp,'$',&len)) {
return ;
} /* Increase length to also consume \r\n */
len += ;
*target = (char*)malloc(len);
if (!readBytes(fp,*target,len)) {
return ;
}
if (!consumeNewline(*target+len-)) {
return ;
}
(*target)[len-] = '\0';
return ;
} int readArgc(FILE *fp, long *target) {
return readLong(fp,'*',target);
} off_t process(FILE *fp) {
long argc;
off_t pos = ;
int i, multi = ;
char *str; while() {
if (!multi) pos = ftello(fp);
if (!readArgc(fp, &argc)) break; for (i = ; i < argc; i++) {
if (!readString(fp,&str)) break;
if (i == ) {
if (strcasecmp(str, "multi") == ) {
if (multi++) {
ERROR("Unexpected MULTI");
break;
}
} else if (strcasecmp(str, "exec") == ) {
if (--multi) {
ERROR("Unexpected EXEC");
break;
}
}
}
free(str);
} /* Stop if the loop did not finish */
if (i < argc) {
if (str) free(str);
break;
}
} if (feof(fp) && multi && strlen(error) == ) {
ERROR("Reached EOF before reading EXEC for MULTI");
}
if (strlen(error) > ) {
printf("%s\n", error);
}
return pos;
} int main(int argc, char **argv) {
char *filename;
int fix = ; if (argc < ) {
printf("Usage: %s [--fix] <file.aof>\n", argv[]);
exit();
} else if (argc == ) {
filename = argv[];
} else if (argc == ) {
if (strcmp(argv[],"--fix") != ) {
printf("Invalid argument: %s\n", argv[]);
exit();
}
filename = argv[];
fix = ;
} else {
printf("Invalid arguments\n");
exit();
} FILE *fp = fopen(filename,"r+");
if (fp == NULL) {
printf("Cannot open file: %s\n", filename);
exit();
} struct redis_stat sb;
if (redis_fstat(fileno(fp),&sb) == -) {
printf("Cannot stat file: %s\n", filename);
exit();
} off_t size = sb.st_size;
if (size == ) {
printf("Empty file: %s\n", filename);
exit();
} off_t pos = process(fp);
off_t diff = size-pos;
printf("AOF analyzed: size=%lld, ok_up_to=%lld, diff=%lld\n",
(long long) size, (long long) pos, (long long) diff);
if (diff > ) {
if (fix) {
char buf[];
printf("This will shrink the AOF from %lld bytes, with %lld bytes, to %lld bytes\n",(long long)size,(long long)diff,(long long)pos);
printf("Continue? [y/N]: ");
if (fgets(buf,sizeof(buf),stdin) == NULL ||
strncasecmp(buf,"y",) != ) {
printf("Aborting...\n");
exit();
}
if (ftruncate(fileno(fp), pos) == -) {
printf("Failed to truncate AOF\n");
exit();
} else {
printf("Successfully truncated AOF\n");
}
} else {
printf("AOF is not valid\n");
exit();
}
} else {
printf("AOF is valid\n");
} fclose(fp);
return ;
}
1、如何使用?这里有什么意义,为什么需要这样使用?
redis源码(八)redis-check-aof.c的更多相关文章
- Redis 源码简洁剖析 15 - AOF
AOF 是什么 AOF 持久化的实现 命令追加 AOF 文件的写入和同步 AOF 文件的载入和数据还原 AOF 重写 为什么需要重写 什么是重写 如何重写 AOF 后台重写 为什么需要后台重写 带来的 ...
- Redis源码研究--redis.h
------------7月3日------------ /* The redisOp structure defines a Redis Operation, that is an instance ...
- Redis源码剖析--源码结构解析
请持续关注我的个人博客:https://zcheng.ren 找工作那会儿,看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识.在面试过程中,redis确实 ...
- Redis源码漂流记(二)-搭建Redis调试环境
Redis源码漂流记(二)-搭建Redis调试环境 一.目标 搭建Redis调试环境 简要理解Redis命令运转流程 二.前提 1.有一些c知识简单基础(变量命名.常用数据类型.指针等) 可以参考这篇 ...
- 玩一把redis源码(一):为redis添加自己的列表类型
2019年第一篇文档,为2019年做个良好的开端,本文档通过step by step的方式向读者展示如何为redis添加一个数据类型,阅读本文档后读者对redis源码的执行逻辑会有比较清晰的认识,并且 ...
- redis源码(一):为redis添加自己的列表类型
本文档分为三大部分: 环境介绍与效果演示 redis接收命令到返回数据的执行逻辑 代码实现 文档的重点和难点在第三部分,完全阅读本文档需要读者具备基本的c语言和数据结构知识. 环境介绍和效果演示环境介 ...
- Redis源码阅读(二)高可用设计——复制
Redis源码阅读(二)高可用设计-复制 复制的概念:Redis的复制简单理解就是一个Redis服务器从另一台Redis服务器复制所有的Redis数据库数据,能保持两台Redis服务器的数据库数据一致 ...
- Redis源码剖析
Redis源码剖析和注释(一)---链表结构 Redis源码剖析和注释(二)--- 简单动态字符串 Redis源码剖析和注释(三)--- Redis 字典结构 Redis源码剖析和注释(四)--- 跳 ...
- Redis源码分析:serverCron - redis源码笔记
[redis源码分析]http://blog.csdn.net/column/details/redis-source.html Redis源代码重要目录 dict.c:也是很重要的两个文件,主要 ...
- Redis源码解析:15Resis主从复制之从节点流程
Redis的主从复制功能,可以实现Redis实例的高可用,避免单个Redis 服务器的单点故障,并且可以实现负载均衡. 一:主从复制过程 Redis的复制功能分为同步(sync)和命令传播(comma ...
随机推荐
- 轻量级RPC设计与实现第三版
在前两个版本中,每次发起请求一次就新建一个netty的channel连接,如果在高并发情况下就会造成资源的浪费,这时实现异步请求就十分重要,当有多个请求线程时,需要设计一个线程池来进行管理.除此之外, ...
- 手动部署:在eclipse导入web项目并更新包到本地部署
一.eclipse导入java web项目 1.file-import-git-next-clone URL-填写git上面的URL,然后一直next,完成后等待即可 二.导入多个版本项目 1.fil ...
- P1268 树的重量【构造】
题目描述 树可以用来表示物种之间的进化关系.一棵“进化树”是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之间的距离,重构相应的“进化树 ...
- Centos7 修改/etc/profile错误后导致所有命令“not found”
因为Centos7中运行着两个版本的php,今天在设置环境变量时导致所有命令都 "not found". 修复方式: 第一:执行 /bin/vi /etc/profile 把文件修 ...
- 牛客练习赛53 C题bitset
题目链接https://ac.nowcoder.com/acm/contest/1114/C #include<bits/stdc++.h> using namespace std; #d ...
- BZOJ 1218: [HNOI2003]激光炸弹(二维前缀和)
Description 一种新型的激光炸弹,可以摧毁一个边长为R的正方形内的所有的目标.现在地图上有n(N<=10000)个目标,用整数Xi,Yi(其值在[0,5000])表示目标在地图上的位置 ...
- HTTPClient模拟Get和Post请求
一.模拟Get请求(无参) 首先导入HttpClient依赖 <dependency> <groupId>org.apache.httpcomponents</group ...
- Maven2: Missing artifact but jars are in place
那是因为没有update project. 项目右键,maven-update project.
- Virtual DOM(八)
Virtual DOM 这个概念相信大部分人都不会陌生,它产生的前提是浏览器中的 DOM 是很“昂贵"的,为了更直观的感受,我们可以简单的把一个简单的 div 元素的属性都打印出来,如图所示 ...
- IntelliJ IDEA 2019.1.3 最新破解教程【最强 可用至2099年】
本文包括最新[2019.1.2]安装 和[2018.3.2](推荐)安装 ①IntelliJ IDEA 2018.3.2安装永久安装[最强] 一. 在官网下载IDEA安装包 链接:https:// ...