Search in sources :

Example 6 with Ingester

use of org.komamitsu.fluency.ingester.Ingester in project fluency by komamitsu.

the class FluentdIngesterTest method ingestWithoutAck.

@Test
void ingestWithoutAck() throws IOException {
    Ingester ingester = new FluentdIngester(new FluentdIngester.Config(), fluentdSender);
    ingester.ingest(TAG, ByteBuffer.wrap(DATA));
    verify(fluentdSender, times(1)).send(byteBuffersArgumentCaptor.capture());
    List<ByteBuffer> byteBuffers = byteBuffersArgumentCaptor.getAllValues().get(0);
    byte[] ingested = getIngestedData(byteBuffers);
    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ingested);
    ImmutableArrayValue arrayValue = unpacker.unpackValue().asArrayValue();
    assertEquals(3, arrayValue.size());
    assertEquals(TAG, arrayValue.get(0).asStringValue().asString());
    assertArrayEquals(DATA, arrayValue.get(1).asRawValue().asByteArray());
    Map<Value, Value> options = arrayValue.get(2).asMapValue().map();
    assertEquals(1, options.size());
    assertEquals(DATA.length, options.get(ValueFactory.newString("size")).asIntegerValue().asInt());
}
Also used : MessageUnpacker(org.msgpack.core.MessageUnpacker) ImmutableArrayValue(org.msgpack.value.ImmutableArrayValue) Value(org.msgpack.value.Value) Ingester(org.komamitsu.fluency.ingester.Ingester) ImmutableArrayValue(org.msgpack.value.ImmutableArrayValue) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with Ingester

use of org.komamitsu.fluency.ingester.Ingester in project fluency by komamitsu.

the class BufferTest method testFileBackup.

@Test
void testFileBackup() throws IOException {
    bufferConfig.setFileBackupDir(System.getProperty("java.io.tmpdir"));
    bufferConfig.setFileBackupPrefix("FileBackupTest");
    // Just for cleaning backup files
    try (Buffer buffer = new Buffer(bufferConfig, recordFormatter)) {
        buffer.clearBackupFiles();
    }
    try (Buffer buffer = new Buffer(bufferConfig, recordFormatter)) {
        assertEquals(0, buffer.getBufferedDataSize());
    }
    long currentTime = System.currentTimeMillis() / 1000;
    Map<String, Object> event0 = ImmutableMap.of("name", "a", "age", 42);
    Map<String, Object> event1 = ImmutableMap.of("name", "b", "age", 99);
    try (Buffer buffer = new Buffer(bufferConfig, recordFormatter)) {
        buffer.append("foo", currentTime, event0);
        buffer.append("bar", currentTime, event1);
    }
    Ingester ingester = mock(Ingester.class);
    try (Buffer buffer = new Buffer(bufferConfig, recordFormatter)) {
        buffer.flushInternal(ingester, true);
    }
    ObjectMapper objectMapper = new ObjectMapper();
    for (Tuple<String, Map<String, Object>> tagAndEvent : ImmutableList.of(new Tuple<>("foo", event0), new Tuple<>("bar", event1))) {
        ArgumentCaptor<ByteBuffer> byteBufferArgumentCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
        verify(ingester, times(1)).ingest(eq(tagAndEvent.getFirst()), byteBufferArgumentCaptor.capture());
        ByteBuffer byteBuffer = byteBufferArgumentCaptor.getValue();
        byte[] bytes = new byte[byteBuffer.remaining()];
        byteBuffer.get(bytes);
        Map<String, Object> map = objectMapper.readValue(bytes, new TypeReference<Map<String, Object>>() {
        });
        assertEquals(tagAndEvent.getSecond(), map);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer) Ingester(org.komamitsu.fluency.ingester.Ingester) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 8 with Ingester

use of org.komamitsu.fluency.ingester.Ingester in project fluency by komamitsu.

the class FluencyTest method testWaitUntilFlusherTerminated.

@ParameterizedTest
@CsvSource({ "1, false", "3, true" })
void testWaitUntilFlusherTerminated(int waitUntilFlusherTerm, boolean expected) throws IOException, InterruptedException {
    flusherConfig.setWaitUntilTerminated(1);
    // Wait before actually closing in Buffer
    int waitBeforeCloseMillis = 2000;
    Buffer buffer = spy(new Buffer(bufferConfig, new JsonRecordFormatter()));
    doAnswer((invocation) -> {
        long start = System.currentTimeMillis();
        try {
            TimeUnit.MILLISECONDS.sleep(waitBeforeCloseMillis);
        } catch (InterruptedException e) {
            long rest = waitBeforeCloseMillis - (System.currentTimeMillis() - start);
            if (rest > 0) {
                try {
                    TimeUnit.MILLISECONDS.sleep(rest);
                } catch (InterruptedException e1) {
                }
            }
        }
        return null;
    }).doCallRealMethod().when(buffer).close();
    Flusher flusher = new Flusher(flusherConfig, buffer, ingester);
    Fluency fluency = new Fluency(buffer, flusher);
    fluency.emit("foo.bar", new HashMap<>());
    fluency.close();
    assertThat(fluency.waitUntilFlusherTerminated(waitUntilFlusherTerm), is(expected));
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(org.komamitsu.fluency.buffer.Buffer) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) CsvSource(org.junit.jupiter.params.provider.CsvSource) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) ByteBuffer(java.nio.ByteBuffer) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Ingester(org.komamitsu.fluency.ingester.Ingester) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Buffer(org.komamitsu.fluency.buffer.Buffer) Flusher(org.komamitsu.fluency.flusher.Flusher) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) Sender(org.komamitsu.fluency.ingester.sender.Sender) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Flusher(org.komamitsu.fluency.flusher.Flusher) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with Ingester

use of org.komamitsu.fluency.ingester.Ingester in project fluency by komamitsu.

the class FluencyBuilderForAwsS3Test method buildWithDefaultConfig.

@ParameterizedTest
@EnumSource(FluencyBuilderForAwsS3.FormatType.class)
void buildWithDefaultConfig(FluencyBuilderForAwsS3.FormatType formatType) {
    FluencyBuilderForAwsS3 builder = builderWithDefaultConfig;
    AwsS3Sender sender = mock(AwsS3Sender.class);
    doReturn(sender).when(builder).createSender(any(AwsS3Sender.Config.class));
    builder.setFormatType(formatType);
    Fluency fluency = builder.build();
    assertEquals(fluency, this.fluency);
    ArgumentCaptor<AwsS3Sender.Config> configArgumentCaptor = ArgumentCaptor.forClass(AwsS3Sender.Config.class);
    verify(builder, times(1)).createSender(configArgumentCaptor.capture());
    AwsS3Sender.Config senderConfig = configArgumentCaptor.getValue();
    assertNull(senderConfig.getEndpoint());
    assertNull(senderConfig.getRegion());
    assertNull(senderConfig.getAwsAccessKeyId());
    assertNull(senderConfig.getAwsSecretAccessKey());
    assertEquals(10, senderConfig.getRetryMax());
    assertEquals(1000, senderConfig.getRetryIntervalMs());
    assertEquals(30000, senderConfig.getMaxRetryIntervalMs());
    assertEquals(2.0, senderConfig.getRetryFactor());
    assertEquals(8192, senderConfig.getWorkBufSize());
    assertTrue(senderConfig.isCompressionEnabled());
    ArgumentCaptor<RecordFormatter> recordFormatterArgumentCaptor = ArgumentCaptor.forClass(RecordFormatter.class);
    ArgumentCaptor<Ingester> ingesterArgumentCaptor = ArgumentCaptor.forClass(Ingester.class);
    verify(builder, times(1)).buildFromIngester(recordFormatterArgumentCaptor.capture(), ingesterArgumentCaptor.capture());
    RecordFormatter recordFormatter = recordFormatterArgumentCaptor.getValue();
    Class<? extends AwsS3RecordFormatter> expectedAwsS3RecordFormatter = null;
    String expectedS3KeySuffix = null;
    switch(formatType) {
        case MESSAGE_PACK:
            expectedAwsS3RecordFormatter = MessagePackRecordFormatter.class;
            expectedS3KeySuffix = ".msgpack.gz";
            break;
        case JSONL:
            expectedAwsS3RecordFormatter = JsonlRecordFormatter.class;
            expectedS3KeySuffix = ".jsonl.gz";
            break;
        case CSV:
            expectedAwsS3RecordFormatter = CsvRecordFormatter.class;
            expectedS3KeySuffix = ".csv.gz";
            break;
    }
    assertEquals(expectedAwsS3RecordFormatter, recordFormatter.getClass());
    AwsS3Ingester ingester = (AwsS3Ingester) ingesterArgumentCaptor.getValue();
    assertEquals(sender, ingester.getSender());
    DefaultS3DestinationDecider destinationDecider = (DefaultS3DestinationDecider) ingester.getS3DestinationDecider();
    assertNull(destinationDecider.getKeyPrefix());
    assertEquals(expectedS3KeySuffix, destinationDecider.getKeySuffix());
    assertEquals(UTC, destinationDecider.getZoneId());
    ArgumentCaptor<Buffer.Config> bufferConfigArgumentCaptor = ArgumentCaptor.forClass(Buffer.Config.class);
    ArgumentCaptor<Flusher.Config> flusherConfigArgumentCaptor = ArgumentCaptor.forClass(Flusher.Config.class);
    verify(builder, times(1)).createFluency(eq(recordFormatter), eq(ingester), bufferConfigArgumentCaptor.capture(), flusherConfigArgumentCaptor.capture());
    Buffer.Config bufferConfig = bufferConfigArgumentCaptor.getValue();
    assertEquals(512 * 1024 * 1024, bufferConfig.getMaxBufferSize());
    assertEquals(4 * 1024 * 1024, bufferConfig.getChunkInitialSize());
    assertEquals(64 * 1024 * 1024, bufferConfig.getChunkRetentionSize());
    assertEquals(30 * 1000, bufferConfig.getChunkRetentionTimeMillis());
}
Also used : Buffer(org.komamitsu.fluency.buffer.Buffer) AwsS3Sender(org.komamitsu.fluency.aws.s3.ingester.sender.AwsS3Sender) Fluency(org.komamitsu.fluency.Fluency) JsonlRecordFormatter(org.komamitsu.fluency.aws.s3.recordformat.JsonlRecordFormatter) AwsS3RecordFormatter(org.komamitsu.fluency.aws.s3.recordformat.AwsS3RecordFormatter) RecordFormatter(org.komamitsu.fluency.recordformat.RecordFormatter) MessagePackRecordFormatter(org.komamitsu.fluency.aws.s3.recordformat.MessagePackRecordFormatter) CsvRecordFormatter(org.komamitsu.fluency.aws.s3.recordformat.CsvRecordFormatter) DefaultS3DestinationDecider(org.komamitsu.fluency.aws.s3.ingester.DefaultS3DestinationDecider) Flusher(org.komamitsu.fluency.flusher.Flusher) Ingester(org.komamitsu.fluency.ingester.Ingester) AwsS3Ingester(org.komamitsu.fluency.aws.s3.ingester.AwsS3Ingester) AwsS3Ingester(org.komamitsu.fluency.aws.s3.ingester.AwsS3Ingester) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Ingester (org.komamitsu.fluency.ingester.Ingester)9 Test (org.junit.jupiter.api.Test)7 ByteBuffer (java.nio.ByteBuffer)6 HashMap (java.util.HashMap)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Buffer (org.komamitsu.fluency.buffer.Buffer)3 Flusher (org.komamitsu.fluency.flusher.Flusher)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Fluency (org.komamitsu.fluency.Fluency)2 AwsS3Ingester (org.komamitsu.fluency.aws.s3.ingester.AwsS3Ingester)2 DefaultS3DestinationDecider (org.komamitsu.fluency.aws.s3.ingester.DefaultS3DestinationDecider)2 AwsS3Sender (org.komamitsu.fluency.aws.s3.ingester.sender.AwsS3Sender)2 AwsS3RecordFormatter (org.komamitsu.fluency.aws.s3.recordformat.AwsS3RecordFormatter)2 CsvRecordFormatter (org.komamitsu.fluency.aws.s3.recordformat.CsvRecordFormatter)2 JsonlRecordFormatter (org.komamitsu.fluency.aws.s3.recordformat.JsonlRecordFormatter)2 MessagePackRecordFormatter (org.komamitsu.fluency.aws.s3.recordformat.MessagePackRecordFormatter)2 RecordFormatter (org.komamitsu.fluency.recordformat.RecordFormatter)2 MessageUnpacker (org.msgpack.core.MessageUnpacker)2