public static void MaybeDeletePath(string zkUrl, string dir) { try { var zk = new ZkClient(zkUrl, 30 * 1000, 30 * 1000, new ZkStringSerializer()); zk.DeleteRecursive(dir); zk.Dispose(); } catch { // swallow } }
public void TestLeaderSelectionForPartition() { var zkClient = new ZkClient(this.zookeeperConnect, 6000, 30000, new ZkStringSerializer()); // create topic topic1 with 1 partition on broker 0 AdminUtils.CreateTopic(zkClient, Topic, 1, 1, new Dictionary <string, string>()); TestUtils.WaitUntilMetadataIsPropagated(this.Servers, Topic, 0, 3000); var sentMessages1 = this.SendMessages( Configs.First(), nMessages, "batch1", CompressionCodecs.NoCompressionCodec, 1); TestUtils.WaitUntilMetadataIsPropagated(this.Servers, Topic, 0, 1000); // create a consuemr var consumerConfig1 = TestUtils.CreateConsumerProperties(ZkConnect, Group, Consumer1); var zkConsumerConnector1 = new ZookeeperConsumerConnector(consumerConfig1); var topicMessageStreams1 = zkConsumerConnector1.CreateMessageStreams( new Dictionary <string, int> { { Topic, 1 } }, new StringDecoder(), new StringDecoder()); var topicRegistry = zkConsumerConnector1.TopicRegistry; Assert.Equal(1, topicRegistry.Select(x => x.Key).Count()); Assert.Equal(Topic, topicRegistry.Select(x => x.Key).First()); var topicsAndPartitionsInRegistry = topicRegistry.Select(x => Tuple.Create(x.Key, x.Value.Select(p => p.Value))).ToList(); var brokerPartition = topicsAndPartitionsInRegistry.First().Item2.First(); Assert.Equal(0, brokerPartition.PartitionId); // also check partition ownership var actual_1 = this.GetZKChildrenValues(this.dirs.ConsumerOwnerDir); var expected_1 = new List <Tuple <string, string> > { Tuple.Create("0", "group1_consumer1-0"), }; Assert.Equal(expected_1, actual_1); var receivedMessages1 = this.GetMessages(nMessages, topicMessageStreams1); Assert.Equal(sentMessages1, receivedMessages1); zkConsumerConnector1.Shutdown(); zkClient.Dispose(); }
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); }
public void TestLeaderSelectionForPartition() { var zkClient = new ZkClient(this.zookeeperConnect, 6000, 30000, new ZkStringSerializer()); // create topic topic1 with 1 partition on broker 0 AdminUtils.CreateTopic(zkClient, Topic, 1, 1, new Dictionary<string, string>()); TestUtils.WaitUntilMetadataIsPropagated(this.Servers, Topic, 0, 3000); var sentMessages1 = this.SendMessages( Configs.First(), nMessages, "batch1", CompressionCodecs.NoCompressionCodec, 1); TestUtils.WaitUntilMetadataIsPropagated(this.Servers, Topic, 0, 1000); // create a consuemr var consumerConfig1 = TestUtils.CreateConsumerProperties(ZkConnect, Group, Consumer1); var zkConsumerConnector1 = new ZookeeperConsumerConnector(consumerConfig1); var topicMessageStreams1 = zkConsumerConnector1.CreateMessageStreams( new Dictionary<string, int> { { Topic, 1 } }, new StringDecoder(), new StringDecoder()); var topicRegistry = zkConsumerConnector1.TopicRegistry; Assert.Equal(1, topicRegistry.Select(x => x.Key).Count()); Assert.Equal(Topic, topicRegistry.Select(x => x.Key).First()); var topicsAndPartitionsInRegistry = topicRegistry.Select(x => Tuple.Create(x.Key, x.Value.Select(p => p.Value))).ToList(); var brokerPartition = topicsAndPartitionsInRegistry.First().Item2.First(); Assert.Equal(0, brokerPartition.PartitionId); // also check partition ownership var actual_1 = this.GetZKChildrenValues(this.dirs.ConsumerOwnerDir); var expected_1 = new List<Tuple<string, string>> { Tuple.Create("0", "group1_consumer1-0"), }; Assert.Equal(expected_1, actual_1); var receivedMessages1 = this.GetMessages(nMessages, topicMessageStreams1); Assert.Equal(sentMessages1, receivedMessages1); zkConsumerConnector1.Shutdown(); zkClient.Dispose(); }