Ejemplo n.º 1
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);
            }
Ejemplo n.º 2
0
        public void ReleaseAllPartitionOwnerships()
        {
            Logger.Info("Releasing all partition ownerships");

            var consumerIdString = GetConsumerIdString();

            foreach (var item in topicRegistry)
            {
                var topic = item.Key;
                try
                {
                    foreach (var partition in item.Value.Keys)
                    {
                        var partitionOwnerPath = ZkUtils.GetConsumerPartitionOwnerPath(config.GroupId, topic,
                                                                                       partition.ToString());
                        Logger.InfoFormat("Consumer {0} will delete ZK path {1} topic:{2} partition:{3} ",
                                          consumerIdString, partitionOwnerPath, topic, partition);
                        try
                        {
                            GetZkClient().SlimLock.EnterWriteLock();
                            ZkUtils.DeletePath(GetZkClient(), partitionOwnerPath);
                            Logger.InfoFormat(
                                "Consumer {0} SUCC delete ZK path {1} topic:{2} partition:{3}  succsessfully.",
                                consumerIdString, partitionOwnerPath, topic, partition);
                        }
                        catch (Exception ex)
                        {
                            Logger.ErrorFormat(
                                "Consumer {0} FAILED delete ZK path {1} topic:{2} partition:{3}  error:{4}.",
                                consumerIdString, partitionOwnerPath, topic, partition, ex.FormatException());
                        }
                        finally
                        {
                            GetZkClient().SlimLock.ExitWriteLock();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("error when call ZkUtils.DeletePath : {0}", ex.FormatException());
                }
            }

            Logger.Info("Released all partition ownerships");
        }