Search in sources :

Example 1 with DataTree

use of org.apache.zookeeper.server.DataTree in project zookeeper by apache.

the class FileTxnSnapLogTest method attemptAutoCreateDb.

private void attemptAutoCreateDb(File dataDir, File snapDir, Map<Long, Integer> sessions, String priorAutocreateDbValue, String autoCreateValue, long expectedValue) throws IOException {
    sessions.clear();
    System.setProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE, autoCreateValue);
    FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(dataDir, snapDir);
    try {
        long zxid = fileTxnSnapLog.restore(new DataTree(), sessions, new FileTxnSnapLog.PlayBackListener() {

            @Override
            public void onTxnLoaded(TxnHeader hdr, Record rec) {
            // empty by default
            }
        });
        Assert.assertEquals("unexpected zxid", expectedValue, zxid);
    } finally {
        if (priorAutocreateDbValue == null) {
            System.clearProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE);
        } else {
            System.setProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE, priorAutocreateDbValue);
        }
    }
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) Record(org.apache.jute.Record) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 2 with DataTree

use of org.apache.zookeeper.server.DataTree in project zookeeper by apache.

the class LoadFromLogNoServerTest method testTxnFailure.

/**
 * For ZOOKEEPER-1046. Verify if cversion and pzxid if incremented
 * after create/delete failure during restore.
 */
@Test
public void testTxnFailure() throws Exception {
    try {
        ZooKeeperServer.setDigestEnabled(true);
        long count = 1;
        File tmpDir = ClientBase.createTmpDir();
        FileTxnSnapLog logFile = new FileTxnSnapLog(tmpDir, tmpDir);
        DataTree dt = new DataTree();
        dt.createNode("/test", new byte[0], null, 0, -1, 1, 1);
        for (count = 1; count <= 3; count++) {
            dt.createNode("/test/" + count, new byte[0], null, 0, -1, count, Time.currentElapsedTime());
        }
        long digestBefore = dt.getTreeDigest();
        DataNode zk = dt.getNode("/test");
        // Make create to fail, then verify cversion.
        LOG.info("Attempting to create /test/{}", (count - 1));
        doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, -1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
        LOG.info("Attempting to create /test/{}", (count - 1));
        digestBefore = dt.getTreeDigest();
        doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, zk.stat.getCversion() + 1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
        LOG.info("Attempting to create /test/{}", (count - 1));
        digestBefore = dt.getTreeDigest();
        doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, zk.stat.getCversion() + 1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
        LOG.info("Attempting to create /test/{}", (count - 1));
        digestBefore = dt.getTreeDigest();
        doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, -1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
    // Make delete fo fail, then verify cversion.
    // this doesn't happen anymore, we only set the cversion on create
    // LOG.info("Attempting to delete " + "/test/" + (count + 1));
    // doOp(logFile, OpCode.delete, "/test/" + (count + 1), dt, zk);
    } finally {
        ZooKeeperServer.setDigestEnabled(false);
    }
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) DataNode(org.apache.zookeeper.server.DataNode) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 3 with DataTree

use of org.apache.zookeeper.server.DataTree in project zookeeper by apache.

the class Zab1_0Test method testInitialAcceptedCurrent.

@Test
public void testInitialAcceptedCurrent() throws Exception {
    File tmpDir = File.createTempFile("test", ".dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    try {
        FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
        File version2 = new File(tmpDir, "version-2");
        version2.mkdir();
        logFactory.save(new DataTree(), new ConcurrentHashMap<Long, Integer>(), false);
        long zxid = ZxidUtils.makeZxid(3, 3);
        logFactory.append(new Request(1, 1, ZooDefs.OpCode.error, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error), new ErrorTxn(1), zxid));
        logFactory.commit();
        ZKDatabase zkDb = new ZKDatabase(logFactory);
        QuorumPeer peer = QuorumPeer.testingQuorumPeer();
        peer.setZKDatabase(zkDb);
        peer.setTxnFactory(logFactory);
        peer.getLastLoggedZxid();
        assertEquals(3, peer.getAcceptedEpoch());
        assertEquals(3, peer.getCurrentEpoch());
        assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME))));
        assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
    } finally {
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) DataTree(org.apache.zookeeper.server.DataTree) Request(org.apache.zookeeper.server.Request) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 4 with DataTree

use of org.apache.zookeeper.server.DataTree in project zookeeper by apache.

the class QuotaMetricsUtilsTest method testQuotaMetrics_emptyDataTree.

@Test
public void testQuotaMetrics_emptyDataTree() {
    // register the metrics
    final String nameSuffix = UUID.randomUUID().toString();
    registerQuotaMetrics(nameSuffix, new DataTree());
    // validate the quota
    validateQuotaMetrics(UUID.randomUUID().toString(), null, null, null, null, nameSuffix);
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) Test(org.junit.jupiter.api.Test)

Example 5 with DataTree

use of org.apache.zookeeper.server.DataTree in project zookeeper by apache.

the class FileTxnSnapLogTest method testSnapshotSerializationCompatibility.

void testSnapshotSerializationCompatibility(Boolean digestEnabled, Boolean snappyEnabled) throws IOException {
    File dataDir = ClientBase.createEmptyTestDir();
    FileTxnSnapLog snaplog = new FileTxnSnapLog(dataDir, dataDir);
    DataTree dataTree = new DataTree();
    ConcurrentHashMap<Long, Integer> sessions = new ConcurrentHashMap<>();
    SnapStream.setStreamMode(snappyEnabled ? SnapStream.StreamMode.SNAPPY : SnapStream.StreamMode.DEFAULT_MODE);
    ZooKeeperServer.setDigestEnabled(digestEnabled);
    TxnHeader txnHeader = new TxnHeader(1, 1, 1, 1 + 1, ZooDefs.OpCode.create);
    CreateTxn txn = new CreateTxn("/" + 1, "data".getBytes(), null, false, 1);
    Request request = new Request(1, 1, 1, txnHeader, txn, 1);
    dataTree.processTxn(request.getHdr(), request.getTxn());
    snaplog.save(dataTree, sessions, true);
    int expectedNodeCount = dataTree.getNodeCount();
    ZooKeeperServer.setDigestEnabled(!digestEnabled);
    snaplog.restore(dataTree, sessions, (hdr, rec, digest) -> {
    });
    assertEquals(expectedNodeCount, dataTree.getNodeCount());
}
Also used : CreateTxn(org.apache.zookeeper.txn.CreateTxn) DataTree(org.apache.zookeeper.server.DataTree) Request(org.apache.zookeeper.server.Request) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Aggregations

DataTree (org.apache.zookeeper.server.DataTree)15 Test (org.junit.jupiter.api.Test)9 File (java.io.File)7 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 Record (org.apache.jute.Record)3 StatsTrack (org.apache.zookeeper.StatsTrack)3 DataNode (org.apache.zookeeper.server.DataNode)3 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BinaryInputArchive (org.apache.jute.BinaryInputArchive)2 Request (org.apache.zookeeper.server.Request)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)1 InputArchive (org.apache.jute.InputArchive)1 OutputArchive (org.apache.jute.OutputArchive)1 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)1