Search in sources :

Example 1 with EmbeddedKafkaServer

use of org.apache.twill.internal.kafka.EmbeddedKafkaServer in project cdap by caskdata.

the class KafkaClientModuleTest method createKafkaServer.

private EmbeddedKafkaServer createKafkaServer(String zkConnectStr, File dir) throws Exception {
    // Don't set port, EmbeddedKafkaServer will find a ephemeral port.
    Properties properties = new Properties();
    properties.setProperty("broker.id", "1");
    properties.setProperty("num.network.threads", "2");
    properties.setProperty("num.io.threads", "2");
    properties.setProperty("socket.send.buffer.bytes", "1048576");
    properties.setProperty("socket.receive.buffer.bytes", "1048576");
    properties.setProperty("socket.request.max.bytes", "104857600");
    properties.setProperty("log.dir", dir.getAbsolutePath());
    properties.setProperty("num.partitions", "1");
    properties.setProperty("log.flush.interval.messages", "10000");
    properties.setProperty("log.flush.interval.ms", "1000");
    properties.setProperty("log.retention.hours", "1");
    properties.setProperty("log.segment.bytes", "536870912");
    properties.setProperty("log.cleanup.interval.mins", "1");
    properties.setProperty("zookeeper.connect", zkConnectStr);
    properties.setProperty("zookeeper.connection.timeout.ms", "1000000");
    return new EmbeddedKafkaServer(properties);
}
Also used : EmbeddedKafkaServer(org.apache.twill.internal.kafka.EmbeddedKafkaServer) Properties(java.util.Properties)

Example 2 with EmbeddedKafkaServer

use of org.apache.twill.internal.kafka.EmbeddedKafkaServer in project cdap by caskdata.

the class KafkaTester method before.

@Override
protected void before() throws Throwable {
    int kafkaPort = Networks.getRandomPort();
    Preconditions.checkState(kafkaPort > 0, "Failed to get random port.");
    int zkServerPort = Networks.getRandomPort();
    Preconditions.checkState(zkServerPort > 0, "Failed to get random port.");
    tmpFolder.create();
    zkServer = InMemoryZKServer.builder().setDataDir(tmpFolder.newFolder()).setPort(zkServerPort).build();
    zkServer.startAndWait();
    LOG.info("In memory ZK started on {}", zkServer.getConnectionStr());
    kafkaServer = new EmbeddedKafkaServer(generateKafkaConfig(kafkaPort));
    kafkaServer.startAndWait();
    initializeCConf(kafkaPort);
    injector = createInjector();
    zkClient = injector.getInstance(ZKClientService.class);
    zkClient.startAndWait();
    kafkaClient = injector.getInstance(KafkaClientService.class);
    kafkaClient.startAndWait();
    brokerService = injector.getInstance(BrokerService.class);
    brokerService.startAndWait();
    LOG.info("Waiting for Kafka server to startup...");
    waitForKafkaStartup();
    LOG.info("Started kafka server on port {}", kafkaPort);
}
Also used : KafkaClientService(org.apache.twill.kafka.client.KafkaClientService) ZKClientService(org.apache.twill.zookeeper.ZKClientService) EmbeddedKafkaServer(org.apache.twill.internal.kafka.EmbeddedKafkaServer) BrokerService(org.apache.twill.kafka.client.BrokerService)

Example 3 with EmbeddedKafkaServer

use of org.apache.twill.internal.kafka.EmbeddedKafkaServer in project cdap by caskdata.

the class SpamClassifierTest method init.

@BeforeClass
public static void init() throws Exception {
    zkServer = InMemoryZKServer.builder().setDataDir(TMP_FOLDER.newFolder()).build();
    zkServer.startAndWait();
    kafkaPort = Networks.getRandomPort();
    kafkaServer = new EmbeddedKafkaServer(generateKafkaConfig(zkServer.getConnectionStr(), kafkaPort, TMP_FOLDER.newFolder()));
    kafkaServer.startAndWait();
    zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
    zkClient.startAndWait();
    kafkaClient = new ZKKafkaClientService(zkClient);
    kafkaClient.startAndWait();
}
Also used : ZKKafkaClientService(org.apache.twill.internal.kafka.client.ZKKafkaClientService) EmbeddedKafkaServer(org.apache.twill.internal.kafka.EmbeddedKafkaServer) BeforeClass(org.junit.BeforeClass)

Example 4 with EmbeddedKafkaServer

use of org.apache.twill.internal.kafka.EmbeddedKafkaServer in project cdap by caskdata.

the class KafkaServerMain method start.

@Override
public void start() {
    LOG.info("Starting embedded kafka server...");
    kafkaServer = new EmbeddedKafkaServer(kafkaProperties);
    Service.State state = kafkaServer.startAndWait();
    if (state != Service.State.RUNNING) {
        throw new IllegalStateException("Kafka server has not started... terminating.");
    }
    LOG.info("Embedded kafka server started successfully.");
}
Also used : ZKClientService(org.apache.twill.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) EmbeddedKafkaServer(org.apache.twill.internal.kafka.EmbeddedKafkaServer)

Example 5 with EmbeddedKafkaServer

use of org.apache.twill.internal.kafka.EmbeddedKafkaServer in project cdap by caskdata.

the class MetricsProcessorServiceTest method testMetricsProcessor.

@Test
public void testMetricsProcessor() throws Exception {
    injector.getInstance(TransactionManager.class).startAndWait();
    injector.getInstance(DatasetOpExecutor.class).startAndWait();
    injector.getInstance(DatasetService.class).startAndWait();
    zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    Properties kafkaConfig = generateKafkaConfig(tmpFolder1);
    EmbeddedKafkaServer kafkaServer = new EmbeddedKafkaServer(kafkaConfig);
    kafkaServer.startAndWait();
    ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
    zkClient.startAndWait();
    KafkaClientService kafkaClient = new ZKKafkaClientService(zkClient);
    kafkaClient.startAndWait();
    final MetricStore metricStore = injector.getInstance(MetricStore.class);
    Set<Integer> partitions = new HashSet<>();
    for (int i = 0; i < PARTITION_SIZE; i++) {
        partitions.add(i);
    }
    KafkaPublisher publisher = kafkaClient.getPublisher(KafkaPublisher.Ack.FIRE_AND_FORGET, Compression.SNAPPY);
    final KafkaPublisher.Preparer preparer = publisher.prepare(TOPIC_PREFIX);
    // Wait for metrics to be successfully published to Kafka. Retry if publishing fails.
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return publishKafkaMetrics(METRICS_CONTEXT, expected, preparer);
        }
    }, 15, TimeUnit.SECONDS, "Failed to publish correct number of metrics to Kafka");
    // Start KafkaMetricsProcessorService after metrics are published to Kafka
    KafkaMetricsProcessorService kafkaMetricsProcessorService = new KafkaMetricsProcessorService(kafkaClient, injector.getInstance(MetricDatasetFactory.class), new MetricsMessageCallbackFactory(injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, 4), TOPIC_PREFIX, partitions, new NoopMetricsContext());
    kafkaMetricsProcessorService.startAndWait();
    // Intentionally set queue size to a small value, so that MessagingMetricsProcessorService
    // internally can persist metrics when more messages are to be fetched
    MessagingMetricsProcessorService messagingMetricsProcessorService = new MessagingMetricsProcessorService(injector.getInstance(MetricDatasetFactory.class), TOPIC_PREFIX, messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, 1000L, 5, partitions, new NoopMetricsContext(), 50, 0);
    messagingMetricsProcessorService.startAndWait();
    long startTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    // Publish metrics with messaging service and record expected metrics
    for (int i = 10; i < 20; i++) {
        publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, SYSTEM_METRIC_PREFIX, MetricType.COUNTER);
    }
    Thread.sleep(500);
    // Stop and restart messagingMetricsProcessorService
    messagingMetricsProcessorService.stopAndWait();
    // Intentionally set queue size to a large value, so that MessagingMetricsProcessorService
    // internally only persists metrics during terminating.
    messagingMetricsProcessorService = new MessagingMetricsProcessorService(injector.getInstance(MetricDatasetFactory.class), TOPIC_PREFIX, messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, 500L, 100, partitions, new NoopMetricsContext(), 50, 0);
    messagingMetricsProcessorService.startAndWait();
    // Publish metrics after MessagingMetricsProcessorService restarts and record expected metrics
    for (int i = 20; i < 30; i++) {
        publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, SYSTEM_METRIC_PREFIX, MetricType.GAUGE);
    }
    final List<String> missingMetricNames = new ArrayList<>();
    // are retrieved when timeout occurs, print out the missing metrics
    try {
        Tasks.waitFor(true, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return canQueryAllMetrics(metricStore, METRICS_CONTEXT, expected, missingMetricNames);
            }
        }, 10000, TimeUnit.MILLISECONDS, "Failed to get all metrics");
    } catch (TimeoutException e) {
        Assert.fail(String.format("Metrics: [%s] cannot be found in the metrics store.", Joiner.on(", ").join(missingMetricNames)));
    }
    // Query metrics from the metricStore and compare them with the expected ones
    assertMetricsResult(metricStore, METRICS_CONTEXT, expected);
    // Query for the 5 counter metrics published with messaging between time 5 - 14
    Collection<MetricTimeSeries> queryResult = metricStore.query(new MetricDataQuery(5, 14, 1, Integer.MAX_VALUE, ImmutableMap.of(SYSTEM_METRIC_PREFIX + COUNTER_METRIC_NAME, AggregationFunction.SUM), METRICS_CONTEXT, ImmutableList.<String>of(), null));
    MetricTimeSeries timeSeries = Iterables.getOnlyElement(queryResult);
    Assert.assertEquals(5, timeSeries.getTimeValues().size());
    for (TimeValue timeValue : timeSeries.getTimeValues()) {
        Assert.assertEquals(1L, timeValue.getValue());
    }
    // Stop services and servers
    kafkaMetricsProcessorService.stopAndWait();
    messagingMetricsProcessorService.stopAndWait();
    kafkaServer.stopAndWait();
    zkServer.stopAndWait();
    // Delete all metrics
    metricStore.deleteAll();
}
Also used : MetricStore(co.cask.cdap.api.metrics.MetricStore) ZKKafkaClientService(org.apache.twill.internal.kafka.client.ZKKafkaClientService) DatumReaderFactory(co.cask.cdap.internal.io.DatumReaderFactory) ArrayList(java.util.ArrayList) MetricTimeSeries(co.cask.cdap.api.metrics.MetricTimeSeries) DatasetService(co.cask.cdap.data2.datafabric.dataset.service.DatasetService) Properties(java.util.Properties) NoopMetricsContext(co.cask.cdap.api.metrics.NoopMetricsContext) MetricDatasetFactory(co.cask.cdap.metrics.store.MetricDatasetFactory) EmbeddedKafkaServer(org.apache.twill.internal.kafka.EmbeddedKafkaServer) TimeValue(co.cask.cdap.api.dataset.lib.cube.TimeValue) HashSet(java.util.HashSet) KafkaPublisher(org.apache.twill.kafka.client.KafkaPublisher) TimeoutException(java.util.concurrent.TimeoutException) ZKKafkaClientService(org.apache.twill.internal.kafka.client.ZKKafkaClientService) KafkaClientService(org.apache.twill.kafka.client.KafkaClientService) SchemaGenerator(co.cask.cdap.internal.io.SchemaGenerator) DatasetOpExecutor(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutor) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ZKClientService(org.apache.twill.zookeeper.ZKClientService) TransactionManager(org.apache.tephra.TransactionManager) MetricDataQuery(co.cask.cdap.api.metrics.MetricDataQuery) Test(org.junit.Test)

Aggregations

EmbeddedKafkaServer (org.apache.twill.internal.kafka.EmbeddedKafkaServer)5 ZKClientService (org.apache.twill.zookeeper.ZKClientService)3 Properties (java.util.Properties)2 ZKKafkaClientService (org.apache.twill.internal.kafka.client.ZKKafkaClientService)2 KafkaClientService (org.apache.twill.kafka.client.KafkaClientService)2 TimeValue (co.cask.cdap.api.dataset.lib.cube.TimeValue)1 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)1 MetricDataQuery (co.cask.cdap.api.metrics.MetricDataQuery)1 MetricStore (co.cask.cdap.api.metrics.MetricStore)1 MetricTimeSeries (co.cask.cdap.api.metrics.MetricTimeSeries)1 NoopMetricsContext (co.cask.cdap.api.metrics.NoopMetricsContext)1 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)1 DatasetOpExecutor (co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutor)1 DatumReaderFactory (co.cask.cdap.internal.io.DatumReaderFactory)1 SchemaGenerator (co.cask.cdap.internal.io.SchemaGenerator)1 MetricDatasetFactory (co.cask.cdap.metrics.store.MetricDatasetFactory)1 Service (com.google.common.util.concurrent.Service)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1