Search in sources :

Example 1 with BulkMessageWriter

use of org.apache.metron.common.writer.BulkMessageWriter in project metron by apache.

the class BulkMessageWriterBoltTest method testFlushOnBatchTimeout.

@Test
public void testFlushOnBatchTimeout() throws Exception {
    FakeClock clock = new FakeClock();
    bulkMessageWriterBolt = bulkMessageWriterBolt.withBatchTimeoutDivisor(3);
    bulkMessageWriterBolt.setCuratorFramework(client);
    bulkMessageWriterBolt.setZKCache(cache);
    bulkMessageWriterBolt.getConfigurations().updateSensorIndexingConfig(BaseEnrichmentBoltTest.sensorType, new FileInputStream("../" + BaseEnrichmentBoltTest.sampleSensorIndexingConfigPath));
    {
        bulkMessageWriterBolt.declareOutputFields(declarer);
        verify(declarer, times(1)).declareStream(eq("error"), argThat(new FieldsMatcher("message")));
    }
    {
        Map stormConf = new HashMap();
        when(bulkMessageWriter.getName()).thenReturn("elasticsearch");
        bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector, clock);
        verify(bulkMessageWriter, times(1)).init(eq(stormConf), any(WriterConfiguration.class));
    }
    {
        int batchTimeout = bulkMessageWriterBolt.getMaxBatchTimeout();
        assertEquals(4, batchTimeout);
        for (int i = 0; i < 4; i++) {
            bulkMessageWriterBolt.execute(tupleList.get(i));
            verify(bulkMessageWriter, times(0)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), any(List.class));
        }
        clock.elapseSeconds(5);
        BulkWriterResponse response = new BulkWriterResponse();
        response.addAllSuccesses(messageIdList);
        when(bulkMessageWriter.write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList))).thenReturn(response);
        bulkMessageWriterBolt.execute(tupleList.get(4));
        verify(bulkMessageWriter, times(1)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList));
        tupleList.forEach(tuple -> verify(outputCollector, times(1)).ack(tuple));
    }
    Mockito.verifyNoMoreInteractions(outputCollector);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) UnitTestHelper(org.apache.metron.test.utils.UnitTestHelper) HashMap(java.util.HashMap) MessageId(org.apache.metron.common.writer.MessageId) Mockito.argThat(org.mockito.Mockito.argThat) Mockito.spy(org.mockito.Mockito.spy) WriterConfiguration(org.apache.metron.common.configuration.writer.WriterConfiguration) ArrayList(java.util.ArrayList) MessageGetters(org.apache.metron.storm.common.message.MessageGetters) Mockito.doThrow(org.mockito.Mockito.doThrow) MockitoAnnotations(org.mockito.MockitoAnnotations) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Values(org.apache.storm.tuple.Values) Tuple(org.apache.storm.tuple.Tuple) ParseException(org.json.simple.parser.ParseException) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Map(java.util.Map) Level(org.apache.log4j.Level) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) FakeClock(org.apache.metron.common.system.FakeClock) BulkWriterComponent(org.apache.metron.writer.BulkWriterComponent) JSONParser(org.json.simple.parser.JSONParser) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) FileInputStream(java.io.FileInputStream) Constants(org.apache.metron.common.Constants) IndexingConfigurations(org.apache.metron.common.configuration.IndexingConfigurations) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) JSONObject(org.json.simple.JSONObject) Mockito.anyList(org.mockito.Mockito.anyList) Multiline(org.adrianwalker.multilinestring.Multiline) Mockito.any(org.mockito.Mockito.any) Mockito.reset(org.mockito.Mockito.reset) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) FakeClock(org.apache.metron.common.system.FakeClock) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.jupiter.api.Test)

Example 2 with BulkMessageWriter

use of org.apache.metron.common.writer.BulkMessageWriter in project metron by apache.

the class ParserTopologyBuilderTest method shouldCreateWriterConfigWithSensorParserConfigOutputTopic.

@Test
public void shouldCreateWriterConfigWithSensorParserConfigOutputTopic() {
    SensorParserConfig snortConfig = new SensorParserConfig();
    snortConfig.setSensorTopic("snort");
    snortConfig.setOutputTopic("snort_topic");
    when(configs.getSensorParserConfig("snort")).thenReturn(snortConfig);
    Map<String, SensorParserConfig> sensorTypeToParserConfig = new HashMap<String, SensorParserConfig>() {

        {
            put("snort", snortConfig);
        }
    };
    Map<String, WriterHandler> writerConfigs = ParserTopologyBuilder.createWriterConfigs("zookeeperUrl", Optional.of("brokerUrl"), sensorTypeToParserConfig, Optional.of("securityProtocol"), configs, Optional.empty());
    assertEquals(1, writerConfigs.size());
    // Can't directly verify against mocks because this is all static. However, knowing that we have a KafkaWriter
    // and the appropriate topic lets us know we've created the underlying config.
    BulkMessageWriter writer = writerConfigs.get("snort").getBulkMessageWriter();
    assertTrue(writer instanceof KafkaWriter);
    assertEquals("snort_topic", ((KafkaWriter) writer).getKafkaTopic(new JSONObject()).get());
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) WriterHandler(org.apache.metron.parsers.bolt.WriterHandler) KafkaWriter(org.apache.metron.writer.kafka.KafkaWriter) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 3 with BulkMessageWriter

use of org.apache.metron.common.writer.BulkMessageWriter in project metron by apache.

the class ParserTopologyBuilderTest method shouldCreateWriterConfig.

@Test
public void shouldCreateWriterConfig() {
    SensorParserConfig broConfig = new SensorParserConfig();
    broConfig.setSensorTopic("bro");
    when(configs.getSensorParserConfig("bro")).thenReturn(broConfig);
    Map<String, SensorParserConfig> sensorTypeToParserConfig = new HashMap<String, SensorParserConfig>() {

        {
            put("bro", broConfig);
        }
    };
    Map<String, WriterHandler> writerConfigs = ParserTopologyBuilder.createWriterConfigs("zookeeperUrl", Optional.of("brokerUrl"), sensorTypeToParserConfig, Optional.of("securityProtocol"), configs, Optional.empty());
    assertEquals(1, writerConfigs.size());
    // Can't directly verify against mocks because this is all static. However, knowing that we have a KafkaWriter
    // and the appropriate topic lets us know we've created the underlying config.
    BulkMessageWriter writer = writerConfigs.get("bro").getBulkMessageWriter();
    assertTrue(writer instanceof KafkaWriter);
    assertEquals(Constants.ENRICHMENT_TOPIC, ((KafkaWriter) writer).getKafkaTopic(new JSONObject()).get());
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) WriterHandler(org.apache.metron.parsers.bolt.WriterHandler) KafkaWriter(org.apache.metron.writer.kafka.KafkaWriter) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 4 with BulkMessageWriter

use of org.apache.metron.common.writer.BulkMessageWriter in project metron by apache.

the class ParserTopologyBuilderTest method shouldCreateWriterConfigWithSuppliedOutputTopic.

@Test
public void shouldCreateWriterConfigWithSuppliedOutputTopic() {
    SensorParserConfig snortConfig = new SensorParserConfig();
    snortConfig.setSensorTopic("snort");
    when(configs.getSensorParserConfig("snort")).thenReturn(snortConfig);
    Map<String, SensorParserConfig> sensorTypeToParserConfig = new HashMap<String, SensorParserConfig>() {

        {
            put("snort", snortConfig);
        }
    };
    Map<String, WriterHandler> writerConfigs = ParserTopologyBuilder.createWriterConfigs("zookeeperUrl", Optional.of("brokerUrl"), sensorTypeToParserConfig, Optional.of("securityProtocol"), configs, Optional.of("supplied_topic"));
    assertEquals(1, writerConfigs.size());
    // Can't directly verify against mocks because this is all static. However, knowing that we have a KafkaWriter
    // and the appropriate topic lets us know we've created the underlying config.
    BulkMessageWriter writer = writerConfigs.get("snort").getBulkMessageWriter();
    assertTrue(writer instanceof KafkaWriter);
    assertEquals("supplied_topic", ((KafkaWriter) writer).getKafkaTopic(new JSONObject()).get());
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) WriterHandler(org.apache.metron.parsers.bolt.WriterHandler) KafkaWriter(org.apache.metron.writer.kafka.KafkaWriter) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 5 with BulkMessageWriter

use of org.apache.metron.common.writer.BulkMessageWriter in project metron by apache.

the class BulkMessageWriterBoltTest method testFlushOnTickTuple.

@Test
public void testFlushOnTickTuple() throws Exception {
    FakeClock clock = new FakeClock();
    bulkMessageWriterBolt.setCuratorFramework(client);
    bulkMessageWriterBolt.setZKCache(cache);
    bulkMessageWriterBolt.getConfigurations().updateSensorIndexingConfig(BaseEnrichmentBoltTest.sensorType, new FileInputStream("../" + BaseEnrichmentBoltTest.sampleSensorIndexingConfigPath));
    {
        bulkMessageWriterBolt.declareOutputFields(declarer);
        verify(declarer, times(1)).declareStream(eq("error"), argThat(new FieldsMatcher("message")));
    }
    {
        Map stormConf = new HashMap();
        when(bulkMessageWriter.getName()).thenReturn("elasticsearch");
        bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector, clock);
        verify(bulkMessageWriter, times(1)).init(eq(stormConf), any(WriterConfiguration.class));
    }
    {
        int batchTimeout = bulkMessageWriterBolt.getMaxBatchTimeout();
        assertEquals(14, batchTimeout);
        for (int i = 0; i < 5; i++) {
            bulkMessageWriterBolt.execute(tupleList.get(i));
            verify(bulkMessageWriter, times(0)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), any());
        }
        Tuple tickTuple = mock(Tuple.class);
        when(tickTuple.getValueByField("message")).thenReturn(null);
        // mark the tuple as a TickTuple, part 1 of 2
        when(tickTuple.getSourceComponent()).thenReturn("__system");
        // mark the tuple as a TickTuple, part 2 of 2
        when(tickTuple.getSourceStreamId()).thenReturn("__tick");
        BulkWriterResponse response = new BulkWriterResponse();
        response.addAllSuccesses(messageIdList);
        when(bulkMessageWriter.write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList))).thenReturn(response);
        clock.advanceToSeconds(2);
        bulkMessageWriterBolt.execute(tickTuple);
        verify(bulkMessageWriter, times(0)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList));
        // 1 tick
        verify(outputCollector, times(1)).ack(tickTuple);
        clock.advanceToSeconds(9);
        bulkMessageWriterBolt.execute(tickTuple);
        verify(bulkMessageWriter, times(1)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList));
        assertEquals(5, tupleList.size());
        tupleList.forEach(tuple -> verify(outputCollector, times(1)).ack(tuple));
        verify(outputCollector, times(2)).ack(tickTuple);
    }
    Mockito.verifyNoMoreInteractions(outputCollector);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) UnitTestHelper(org.apache.metron.test.utils.UnitTestHelper) HashMap(java.util.HashMap) MessageId(org.apache.metron.common.writer.MessageId) Mockito.argThat(org.mockito.Mockito.argThat) Mockito.spy(org.mockito.Mockito.spy) WriterConfiguration(org.apache.metron.common.configuration.writer.WriterConfiguration) ArrayList(java.util.ArrayList) MessageGetters(org.apache.metron.storm.common.message.MessageGetters) Mockito.doThrow(org.mockito.Mockito.doThrow) MockitoAnnotations(org.mockito.MockitoAnnotations) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Values(org.apache.storm.tuple.Values) Tuple(org.apache.storm.tuple.Tuple) ParseException(org.json.simple.parser.ParseException) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Map(java.util.Map) Level(org.apache.log4j.Level) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) FakeClock(org.apache.metron.common.system.FakeClock) BulkWriterComponent(org.apache.metron.writer.BulkWriterComponent) JSONParser(org.json.simple.parser.JSONParser) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) FileInputStream(java.io.FileInputStream) Constants(org.apache.metron.common.Constants) IndexingConfigurations(org.apache.metron.common.configuration.IndexingConfigurations) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) JSONObject(org.json.simple.JSONObject) Mockito.anyList(org.mockito.Mockito.anyList) Multiline(org.adrianwalker.multilinestring.Multiline) Mockito.any(org.mockito.Mockito.any) Mockito.reset(org.mockito.Mockito.reset) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) FakeClock(org.apache.metron.common.system.FakeClock) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)6 BulkMessageWriter (org.apache.metron.common.writer.BulkMessageWriter)6 JSONObject (org.json.simple.JSONObject)6 Test (org.junit.jupiter.api.Test)6 FileInputStream (java.io.FileInputStream)3 StandardCharsets (java.nio.charset.StandardCharsets)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Multiline (org.adrianwalker.multilinestring.Multiline)3 Level (org.apache.log4j.Level)3 Constants (org.apache.metron.common.Constants)3 IndexingConfigurations (org.apache.metron.common.configuration.IndexingConfigurations)3 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)3 FakeClock (org.apache.metron.common.system.FakeClock)3 BulkMessage (org.apache.metron.common.writer.BulkMessage)3 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)3 MessageId (org.apache.metron.common.writer.MessageId)3 MessageGetters (org.apache.metron.storm.common.message.MessageGetters)3 BaseEnrichmentBoltTest (org.apache.metron.test.bolt.BaseEnrichmentBoltTest)3