Search in sources :

Example 61 with ZkUtils

use of kafka.utils.ZkUtils in project presto by prestodb.

the class EmbeddedKafka method createTopics.

public void createTopics(int partitions, int replication, Properties topicProperties, String... topics) {
    checkState(started.get() && !stopped.get(), "not started!");
    ZkUtils zkUtils = ZkUtils.apply(getZookeeperConnectString(), 30_000, 30_000, false);
    try {
        for (String topic : topics) {
            AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicProperties, RackAwareMode.Disabled$.MODULE$);
        }
    } finally {
        zkUtils.close();
    }
}
Also used : ZkUtils(kafka.utils.ZkUtils)

Example 62 with ZkUtils

use of kafka.utils.ZkUtils in project incubator-atlas by apache.

the class AtlasTopicCreatorTest method shouldCreateTopicIfItDoesNotExist.

@Test
public void shouldCreateTopicIfItDoesNotExist() {
    Configuration configuration = mock(Configuration.class);
    when(configuration.getBoolean(AtlasTopicCreator.ATLAS_NOTIFICATION_CREATE_TOPICS_KEY, true)).thenReturn(true);
    when(configuration.getString("atlas.authentication.method.kerberos")).thenReturn("false");
    final ZkUtils zookeeperUtils = mock(ZkUtils.class);
    final boolean[] createdTopic = new boolean[] { false };
    AtlasTopicCreator atlasTopicCreator = new AtlasTopicCreator() {

        @Override
        protected boolean ifTopicExists(String topicName, ZkUtils zkUtils) {
            return false;
        }

        @Override
        protected ZkUtils createZkUtils(Configuration atlasProperties) {
            return zookeeperUtils;
        }

        @Override
        protected void createTopic(Configuration atlasProperties, String topicName, ZkUtils zkUtils) {
            createdTopic[0] = true;
        }
    };
    atlasTopicCreator.createAtlasTopic(configuration, "ATLAS_HOOK");
    assertTrue(createdTopic[0]);
}
Also used : Configuration(org.apache.commons.configuration.Configuration) ZkUtils(kafka.utils.ZkUtils) Test(org.testng.annotations.Test)

Example 63 with ZkUtils

use of kafka.utils.ZkUtils in project incubator-atlas by apache.

the class AtlasTopicCreatorTest method shouldCreateMultipleTopics.

@Test
public void shouldCreateMultipleTopics() {
    Configuration configuration = mock(Configuration.class);
    when(configuration.getBoolean(AtlasTopicCreator.ATLAS_NOTIFICATION_CREATE_TOPICS_KEY, true)).thenReturn(true);
    when(configuration.getString("atlas.authentication.method.kerberos")).thenReturn("false");
    final ZkUtils zookeeperUtils = mock(ZkUtils.class);
    final Map<String, Boolean> createdTopics = new HashMap<>();
    createdTopics.put("ATLAS_HOOK", false);
    createdTopics.put("ATLAS_ENTITIES", false);
    AtlasTopicCreator atlasTopicCreator = new AtlasTopicCreator() {

        @Override
        protected boolean ifTopicExists(String topicName, ZkUtils zkUtils) {
            return false;
        }

        @Override
        protected ZkUtils createZkUtils(Configuration atlasProperties) {
            return zookeeperUtils;
        }

        @Override
        protected void createTopic(Configuration atlasProperties, String topicName, ZkUtils zkUtils) {
            createdTopics.put(topicName, true);
        }
    };
    atlasTopicCreator.createAtlasTopic(configuration, "ATLAS_HOOK", "ATLAS_ENTITIES");
    assertTrue(createdTopics.get("ATLAS_HOOK"));
    assertTrue(createdTopics.get("ATLAS_ENTITIES"));
}
Also used : Configuration(org.apache.commons.configuration.Configuration) HashMap(java.util.HashMap) ZkUtils(kafka.utils.ZkUtils) Test(org.testng.annotations.Test)

Example 64 with ZkUtils

use of kafka.utils.ZkUtils in project incubator-atlas by apache.

the class AtlasTopicCreatorTest method shouldCreateTopicEvenIfEarlierOneFails.

@Test
public void shouldCreateTopicEvenIfEarlierOneFails() {
    Configuration configuration = mock(Configuration.class);
    when(configuration.getBoolean(AtlasTopicCreator.ATLAS_NOTIFICATION_CREATE_TOPICS_KEY, true)).thenReturn(true);
    when(configuration.getString("atlas.authentication.method.kerberos")).thenReturn("false");
    final ZkUtils zookeeperUtils = mock(ZkUtils.class);
    final Map<String, Boolean> createdTopics = new HashMap<>();
    createdTopics.put("ATLAS_ENTITIES", false);
    AtlasTopicCreator atlasTopicCreator = new AtlasTopicCreator() {

        @Override
        protected boolean ifTopicExists(String topicName, ZkUtils zkUtils) {
            return false;
        }

        @Override
        protected ZkUtils createZkUtils(Configuration atlasProperties) {
            return zookeeperUtils;
        }

        @Override
        protected void createTopic(Configuration atlasProperties, String topicName, ZkUtils zkUtils) {
            if (topicName.equals("ATLAS_HOOK")) {
                throw new RuntimeException("Simulating failure when creating ATLAS_HOOK topic");
            } else {
                createdTopics.put(topicName, true);
            }
        }
    };
    atlasTopicCreator.createAtlasTopic(configuration, "ATLAS_HOOK", "ATLAS_ENTITIES");
    assertTrue(createdTopics.get("ATLAS_ENTITIES"));
}
Also used : Configuration(org.apache.commons.configuration.Configuration) HashMap(java.util.HashMap) ZkUtils(kafka.utils.ZkUtils) Test(org.testng.annotations.Test)

Example 65 with ZkUtils

use of kafka.utils.ZkUtils in project metron by apache.

the class KafkaComponent method createTopic.

public void createTopic(String name, int numPartitions, long waitThisLongForMetadataToPropagate) throws InterruptedException {
    ZkUtils zkUtils = null;
    Level oldLevel = UnitTestHelper.getJavaLoggingLevel();
    try {
        UnitTestHelper.setJavaLoggingLevel(Level.OFF);
        zkUtils = ZkUtils.apply(zookeeperConnectString, 30000, 30000, false);
        AdminUtilsWrapper.createTopic(zkUtils, name, numPartitions, 1, new Properties());
        if (waitThisLongForMetadataToPropagate > 0) {
            waitUntilMetadataIsPropagated(name, numPartitions, waitThisLongForMetadataToPropagate);
        }
    } catch (TopicExistsException tee) {
    } finally {
        if (zkUtils != null) {
            zkUtils.close();
        }
        UnitTestHelper.setJavaLoggingLevel(oldLevel);
    }
}
Also used : Level(java.util.logging.Level) ZkUtils(kafka.utils.ZkUtils) Properties(java.util.Properties) TopicExistsException(kafka.common.TopicExistsException)

Aggregations

ZkUtils (kafka.utils.ZkUtils)65 ZkClient (org.I0Itec.zkclient.ZkClient)29 Properties (java.util.Properties)28 ZkConnection (org.I0Itec.zkclient.ZkConnection)25 Test (org.testng.annotations.Test)18 Configuration (org.apache.commons.configuration.Configuration)16 KafkaConfig (kafka.server.KafkaConfig)11 TestingServer (org.apache.curator.test.TestingServer)11 KafkaServerStartable (kafka.server.KafkaServerStartable)10 ServerSocket (java.net.ServerSocket)9 HashMap (java.util.HashMap)9 File (java.io.File)6 Test (org.junit.Test)4 Path (java.nio.file.Path)3 InstanceSpec (org.apache.curator.test.InstanceSpec)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 KeyStore (java.security.KeyStore)2 ArrayList (java.util.ArrayList)2 Level (java.util.logging.Level)2