转载请注明源地址http://www.cnblogs.com/dongxiao-yang/p/4910059.html

zookeeper具有自动清除快照日志和事务日志的工能,可以在配置文件设置autopurge.purgeInterval来实现,问题是这个属性的时间单位是小时,

有些情况下,一小时的日志过大(比如把事务日志放到内存),需要手动删除,所以需要研究下zk删除日志文件的源码。

清理日志主类:org.apache.zookeeper.server.PurgeTxnLog,包含如下几个静态工具方法

static void printUsage(){

System.out.println("PurgeTxnLog dataLogDir [snapDir] -n count");

System.out.println("\tdataLogDir -- path to the txn log directory");

System.out.println("\tsnapDir -- path to the snapshot directory");

System.out.println("\tcount -- the number of old snaps/logs you want to keep");

System.exit(1);

}

常见的帮助方法,告诉使用者参数的传入顺序,其中snapdir参数为可选,假如两种日志配置在同一个路径下,只传一个路径参数就好。

main方法,没什么好说的,只是解析参数。

public static void purge(File dataDir, File snapDir, int num) throws IOException {

if (num < 3) {

throw new IllegalArgumentException("count should be greater than 3");

}

FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir);

List<File> snaps = txnLog.findNRecentSnapshots(num);

retainNRecentSnapshots(txnLog, snaps);

}

删除文件的主方法,主要分两个部分

1:txnLog.findNRecentSnapshots(num);

找到需要保留的文件

主要逻辑代码为

public List<File> findNRecentSnapshots(int n) throws IOException {

List<File> files = Util.sortDataDir(snapDir.listFiles(), "snapshot", false);

int i = 0;

List<File> list = new ArrayList<File>();

for (File f: files) {

if (i==n)

break;

i++;

list.add(f);

}

return list;

}

private static class DataDirFileComparator

implements Comparator<File>, Serializable

{

private static final long serialVersionUID = -2648639884525140318L;

private String prefix;

private boolean ascending;

public DataDirFileComparator(String prefix, boolean ascending) {

this.prefix = prefix;

this.ascending = ascending;

}

public int compare(File o1, File o2) {

long z1 = Util.getZxidFromName(o1.getName(), prefix);

long z2 = Util.getZxidFromName(o2.getName(), prefix);

int result = z1 < z2 ? -1 : (z1 > z2 ? 1 : 0);

return ascending ? result : -result;

}

}

/**

* Sort the list of files. Recency as determined by the version component

* of the file name.

*

* @param files array of files

* @param prefix files not matching this prefix are assumed to have a

* version = -1)

* @param ascending true sorted in ascending order, false results in

* descending order

* @return sorted input files

*/

public static List<File> sortDataDir(File[] files, String prefix, boolean ascending)

{

if(files==null)

return new ArrayList<File>(0);

List<File> filelist = Arrays.asList(files);

Collections.sort(filelist, new DataDirFileComparator(prefix, ascending));

return filelist;

}

2 删除文件

// VisibleForTesting

static void retainNRecentSnapshots(FileTxnSnapLog txnLog, List<File> snaps) {

// found any valid recent snapshots?

if (snaps.size() == 0)

return;

File snapShot = snaps.get(snaps.size() -1);

int ii=snaps.size() -1;

System.out.println(ii);

final long leastZxidToBeRetain = Util.getZxidFromName(

snapShot.getName(), PREFIX_SNAPSHOT);

class MyFileFilter implements FileFilter{

private final String prefix;

MyFileFilter(String prefix){

this.prefix=prefix;

}

public boolean accept(File f){

if(!f.getName().startsWith(prefix + "."))

return false;

long fZxid = Util.getZxidFromName(f.getName(), prefix);

if (fZxid >= leastZxidToBeRetain) {

return false;

}

return true;

}

}

// add all non-excluded log files

List<File> files = new ArrayList<File>(Arrays.asList(txnLog

.getDataDir().listFiles(new MyFileFilter(PREFIX_LOG))));

// add all non-excluded snapshot files to the deletion list

files.addAll(Arrays.asList(txnLog.getSnapDir().listFiles(

new MyFileFilter(PREFIX_SNAPSHOT))));

// remove the old files

for(File f: files)

{

System.out.println("Removing file: "+

DateFormat.getDateTimeInstance().format(f.lastModified())+

"\t"+f.getPath());

if(!f.delete()){

System.err.println("Failed to remove "+f.getPath());

}

}

}

Util.getZxidFromName工具方法代码

public static long getZxidFromName(String name, String prefix) {

long zxid = -1;

String nameParts[] = name.split("\\.");

if (nameParts.length == 2 && nameParts[0].equals(prefix)) {

try {

zxid = Long.parseLong(nameParts[1], 16);

} catch (NumberFormatException e) {

}

}

return zxid;

}

zookeeper 删除snapshot和transaction log的源码解读的更多相关文章

  1. HttpClient 4.3连接池参数配置及源码解读

    目前所在公司使用HttpClient 4.3.3版本发送Rest请求,调用接口.最近出现了调用查询接口服务慢的生产问题,在排查整个调用链可能存在的问题时(从客户端发起Http请求->ESB-&g ...

  2. go语言nsq源码解读八 http.go、http_server.go

    这篇讲另两个文件http.go.http_server.go,这两个文件和第六讲go语言nsq源码解读六 tcp.go.tcp_server.go里的两个文件是相对应的.那两个文件用于处理tcp请求, ...

  3. ThreadLocal源码解读

    1. 背景 ThreadLocal源码解读,网上面早已经泛滥了,大多比较浅,甚至有的连基本原理都说的很有问题,包括百度搜索出来的第一篇高访问量博文,说ThreadLocal内部有个map,键为线程对象 ...

  4. 从koa-session源码解读session本质

    前言 Session,又称为"会话控制",存储特定用户会话所需的属性及配置信息.存于服务器,在整个用户会话中一直存在. 然而: session 到底是什么? session 是存在 ...

  5. ScheduledThreadPoolExecutor源码解读

    1. 背景 在之前的博文--ThreadPoolExecutor源码解读已经对ThreadPoolExecutor的实现原理与源码进行了分析.ScheduledExecutorService也是我们在 ...

  6. HttpClient4.3 连接池参数配置及源码解读

    目前所在公司使用HttpClient 4.3.3版本发送Rest请求,调用接口.最近出现了调用查询接口服务慢的生产问题,在排查整个调用链可能存在的问题时(从客户端发起Http请求->ESB-&g ...

  7. etcd学习(6)-etcd实现raft源码解读

    etcd中raft实现源码解读 前言 raft实现 看下etcd中的raftexample newRaftNode startRaft serveChannels 领导者选举 启动并初始化node节点 ...

  8. Vue 源码解读(3)—— 响应式原理

    前言 上一篇文章 Vue 源码解读(2)-- Vue 初始化过程 详细讲解了 Vue 的初始化过程,明白了 new Vue(options) 都做了什么,其中关于 数据响应式 的实现用一句话简单的带过 ...

  9. SDWebImage源码解读 之 NSData+ImageContentType

    第一篇 前言 从今天开始,我将开启一段源码解读的旅途了.在这里先暂时不透露具体解读的源码到底是哪些?因为也可能随着解读的进行会更改计划.但能够肯定的是,这一系列之中肯定会有Swift版本的代码. 说说 ...

随机推荐

  1. 解决右滑返回手势和UIScrollView中的手势冲突

    当在一个viewController中添加了scrollView或者tableView的时候,贴边侧滑返回的时候会首先触发滚动而失效,要解决这个问题,需要通过requireGestureRecogni ...

  2. xml有哪些解析技术?区别是什么?

    xml有哪些解析技术?区别是什么? Answer: 有DOM,SAX,STAX等 (1):DOM:处理大型文件时其性能下降的非常厉害.这个问题是由DOM的树结构所造成的,这种结构占用的内存较多,而且D ...

  3. SGU 231.Prime Sum

    题意: 求有多少对质数(a,b)满足a<=b 且a+b也为质数.(a+b<=10^6) Solution: 除了2之外的质数都是奇数,两个奇数的和是偶数,不可能是质数.所以题目就是求差为2 ...

  4. nginx插件ngx_lua

    ngx_lua是淘宝的维护的产品,真心不错.配置文件包含可以做很多事情的lua脚本. 公司有个产品对注册的广告盒子进行反向代理,这样可以在盒子上做很多事情:和服务器通信,远程控制盒子等等.nginx反 ...

  5. 算法的优化(C语言描述)

    算法的优化 算法的优化分为全局优化和局部优化两个层次.全局优化也称为结构优化,主要是从基本控制结构优化.算法.数据结构的选择上考虑:局部优化即为代码优化,包括使用尽量小的数据类型.优化表达式.优化赋值 ...

  6. request.getContextPath获取绝对路径

    request.getContextPath获取绝对路径 博客分类: 经验+注意 其他 request.getContextPath 项目需求:所有jsp页必须通过Action转发,不能直接在地址栏链 ...

  7. Spring Data JPA 多个实体类表联合视图查询

    Spring Data JPA 查询数据库时,如果两个表有关联,那么就设个外键,在查询的时候用Specification创建Join 查询便可.但是只支持左连接,不支持右连接,虽说左右连接反过来就能实 ...

  8. 搞Solr这一年(本人QQ 282335345 群412268049 欢迎大家一起学习Solr 非诚勿扰)

    搞Solr这一年 去年6月份毕业到现在已经快一年半了,很庆幸从事了搜索引擎这份工作,虽然谈不上有多深入,但至少已经入门了.在这一年半里,搞了3个月的hbase和mapreduce,搞了一个月的nutc ...

  9. 转:基于ASP.NET的Comet长连接技术解析

    原文来自于: Comet技术原理 来自维基百科:Comet是一种用于web的技术,能使服务器能实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流. 简单的 ...

  10. 【MySQL】SQL语法,between and 使用注意事项

    业务代码中有条查询学生姓名的sql: select stu_name from stu_info where stu_id between id_1 and id_2; 估计当时一时恍惚,拼接sql时 ...