use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class EmptiedSnapshotRecoveryTest method runTest.
public void runTest(boolean leaveEmptyFile, boolean trustEmptySnap) throws Exception {
File tmpSnapDir = ClientBase.createTmpDir();
File tmpLogDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000);
SyncRequestProcessor.setSnapCount(SNAP_COUNT);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
try {
for (int i = 0; i < N_TRANSACTIONS; i++) {
zk.create("/node-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} finally {
zk.close();
}
f.shutdown();
zks.shutdown();
assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server to shutdown");
// start server again with intact database
zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000);
zks.startdata();
long zxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
LOG.info("After clean restart, zxid = {}", zxid);
assertTrue(zxid > 0, "zxid > 0");
zks.shutdown();
// Make all snapshots empty
FileTxnSnapLog txnLogFactory = zks.getTxnLogFactory();
List<File> snapshots = txnLogFactory.findNRecentSnapshots(10);
assertTrue(snapshots.size() > 0, "We have a snapshot to corrupt");
for (File file : snapshots) {
if (leaveEmptyFile) {
new PrintWriter(file).close();
} else {
file.delete();
}
}
if (trustEmptySnap) {
System.setProperty(FileTxnSnapLog.ZOOKEEPER_SNAPSHOT_TRUST_EMPTY, "true");
}
// start server again with corrupted database
zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000);
try {
zks.startdata();
long currentZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
if (!trustEmptySnap) {
fail("Should have gotten exception for corrupted database");
}
assertEquals(currentZxid, zxid, "zxid mismatch after restoring database");
} catch (IOException e) {
// expected behavior
if (trustEmptySnap) {
fail("Should not get exception for empty database");
}
} finally {
if (trustEmptySnap) {
System.clearProperty(FileTxnSnapLog.ZOOKEEPER_SNAPSHOT_TRUST_EMPTY);
}
}
zks.shutdown();
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class PurgeTxnTest method internalTestSnapFilesEqualsToRetain.
public void internalTestSnapFilesEqualsToRetain(boolean testWithPrecedingLogFile) throws Exception {
int nRecentCount = 3;
AtomicInteger offset = new AtomicInteger(0);
File version2 = new File(tmpDir.toString(), "version-2");
assertTrue(version2.mkdir(), "Failed to create version_2 dir:" + version2.toString());
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);
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class PurgeTxnTest method testFindNValidSnapshots.
/**
* Tests finding n recent valid snapshots from set of snapshots and data logs
*/
@Test
public void testFindNValidSnapshots() throws Exception {
// n recent snap shots
int nRecentSnap = 4;
int nRecentCount = 30;
int offset = 0;
File version2 = new File(tmpDir.toString(), "version-2");
assertTrue(version2.mkdir(), "Failed to create version_2 dir:" + version2.toString());
// Test that with no snaps, findNValidSnapshots returns empty list
FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
List<File> foundSnaps = txnLog.findNValidSnapshots(1);
assertEquals(0, foundSnaps.size());
List<File> expectedNRecentSnapFiles = new ArrayList<File>();
int counter = offset + (2 * nRecentCount);
for (int i = 0; i < nRecentCount; i++) {
// simulate log file
File logFile = new File(version2 + "/log." + Long.toHexString(--counter));
assertTrue(logFile.createNewFile(), "Failed to create log File:" + logFile.toString());
// simulate snapshot file
File snapFile = new File(version2 + "/snapshot." + Long.toHexString(--counter));
assertTrue(snapFile.createNewFile(), "Failed to create snap File:" + snapFile.toString());
makeValidSnapshot(snapFile);
// add the n recent snap files for assertion
if (i < nRecentSnap) {
expectedNRecentSnapFiles.add(snapFile);
}
}
// Test that when we ask for recent snaps we get the number we asked for and
// the files we expected
List<File> nRecentValidSnapFiles = txnLog.findNValidSnapshots(nRecentSnap);
assertEquals(4, nRecentValidSnapFiles.size(), "exactly 4 snapshots ");
expectedNRecentSnapFiles.removeAll(nRecentValidSnapFiles);
assertEquals(0, expectedNRecentSnapFiles.size(), "Didn't get the recent snap files");
// Test that when asking for more snaps than we created, we still only get snaps
// not logs or anything else (per ZOOKEEPER-2420)
nRecentValidSnapFiles = txnLog.findNValidSnapshots(nRecentCount + 5);
assertEquals(nRecentCount, nRecentValidSnapFiles.size());
for (File f : nRecentValidSnapFiles) {
assertTrue((Util.getZxidFromName(f.getName(), "snapshot") != -1), "findNValidSnapshots() returned a non-snapshot: " + f.getPath());
}
txnLog.close();
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class PurgeTxnTest method testPurge.
/**
* test the purge
* @throws Exception an exception might be thrown here
*/
@Test
public void testPurge() throws Exception {
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(100);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
try {
for (int i = 0; i < 2000; i++) {
zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} finally {
zk.close();
}
f.shutdown();
zks.getTxnLogFactory().close();
assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server to shutdown");
// now corrupt the snapshot
PurgeTxnLog.purge(tmpDir, tmpDir, 3);
FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir);
List<File> listLogs = snaplog.findNValidSnapshots(4);
int numSnaps = 0;
for (File ff : listLogs) {
if (ff.getName().startsWith("snapshot")) {
numSnaps++;
}
}
assertTrue((numSnaps == 3), "exactly 3 snapshots ");
snaplog.close();
zks.shutdown();
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class SnapshotDigestTest method testDifferentDigestVersion.
/**
* Make sure the code will skip digest check when it's comparing
* digest with different version.
*
* This enables us to smoonthly add new fields into digest or using
* new digest calculation.
*/
@Test
public void testDifferentDigestVersion() throws Exception {
// check the current digest version
int currentVersion = new DigestCalculator().getDigestVersion();
// create a node
String path = "/testDifferentDigestVersion";
zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// take a full snapshot
server.takeSnapshot();
// increment the digest version
int newVersion = currentVersion + 1;
DigestCalculator newVersionDigestCalculator = Mockito.spy(DigestCalculator.class);
Mockito.when(newVersionDigestCalculator.getDigestVersion()).thenReturn(newVersion);
assertEquals(newVersion, newVersionDigestCalculator.getDigestVersion());
// using mock to return different digest value when the way we
// calculate digest changed
FileTxnSnapLog txnSnapLog = new FileTxnSnapLog(tmpDir, tmpDir);
DataTree dataTree = Mockito.spy(new DataTree(newVersionDigestCalculator));
Mockito.when(dataTree.getTreeDigest()).thenReturn(0L);
txnSnapLog.restore(dataTree, new ConcurrentHashMap<>(), Mockito.mock(FileTxnSnapLog.PlayBackListener.class));
// make sure the reportDigestMismatch function is never called
Mockito.verify(dataTree, Mockito.never()).reportDigestMismatch(Mockito.anyLong());
}
Aggregations