|
@@ -28,38 +28,55 @@ import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
+import org.apache.log4j.Logger;
|
|
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
|
|
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
|
|
import org.apache.zookeeper.server.persistence.Util;
|
|
import org.apache.zookeeper.server.persistence.Util;
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * this class is used to clean up the
|
|
|
|
+ * snapshot and data log dir's. This is usually
|
|
|
|
+ * run as a cronjob on the zookeeper server machine.
|
|
|
|
+ * Invocation of this class will clean up the datalogdir
|
|
|
|
+ * files and snapdir files keeping the last "-n" snapshot files
|
|
|
|
+ * and the corresponding logs.
|
|
|
|
+ */
|
|
public class PurgeTxnLog {
|
|
public class PurgeTxnLog {
|
|
|
|
+ private static final Logger LOG = Logger.getLogger(PurgeTxnLog.class);
|
|
|
|
|
|
static void printUsage(){
|
|
static void printUsage(){
|
|
- System.out.println("PurgeTxnLog dataLogDir [snapDir]");
|
|
|
|
|
|
+ System.out.println("PurgeTxnLog dataLogDir [snapDir] -n count");
|
|
System.out.println("\tdataLogDir -- path to the txn log directory");
|
|
System.out.println("\tdataLogDir -- path to the txn log directory");
|
|
System.out.println("\tsnapDir -- path to the snapshot 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);
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * @param args PurgeTxnLog dataLogDir
|
|
|
|
- * dataLogDir -- txn log directory
|
|
|
|
|
|
+ * purges the snapshot and logs keeping the last num snapshots
|
|
|
|
+ * and the corresponding logs.
|
|
|
|
+ * @param dataDir the dir that has the logs
|
|
|
|
+ * @param snapDir the dir that has the snapshots
|
|
|
|
+ * @param num the number of snapshots to keep
|
|
|
|
+ * @throws IOException
|
|
*/
|
|
*/
|
|
- public static void main(String[] args) throws IOException {
|
|
|
|
- if(args.length<1 || args.length>2)
|
|
|
|
- printUsage();
|
|
|
|
|
|
+ public static void purge(File dataDir, File snapDir, int num) throws IOException {
|
|
|
|
+ if (num < 3) {
|
|
|
|
+ throw new IllegalArgumentException("count should be greater than 3");
|
|
|
|
+ }
|
|
|
|
|
|
- File dataDir=new File(args[0]);
|
|
|
|
- File snapDir=dataDir;
|
|
|
|
- if(args.length==2){
|
|
|
|
- snapDir=new File(args[1]);
|
|
|
|
- }
|
|
|
|
FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir);
|
|
FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir);
|
|
|
|
|
|
// found any valid recent snapshots?
|
|
// found any valid recent snapshots?
|
|
|
|
|
|
// files to exclude from deletion
|
|
// files to exclude from deletion
|
|
Set<File> exc=new HashSet<File>();
|
|
Set<File> exc=new HashSet<File>();
|
|
- File snapShot = txnLog.findMostRecentSnapshot();
|
|
|
|
- exc.add(txnLog.findMostRecentSnapshot());
|
|
|
|
|
|
+ List<File> snaps = txnLog.findNRecentSnapshots(num);
|
|
|
|
+ if (snaps.size() == 0)
|
|
|
|
+ return;
|
|
|
|
+ File snapShot = snaps.get(snaps.size() -1);
|
|
|
|
+ for (File f: snaps) {
|
|
|
|
+ exc.add(f);
|
|
|
|
+ }
|
|
long zxid = Util.getZxidFromName(snapShot.getName(),"snapshot");
|
|
long zxid = Util.getZxidFromName(snapShot.getName(),"snapshot");
|
|
exc.addAll(Arrays.asList(txnLog.getSnapshotLogs(zxid)));
|
|
exc.addAll(Arrays.asList(txnLog.getSnapshotLogs(zxid)));
|
|
|
|
|
|
@@ -77,9 +94,9 @@ public class PurgeTxnLog {
|
|
}
|
|
}
|
|
// add all non-excluded log files
|
|
// add all non-excluded log files
|
|
List<File> files=new ArrayList<File>(
|
|
List<File> files=new ArrayList<File>(
|
|
- Arrays.asList(dataDir.listFiles(new MyFileFilter("log."))));
|
|
|
|
|
|
+ Arrays.asList(txnLog.getDataDir().listFiles(new MyFileFilter("log."))));
|
|
// add all non-excluded snapshot files to the deletion list
|
|
// add all non-excluded snapshot files to the deletion list
|
|
- files.addAll(Arrays.asList(snapDir.listFiles(new MyFileFilter("snapshot."))));
|
|
|
|
|
|
+ files.addAll(Arrays.asList(txnLog.getSnapDir().listFiles(new MyFileFilter("snapshot."))));
|
|
// remove the old files
|
|
// remove the old files
|
|
for(File f: files)
|
|
for(File f: files)
|
|
{
|
|
{
|
|
@@ -90,5 +107,26 @@ public class PurgeTxnLog {
|
|
System.err.println("Failed to remove "+f.getPath());
|
|
System.err.println("Failed to remove "+f.getPath());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param args PurgeTxnLog dataLogDir
|
|
|
|
+ * dataLogDir -- txn log directory
|
|
|
|
+ * -n num (number of snapshots to keep)
|
|
|
|
+ */
|
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
|
+ if(args.length<3 || args.length>4)
|
|
|
|
+ printUsage();
|
|
|
|
+ int i = 0;
|
|
|
|
+ File dataDir=new File(args[0]);
|
|
|
|
+ File snapDir=dataDir;
|
|
|
|
+ if(args.length==4){
|
|
|
|
+ i++;
|
|
|
|
+ snapDir=new File(args[i]);
|
|
|
|
+ }
|
|
|
|
+ i++; i++;
|
|
|
|
+ int num = Integer.parseInt(args[i]);
|
|
|
|
+ purge(dataDir, snapDir, num);
|
|
}
|
|
}
|
|
}
|
|
}
|