internal static SortedDictionary<int, string> GetComsumerGroupOwners(ZooKeeperClient zkClient, string topic, string consumerGroupName) { SortedDictionary<int, string> partitionsOwners = new SortedDictionary<int, string>(); string path = string.Format("/consumers/{0}/owners/{1}" , consumerGroupName, topic); IEnumerable<string> partitions = zkClient.GetChildrenParentMayNotExist(path); if (partitions != null) { foreach (var p in partitions) { string fullPatht = string.Format("/consumers/{0}/owners/{1}/{2}" , consumerGroupName, topic, p); string data = zkClient.ReadData<string>(fullPatht, true); partitionsOwners.Add(Convert.ToInt32(p), data); } } return partitionsOwners; }
//{"controller_epoch":4,"leader":2,"version":1,"leader_epoch":5,"isr":[2]} internal static ArrayList GetIsr(ZooKeeperClient zkClient, string topic, int partition) { string data = zkClient.ReadData<string>(string.Format("/brokers/topics/{0}/partitions/{1}/state" , topic, partition), true); Dictionary<string, object> ctx = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(data); Type ty = ctx["isr"].GetType(); return (ArrayList)ctx["isr"]; }
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); }
//[zk: localhost(CONNECTED) 12] get /brokers/topics/mvlogs //{"version":1,"partitions":{"1":[3,2],"0":[2,3]}} public static Dictionary<int, int[]> GetTopicMetadataInzookeeper(ZooKeeperClient zkClient, string topic) { Dictionary<int, int[]> treturn = new Dictionary<int, int[]>(); try { string data = zkClient.ReadData<string>(string.Format("/brokers/topics/{0}", topic), true); Dictionary<string, object> ctx = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(data); Type ty = ctx["partitions"].GetType(); //Logger.InfoFormat("The type for partitions :{0}", ty.FullName); Dictionary<string, object> tpartitons = (Dictionary<string, object>)ctx["partitions"]; foreach (KeyValuePair<string, object> kv in tpartitons) { int partitionID = Convert.ToInt32(kv.Key); //Logger.InfoFormat("The type for partitions value :{0}", kv.Value.GetType().FullName); ArrayList rep = (ArrayList)kv.Value; int[] partitionReplicas = new int[rep.Count]; for (int i = 0; i < rep.Count; i++) partitionReplicas[i] = Convert.ToInt32(rep[i]); treturn.Add(partitionID, partitionReplicas); } Logger.InfoFormat("Get topic data directly from zookeeper Topic:{0} Data:{1} Partition count:{2}", topic, data, treturn.Count); } catch (Exception ex) { Logger.Error("Failed to get topic " + topic + " data directly from zookeeper: " + ex.FormatException()); } return treturn; }