Search in sources :

Example 1 with TopicExistsException

use of kafka.common.TopicExistsException in project metron by apache.

the class KafkaComponent method createTopic.

public void createTopic(String name, int numPartitions, boolean waitUntilMetadataIsPropagated) 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 (waitUntilMetadataIsPropagated) {
            waitUntilMetadataIsPropagated(name, numPartitions);
        }
    } 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)

Example 2 with TopicExistsException

use of kafka.common.TopicExistsException in project druid by druid-io.

the class ITKafkaIndexingServiceTest method testKafka.

@Test
public void testKafka() {
    LOG.info("Starting test: ITKafkaIndexingServiceTest");
    // create topic
    try {
        int sessionTimeoutMs = 10000;
        int connectionTimeoutMs = 10000;
        String zkHosts = config.getZookeeperHosts();
        zkClient = new ZkClient(zkHosts, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
        int numPartitions = 4;
        int replicationFactor = 1;
        Properties topicConfig = new Properties();
        AdminUtils.createTopic(zkClient, TOPIC_NAME, numPartitions, replicationFactor, topicConfig);
    } catch (TopicExistsException e) {
    // it's ok if the topic already exists
    } catch (Exception e) {
        throw new ISE(e, "could not create kafka topic");
    }
    String spec;
    try {
        LOG.info("supervisorSpec name: [%s]", INDEXER_FILE);
        spec = getTaskAsString(INDEXER_FILE).replaceAll("%%DATASOURCE%%", DATASOURCE).replaceAll("%%TOPIC%%", TOPIC_NAME).replaceAll("%%KAFKA_BROKER%%", config.getKafkaHost());
        LOG.info("supervisorSpec: [%s]\n", spec);
    } catch (Exception e) {
        LOG.error("could not read file [%s]", INDEXER_FILE);
        throw new ISE(e, "could not read file [%s]", INDEXER_FILE);
    }
    // start supervisor
    supervisorId = indexer.submitSupervisor(spec);
    LOG.info("Submitted supervisor");
    // set up kafka producer
    Properties properties = new Properties();
    properties.put("bootstrap.servers", config.getKafkaHost());
    LOG.info("Kafka bootstrap.servers: [%s]", config.getKafkaHost());
    properties.put("acks", "all");
    properties.put("retries", "3");
    KafkaProducer<String, String> producer = new KafkaProducer<>(properties, new StringSerializer(), new StringSerializer());
    DateTimeZone zone = DateTimeZone.forID("UTC");
    // format for putting into events
    DateTimeFormatter event_fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
    // timestamp to put on events
    DateTime dt = new DateTime(zone);
    // timestamp of 1st event
    dtFirst = dt;
    // timestamp of last event
    dtLast = dt;
    // these are used to compute the expected aggregations
    int added = 0;
    int num_events = 0;
    // send data to kafka
    while (num_events < NUM_EVENTS_TO_SEND) {
        num_events++;
        added += num_events;
        // construct the event to send
        String event = String.format(event_template, event_fmt.print(dt), num_events, 0, num_events);
        LOG.info("sending event: [%s]", event);
        try {
            producer.send(new ProducerRecord<String, String>(TOPIC_NAME, event)).get();
        } catch (Exception ioe) {
            throw Throwables.propagate(ioe);
        }
        dtLast = dt;
        dt = new DateTime(zone);
    }
    producer.close();
    LOG.info("Waiting for [%s] millis for Kafka indexing tasks to consume events", WAIT_TIME_MILLIS);
    try {
        Thread.sleep(WAIT_TIME_MILLIS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    }
    InputStream is = ITKafkaIndexingServiceTest.class.getResourceAsStream(QUERIES_FILE);
    if (null == is) {
        throw new ISE("could not open query file: %s", QUERIES_FILE);
    }
    // put the timestamps into the query structure
    String query_response_template;
    try {
        query_response_template = IOUtils.toString(is, "UTF-8");
    } catch (IOException e) {
        throw new ISE(e, "could not read query file: %s", QUERIES_FILE);
    }
    String queryStr = query_response_template.replaceAll("%%DATASOURCE%%", DATASOURCE).replace("%%TIMEBOUNDARY_RESPONSE_TIMESTAMP%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMEBOUNDARY_RESPONSE_MAXTIME%%", TIMESTAMP_FMT.print(dtLast)).replace("%%TIMEBOUNDARY_RESPONSE_MINTIME%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMESERIES_QUERY_START%%", INTERVAL_FMT.print(dtFirst)).replace("%%TIMESERIES_QUERY_END%%", INTERVAL_FMT.print(dtLast.plusMinutes(2))).replace("%%TIMESERIES_RESPONSE_TIMESTAMP%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMESERIES_ADDED%%", Integer.toString(added)).replace("%%TIMESERIES_NUMEVENTS%%", Integer.toString(num_events));
    // this query will probably be answered from the indexing tasks but possibly from 2 historical segments / 2 indexing
    try {
        this.queryHelper.testQueriesFromString(queryStr, 2);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    LOG.info("Shutting down Kafka Supervisor");
    indexer.shutdownSupervisor(supervisorId);
    // wait for all kafka indexing tasks to finish
    LOG.info("Waiting for all kafka indexing tasks to finish");
    RetryUtil.retryUntilTrue(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return (indexer.getPendingTasks().size() + indexer.getRunningTasks().size() + indexer.getWaitingTasks().size()) == 0;
        }
    }, "Waiting for Tasks Completion");
    // wait for segments to be handed off
    try {
        RetryUtil.retryUntil(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return coordinator.areSegmentsLoaded(DATASOURCE);
            }
        }, true, 30000, 10, "Real-time generated segments loaded");
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    LOG.info("segments are present");
    segmentsExist = true;
    // this query will be answered by at least 1 historical segment, most likely 2, and possibly up to all 4
    try {
        this.queryHelper.testQueriesFromString(queryStr, 2);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) TopicExistsException(kafka.common.TopicExistsException) TopicExistsException(kafka.common.TopicExistsException) IOException(java.io.IOException) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ISE(io.druid.java.util.common.ISE) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Example 3 with TopicExistsException

use of kafka.common.TopicExistsException in project druid by druid-io.

the class ITKafkaTest method testKafka.

@Test
public void testKafka() {
    LOG.info("Starting test: ITKafkaTest");
    // create topic
    try {
        int sessionTimeoutMs = 10000;
        int connectionTimeoutMs = 10000;
        String zkHosts = config.getZookeeperHosts();
        zkClient = new ZkClient(zkHosts, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
        int numPartitions = 1;
        int replicationFactor = 1;
        Properties topicConfig = new Properties();
        AdminUtils.createTopic(zkClient, TOPIC_NAME, numPartitions, replicationFactor, topicConfig);
    } catch (TopicExistsException e) {
    // it's ok if the topic already exists
    } catch (Exception e) {
        throw new ISE(e, "could not create kafka topic");
    }
    // set up kafka producer
    Properties properties = new Properties();
    properties.put("bootstrap.servers", config.getKafkaHost());
    LOG.info("Kafka bootstrap.servers: [%s]", config.getKafkaHost());
    properties.put("acks", "all");
    properties.put("retries", "3");
    KafkaProducer<String, String> producer = new KafkaProducer<>(properties, new StringSerializer(), new StringSerializer());
    DateTimeZone zone = DateTimeZone.forID("UTC");
    // format for putting into events
    DateTimeFormatter event_fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
    // timestamp to put on events
    DateTime dt = new DateTime(zone);
    // timestamp of 1st event
    dtFirst = dt;
    // timestamp of last event
    dtLast = dt;
    // these are used to compute the expected aggregations
    int added = 0;
    int num_events = 10;
    // send data to kafka
    for (int i = 0; i < num_events; i++) {
        added += i;
        // construct the event to send
        String event = String.format(event_template, event_fmt.print(dt), i, 0, i);
        LOG.info("sending event: [%s]", event);
        try {
            // Send event to kafka
            producer.send(new ProducerRecord<String, String>(TOPIC_NAME, event)).get();
        } catch (Exception ioe) {
            throw Throwables.propagate(ioe);
        }
        dtLast = dt;
        dt = new DateTime(zone);
    }
    producer.close();
    String indexerSpec;
    // replace temp strings in indexer file
    try {
        LOG.info("indexerFile name: [%s]", INDEXER_FILE);
        indexerSpec = getTaskAsString(INDEXER_FILE).replaceAll("%%DATASOURCE%%", DATASOURCE).replaceAll("%%TOPIC%%", TOPIC_NAME).replaceAll("%%ZOOKEEPER_SERVER%%", config.getZookeeperHosts()).replaceAll("%%GROUP_ID%%", Long.toString(System.currentTimeMillis())).replaceAll("%%COUNT%%", Integer.toString(num_events));
        LOG.info("indexerFile: [%s]\n", indexerSpec);
    } catch (Exception e) {
        // log here so the message will appear in the console output
        LOG.error("could not read indexer file [%s]", INDEXER_FILE);
        throw new ISE(e, "could not read indexer file [%s]", INDEXER_FILE);
    }
    // start indexing task
    taskID = indexer.submitTask(indexerSpec);
    LOG.info("-------------SUBMITTED TASK");
    // wait for the task to finish
    indexer.waitUntilTaskCompletes(taskID, 20000, 30);
    // wait for segments to be handed off
    try {
        RetryUtil.retryUntil(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return coordinator.areSegmentsLoaded(DATASOURCE);
            }
        }, true, 30000, 10, "Real-time generated segments loaded");
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    LOG.info("segments are present");
    segmentsExist = true;
    // put the timestamps into the query structure
    String query_response_template = null;
    InputStream is = ITKafkaTest.class.getResourceAsStream(QUERIES_FILE);
    if (null == is) {
        throw new ISE("could not open query file: %s", QUERIES_FILE);
    }
    try {
        query_response_template = IOUtils.toString(is, "UTF-8");
    } catch (IOException e) {
        throw new ISE(e, "could not read query file: %s", QUERIES_FILE);
    }
    String queryStr = query_response_template.replaceAll("%%DATASOURCE%%", DATASOURCE).replace("%%TIMEBOUNDARY_RESPONSE_TIMESTAMP%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMEBOUNDARY_RESPONSE_MAXTIME%%", TIMESTAMP_FMT.print(dtLast)).replace("%%TIMEBOUNDARY_RESPONSE_MINTIME%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMESERIES_QUERY_START%%", INTERVAL_FMT.print(dtFirst)).replace("%%TIMESERIES_QUERY_END%%", INTERVAL_FMT.print(dtFirst.plusMinutes(MINUTES_TO_SEND + 2))).replace("%%TIMESERIES_RESPONSE_TIMESTAMP%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMESERIES_ADDED%%", Integer.toString(added)).replace("%%TIMESERIES_NUMEVENTS%%", Integer.toString(num_events));
    // this query will probably be answered from the realtime task
    try {
        this.queryHelper.testQueriesFromString(queryStr, 2);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) TopicExistsException(kafka.common.TopicExistsException) TopicExistsException(kafka.common.TopicExistsException) IOException(java.io.IOException) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ISE(io.druid.java.util.common.ISE) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Aggregations

Properties (java.util.Properties)3 TopicExistsException (kafka.common.TopicExistsException)3 ISE (io.druid.java.util.common.ISE)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ZkClient (org.I0Itec.zkclient.ZkClient)2 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 DateTime (org.joda.time.DateTime)2 DateTimeZone (org.joda.time.DateTimeZone)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 Test (org.testng.annotations.Test)2 Level (java.util.logging.Level)1 ZkUtils (kafka.utils.ZkUtils)1