private void ReleasePartitionOwnership() { zkClient.SlimLock.EnterWriteLock(); try { foreach (KeyValuePair <string, IDictionary <Partition, PartitionTopicInfo> > item in topicRegistry) { var topicDirs = new ZKGroupTopicDirs(this.config.GroupId, item.Key); foreach (var partition in item.Value.Keys) { string znode = topicDirs.ConsumerOwnerDir + "/" + partition.Name; ZkUtils.DeletePath(zkClient, znode); if (Logger.IsDebugEnabled) { Logger.DebugFormat(CultureInfo.CurrentCulture, "Consumer {0} releasing {1}", this.consumerIdString, znode); } } topicRegistry.Remove(item.Key); } } finally { zkClient.SlimLock.ExitWriteLock(); } }
private void DeletePartitionOwnershipFromZK(string topic, int partition) { var topicDirs = new ZKGroupTopicDirs(group, topic); var znode = topicDirs.ConsumerOwnerDir + "/" + partition; ZkUtils.DeletePath(this.parent.zkClient, znode); Logger.DebugFormat("Consumer {0} releasing {1}", consumerIdString, znode); }
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"); }
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); }
private void DeleteConsumerIdNode() { string consumerIdString = GetConsumerIdString(); ZKGroupDirs dirs = new ZKGroupDirs(this.config.GroupId); string idsPath = dirs.ConsumerRegistryDir + "/" + consumerIdString; Logger.InfoFormat("Will delete {0} in zookeeper due to zookeeperConsumerConnector dispose.", idsPath); try { GetZkClient().SlimLock.EnterWriteLock(); ZkUtils.DeletePath(GetZkClient(), idsPath); Logger.InfoFormat("Path {0} deleted succsessfully.", idsPath); } catch (Exception ex) { Logger.ErrorFormat("Path {0} FAILED to be deleted: {1}", idsPath, ex.FormatException()); } finally { GetZkClient().SlimLock.ExitWriteLock(); } }