Пример #1
0
        public void WhenNewBrokerInTopicIsAddedBrokerTopicsListenerUpdatesMappings()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            IDictionary <string, SortedSet <Partition> > mappings;
            IDictionary <int, Broker> brokers;
            string brokerPath      = ZooKeeperClient.DefaultBrokerIdsPath + "/" + 2345;
            string topicPath       = ZooKeeperClient.DefaultBrokerTopicsPath + "/" + CurrentTestTopic;
            string topicBrokerPath = topicPath + "/" + 2345;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                using (var brokerPartitionInfo = new ZKBrokerPartitionInfo(client))
                {
                    brokers  = brokerPartitionInfo.GetAllBrokerInfo();
                    mappings =
                        ReflectionHelper.GetInstanceField <IDictionary <string, SortedSet <Partition> > >(
                            "topicBrokerPartitions", brokerPartitionInfo);
                }
            }

            Assert.NotNull(brokers);
            Assert.Greater(brokers.Count, 0);
            Assert.NotNull(mappings);
            Assert.Greater(mappings.Count, 0);
            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                WaitUntillIdle(client, 500);
                var brokerTopicsListener = new BrokerTopicsListener(client, mappings, brokers, null);
                client.Subscribe(ZooKeeperClient.DefaultBrokerIdsPath, brokerTopicsListener);
                client.Subscribe(ZooKeeperClient.DefaultBrokerTopicsPath, brokerTopicsListener);
                client.CreatePersistent(brokerPath, true);
                client.WriteData(brokerPath, "172.16.66.2-1310449279123:172.16.66.2:9092");
                client.CreatePersistent(topicPath, true);
                WaitUntillIdle(client, 500);
                Assert.IsTrue(brokers.ContainsKey(2345));
                Assert.IsTrue(mappings.ContainsKey(CurrentTestTopic));
                client.CreatePersistent(topicBrokerPath, true);
                client.WriteData(topicBrokerPath, 5);
                WaitUntillIdle(client, 500);
                client.UnsubscribeAll();
                WaitUntillIdle(client, 500);
                client.DeleteRecursive(brokerPath);
                client.DeleteRecursive(topicPath);
            }

            Assert.IsTrue(brokers.ContainsKey(2345));
            Assert.IsTrue(mappings.Keys.Contains(CurrentTestTopic));
            Assert.AreEqual(5, mappings[CurrentTestTopic].Count);
        }
Пример #2
0
        public void WhenZNodeIsDeletedChildAndDataDeletedListenersFire()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            string myPath = "/" + Guid.NewGuid();

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                client.CreatePersistent(myPath, true);
                WaitUntillIdle(client, 500);
                client.Subscribe(myPath, this as IZooKeeperChildListener);
                client.Subscribe(myPath, this as IZooKeeperDataListener);
                client.Delete(myPath);
                WaitUntillIdle(client, 500);
            }

            Assert.AreEqual(2, this.events.Count);
            ZooKeeperEventArgs e = this.events[0];

            Assert.AreEqual(ZooKeeperEventTypes.ChildChanged, e.Type);
            Assert.IsInstanceOf <ZooKeeperChildChangedEventArgs>(e);
            Assert.AreEqual(((ZooKeeperChildChangedEventArgs)e).Path, myPath);
            Assert.IsNull(((ZooKeeperChildChangedEventArgs)e).Children);
            e = this.events[1];
            Assert.AreEqual(ZooKeeperEventTypes.DataChanged, e.Type);
            Assert.IsInstanceOf <ZooKeeperDataChangedEventArgs>(e);
            Assert.AreEqual(((ZooKeeperDataChangedEventArgs)e).Path, myPath);
            Assert.IsNull(((ZooKeeperDataChangedEventArgs)e).Data);
        }
Пример #3
0
        public void WhenChildIsDeletedChildListenerOnParentFires()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            string myPath = "/" + Guid.NewGuid();

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                client.CreatePersistent(myPath, true);
                WaitUntillIdle(client, 500);
                client.Subscribe("/", this as IZooKeeperChildListener);
                client.Delete(myPath);
                WaitUntillIdle(client, 500);
            }

            Assert.AreEqual(1, this.events.Count);
            ZooKeeperEventArgs e = this.events[0];

            Assert.AreEqual(ZooKeeperEventTypes.ChildChanged, e.Type);
            Assert.IsInstanceOf <ZooKeeperChildChangedEventArgs>(e);
            Assert.AreEqual(((ZooKeeperChildChangedEventArgs)e).Path, "/");
            Assert.Greater(((ZooKeeperChildChangedEventArgs)e).Children.Count, 0);
            Assert.IsFalse(((ZooKeeperChildChangedEventArgs)e).Children.Contains(myPath.Replace("/", string.Empty)));
        }
Пример #4
0
        public void WhenBrokerIsRemovedZkBrokerPartitionInfoUpdatesBrokersList()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            IDictionary <int, Broker> brokers;
            string brokerPath = ZooKeeperClient.DefaultBrokerIdsPath + "/" + 2345;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                using (var brokerPartitionInfo = new ZKBrokerPartitionInfo(client))
                {
                    WaitUntillIdle(client, 500);
                    brokers = brokerPartitionInfo.GetAllBrokerInfo();
                    client.CreatePersistent(brokerPath, true);
                    client.WriteData(brokerPath, "172.16.66.2-1310449279123:172.16.66.2:9092");
                    WaitUntillIdle(client, 500);
                    Assert.NotNull(brokers);
                    Assert.Greater(brokers.Count, 0);
                    Assert.IsTrue(brokers.ContainsKey(2345));
                    client.DeleteRecursive(brokerPath);
                    WaitUntillIdle(client, 500);
                }
            }

            Assert.NotNull(brokers);
            Assert.Greater(brokers.Count, 0);
            Assert.IsFalse(brokers.ContainsKey(2345));
        }
Пример #5
0
        public void WhenNewTopicIsAddedZkBrokerPartitionInfoUpdatesMappings()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            IDictionary <string, SortedSet <Partition> > mappings;
            string topicPath = ZooKeeperClient.DefaultBrokerTopicsPath + "/" + CurrentTestTopic;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                using (var brokerPartitionInfo = new ZKBrokerPartitionInfo(client))
                {
                    mappings =
                        ReflectionHelper.GetInstanceField <IDictionary <string, SortedSet <Partition> > >(
                            "topicBrokerPartitions", brokerPartitionInfo);
                    client.CreatePersistent(topicPath, true);
                    WaitUntillIdle(client, 500);
                    client.UnsubscribeAll();
                    WaitUntillIdle(client, 500);
                    client.DeleteRecursive(topicPath);
                }
            }

            Assert.NotNull(mappings);
            Assert.Greater(mappings.Count, 0);
            Assert.IsTrue(mappings.ContainsKey(CurrentTestTopic));
        }
Пример #6
0
        public void WhenNewBrokerIsAddedZkBrokerPartitionInfoUpdatesBrokersList()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            IDictionary <int, Broker> brokers;
            string brokerPath = ZooKeeperClient.DefaultBrokerIdsPath + "/" + 2345;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                using (var brokerPartitionInfo = new ZKBrokerPartitionInfo(client))
                {
                    brokers = brokerPartitionInfo.GetAllBrokerInfo();
                    client.CreatePersistent(brokerPath, true);
                    client.WriteData(brokerPath, "192.168.1.39-1310449279123:192.168.1.39:9102");
                    WaitUntillIdle(client, 500);
                    client.UnsubscribeAll();
                    WaitUntillIdle(client, 500);
                    client.DeleteRecursive(brokerPath);
                }
            }

            Assert.NotNull(brokers);
            Assert.Greater(brokers.Count, 0);
            Assert.IsTrue(brokers.ContainsKey(2345));
            Assert.AreEqual("192.168.1.39", brokers[2345].Host);
            Assert.AreEqual(9102, brokers[2345].Port);
            Assert.AreEqual(2345, brokers[2345].Id);
        }
Пример #7
0
        static void SubscribeChildrenListener(ZooKeeperClient client)
        {
            var rootPath = "/cluster/calc";

            client.CreatePersistent(rootPath, new byte[0]);

            var childrenListener = new DebugChildrenSubscriber(rootPath);

            client.SubscribeChildListener(childrenListener);

            // create child
            for (int i = 11; i <= 15; i++)
            {
                client.CreatePersistent("/cluster/calc/" + i, new byte[0]);
                Thread.Sleep(1000);
            }

            // delete child
            for (int i = 11; i <= 15; i++)
            {
                client.Delete("/cluster/calc/" + i);
                Thread.Sleep(1000);
            }
        }
Пример #8
0
        public void ZooKeeperClientCreatesANewPathAndDeletesIt()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                string myPath = "/" + Guid.NewGuid();
                client.CreatePersistent(myPath, false);
                Assert.IsTrue(client.Exists(myPath));
                client.Delete(myPath);
                Assert.IsFalse(client.Exists(myPath));
            }
        }
Пример #9
0
        public void WhenBrokerIsRemovedBrokerTopicsListenerUpdatesBrokersList()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            IDictionary <string, SortedSet <Partition> > mappings;
            IDictionary <int, Broker> brokers;
            string brokerPath = ZooKeeperClient.DefaultBrokerIdsPath + "/" + 2345;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                using (var brokerPartitionInfo = new ZKBrokerPartitionInfo(client))
                {
                    brokers  = brokerPartitionInfo.GetAllBrokerInfo();
                    mappings =
                        ReflectionHelper.GetInstanceField <IDictionary <string, SortedSet <Partition> > >(
                            "topicBrokerPartitions", brokerPartitionInfo);
                }
            }

            Assert.NotNull(brokers);
            Assert.Greater(brokers.Count, 0);
            Assert.NotNull(mappings);
            Assert.Greater(mappings.Count, 0);
            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                WaitUntillIdle(client, 500);
                var brokerTopicsListener = new BrokerTopicsListener(client, mappings, brokers, null);
                client.Subscribe(ZooKeeperClient.DefaultBrokerIdsPath, brokerTopicsListener);
                client.CreatePersistent(brokerPath, true);
                client.WriteData(brokerPath, "192.168.1.39-1310449279123:192.168.1.39:9102");
                WaitUntillIdle(client, 500);
                Assert.IsTrue(brokers.ContainsKey(2345));
                client.DeleteRecursive(brokerPath);
                WaitUntillIdle(client, 500);
                Assert.IsFalse(brokers.ContainsKey(2345));
            }
        }
Пример #10
0
        public void ZooKeeperClientCreatesAChildAndGetsChildren()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                string child  = Guid.NewGuid().ToString();
                string myPath = "/" + child;
                client.CreatePersistent(myPath, false);
                IList <string> children      = client.GetChildren("/", false);
                int            countChildren = client.CountChildren("/");
                Assert.Greater(children.Count, 0);
                Assert.AreEqual(children.Count, countChildren);
                Assert.IsTrue(children.Contains(child));
                client.Delete(myPath);
            }
        }
Пример #11
0
        public void WhenDataChangedDataListenerFires()
        {
            var prodConfig = this.ZooKeeperBasedSyncProdConfig;

            string myPath     = "/" + Guid.NewGuid();
            string sourceData = "my test data";
            string resultData;

            using (IZooKeeperClient client = new ZooKeeperClient(
                       prodConfig.ZooKeeper.ZkConnect,
                       prodConfig.ZooKeeper.ZkSessionTimeoutMs,
                       ZooKeeperStringSerializer.Serializer))
            {
                client.Connect();
                client.CreatePersistent(myPath, true);
                WaitUntillIdle(client, 500);
                client.Subscribe(myPath, this as IZooKeeperDataListener);
                client.Subscribe(myPath, this as IZooKeeperChildListener);
                client.WriteData(myPath, sourceData);
                WaitUntillIdle(client, 500);
                client.UnsubscribeAll();
                resultData = client.ReadData <string>(myPath);
                client.Delete(myPath);
            }

            Assert.IsTrue(!string.IsNullOrEmpty(resultData));
            Assert.AreEqual(sourceData, resultData);
            Assert.AreEqual(1, this.events.Count);
            ZooKeeperEventArgs e = this.events[0];

            Assert.AreEqual(ZooKeeperEventTypes.DataChanged, e.Type);
            Assert.IsInstanceOf <ZooKeeperDataChangedEventArgs>(e);
            Assert.AreEqual(((ZooKeeperDataChangedEventArgs)e).Path, myPath);
            Assert.IsNotNull(((ZooKeeperDataChangedEventArgs)e).Data);
            Assert.AreEqual(((ZooKeeperDataChangedEventArgs)e).Data, sourceData);
        }