Search in sources :

Example 1 with FileSnap

use of org.apache.zookeeper.server.persistence.FileSnap in project zookeeper by apache.

the class SnapshotFormatter method run.

public void run(String snapshotFileName) throws IOException {
    InputStream is = new CheckedInputStream(new BufferedInputStream(new FileInputStream(snapshotFileName)), new Adler32());
    InputArchive ia = BinaryInputArchive.getArchive(is);
    FileSnap fileSnap = new FileSnap(null);
    DataTree dataTree = new DataTree();
    Map<Long, Integer> sessions = new HashMap<Long, Integer>();
    fileSnap.deserialize(dataTree, sessions, ia);
    printDetails(dataTree, sessions);
}
Also used : FileSnap(org.apache.zookeeper.server.persistence.FileSnap) BufferedInputStream(java.io.BufferedInputStream) HashMap(java.util.HashMap) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32)

Example 2 with FileSnap

use of org.apache.zookeeper.server.persistence.FileSnap in project zookeeper by apache.

the class CRCTest method testChecksums.

/** test checksums for the logs and snapshots.
     * the reader should fail on reading
     * a corrupt snapshot and a corrupt log
     * file
     * @throws Exception
     */
@Test
public void testChecksums() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(150);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    LOG.info("starting up the zookeeper server .. waiting");
    Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        for (int i = 0; i < 2000; i++) {
            zk.create("/crctest- " + i, ("/crctest- " + i).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    zks.shutdown();
    Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
    File versionDir = new File(tmpDir, "version-2");
    File[] list = versionDir.listFiles();
    //there should be only two files
    // one the snapshot and the other logFile
    File snapFile = null;
    File logFile = null;
    for (File file : list) {
        LOG.info("file is " + file);
        if (file.getName().startsWith("log")) {
            logFile = file;
            corruptFile(logFile);
        }
    }
    FileTxnLog flog = new FileTxnLog(versionDir);
    TxnIterator itr = flog.read(1);
    //we will get a checksum failure
    try {
        while (itr.next()) {
        }
        Assert.assertTrue(false);
    } catch (IOException ie) {
        LOG.info("crc corruption", ie);
    }
    itr.close();
    // find the last snapshot
    FileSnap snap = new FileSnap(versionDir);
    List<File> snapFiles = snap.findNRecentSnapshots(2);
    snapFile = snapFiles.get(0);
    corruptFile(snapFile);
    boolean cfile = false;
    try {
        cfile = getCheckSum(snap, snapFile);
    } catch (IOException ie) {
        //the last snapshot seems incompelte
        // corrupt the last but one
        // and use that
        snapFile = snapFiles.get(1);
        corruptFile(snapFile);
        cfile = getCheckSum(snap, snapFile);
    }
    Assert.assertTrue(cfile);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) FileSnap(org.apache.zookeeper.server.persistence.FileSnap) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) Test(org.junit.Test)

Aggregations

FileSnap (org.apache.zookeeper.server.persistence.FileSnap)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 Adler32 (java.util.zip.Adler32)1 CheckedInputStream (java.util.zip.CheckedInputStream)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 InputArchive (org.apache.jute.InputArchive)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 FileTxnLog (org.apache.zookeeper.server.persistence.FileTxnLog)1 TxnIterator (org.apache.zookeeper.server.persistence.TxnLog.TxnIterator)1 Test (org.junit.Test)1