leventov commented on a change in pull request #9172: Fix huge number of watches in zk URL: https://github.com/apache/druid/pull/9172#discussion_r365807298
########## File path: server/src/test/java/org/apache/druid/curator/announcement/NodeAnnouncerTest.java ########## @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.curator.announcement; + +import com.google.common.collect.Sets; +import org.apache.curator.framework.api.CuratorEventType; +import org.apache.curator.framework.api.transaction.CuratorOp; +import org.apache.curator.framework.api.transaction.CuratorTransactionResult; +import org.apache.curator.test.KillSession; +import org.apache.curator.utils.ZKPaths; +import org.apache.druid.curator.CuratorTestBase; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.data.Stat; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collection; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +/** + */ +public class NodeAnnouncerTest extends CuratorTestBase +{ + + @Before + public void setUp() throws Exception + { + setupServerAndCurator(); + } + + @After + public void tearDown() + { + tearDownServerAndCurator(); + } + + @Test(timeout = 60_000L) + public void testSanity() throws Exception + { + curator.start(); + curator.blockUntilConnected(); + NodeAnnouncer announcer = new NodeAnnouncer(curator); + + final byte[] billy = StringUtils.toUtf8("billy"); + final String testPath1 = "/test1"; + final String testPath2 = "/somewhere/test2"; + announcer.announce(testPath1, billy); + + Assert.assertNull("/test1 does not exists", curator.checkExists().forPath(testPath1)); + Assert.assertNull("/somewhere/test2 does not exists", curator.checkExists().forPath(testPath2)); + + announcer.start(); + while (!announcer.getAddedPaths().contains("/test1")) { + Thread.sleep(100); + } + + try { + Assert.assertArrayEquals("/test1 has data", billy, curator.getData().decompressed().forPath(testPath1)); + Assert.assertNull("/somewhere/test2 still does not exist", curator.checkExists().forPath(testPath2)); + + announcer.announce(testPath2, billy); + + Assert.assertArrayEquals("/test1 still has data", billy, curator.getData().decompressed().forPath(testPath1)); + Assert.assertArrayEquals( + "/somewhere/test2 has data", + billy, + curator.getData().decompressed().forPath(testPath2) + ); + + final CountDownLatch latch = new CountDownLatch(1); + curator.getCuratorListenable().addListener( + (client, event) -> { + if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(testPath1)) { + latch.countDown(); + } + } + ); + final CuratorOp deleteOp = curator.transactionOp().delete().forPath(testPath1); + final Collection<CuratorTransactionResult> results = curator.transaction().forOperations(deleteOp); + Assert.assertEquals(1, results.size()); + final CuratorTransactionResult result = results.iterator().next(); + Assert.assertEquals(Code.OK.intValue(), result.getError()); // assert delete + + Assert.assertTrue("Wait for /test1 to be created", timing.forWaiting().awaitLatch(latch)); + + Assert.assertArrayEquals( + "expect /test1 data is restored", + billy, + curator.getData().decompressed().forPath(testPath1) + ); + Assert.assertArrayEquals( + "expect /somewhere/test2 is still there", + billy, + curator.getData().decompressed().forPath(testPath2) + ); + + announcer.unannounce(testPath1); + Assert.assertNull("expect /test1 unannounced", curator.checkExists().forPath(testPath1)); + Assert.assertArrayEquals( + "expect /somewhere/test2 is still still there", + billy, + curator.getData().decompressed().forPath(testPath2) + ); + } + finally { + announcer.stop(); + } + + Assert.assertNull("expect /test1 remains unannounced", curator.checkExists().forPath(testPath1)); + Assert.assertNull("expect /somewhere/test2 unannounced", curator.checkExists().forPath(testPath2)); + } + + @Test(timeout = 60_000L) + public void testSessionKilled() throws Exception + { + curator.start(); + curator.blockUntilConnected(); + NodeAnnouncer announcer = new NodeAnnouncer(curator); + try { + curator.inTransaction().create().forPath("/somewhere").and().commit(); + announcer.start(); + + final byte[] billy = StringUtils.toUtf8("billy"); + final String testPath1 = "/test1"; + final String testPath2 = "/somewhere/test2"; + final Set<String> paths = Sets.newHashSet(testPath1, testPath2); + announcer.announce(testPath1, billy); + announcer.announce(testPath2, billy); + + Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1)); + Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2)); + + final CountDownLatch latch = new CountDownLatch(1); + curator.getCuratorListenable().addListener( + (client, event) -> { + if (event.getType() == CuratorEventType.CREATE) { + paths.remove(event.getPath()); + if (paths.isEmpty()) { + latch.countDown(); + } + } + } + ); + KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString()); + + Assert.assertTrue(timing.forWaiting().awaitLatch(latch)); + + Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1)); + Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2)); + + announcer.stop(); + + while ((curator.checkExists().forPath(testPath1) != null) || (curator.checkExists().forPath(testPath2) != null)) { + Thread.sleep(100); + } + + Assert.assertNull(curator.checkExists().forPath(testPath1)); + Assert.assertNull(curator.checkExists().forPath(testPath2)); + } + finally { + announcer.stop(); + } + } + + @Test + public void testCleansUpItsLittleTurdlings() throws Exception + { + curator.start(); + curator.blockUntilConnected(); + NodeAnnouncer announcer = new NodeAnnouncer(curator); + + final byte[] billy = StringUtils.toUtf8("billy"); + final String testPath = "/somewhere/test2"; + final String parent = ZKPaths.getPathAndNode(testPath).getPath(); Review comment: Can use `ZKPathUtils.getParentPath()` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
