Search in sources :

Example 11 with Transaction

use of org.apache.flume.Transaction in project logging-log4j2 by apache.

the class FlumeAppenderTest method testIncompleteBatch.

//@Ignore //(Remko: this test hangs my build...)
@Test
public void testIncompleteBatch() throws IOException {
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) };
    final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, null, "false", "Avro", null, "1000", "1000", "1", "500", "avro", "false", null, null, null, null, null, "true", "10", null, null, null, null);
    avroAppender.start();
    avroLogger.addAppender(avroAppender);
    avroLogger.setLevel(Level.ALL);
    Assert.assertNotNull(avroLogger);
    avroLogger.info("Test message 0");
    final Transaction transaction = channel.getTransaction();
    transaction.begin();
    Event event = channel.take();
    Assert.assertNull("Received event", event);
    try {
        Thread.sleep(500);
    } catch (final InterruptedException ie) {
    }
    avroLogger.info("Test message 1");
    for (int i = 0; i < 2; ++i) {
        event = channel.take();
        Assert.assertNotNull("No event for item " + i, event);
        Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Test message " + i));
    }
    transaction.commit();
    transaction.close();
    eventSource.stop();
}
Also used : Transaction(org.apache.flume.Transaction) Event(org.apache.flume.Event) Test(org.junit.Test)

Example 12 with Transaction

use of org.apache.flume.Transaction in project logging-log4j2 by apache.

the class FlumeAppenderTest method testLog4jAvroAppender.

@Test
public void testLog4jAvroAppender() throws IOException {
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) };
    final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, null, "false", "Avro", null, "1000", "1000", "1", "1000", "avro", "false", null, null, null, null, null, "true", "1", null, null, null, null);
    avroAppender.start();
    avroLogger.addAppender(avroAppender);
    avroLogger.setLevel(Level.ALL);
    Assert.assertNotNull(avroLogger);
    avroLogger.info("Test message");
    final Transaction transaction = channel.getTransaction();
    transaction.begin();
    final Event event = channel.take();
    Assert.assertNotNull(event);
    Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Test message"));
    transaction.commit();
    transaction.close();
    eventSource.stop();
}
Also used : Transaction(org.apache.flume.Transaction) Event(org.apache.flume.Event) Test(org.junit.Test)

Example 13 with Transaction

use of org.apache.flume.Transaction in project logging-log4j2 by apache.

the class FlumeAppenderTest method testBatch.

@Test
public void testBatch() throws IOException {
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) };
    final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, null, "false", "Avro", null, "1000", "1000", "1", "1000", "avro", "false", null, null, null, null, null, "true", "10", null, null, null, null);
    avroAppender.start();
    avroLogger.addAppender(avroAppender);
    avroLogger.setLevel(Level.ALL);
    Assert.assertNotNull(avroLogger);
    for (int i = 0; i < 10; ++i) {
        avroLogger.info("Test message " + i);
    }
    final Transaction transaction = channel.getTransaction();
    transaction.begin();
    for (int i = 0; i < 10; ++i) {
        final Event event = channel.take();
        Assert.assertNotNull("No event for item " + i, event);
        Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Test message " + i));
    }
    transaction.commit();
    transaction.close();
    eventSource.stop();
}
Also used : Transaction(org.apache.flume.Transaction) Event(org.apache.flume.Event) Test(org.junit.Test)

Example 14 with Transaction

use of org.apache.flume.Transaction in project cdap-ingest by caskdata.

the class StreamSink method process.

@Override
public Status process() throws EventDeliveryException {
    Status status = Status.READY;
    Channel channel = getChannel();
    Transaction transaction = channel.getTransaction();
    try {
        tryReopenClientConnection();
        transaction.begin();
        Event event = channel.take();
        if (event != null) {
            try {
                writer.write(ByteBuffer.wrap(event.getBody()), event.getHeaders()).get();
                LOG.trace("Success write to stream: {} ", streamName);
            } catch (Throwable t) {
                if (t instanceof ExecutionException) {
                    t = t.getCause();
                }
                LOG.error("Error during writing event to stream {}", streamName, t);
                throw new EventDeliveryException("Failed to send events to stream: " + streamName, t);
            }
        }
        transaction.commit();
    } catch (Throwable t) {
        transaction.rollback();
        if (t instanceof Error) {
            throw (Error) t;
        } else if (t instanceof ChannelException) {
            LOG.error("Stream Sink {}: Unable to get event from channel {} ", getName(), channel.getName());
            status = Status.BACKOFF;
        } else {
            LOG.debug("Closing writer due to stream error ", t);
            closeClientQuietly();
            closeWriterQuietly();
            throw new EventDeliveryException("Sink event sending error", t);
        }
    } finally {
        transaction.close();
    }
    return status;
}
Also used : Transaction(org.apache.flume.Transaction) EventDeliveryException(org.apache.flume.EventDeliveryException) Channel(org.apache.flume.Channel) Event(org.apache.flume.Event) ExecutionException(java.util.concurrent.ExecutionException) ChannelException(org.apache.flume.ChannelException)

Example 15 with Transaction

use of org.apache.flume.Transaction in project cdap-ingest by caskdata.

the class MockStreamSink method getMockChannel.

private Channel getMockChannel() {
    Channel channel = Mockito.mock(Channel.class);
    Transaction transaction = Mockito.mock(Transaction.class);
    Mockito.doNothing().when(transaction).begin();
    Mockito.doNothing().when(transaction).commit();
    Mockito.doNothing().when(transaction).close();
    Mockito.doNothing().when(transaction).rollback();
    Mockito.when(channel.getTransaction()).thenReturn(transaction);
    Mockito.when(channel.take()).thenReturn(new SimpleEvent());
    return channel;
}
Also used : SimpleEvent(org.apache.flume.event.SimpleEvent) Transaction(org.apache.flume.Transaction) Channel(org.apache.flume.Channel)

Aggregations

Transaction (org.apache.flume.Transaction)41 Event (org.apache.flume.Event)38 Test (org.junit.Test)34 Channel (org.apache.flume.Channel)29 MemoryChannel (org.apache.flume.channel.MemoryChannel)26 PhoenixSink (org.apache.phoenix.flume.sink.PhoenixSink)22 Context (org.apache.flume.Context)11 EventDeliveryException (org.apache.flume.EventDeliveryException)9 Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 Properties (java.util.Properties)5 SQLException (java.sql.SQLException)4 ChannelException (org.apache.flume.ChannelException)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Sink (org.apache.flume.Sink)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 FlumeException (org.apache.flume.FlumeException)2