Пример #1
0
 internal void RegisterConsumerInZk(ZKGroupDirs dirs, string consumerIdString, TopicCount topicCount)
 {
     this.EnsuresNotDisposed();
     Logger.InfoFormat(CultureInfo.CurrentCulture, "begin registering consumer {0} in ZK", consumerIdString);
     ZkUtils.CreateEphemeralPathExpectConflict(this.zkClient, dirs.ConsumerRegistryDir + "/" + consumerIdString, topicCount.ToJsonString());
     Logger.InfoFormat(CultureInfo.CurrentCulture, "end registering consumer {0} in ZK", consumerIdString);
 }
Пример #2
0
            private bool ReflectPartitionOwnershipDecision(
                IDictionary <Tuple <string, int>, string> partitionOwnershipDecision)
            {
                var successfullyOwnedPartitions  = new List <Tuple <string, int> >();
                var partitionOwnershipSuccessful = partitionOwnershipDecision.Select(
                    partitionOwner =>
                {
                    var topic              = partitionOwner.Key.Item1;
                    var partition          = partitionOwner.Key.Item2;
                    var consumerThreadId   = partitionOwner.Value;
                    var partitionOwnerPath = ZkUtils.GetConsumerPartitionOwnerPath(group, topic, partition);
                    try
                    {
                        ZkUtils.CreateEphemeralPathExpectConflict(
                            parent.zkClient, partitionOwnerPath, consumerThreadId);
                        Logger.InfoFormat(
                            "{0} successfully owner partition {1} for topic {2}",
                            consumerThreadId,
                            partition,
                            topic);
                        successfullyOwnedPartitions.Add(Tuple.Create(topic, partition));
                        return(true);
                    }
                    catch (ZkNodeExistsException)
                    {
                        // The node hasn't been deleted by the original owner. So wait a bit and retry.
                        Logger.InfoFormat("waiting for the partition ownership to be deleted:" + partition);
                        return(false);
                    }
                }).ToList();
                var hasPartitionOwnershipFailed = partitionOwnershipSuccessful.Aggregate(
                    0, (sum, decision) => (sum + (decision ? 0 : 1)));

                /* even if one of the partition ownership attempt has failed, return false */

                if (hasPartitionOwnershipFailed > 0)
                {
                    // remove all paths that we have owned in ZK
                    foreach (var topicAndPartition in successfullyOwnedPartitions)
                    {
                        this.DeletePartitionOwnershipFromZK(topicAndPartition.Item1, topicAndPartition.Item2);
                    }

                    return(false);
                }

                return(true);
            }
Пример #3
0
        private bool ReflectPartitionOwnershipDecision(string topic, string partition, string consumerThreadId)
        {
            var ownershipReflected = false;
            var topicDirs          = new ZKGroupTopicDirs(config.GroupId, topic);
            var partitionOwnerPath = topicDirs.ConsumerOwnerDir + "/" + partition;

            try
            {
                Logger.InfoFormat("Consumer {0} will own partition {1} for topic {2}, will create ZK path {3}",
                                  consumerThreadId, partition, topic, partitionOwnerPath);
                try
                {
                    zkClient.SlimLock.EnterWriteLock();
                    ZkUtils.CreateEphemeralPathExpectConflict(zkClient, partitionOwnerPath, consumerThreadId);
                    Logger.InfoFormat(
                        "Consumer {0} SUCC owned partition {1} for topic {2} . finish create ZK path {3}  .",
                        consumerThreadId, partition, topic, partitionOwnerPath);
                    ownershipReflected = true;
                }
                catch (Exception ex)
                {
                    Logger.InfoFormat(
                        "Consumer {0} FAILED owned partition {1} for topic {2} . finish create ZK path {3}  error:{4}.",
                        consumerThreadId, partition, topic, partitionOwnerPath, ex.FormatException());
                }
                finally
                {
                    zkClient.SlimLock.ExitWriteLock();
                }
            }
            catch (KeeperException e)
            {
                if (e.ErrorCode == KeeperException.Code.NODEEXISTS)
                {
                    Logger.InfoFormat(
                        "{0} failed to own partition {1} for topic {2}. Waiting for the partition owner to be deleted",
                        consumerThreadId, partition, topic);
                    ownershipReflected = false;
                }
                else
                {
                    throw;
                }
            }

            return(ownershipReflected);
        }
Пример #4
0
        private bool ProcessPartition(ZKGroupTopicDirs topicDirs, string partition, string topic, string consumerThreadId)
        {
            var partitionOwnerPath = topicDirs.ConsumerOwnerDir + "/" + partition;

            try
            {
                ZkUtils.CreateEphemeralPathExpectConflict(zkClient, partitionOwnerPath, consumerThreadId);
            }
            catch (KeeperException.NodeExistsException)
            {
                //// The node hasn't been deleted by the original owner. So wait a bit and retry.
                Logger.InfoFormat(CultureInfo.CurrentCulture, "waiting for the partition ownership to be deleted: {0}", partition);
                return(false);
            }

            AddPartitionTopicInfo(topicDirs, partition, topic, consumerThreadId);
            return(true);
        }
Пример #5
0
 internal void RegisterConsumerInZk(ZKGroupDirs dirs, string consumerIdString, TopicCount topicCount)
 {
     this.EnsuresNotDisposed();
     Logger.InfoFormat("begin registering consumer {0} in ZK", consumerIdString);
     try
     {
         this.GetZkClient().SlimLock.EnterWriteLock();
         ZkUtils.CreateEphemeralPathExpectConflict(this.GetZkClient(), dirs.ConsumerRegistryDir + "/" + consumerIdString, topicCount.ToJsonString());
         Logger.InfoFormat("successfully registering consumer {0} in ZK", consumerIdString);
     }
     catch (Exception ex)
     {
         Logger.ErrorFormat("error in RegisterConsumerInZk CreateEphemeralPathExpectConflict : {0}", ex.FormatException());
     }
     finally
     {
         GetZkClient().SlimLock.ExitWriteLock();
     }
 }
Пример #6
0
        private bool ReflectPartitionOwnership(Dictionary <Tuple <string, string>, string> partitionOwnershipDecision)
        {
            var successfullyOwnedPartitions  = new List <Tuple <string, string> >();
            var partitionOwnershipSuccessful = new List <bool>();

            foreach (var partitionOwner in partitionOwnershipDecision)
            {
                var topic              = partitionOwner.Key.Item1;
                var partition          = partitionOwner.Key.Item2;
                var consumerThreadId   = partitionOwner.Value;
                var topicDirs          = new ZKGroupTopicDirs(config.GroupId, topic);
                var partitionOwnerPath = topicDirs.ConsumerOwnerDir + "/" + partition;
                try
                {
                    ZkUtils.CreateEphemeralPathExpectConflict(zkClient, partitionOwnerPath, consumerThreadId);
                    Logger.InfoFormat("{0} successfully owned partition {1} for topic {2}", consumerThreadId, partition, topic);
                    successfullyOwnedPartitions.Add(new Tuple <string, string>(topic, partition));
                    partitionOwnershipSuccessful.Add(true);
                }
                catch (KeeperException.NodeExistsException)
                {
                    Logger.InfoFormat("waiting for the partition owner to be deleted: {0}", partition);
                    partitionOwnershipSuccessful.Add(false);
                }
            }
            var hasPartitionOwnershipFailed = partitionOwnershipSuccessful.Contains(false);

            if (hasPartitionOwnershipFailed)
            {
                foreach (var topicAndPartition in successfullyOwnedPartitions)
                {
                    var topicDirs = new ZKGroupTopicDirs(config.GroupId, topicAndPartition.Item1);
                    var znode     = topicDirs.ConsumerOwnerDir + "/" + topicAndPartition.Item2;
                    ZkUtils.DeletePath(zkClient, znode);
                    Logger.DebugFormat("Consumer {0} releasing {1}", consumerIdString, znode);
                }
                return(false);
            }
            return(true);
        }
Пример #7
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);
        }