Search in sources :

Example 16 with RetryOneTime

use of org.apache.curator.retry.RetryOneTime in project storm by apache.

the class KafkaOffsetLagUtil method getLeadersAndTopicPartitions.

private static Map<String, List<TopicPartition>> getLeadersAndTopicPartitions(OldKafkaSpoutOffsetQuery oldKafkaSpoutOffsetQuery) throws Exception {
    Map<String, List<TopicPartition>> result = new HashMap<>();
    // this means that kafka spout was configured with StaticHosts hosts (leader for partition)
    if (oldKafkaSpoutOffsetQuery.getPartitions() != null) {
        String[] partitions = oldKafkaSpoutOffsetQuery.getPartitions().split(",");
        String[] leaders = oldKafkaSpoutOffsetQuery.getLeaders().split(",");
        for (int i = 0; i < leaders.length; ++i) {
            if (!result.containsKey(leaders[i])) {
                result.put(leaders[i], new ArrayList<TopicPartition>());
            }
            result.get(leaders[i]).add(new TopicPartition(oldKafkaSpoutOffsetQuery.getTopic(), Integer.parseInt(partitions[i])));
        }
    } else {
        // else use zk nodes to figure out partitions and leaders for topics i.e. ZkHosts
        CuratorFramework curatorFramework = null;
        try {
            String brokersZkNode = oldKafkaSpoutOffsetQuery.getBrokersZkPath();
            if (!brokersZkNode.endsWith("/")) {
                brokersZkNode += "/";
            }
            String topicsZkPath = brokersZkNode + "topics";
            curatorFramework = CuratorFrameworkFactory.newClient(oldKafkaSpoutOffsetQuery.getZkServers(), 20000, 15000, new RetryOneTime(1000));
            curatorFramework.start();
            List<String> topics = new ArrayList<>();
            if (oldKafkaSpoutOffsetQuery.isWildCardTopic()) {
                List<String> children = curatorFramework.getChildren().forPath(topicsZkPath);
                for (String child : children) {
                    if (child.matches(oldKafkaSpoutOffsetQuery.getTopic())) {
                        topics.add(child);
                    }
                }
            } else {
                topics.add(oldKafkaSpoutOffsetQuery.getTopic());
            }
            for (String topic : topics) {
                String partitionsPath = topicsZkPath + "/" + topic + "/partitions";
                List<String> children = curatorFramework.getChildren().forPath(partitionsPath);
                for (int i = 0; i < children.size(); ++i) {
                    byte[] leaderData = curatorFramework.getData().forPath(partitionsPath + "/" + i + "/state");
                    Map<Object, Object> value = (Map<Object, Object>) JSONValue.parseWithException(new String(leaderData, "UTF-8"));
                    Integer leader = ((Number) value.get("leader")).intValue();
                    byte[] brokerData = curatorFramework.getData().forPath(brokersZkNode + "ids/" + leader);
                    Map<Object, Object> broker = (Map<Object, Object>) JSONValue.parseWithException(new String(brokerData, "UTF-8"));
                    String host = (String) broker.get("host");
                    Integer port = ((Long) broker.get("port")).intValue();
                    String leaderBroker = host + ":" + port;
                    if (!result.containsKey(leaderBroker)) {
                        result.put(leaderBroker, new ArrayList<TopicPartition>());
                    }
                    result.get(leaderBroker).add(new TopicPartition(topic, i));
                }
            }
        } finally {
            if (curatorFramework != null) {
                curatorFramework.close();
            }
        }
    }
    return result;
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CuratorFramework(org.apache.curator.framework.CuratorFramework) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with RetryOneTime

use of org.apache.curator.retry.RetryOneTime in project hive by apache.

the class TestSlotZnode method newCurator.

private CuratorFramework newCurator() throws IOException {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 10000, 10000, new RetryOneTime(1));
    client.start();
    curatorInstances.add(client);
    return client;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime)

Example 18 with RetryOneTime

use of org.apache.curator.retry.RetryOneTime in project elastic-job by dangdangdotcom.

the class ZookeeperElectionServiceTest method assertContend.

@Test
public void assertContend() throws Exception {
    ElectionCandidate anotherElectionCandidate = mock(ElectionCandidate.class);
    CuratorFramework anotherClient = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
    ZookeeperElectionService anotherService = new ZookeeperElectionService("ANOTHER_CLIENT:8899", anotherClient, ELECTION_PATH, anotherElectionCandidate);
    anotherClient.start();
    anotherClient.blockUntilConnected();
    anotherService.start();
    KillSession.kill(client.getZookeeperClient().getZooKeeper(), EmbedTestingServer.getConnectionString());
    service.stop();
    verify(anotherElectionCandidate).startLeadership();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ElectionCandidate(com.dangdang.ddframe.job.reg.base.ElectionCandidate) Test(org.junit.Test)

Example 19 with RetryOneTime

use of org.apache.curator.retry.RetryOneTime in project elastic-job by dangdangdotcom.

the class ZookeeperRegistryCenterModifyTest method assertPersistEphemeralSequential.

@Test
public void assertPersistEphemeralSequential() throws Exception {
    zkRegCenter.persistEphemeralSequential("/sequential/test_ephemeral_sequential");
    zkRegCenter.persistEphemeralSequential("/sequential/test_ephemeral_sequential");
    CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
    client.start();
    client.blockUntilConnected();
    List<String> actual = client.getChildren().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/sequential");
    assertThat(actual.size(), is(2));
    for (String each : actual) {
        assertThat(each, startsWith("test_ephemeral_sequential"));
    }
    zkRegCenter.close();
    actual = client.getChildren().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/sequential");
    assertTrue(actual.isEmpty());
    zkRegCenter.init();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Test(org.junit.Test)

Example 20 with RetryOneTime

use of org.apache.curator.retry.RetryOneTime in project druid by druid-io.

the class OverlordTest method setupServerAndCurator.

private void setupServerAndCurator() throws Exception {
    server = new TestingServer();
    timing = new Timing();
    curator = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).compressionProvider(new PotentiallyGzippedCompressionProvider(true)).build();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) PotentiallyGzippedCompressionProvider(io.druid.curator.PotentiallyGzippedCompressionProvider)

Aggregations

RetryOneTime (org.apache.curator.retry.RetryOneTime)25 CuratorFramework (org.apache.curator.framework.CuratorFramework)16 Test (org.junit.Test)10 Timing (org.apache.curator.test.Timing)8 TestingServer (org.apache.curator.test.TestingServer)6 Stat (org.apache.zookeeper.data.Stat)4 HashMap (java.util.HashMap)3 Before (org.junit.Before)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)2 TestingCluster (org.apache.curator.test.TestingCluster)2 ElectionCandidate (com.dangdang.ddframe.job.reg.base.ElectionCandidate)1 ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)1 CuratorZKClientBridge (com.netflix.curator.x.zkclientbridge.CuratorZKClientBridge)1 PotentiallyGzippedCompressionProvider (io.druid.curator.PotentiallyGzippedCompressionProvider)1 Random (java.util.Random)1 NodeState (org.apache.camel.component.zookeepermaster.group.NodeState)1