public static TopicCount ConstructTopicCount(string group, string consumerId, ZkClient zkClient)
        {
            var    dirs             = new ZKGroupDirs(group);
            var    topicCountString = ZkUtils.ReadData(zkClient, dirs.ConsumerRegistryDir + "/" + consumerId).Item1;
            string subscriptionPattern;
            IDictionary <string, int> topMap;

            try
            {
                var parsedJson = JObject.Parse(topicCountString);
                if (parsedJson != null)
                {
                    var pattern = parsedJson.Get("pattern");
                    if (pattern != null)
                    {
                        subscriptionPattern = pattern.Value <string>();
                    }
                    else
                    {
                        throw new KafkaException("error constructing TopicCount:" + topicCountString);
                    }

                    var topMapObject = (IEnumerable <KeyValuePair <string, JToken> >)parsedJson.Get("subscription");
                    if (topMapObject != null)
                    {
                        topMap = topMapObject.ToDictionary(x => x.Key, x => x.Value.Value <int>());
                    }
                    else
                    {
                        throw new KafkaException("error constructing TopicCount:" + topicCountString);
                    }
                }
                else
                {
                    throw new KafkaException("error constructing TopicCount:" + topicCountString);
                }
            }
            catch (Exception e)
            {
                Logger.Error("error parsing consumer json string " + topicCountString, e);
                throw;
            }

            var hasWhiteList = WhiteListPattern.Equals(subscriptionPattern);
            var hasBlackList = BlackListPattern.Equals(subscriptionPattern);

            if (topMap.Count == 0 || !(hasWhiteList || hasBlackList))
            {
                return(new StaticTopicCount(consumerId, topMap));
            }
            else
            {
                var         regex      = topMap.First().Key;
                var         numStreams = topMap.First().Value;
                TopicFilter filter     = hasWhiteList ? (TopicFilter) new Whitelist(regex) : new Blacklist(regex);
                return(new WildcardTopicCount(zkClient, consumerId, filter, numStreams));
            }
        }
Exemple #2
0
        public void TestEphemeralNodeCleanup()
        {
            var zkClient = new ZkClient(ZkConnect, ZkSessionTimeoutMs, ZkConnectionTimeout, new ZkStringSerializer());

            try
            {
                ZkUtils.CreateEphemeralPathExpectConflict(zkClient, "/tmp/zktest", "node created");
            }
            catch
            {
                // ok
            }

            var testData = ZkUtils.ReadData(this.ZkClient, "/tmp/zktest").Item1;

            Assert.NotNull(testData);
            zkClient.Dispose();
            zkClient = new ZkClient(ZkConnect, ZkSessionTimeoutMs, ZkConnectionTimeout, new ZkStringSerializer());
            var nodeExists = ZkUtils.PathExists(zkClient, "/tmp/zktest");

            Assert.False(nodeExists);
        }