Search in sources :

Example 11 with DataTree

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

the class WatchCommand method commandRun.

@Override
public void commandRun() {
    if (!isZKServerRunning()) {
        pw.println(ZK_NOT_SERVING);
    } else {
        DataTree dt = zkServer.getZKDatabase().getDataTree();
        if (len == FourLetterCommands.wchsCmd) {
            dt.dumpWatchesSummary(pw);
        } else if (len == FourLetterCommands.wchpCmd) {
            dt.dumpWatches(pw, true);
        } else {
            dt.dumpWatches(pw, false);
        }
        pw.println();
    }
}
Also used : DataTree(org.apache.zookeeper.server.DataTree)

Example 12 with DataTree

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

the class EmptySnapshotTest method testNoEmptySnapshot.

@Test
public void testNoEmptySnapshot() throws Exception {
    File tmpFile = File.createTempFile("empty-snapshot-test", ".junit", new File(System.getProperty("build.test.dir", "build")));
    File tmpDataDir = new File(tmpFile + ".dir");
    assertFalse(tmpDataDir.exists());
    assertTrue(tmpDataDir.mkdirs());
    FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDataDir, tmpDataDir);
    snapLog.snapLog = new MockFileSnap(snapLog.dataDir);
    assertEquals(0, ((FileSnap) snapLog.snapLog).findNRecentSnapshots(10).size());
    DataTree tree = new DataTree();
    tree.createNode("/empty-snapshot-test-1", "data".getBytes(), null, -1, -1, 1, 1);
    try {
        snapLog.save(tree, new ConcurrentHashMap<>(), false);
        fail("Should have thrown an IOException");
    } catch (IOException e) {
    // no op
    }
    assertEquals(0, ((FileSnap) snapLog.snapLog).findNRecentSnapshots(10).size());
    snapLog.snapLog = new FileSnap(snapLog.dataDir);
    snapLog.save(tree, new ConcurrentHashMap<>(), false);
    assertEquals(1, ((FileSnap) snapLog.snapLog).findNRecentSnapshots(10).size());
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) IOException(java.io.IOException) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 13 with DataTree

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

the class FileTxnSnapLogTest method testACLCreatedDuringFuzzySnapshotSync.

/**
 * Make sure the ACL is exist in the ACL map after SNAP syncing.
 *
 * ZooKeeper uses ACL reference id and count to save the space in snapshot.
 * During fuzzy snapshot sync, the reference count may not be updated
 * correctly in case like the znode is already exist.
 *
 * When ACL reference count reaches 0, it will be deleted from the cache,
 * but actually there might be other nodes still using it. When visiting
 * a node with the deleted ACL id, it will be rejected because it doesn't
 * exist anymore.
 *
 * Here is the detailed flow for one of the scenario here:
 *   1. Server A starts to have snap sync with leader
 *   2. After serializing the ACL map to Server A, there is a txn T1 to
 *      create a node N1 with new ACL_1 which was not exist in ACL map
 *   3. On leader, after this txn, the ACL map will be ID1 -&gt; (ACL_1, COUNT: 1),
 *      and data tree N1 -&gt; ID1
 *   4. On server A, it will be empty ACL map, and N1 -&gt; ID1 in fuzzy snapshot
 *   5. When replaying the txn T1, it will skip at the beginning since the
 *      node is already exist, which leaves an empty ACL map, and N1 is
 *      referencing to a non-exist ACL ID1
 *   6. Node N1 will be not accessible because the ACL not exist, and if it
 *      became leader later then all the write requests will be rejected as
 *      well with marshalling error.
 */
@Test
public void testACLCreatedDuringFuzzySnapshotSync() throws IOException {
    DataTree leaderDataTree = new DataTree();
    // Start the simulated snap-sync by serializing ACL cache.
    File file = File.createTempFile("snapshot", "zk");
    FileOutputStream os = new FileOutputStream(file);
    OutputArchive oa = BinaryOutputArchive.getArchive(os);
    leaderDataTree.serializeAcls(oa);
    // Add couple of transaction in-between.
    TxnHeader hdr1 = new TxnHeader(1, 2, 2, 2, ZooDefs.OpCode.create);
    Record txn1 = new CreateTxn("/a1", "foo".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, false, -1);
    leaderDataTree.processTxn(hdr1, txn1);
    // Finish the snapshot.
    leaderDataTree.serializeNodes(oa);
    os.close();
    // Simulate restore on follower and replay.
    FileInputStream is = new FileInputStream(file);
    InputArchive ia = BinaryInputArchive.getArchive(is);
    DataTree followerDataTree = new DataTree();
    followerDataTree.deserialize(ia, "tree");
    followerDataTree.processTxn(hdr1, txn1);
    DataNode a1 = leaderDataTree.getNode("/a1");
    assertNotNull(a1);
    assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, leaderDataTree.getACL(a1));
    assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, followerDataTree.getACL(a1));
}
Also used : CreateTxn(org.apache.zookeeper.txn.CreateTxn) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) DataTree(org.apache.zookeeper.server.DataTree) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) DataNode(org.apache.zookeeper.server.DataNode) FileOutputStream(java.io.FileOutputStream) Record(org.apache.jute.Record) File(java.io.File) FileInputStream(java.io.FileInputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 14 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 autoCreateValue, long expectedValue) throws IOException {
    sessions.clear();
    FileTxnSnapLog fileTxnSnapLog = createFileTxnSnapLogWithAutoCreateDB(dataDir, snapDir, autoCreateValue);
    long zxid = fileTxnSnapLog.restore(new DataTree(), sessions, new FileTxnSnapLog.PlayBackListener() {

        @Override
        public void onTxnLoaded(TxnHeader hdr, Record rec, TxnDigest digest) {
        // empty by default
        }
    });
    assertEquals(expectedValue, zxid, "unexpected zxid");
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) Record(org.apache.jute.Record) TxnDigest(org.apache.zookeeper.txn.TxnDigest) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 15 with DataTree

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

the class FileTxnSnapLogTest method testEmptySnapshotSerialization.

@Test
public void testEmptySnapshotSerialization() throws IOException {
    File dataDir = ClientBase.createEmptyTestDir();
    FileTxnSnapLog snaplog = new FileTxnSnapLog(dataDir, dataDir);
    DataTree dataTree = new DataTree();
    ConcurrentHashMap<Long, Integer> sessions = new ConcurrentHashMap<>();
    ZooKeeperServer.setDigestEnabled(true);
    snaplog.save(dataTree, sessions, true);
    snaplog.restore(dataTree, sessions, (hdr, rec, digest) -> {
    });
    assertNull(dataTree.getDigestFromLoadedSnapshot());
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File) Test(org.junit.jupiter.api.Test)

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