Search in sources :

Example 1 with InfluxDBIOException

use of org.influxdb.InfluxDBIOException in project nifi by apache.

the class TestPutInfluxDB method testWriteThrowsSocketTimeoutException.

@Test
public void testWriteThrowsSocketTimeoutException() {
    mockPutInfluxDB = new PutInfluxDB() {

        @Override
        protected void writeToInfluxDB(ProcessContext context, String consistencyLevel, String database, String retentionPolicy, String records) {
            throw new InfluxDBIOException(new SocketTimeoutException("SocketTimeoutException"));
        }
    };
    runner = TestRunners.newTestRunner(mockPutInfluxDB);
    runner.setProperty(PutInfluxDB.DB_NAME, "test");
    runner.setProperty(PutInfluxDB.USERNAME, "u1");
    runner.setProperty(PutInfluxDB.PASSWORD, "p1");
    runner.setProperty(PutInfluxDB.CHARSET, "UTF-8");
    runner.setProperty(PutInfluxDB.INFLUX_DB_URL, "http://dbUrl");
    runner.setProperty(PutInfluxDB.CONSISTENCY_LEVEL, PutInfluxDB.CONSISTENCY_LEVEL_ONE.getValue());
    runner.setProperty(PutInfluxDB.RETENTION_POLICY, "autogen");
    runner.setProperty(PutInfluxDB.MAX_RECORDS_SIZE, "1 KB");
    runner.assertValid();
    byte[] bytes = "test".getBytes();
    runner.enqueue(bytes);
    runner.run(1, true, true);
    runner.assertAllFlowFilesTransferred(PutInfluxDB.REL_RETRY, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(PutInfluxDB.REL_RETRY);
    assertEquals(flowFiles.get(0).getAttribute(PutInfluxDB.INFLUX_DB_ERROR_MESSAGE), "java.net.SocketTimeoutException: SocketTimeoutException");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) SocketTimeoutException(java.net.SocketTimeoutException) InfluxDBIOException(org.influxdb.InfluxDBIOException) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 2 with InfluxDBIOException

use of org.influxdb.InfluxDBIOException in project nifi by apache.

the class TestPutInfluxDB method testWriteThrowsIOException.

@Test
public void testWriteThrowsIOException() {
    mockPutInfluxDB = new PutInfluxDB() {

        @Override
        protected void writeToInfluxDB(ProcessContext context, String consistencyLevel, String database, String retentionPolicy, String records) {
            throw new InfluxDBIOException(new EOFException("EOFException"));
        }
    };
    runner = TestRunners.newTestRunner(mockPutInfluxDB);
    runner.setProperty(PutInfluxDB.DB_NAME, "test");
    runner.setProperty(PutInfluxDB.USERNAME, "u1");
    runner.setProperty(PutInfluxDB.PASSWORD, "p1");
    runner.setProperty(PutInfluxDB.CHARSET, "UTF-8");
    runner.setProperty(PutInfluxDB.INFLUX_DB_URL, "http://dbUrl");
    runner.setProperty(PutInfluxDB.CONSISTENCY_LEVEL, PutInfluxDB.CONSISTENCY_LEVEL_ONE.getValue());
    runner.setProperty(PutInfluxDB.RETENTION_POLICY, "autogen");
    runner.setProperty(PutInfluxDB.MAX_RECORDS_SIZE, "1 KB");
    runner.assertValid();
    byte[] bytes = "test".getBytes();
    runner.enqueue(bytes);
    runner.run(1, true, true);
    runner.assertAllFlowFilesTransferred(PutInfluxDB.REL_FAILURE, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(PutInfluxDB.REL_FAILURE);
    assertEquals(flowFiles.get(0).getAttribute(PutInfluxDB.INFLUX_DB_ERROR_MESSAGE), "java.io.EOFException: EOFException");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) InfluxDBIOException(org.influxdb.InfluxDBIOException) EOFException(java.io.EOFException) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 3 with InfluxDBIOException

use of org.influxdb.InfluxDBIOException in project nifi by apache.

the class PutInfluxDB method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    if (flowFile.getSize() == 0) {
        getLogger().error("Empty measurements");
        flowFile = session.putAttribute(flowFile, INFLUX_DB_ERROR_MESSAGE, "Empty measurement size " + flowFile.getSize());
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    if (flowFile.getSize() > maxRecordsSize) {
        getLogger().error("Message size of records exceeded {} max allowed is {}", new Object[] { flowFile.getSize(), maxRecordsSize });
        flowFile = session.putAttribute(flowFile, INFLUX_DB_ERROR_MESSAGE, "Max records size exceeded " + flowFile.getSize());
        session.transfer(flowFile, REL_MAX_SIZE_EXCEEDED);
        return;
    }
    Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());
    String consistencyLevel = context.getProperty(CONSISTENCY_LEVEL).evaluateAttributeExpressions(flowFile).getValue();
    String database = context.getProperty(DB_NAME).evaluateAttributeExpressions(flowFile).getValue();
    String retentionPolicy = context.getProperty(RETENTION_POLICY).evaluateAttributeExpressions(flowFile).getValue();
    try {
        long startTimeMillis = System.currentTimeMillis();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        session.exportTo(flowFile, baos);
        String records = new String(baos.toByteArray(), charset);
        writeToInfluxDB(context, consistencyLevel, database, retentionPolicy, records);
        final long endTimeMillis = System.currentTimeMillis();
        getLogger().debug("Records {} inserted", new Object[] { records });
        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().send(flowFile, new StringBuilder("influxdb://").append(context.getProperty(INFLUX_DB_URL).evaluateAttributeExpressions().getValue()).append("/").append(database).toString(), (endTimeMillis - startTimeMillis));
    } catch (InfluxDBIOException exception) {
        flowFile = session.putAttribute(flowFile, INFLUX_DB_ERROR_MESSAGE, String.valueOf(exception.getMessage()));
        if (exception.getCause() instanceof SocketTimeoutException) {
            getLogger().error("Failed to insert into influxDB due SocketTimeoutException to {} and retrying", new Object[] { exception.getLocalizedMessage() }, exception);
            session.transfer(flowFile, REL_RETRY);
        } else {
            getLogger().error("Failed to insert into influxDB due to {}", new Object[] { exception.getLocalizedMessage() }, exception);
            session.transfer(flowFile, REL_FAILURE);
        }
        context.yield();
    } catch (Exception exception) {
        getLogger().error("Failed to insert into influxDB due to {}", new Object[] { exception.getLocalizedMessage() }, exception);
        flowFile = session.putAttribute(flowFile, INFLUX_DB_ERROR_MESSAGE, String.valueOf(exception.getMessage()));
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) SocketTimeoutException(java.net.SocketTimeoutException) InfluxDBIOException(org.influxdb.InfluxDBIOException) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessException(org.apache.nifi.processor.exception.ProcessException) SocketTimeoutException(java.net.SocketTimeoutException) InfluxDBIOException(org.influxdb.InfluxDBIOException)

Aggregations

InfluxDBIOException (org.influxdb.InfluxDBIOException)3 SocketTimeoutException (java.net.SocketTimeoutException)2 ProcessContext (org.apache.nifi.processor.ProcessContext)2 MockFlowFile (org.apache.nifi.util.MockFlowFile)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EOFException (java.io.EOFException)1 Charset (java.nio.charset.Charset)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1