/** * find the last n snapshots. this does not have * any checks if the snapshot might be valid or not * @param n the number of most recent snapshots * @return the last n snapshots * @throws IOException */ public List<File> findNRecentSnapshots(int n) throws IOException { List<File> files = Util.sortDataDir(snapDir.listFiles(), SNAPSHOT_FILE_PREFIX, false); int count = 0; List<File> list = new ArrayList<File>(); for (File f: files) { if (count == n) break; if (Util.getZxidFromName(f.getName(), SNAPSHOT_FILE_PREFIX) != -1) { count++; list.add(f); } } return list; }
/** * find the most recent snapshot * in the snapshot directory * @return * @throws IOException */ private File findMostRecentSnapshot() throws IOException { List<File> files = Util.sortDataDir(snapShotDir.listFiles(), "snapshot", false); for (File f: files) { try { if (Util.isValidSnapshot(f)) return f; } catch(IOException e) { LOG.info("Invalid snapshot " + f, e); } } return null; }
/** * save the datatree and the sessions into a snapshot * @param dataTree the datatree to be serialized onto disk * @param sessionsWithTimeouts the sesssion timeouts to be * serialized onto disk * @throws IOException */ public void save(DataTree dataTree, ConcurrentHashMap<Long, Integer> sessionsWithTimeouts) throws IOException { long lastZxid = dataTree.lastProcessedZxid; File snapshotFile = new File(snapDir, Util.makeSnapshotName(lastZxid)); LOG.info("Snapshotting: 0x{} to {}", Long.toHexString(lastZxid), snapshotFile); snapLog.serialize(dataTree, sessionsWithTimeouts, snapshotFile); }
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; } }
LOG.info("Creating new log file: " + Util.makeLogName(hdr.getZxid())); logFileWrite = new File(logDir, Util.makeLogName(hdr.getZxid())); fos = new FileOutputStream(logFileWrite); logStream=new BufferedOutputStream(fos); byte[] buf = Util.marshallTxnEntry(hdr, txn); if (buf == null || buf.length == 0) { throw new IOException("Faulty serialization for header " + crc.update(buf, 0, buf.length); oa.writeLong(crc.getValue(), "txnEntryCRC"); Util.writeTxnBytes(oa, buf);
/** * Given two directory files the method returns a well-formed * logfile provider URI. This method is for backward compatibility with the * existing code that only supports logfile persistence and expects these two * parameters passed either on the command-line or in the configuration file. * * @param dataDir snapshot directory * @param dataLogDir transaction log directory * @return logfile provider URI */ public static URI makeFileLoggerURL(File dataDir, File dataLogDir){ return URI.create(makeURIString(dataDir.getPath(),dataLogDir.getPath(),null)); }
@Test public void testSortDataDirAscending() { File[] files = new File[5]; files[0] = new File("foo.10027c6de"); files[1] = new File("foo.10027c6df"); files[2] = new File("bar.10027c6dd"); files[3] = new File("foo.10027c6dc"); files[4] = new File("foo.20027c6dc"); File[] orig = files.clone(); List<File> filelist = Util.sortDataDir(files, "foo", true); Assert.assertEquals(orig[2], filelist.get(0)); Assert.assertEquals(orig[3], filelist.get(1)); Assert.assertEquals(orig[0], filelist.get(2)); Assert.assertEquals(orig[1], filelist.get(3)); Assert.assertEquals(orig[4], filelist.get(4)); }
byte[] buf = Util.marshallTxnEntry(hdr, txn); if (buf == null || buf.length == 0) { throw new IOException("Faulty serialization for header " + crc.update(buf, 0, buf.length); oa.writeLong(crc.getValue(), "txnEntryCRC"); Util.writeTxnBytes(oa, buf);
byte[] bytes = Util.readTxnBytes(ia);
@Test public void testInvalidSnapshot() { File f = null; File tmpFileDir = null; try { tmpFileDir = ClientBase.createTmpDir(); f = new File(tmpFileDir, "snapshot.0"); if (!f.exists()) { f.createNewFile(); } Assert.assertFalse("Snapshot file size is greater than 9 bytes", Util.isValidSnapshot(f)); Assert.assertTrue("Can't delete file", f.delete()); } catch (IOException e) { } finally { if (null != tmpFileDir) { ClientBase.recursiveDelete(tmpFileDir); } } } }
private void createLogFile(File dir, long zxid) throws IOException { File file = new File(dir.getPath() + File.separator + Util.makeLogName(zxid)); file.createNewFile(); }
@Override public boolean accept(File dir, String name) { return Util.isLogFileName(name); } });
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; } }
LOG.info("Creating new log file: " + Util.makeLogName(hdr.getZxid())); logFileWrite = new File(logDir, Util.makeLogName(hdr.getZxid())); fos = new FileOutputStream(logFileWrite); logStream=new BufferedOutputStream(fos); byte[] buf = Util.marshallTxnEntry(hdr, txn); if (buf == null || buf.length == 0) { throw new IOException("Faulty serialization for header " + crc.update(buf, 0, buf.length); oa.writeLong(crc.getValue(), "txnEntryCRC"); Util.writeTxnBytes(oa, buf);
public static URI makeFileLoggerURL(File dataDir, File dataLogDir,String convPolicy){ return URI.create(makeURIString(dataDir.getPath(),dataLogDir.getPath(),convPolicy)); }
@Test public void testSortDataDirDescending() { File[] files = new File[5]; files[0] = new File("foo.10027c6de"); files[1] = new File("foo.10027c6df"); files[2] = new File("bar.10027c6dd"); files[3] = new File("foo.10027c6dc"); files[4] = new File("foo.20027c6dc"); File[] orig = files.clone(); List<File> filelist = Util.sortDataDir(files, "foo", false); Assert.assertEquals(orig[4], filelist.get(0)); Assert.assertEquals(orig[1], filelist.get(1)); Assert.assertEquals(orig[0], filelist.get(2)); Assert.assertEquals(orig[3], filelist.get(3)); Assert.assertEquals(orig[2], filelist.get(4)); }
byte[] bytes = Util.readTxnBytes(ia);
@Override public boolean accept(File dir, String name) { return Util.isLogFileName(name); } });
List<File> files = Util.sortDataDir(logDirList, LOG_FILE_PREFIX, true); long logZxid = 0; long fzxid = Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX); if (fzxid > snapshotZxid) { break; long fzxid = Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX); if (fzxid < logZxid) { continue;
List<File> files = Util.sortDataDir(snapDir.listFiles(), SNAPSHOT_FILE_PREFIX, false); int count = 0; List<File> list = new ArrayList<File>(); if (Util.isValidSnapshot(f)) { list.add(f); count++;