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);
}
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);
}
Aggregations