/** * Add a new node to the DataTree. * @param path * Path for the new node. * @param data * Data to store in the node. * @param acl * Node acls * @param ephemeralOwner * the session id that owns this node. -1 indicates this is not * an ephemeral node. * @param zxid * Transaction ID * @param time * @throws NodeExistsException * @throws NoNodeException * @throws KeeperException */ public void createNode(final String path, byte data[], List<ACL> acl, long ephemeralOwner, int parentCVersion, long zxid, long time) throws NoNodeException, NodeExistsException { createNode(path, data, acl, ephemeralOwner, parentCVersion, zxid, time, null); }
@Override public void createNode(final String path, byte data[], List<ACL> acl, long ephemeralOwner, int parentCVersion, long zxid, long time, Stat outputStat) throws NoNodeException, NodeExistsException { NodeCreateListener listener = nodeCreateListeners.get(path); if (listener != null) { listener.process(path); } super.createNode(path, data, acl, ephemeralOwner, parentCVersion, zxid, time, outputStat); }
static int createNodes(DataTree tree, String path, int depth, int childcount, int parentCVersion, byte[] data) throws KeeperException.NodeExistsException, KeeperException.NoNodeException { path += "node" + depth; tree.createNode(path, data, null, -1, ++parentCVersion, 1, 1); if (--depth == 0) { return 1; } path += "/"; int count = 1; for (int i = 0; i < childcount; i++) { count += createNodes(tree, path + i, depth, childcount, 1, data); } return count; }
private void createEphemeralNode(long session, final DataTree dataTree, int count) throws NoNodeException, NodeExistsException { for (int i = 0; i < count; i++) { dataTree.createNode("/test" + i, new byte[0], null, session + i, dataTree.getNode("/").stat.getCversion() + 1, 1, 1); } }
@Test(timeout = 60000) public void testPathTrieClearOnDeserialize() throws Exception { //Create a DataTree with quota nodes so PathTrie get updated DataTree dserTree = new DataTree(); dserTree.createNode("/bug", new byte[20], null, -1, 1, 1, 1); dserTree.createNode(Quotas.quotaZookeeper+"/bug", null, null, -1, 1, 1, 1); dserTree.createNode(Quotas.quotaPath("/bug"), new byte[20], null, -1, 1, 1, 1); dserTree.createNode(Quotas.statPath("/bug"), new byte[20], null, -1, 1, 1, 1); //deserialize a DataTree; this should clear the old /bug nodes and pathTrie DataTree tree = new DataTree(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); tree.serialize(oa, "test"); baos.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); BinaryInputArchive ia = BinaryInputArchive.getArchive(bais); dserTree.deserialize(ia, "test"); Field pfield = DataTree.class.getDeclaredField("pTrie"); pfield.setAccessible(true); PathTrie pTrie = (PathTrie)pfield.get(dserTree); //Check that the node path is removed from pTrie Assert.assertEquals("/bug is still in pTrie", "", pTrie.findMaxPrefix("/bug")); }
/** * ZOOKEEPER-2052: * This test checks that if a multi operation aborted, and during the multi there is side effect * that changed outstandingChangesForPath, after aborted the side effect should be removed and * everything should be restored correctly. */ @Test public void testMultiRollbackNoLastChange() throws Exception { zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0); zks.getZKDatabase().dataTree.createNode("/foo/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0); Assert.assertNull(zks.outstandingChangesForPath.get("/foo")); // multi record: // set "/foo" => succeed, leave a outstanding change // delete "/foo" => fail, roll back change process(Arrays.asList( Op.setData("/foo", new byte[0], -1), Op.delete("/foo", -1))); // aborting multi shouldn't leave any record. Assert.assertNull(zks.outstandingChangesForPath.get("/foo")); }
@Test(timeout = 60000) public void testSerializeDoesntLockDataNodeWhileWriting() throws Exception { DataTree tree = new DataTree(); tree.createNode("/marker", new byte[] {42}, null, -1, 1, 1, 1); final DataNode markerNode = tree.getNode("/marker"); final AtomicBoolean ranTestCase = new AtomicBoolean();
@Test(timeout = 60000) public void testRootWatchTriggered() throws Exception { class MyWatcher implements Watcher{ boolean fired=false; public void process(WatchedEvent event) { if(event.getPath().equals("/")) fired=true; } } MyWatcher watcher=new MyWatcher(); // set a watch on the root node dt.getChildren("/", new Stat(), watcher); // add a new node, should trigger a watch dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion()+1, 1, 1); Assert.assertFalse("Root node watch not triggered",!watcher.fired); }
tree.createNode("/empty-snapshot-test-1", "data".getBytes(), null, -1, -1, 1, 1); try {
@Test public void testNoCversionRevert() throws Exception { DataNode parent = dt.getNode("/"); dt.createNode("/test", new byte[0], null, 0, parent.stat.getCversion() + 1, 1, 1); int currentCversion = parent.stat.getCversion(); long currentPzxid = parent.stat.getPzxid(); dt.createNode("/test1", new byte[0], null, 0, currentCversion - 1, 1, 1); parent = dt.getNode("/"); int newCversion = parent.stat.getCversion(); long newPzxid = parent.stat.getPzxid(); Assert.assertTrue("<cversion, pzxid> verification failed. Expected: <" + currentCversion + ", " + currentPzxid + ">, found: <" + newCversion + ", " + newPzxid + ">", (newCversion >= currentCversion && newPzxid >= currentPzxid)); }
/** * This test checks that a successful multi will change outstanding record * and failed multi shouldn't change outstanding record. */ @Test public void testMultiOutstandingChange() throws Exception { zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0); Assert.assertNull(zks.outstandingChangesForPath.get("/foo")); process(Arrays.asList( Op.setData("/foo", new byte[0], -1))); ChangeRecord cr = zks.outstandingChangesForPath.get("/foo"); Assert.assertNotNull("Change record wasn't set", cr); Assert.assertEquals("Record zxid wasn't set correctly", 1, cr.zxid); process(Arrays.asList( Op.delete("/foo", -1))); cr = zks.outstandingChangesForPath.get("/foo"); Assert.assertEquals("Record zxid wasn't set correctly", 2, cr.zxid); // It should fail and shouldn't change outstanding record. process(Arrays.asList( Op.delete("/foo", -1))); cr = zks.outstandingChangesForPath.get("/foo"); // zxid should still be previous result because record's not changed. Assert.assertEquals("Record zxid wasn't set correctly", 2, cr.zxid); }
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());
@Test public void test353TTL() throws KeeperException, InterruptedException { DataTree dataTree = serverFactory.zkServer.getZKDatabase().dataTree; long ephemeralOwner = EphemeralTypeEmulate353.ttlToEphemeralOwner(100); dataTree.createNode("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, ephemeralOwner, dataTree.getNode("/").stat.getCversion()+1, 1, 1); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); fakeElapsed.set(1000); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
tree.createNode( "/bug", new byte[20], ZooDefs.Ids.OPEN_ACL_UNSAFE, -1, 1, 1, 1);
@Test public void testCachedApproximateDataSize() throws Exception { DataTree dt = new DataTree(); long initialSize = dt.approximateDataSize(); Assert.assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); // create a node dt.createNode("/testApproximateDataSize", new byte[20], null, -1, 1, 1, 1); dt.createNode("/testApproximateDataSize1", new byte[20], null, -1, 1, 1, 1); Assert.assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); // update data dt.setData("/testApproximateDataSize1", new byte[32], -1, 1, 1); Assert.assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); // delete a node dt.deleteNode("/testApproximateDataSize", -1); Assert.assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); } }
CreateTxn createTxn = (CreateTxn) txn; rc.path = createTxn.getPath(); createNode( createTxn.getPath(), createTxn.getData(), rc.path = create2Txn.getPath(); Stat stat = new Stat(); createNode( create2Txn.getPath(), create2Txn.getData(), rc.path = createTtlTxn.getPath(); stat = new Stat(); createNode( createTtlTxn.getPath(), createTtlTxn.getData(), rc.path = createContainerTxn.getPath(); stat = new Stat(); createNode( createContainerTxn.getPath(), createContainerTxn.getData(),
/** * For ZOOKEEPER-1046 test if cversion is getting incremented correctly. */ @Test(timeout = 60000) public void testIncrementCversion() throws Exception { dt.createNode("/test", new byte[0], null, 0, dt.getNode("/").stat.getCversion()+1, 1, 1); DataNode zk = dt.getNode("/test"); int prevCversion = zk.stat.getCversion(); long prevPzxid = zk.stat.getPzxid(); dt.setCversionPzxid("/test/", prevCversion + 1, prevPzxid + 1); int newCversion = zk.stat.getCversion(); long newPzxid = zk.stat.getPzxid(); Assert.assertTrue("<cversion, pzxid> verification failed. Expected: <" + (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + newCversion + ", " + newPzxid + ">", (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1)); }
CreateTxn createTxn = (CreateTxn) txn; rc.path = createTxn.getPath(); createNode( createTxn.getPath(), createTxn.getData(),
CreateTxn createTxn = (CreateTxn) txn; debug = "Create transaction for " + createTxn.getPath(); createNode( createTxn.getPath(), createTxn.getData(),