Search in sources :

Example 16 with FileTxnSnapLog

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) PrintWriter(java.io.PrintWriter)

Example 17 with FileTxnSnapLog

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 18 with FileTxnSnapLog

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();
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 19 with FileTxnSnapLog

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 20 with FileTxnSnapLog

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());
}
Also used : FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) QuorumPeerMainTest(org.apache.zookeeper.server.quorum.QuorumPeerMainTest) Test(org.junit.jupiter.api.Test)

Aggregations

FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)54 File (java.io.File)37 Test (org.junit.jupiter.api.Test)20 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)17 IOException (java.io.IOException)13 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)10 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)10 ServerConfig (org.apache.zookeeper.server.ServerConfig)8 ZooKeeper (org.apache.zookeeper.ZooKeeper)6 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)6 ArrayList (java.util.ArrayList)5 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)4 ConfigException (org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Socket (java.net.Socket)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Record (org.apache.jute.Record)3 Test (org.junit.Test)3