@Override public void run() { LOG.info("Purge task started."); try { PurgeTxnLog.purge(logsDir, snapsDir, snapRetainCount); } catch (Exception e) { LOG.error("Error occurred while purging.", e); } LOG.info("Purge task completed."); } }
private static void printUsageThenExit() { printUsage(); System.exit(1); } }
/** * Purges the snapshot and logs keeping the last num snapshots and the * corresponding logs. If logs are rolling or a new snapshot is created * during this process, these newest N snapshots or any data logs will be * excluded from current purging cycle. * * @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 purge(File dataDir, File snapDir, int num) throws IOException { if (num < 3) { throw new IllegalArgumentException(COUNT_ERR_MSG); } FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir); List<File> snaps = txnLog.findNRecentSnapshots(num); int numSnaps = snaps.size(); if (numSnaps > 0) { purgeOlderSnapshots(txnLog, snaps.get(numSnaps - 1)); } }
/** * @param args dataLogDir [snapDir] -n count * dataLogDir -- path to the txn log directory * snapDir -- path to the snapshot directory * count -- the number of old snaps/logs you want to keep, value should be greater than or equal to 3<br> */ public static void main(String[] args) throws IOException { if (args.length < 3 || args.length > 4) { printUsageThenExit(); } File dataDir = validateAndGetFile(args[0]); File snapDir = dataDir; int num = -1; String countOption = ""; if (args.length == 3) { countOption = args[1]; num = validateAndGetCount(args[2]); } else { snapDir = validateAndGetFile(args[1]); countOption = args[2]; num = validateAndGetCount(args[3]); } if (!"-n".equals(countOption)) { printUsageThenExit(); } purge(dataDir, snapDir, num); }
/** * @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); } }
/** * Returns integer if parsed successfully and it is valid otherwise prints * error and usage and then exits * * @param number * @return count */ private static int validateAndGetCount(String number) { int result = 0; try { result = Integer.parseInt(number); if (result < 3) { System.err.println(COUNT_ERR_MSG); printUsageThenExit(); } } catch (NumberFormatException e) { System.err .println("'" + number + "' can not be parsed to integer."); printUsageThenExit(); } return result; }
/** * @param args dataLogDir [snapDir] -n count * dataLogDir -- path to the txn log directory * snapDir -- path to the snapshot directory * count -- the number of old snaps/logs you want to keep, value should be greater than or equal to 3<br> */ public static void main(String[] args) throws IOException { if (args.length < 3 || args.length > 4) { printUsageThenExit(); } File dataDir = validateAndGetFile(args[0]); File snapDir = dataDir; int num = -1; String countOption = ""; if (args.length == 3) { countOption = args[1]; num = validateAndGetCount(args[2]); } else { snapDir = validateAndGetFile(args[1]); countOption = args[2]; num = validateAndGetCount(args[3]); } if (!"-n".equals(countOption)) { printUsageThenExit(); } purge(dataDir, snapDir, num); }
/** * validates file existence and returns the file * * @param path * @return File */ private static File validateAndGetFile(String path) { File file = new File(path); if (!file.exists()) { System.err.println("Path '" + file.getAbsolutePath() + "' does not exist. "); printUsageThenExit(); } return file; }
@Override public void run() { LOG.info("Purge task started."); try { PurgeTxnLog.purge(new File(logsDir), new File(snapsDir), snapRetainCount); } catch (Exception e) { LOG.error("Error occurred while purging.", e); } LOG.info("Purge task completed."); } }
/** * Purges the snapshot and logs keeping the last num snapshots and the * corresponding logs. If logs are rolling or a new snapshot is created * during this process, these newest N snapshots or any data logs will be * excluded from current purging cycle. * * @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 purge(File dataDir, File snapDir, int num) throws IOException { if (num < 3) { throw new IllegalArgumentException(COUNT_ERR_MSG); } FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir); List<File> snaps = txnLog.findNRecentSnapshots(num); int numSnaps = snaps.size(); if (numSnaps > 0) { purgeOlderSnapshots(txnLog, snaps.get(numSnaps - 1)); } }
private static void printUsageThenExit() { printUsage(); System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); } }
/** * validates file existence and returns the file * * @param path * @return File */ private static File validateAndGetFile(String path) { File file = new File(path); if (!file.exists()) { System.err.println("Path '" + file.getAbsolutePath() + "' does not exist. "); printUsageThenExit(); } return file; }
public void run() { try { doPurge.await(OP_TIMEOUT_IN_MILLIS / 2, TimeUnit.MILLISECONDS); PurgeTxnLog.purge(tmpDir, tmpDir, 3); } catch (IOException ioe) { LOG.error("Exception when purge", ioe); opFailed.set(true); } catch (InterruptedException ie) { LOG.error("Exception when purge", ie); opFailed.set(true); } finally { purgeFinished.countDown(); } }; }.start();
public void internalTestSnapFilesEqualsToRetain(boolean testWithPrecedingLogFile) throws Exception { int nRecentCount = 3; AtomicInteger offset = new AtomicInteger(0); tmpDir = ClientBase.createTmpDir(); File version2 = new File(tmpDir.toString(), "version-2"); Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(), version2.mkdir()); List<File> snaps = new ArrayList<File>(); List<File> logs = new ArrayList<File>(); createDataDirFiles(offset, nRecentCount, testWithPrecedingLogFile, version2, snaps, logs); FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir); PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1)); txnLog.close(); verifyFilesAfterPurge(snaps, true); verifyFilesAfterPurge(logs, true); }
/** * Returns integer if parsed successfully and it is valid otherwise prints * error and usage and then exits * * @param number * @return count */ private static int validateAndGetCount(String number) { int result = 0; try { result = Integer.parseInt(number); if (result < 3) { System.err.println(COUNT_ERR_MSG); printUsageThenExit(); } } catch (NumberFormatException e) { System.err .println("'" + number + "' can not be parsed to integer."); printUsageThenExit(); } return result; }
ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); PurgeTxnLog.purge(tmpDir, tmpDir, 3); FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir); List<File> listLogs = snaplog.findNRecentSnapshots(4);
PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1)); txnLog.close(); verifyFilesAfterPurge(snapsToPurge, false);
PurgeTxnLog.purge(tmpDir, tmpDir, SNAP_RETAIN_COUNT);
PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1)); txnLog.close(); verifyFilesAfterPurge(snapsToPurge, false);
private void purgeTxnLogs() { logger.info("Purging old ZooKeeper snapshots and logs .."); try { PurgeTxnLog.purge(purgeManager.getDataLogDir(), purgeManager.getSnapDir(), purgeManager.getSnapRetainCount()); logger.info("Purged old ZooKeeper snapshots and logs."); } catch (IOException e) { logger.error("Failed to purge old ZooKeeper snapshots and logs:", e); } } }