Search in sources :

Example 1 with ChannelException

use of org.apache.flume.ChannelException in project phoenix by apache.

the class PhoenixSink method process.

@Override
public Status process() throws EventDeliveryException {
    Status status = Status.READY;
    Channel channel = getChannel();
    Transaction transaction = null;
    List<Event> events = Lists.newArrayListWithExpectedSize(this.batchSize);
    long startTime = System.nanoTime();
    try {
        transaction = channel.getTransaction();
        transaction.begin();
        for (long i = 0; i < this.batchSize; i++) {
            Event event = channel.take();
            if (event == null) {
                status = Status.BACKOFF;
                if (i == 0) {
                    sinkCounter.incrementBatchEmptyCount();
                } else {
                    sinkCounter.incrementBatchUnderflowCount();
                }
                break;
            } else {
                events.add(event);
            }
        }
        if (!events.isEmpty()) {
            if (events.size() == this.batchSize) {
                sinkCounter.incrementBatchCompleteCount();
            } else {
                sinkCounter.incrementBatchUnderflowCount();
                status = Status.BACKOFF;
            }
            // save to Hbase
            serializer.upsertEvents(events);
            sinkCounter.addToEventDrainSuccessCount(events.size());
        } else {
            logger.debug("no events to process ");
            sinkCounter.incrementBatchEmptyCount();
            status = Status.BACKOFF;
        }
        transaction.commit();
    } catch (ChannelException e) {
        transaction.rollback();
        status = Status.BACKOFF;
        sinkCounter.incrementConnectionFailedCount();
    } catch (SQLException e) {
        sinkCounter.incrementConnectionFailedCount();
        transaction.rollback();
        logger.error("exception while persisting to Hbase ", e);
        throw new EventDeliveryException("Failed to persist message to Hbase", e);
    } catch (Throwable e) {
        transaction.rollback();
        logger.error("exception while processing in Phoenix Sink", e);
        throw new EventDeliveryException("Failed to persist message", e);
    } finally {
        logger.info(String.format("Time taken to process [%s] events was [%s] seconds", events.size(), TimeUnit.SECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS)));
        if (transaction != null) {
            transaction.close();
        }
    }
    return status;
}
Also used : Transaction(org.apache.flume.Transaction) EventDeliveryException(org.apache.flume.EventDeliveryException) SQLException(java.sql.SQLException) Channel(org.apache.flume.Channel) Event(org.apache.flume.Event) ChannelException(org.apache.flume.ChannelException)

Example 2 with ChannelException

use of org.apache.flume.ChannelException 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 3 with ChannelException

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

the class FlumeAppenderTest method testNotConnected.

@Test
public void testNotConnected() throws Exception {
    eventSource.stop();
    final String altPort = Integer.toString(Integer.parseInt(testPort) + 1);
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort), Agent.createAgent("localhost", altPort) };
    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();
    Assert.assertTrue("Appender Not started", avroAppender.isStarted());
    avroLogger.addAppender(avroAppender);
    avroLogger.setLevel(Level.ALL);
    try {
        avroLogger.info("Test message");
        Assert.fail("Exception should have been thrown");
    } catch (final Exception ex) {
    }
    try {
        final Context context = new Context();
        context.put("port", altPort);
        context.put("bind", "0.0.0.0");
        Configurables.configure(eventSource, context);
        eventSource.start();
    } catch (final ChannelException e) {
        Assert.fail("Caught exception while resetting port to " + altPort + " : " + e.getMessage());
    }
    avroLogger.info("Test message 2");
    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 2"));
    transaction.commit();
    transaction.close();
}
Also used : ThreadContext(org.apache.logging.log4j.ThreadContext) Context(org.apache.flume.Context) Transaction(org.apache.flume.Transaction) Event(org.apache.flume.Event) IOException(java.io.IOException) ChannelException(org.apache.flume.ChannelException) ChannelException(org.apache.flume.ChannelException) Test(org.junit.Test)

Example 4 with ChannelException

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

the class FlumeAppenderTest method testReconnect.

@Test
public void testReconnect() throws Exception {
    final String altPort = Integer.toString(Integer.parseInt(testPort) + 1);
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort), Agent.createAgent("localhost", altPort) };
    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);
    avroLogger.info("Test message");
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    Event event = channel.take();
    Assert.assertNotNull(event);
    Assert.assertTrue("Channel contained event, but not expected message. Received : " + getBody(event), getBody(event).endsWith("Test message"));
    transaction.commit();
    transaction.close();
    eventSource.stop();
    try {
        final Context context = new Context();
        context.put("port", altPort);
        context.put("bind", "0.0.0.0");
        Configurables.configure(eventSource, context);
        eventSource.start();
    } catch (final ChannelException e) {
        Assert.fail("Caught exception while resetting port to " + altPort + " : " + e.getMessage());
    }
    avroLogger.info("Test message 2");
    transaction = channel.getTransaction();
    transaction.begin();
    event = channel.take();
    Assert.assertNotNull(event);
    Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Test message 2"));
    transaction.commit();
    transaction.close();
}
Also used : ThreadContext(org.apache.logging.log4j.ThreadContext) Context(org.apache.flume.Context) Transaction(org.apache.flume.Transaction) Event(org.apache.flume.Event) ChannelException(org.apache.flume.ChannelException) Test(org.junit.Test)

Aggregations

ChannelException (org.apache.flume.ChannelException)4 Event (org.apache.flume.Event)4 Transaction (org.apache.flume.Transaction)4 Channel (org.apache.flume.Channel)2 Context (org.apache.flume.Context)2 EventDeliveryException (org.apache.flume.EventDeliveryException)2 ThreadContext (org.apache.logging.log4j.ThreadContext)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ExecutionException (java.util.concurrent.ExecutionException)1