Search in sources :

Example 1 with Context

use of io.mantisrx.runtime.Context in project mantis by Netflix.

the class MantisKafkaConsumerConfigTest method testJobParamOverrides.

@Test
public void testJobParamOverrides() {
    Context context = mock(Context.class);
    String testTopic = "topic123";
    String testConsumerGroupId = "testKafkaConsumer-1";
    Parameters params = ParameterTestUtils.createParameters(KafkaSourceParameters.TOPIC, testTopic, KafkaSourceParameters.PREFIX + ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest", KafkaSourceParameters.PREFIX + ConsumerConfig.GROUP_ID_CONFIG, testConsumerGroupId, KafkaSourceParameters.PREFIX + ConsumerConfig.DEFAULT_API_TIMEOUT_MS_CONFIG, 500);
    when(context.getParameters()).then((Answer<Parameters>) invocation -> params);
    MantisKafkaConsumerConfig mantisKafkaConsumerConfig = new MantisKafkaConsumerConfig(context);
    Map<String, Object> consumerProperties = mantisKafkaConsumerConfig.getConsumerProperties();
    // MantisKafkaConsumerConfig only affects Kafka's ConsumerConfig defined properties
    assertFalse(ConsumerConfig.configNames().contains(KafkaSourceParameters.TOPIC));
    assertFalse(consumerProperties.containsKey(KafkaSourceParameters.TOPIC));
    assertEquals("earliest", consumerProperties.get(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG));
    assertEquals(testConsumerGroupId, consumerProperties.get(ConsumerConfig.GROUP_ID_CONFIG));
    assertEquals(500, consumerProperties.get(ConsumerConfig.DEFAULT_API_TIMEOUT_MS_CONFIG));
}
Also used : Context(io.mantisrx.runtime.Context) KafkaSourceParameters(io.mantisrx.connector.kafka.KafkaSourceParameters) Arrays(java.util.Arrays) ParameterTestUtils(io.mantisrx.connector.kafka.ParameterTestUtils) Parameters(io.mantisrx.runtime.parameter.Parameters) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Context(io.mantisrx.runtime.Context) Answer(org.mockito.stubbing.Answer) RangeAssignor(org.apache.kafka.clients.consumer.RangeAssignor) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) JmxReporter(org.apache.kafka.common.metrics.JmxReporter) KafkaSourceParameters(io.mantisrx.connector.kafka.KafkaSourceParameters) Parameters(io.mantisrx.runtime.parameter.Parameters) Test(org.junit.Test)

Example 2 with Context

use of io.mantisrx.runtime.Context in project mantis by Netflix.

the class MantisKafkaSourceConfigTest method testJobParamOverrides.

@Test
public void testJobParamOverrides() {
    Context context = mock(Context.class);
    String testTopic = "topic123";
    int checkpointIntervalOverride = 100;
    boolean staticPartitionAssignEnableOverride = true;
    Parameters params = ParameterTestUtils.createParameters(KafkaSourceParameters.TOPIC, testTopic, KafkaSourceParameters.CHECKPOINT_INTERVAL_MS, checkpointIntervalOverride, KafkaSourceParameters.ENABLE_STATIC_PARTITION_ASSIGN, staticPartitionAssignEnableOverride, KafkaSourceParameters.TOPIC_PARTITION_COUNTS, testTopic + ":1024");
    when(context.getParameters()).then((Answer<Parameters>) invocation -> params);
    MantisKafkaSourceConfig mantisKafkaSourceConfig = new MantisKafkaSourceConfig(context);
    assertEquals(checkpointIntervalOverride, mantisKafkaSourceConfig.getCheckpointIntervalMs());
    assertEquals(staticPartitionAssignEnableOverride, mantisKafkaSourceConfig.getStaticPartitionAssignmentEnabled());
    Map<String, Integer> topicPartitionCounts = new HashMap<>();
    topicPartitionCounts.put(testTopic, 1024);
    assertEquals(Optional.ofNullable(topicPartitionCounts), mantisKafkaSourceConfig.getTopicPartitionCounts());
}
Also used : Context(io.mantisrx.runtime.Context) KafkaSourceParameters(io.mantisrx.connector.kafka.KafkaSourceParameters) Arrays(java.util.Arrays) ParserType(io.mantisrx.connector.kafka.source.serde.ParserType) ParameterTestUtils(io.mantisrx.connector.kafka.ParameterTestUtils) Parameters(io.mantisrx.runtime.parameter.Parameters) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Context(io.mantisrx.runtime.Context) CheckpointStrategyOptions(io.mantisrx.connector.kafka.source.checkpoint.strategy.CheckpointStrategyOptions) Answer(org.mockito.stubbing.Answer) Map(java.util.Map) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) KafkaSourceParameters(io.mantisrx.connector.kafka.KafkaSourceParameters) Parameters(io.mantisrx.runtime.parameter.Parameters) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with Context

use of io.mantisrx.runtime.Context in project mantis by Netflix.

the class KafkaSinkTest method testKafkaSink.

@Test
public void testKafkaSink() throws InterruptedException {
    String testTopic = "testTopic" + topicNum.incrementAndGet();
    int numPartitions = 1;
    kafkaServer.createTopic(testTopic, numPartitions);
    int numMessages = 10;
    KafkaSink<String> kafkaSink = new KafkaSink<>(new NoopRegistry(), s -> s.getBytes());
    Context context = mock(Context.class);
    Parameters params = ParameterTestUtils.createParameters(KafkaSinkJobParameters.TOPIC, testTopic);
    when(context.getParameters()).then((Answer<Parameters>) invocation -> params);
    when(context.getWorkerInfo()).then((Answer<WorkerInfo>) invocation -> new WorkerInfo("testJobName", "testJobName-1", 1, 0, 1, MantisJobDurationType.Perpetual, "1.1.1.1"));
    when(context.getJobId()).then((Answer<String>) invocation -> "testJobName-1");
    kafkaSink.call(context, mock(PortRequest.class), Observable.range(0, numMessages).map(x -> String.valueOf(x)));
    List<String> messages = kafkaServer.readAllMessages(testTopic);
    LOGGER.info("got {}", messages);
    assertEquals(numMessages, messages.size());
    for (int i = 0; i < numMessages; i++) {
        assertEquals(i, Integer.parseInt(messages.get(i)));
    }
    kafkaServer.deleteTopic(testTopic);
}
Also used : Context(io.mantisrx.runtime.Context) NoopRegistry(com.netflix.spectator.api.NoopRegistry) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) BeforeClass(org.junit.BeforeClass) KafkaSourceTest(io.mantisrx.connector.kafka.source.KafkaSourceTest) MantisJobDurationType(io.mantisrx.runtime.MantisJobDurationType) LoggerFactory(org.slf4j.LoggerFactory) PortRequest(io.mantisrx.runtime.PortRequest) ParameterTestUtils(io.mantisrx.connector.kafka.ParameterTestUtils) Parameters(io.mantisrx.runtime.parameter.Parameters) Random(java.util.Random) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Context(io.mantisrx.runtime.Context) Observable(rx.Observable) Answer(org.mockito.stubbing.Answer) List(java.util.List) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KafkaUnit(info.batey.kafka.unit.KafkaUnit) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WorkerInfo(io.mantisrx.runtime.WorkerInfo) Parameters(io.mantisrx.runtime.parameter.Parameters) NoopRegistry(com.netflix.spectator.api.NoopRegistry) PortRequest(io.mantisrx.runtime.PortRequest) WorkerInfo(io.mantisrx.runtime.WorkerInfo) KafkaSourceTest(io.mantisrx.connector.kafka.source.KafkaSourceTest) Test(org.junit.Test)

Example 4 with Context

use of io.mantisrx.runtime.Context in project mantis by Netflix.

the class KafkaSourceTest method testKafkaSourceSingleConsumerReadsAllMessagesInOrderFromSinglePartition.

@Test
public void testKafkaSourceSingleConsumerReadsAllMessagesInOrderFromSinglePartition() throws InterruptedException {
    String testTopic = "testTopic" + topicNum.incrementAndGet();
    int numPartitions = 1;
    kafkaServer.createTopic(testTopic, numPartitions);
    int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ProducerRecord<String, String> keyedMessage = new ProducerRecord<>(testTopic, "{\"messageNum\":" + i + "}");
        kafkaServer.sendMessages(keyedMessage);
    }
    KafkaSource kafkaSource = new KafkaSource(new NoopRegistry());
    Context context = mock(Context.class);
    Parameters params = ParameterTestUtils.createParameters(KafkaSourceParameters.TOPIC, testTopic, KafkaSourceParameters.PREFIX + ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest", KafkaSourceParameters.PREFIX + ConsumerConfig.GROUP_ID_CONFIG, "testKafkaConsumer-" + random.nextInt());
    when(context.getParameters()).then((Answer<Parameters>) invocation -> params);
    when(context.getWorkerInfo()).then((Answer<WorkerInfo>) invocation -> new WorkerInfo("testJobName", "testJobName-1", 1, 0, 1, MantisJobDurationType.Perpetual, "1.1.1.1"));
    when(context.getJobId()).then((Answer<String>) invocation -> "testJobName-1");
    Index index = new Index(0, 10);
    Observable<Observable<KafkaAckable>> sourceObs = kafkaSource.call(context, index);
    final CountDownLatch latch = new CountDownLatch(numMessages);
    final AtomicInteger counter = new AtomicInteger(0);
    sourceObs.flatMap(kafkaAckableObs -> kafkaAckableObs).map(kafkaAckable -> {
        Optional<Map<String, Object>> parsedEvent = kafkaAckable.getKafkaData().getParsedEvent();
        assertTrue(parsedEvent.isPresent());
        assertEquals(counter.getAndIncrement(), parsedEvent.get().get("messageNum"));
        LOGGER.info("got message on topic {} consumer Id {}", parsedEvent.get(), kafkaAckable.getKafkaData().getMantisKafkaConsumerId());
        kafkaAckable.ack();
        latch.countDown();
        return parsedEvent;
    }).subscribe();
    assertTrue("timed out waiting to get all messages from Kafka", latch.await(10, TimeUnit.SECONDS));
    kafkaServer.deleteTopic(testTopic);
}
Also used : Context(io.mantisrx.runtime.Context) NoopRegistry(com.netflix.spectator.api.NoopRegistry) Index(io.mantisrx.runtime.source.Index) KafkaSourceParameters(io.mantisrx.connector.kafka.KafkaSourceParameters) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) BeforeClass(org.junit.BeforeClass) MantisJobDurationType(io.mantisrx.runtime.MantisJobDurationType) LoggerFactory(org.slf4j.LoggerFactory) ParameterTestUtils(io.mantisrx.connector.kafka.ParameterTestUtils) Parameters(io.mantisrx.runtime.parameter.Parameters) Random(java.util.Random) Observable(rx.Observable) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KafkaAckable(io.mantisrx.connector.kafka.KafkaAckable) Map(java.util.Map) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) TestCase.fail(junit.framework.TestCase.fail) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Context(io.mantisrx.runtime.Context) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Ignore(org.junit.Ignore) KafkaUnit(info.batey.kafka.unit.KafkaUnit) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WorkerInfo(io.mantisrx.runtime.WorkerInfo) KafkaSourceParameters(io.mantisrx.connector.kafka.KafkaSourceParameters) Parameters(io.mantisrx.runtime.parameter.Parameters) Optional(java.util.Optional) WorkerInfo(io.mantisrx.runtime.WorkerInfo) Index(io.mantisrx.runtime.source.Index) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(rx.Observable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NoopRegistry(com.netflix.spectator.api.NoopRegistry) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 5 with Context

use of io.mantisrx.runtime.Context in project mantis by Netflix.

the class SinePointGeneratorStage method call.

@Override
public Observable<Point> call(Context context, Observable<Integer> o) {
    final double amplitude = (double) context.getParameters().get(SineFunctionJob.AMPLITUDE);
    final double frequency = (double) context.getParameters().get(SineFunctionJob.FREQUENCY);
    final double phase = (double) context.getParameters().get(SineFunctionJob.PHASE);
    return o.filter(x -> x % 2 == 0).map(x -> new Point(x, amplitude * Math.sin((frequency * x) + phase)));
}
Also used : ScalarComputation(io.mantisrx.runtime.computation.ScalarComputation) Point(io.mantisrx.mantis.examples.sinefunction.core.Point) Context(io.mantisrx.runtime.Context) SineFunctionJob(io.mantisrx.mantis.examples.sinefunction.SineFunctionJob) Observable(rx.Observable) Point(io.mantisrx.mantis.examples.sinefunction.core.Point)

Aggregations

Context (io.mantisrx.runtime.Context)49 Test (org.junit.Test)34 CountDownLatch (java.util.concurrent.CountDownLatch)24 Index (io.mantisrx.runtime.source.Index)23 Observable (rx.Observable)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Action1 (rx.functions.Action1)19 Action0 (rx.functions.Action0)17 Map (java.util.Map)16 Parameters (io.mantisrx.runtime.parameter.Parameters)15 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 ParameterTestUtils (io.mantisrx.connector.kafka.ParameterTestUtils)11 TimeUnit (java.util.concurrent.TimeUnit)11 StageConfig (io.mantisrx.runtime.StageConfig)10 HashMap (java.util.HashMap)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 Endpoint (io.mantisrx.common.network.Endpoint)9 WorkerInfo (io.mantisrx.runtime.WorkerInfo)9 ByteBuf (io.netty.buffer.ByteBuf)8