Search in sources :

Example 76 with ComponentLog

use of org.apache.nifi.logging.ComponentLog in project nifi by apache.

the class TestMonitorDiskUsage method testGeneratesMessageIfTooFull.

@Test
public void testGeneratesMessageIfTooFull() {
    final AtomicInteger callCounter = new AtomicInteger(0);
    final ComponentLog logger = Mockito.mock(ComponentLog.class);
    Mockito.doAnswer(invocation -> {
        final String message = (String) invocation.getArguments()[0];
        System.out.println(message);
        callCounter.incrementAndGet();
        return null;
    }).when(logger).warn(Mockito.anyString());
    MonitorDiskUsage.checkThreshold("Test Path", Paths.get("."), 0, logger);
    assertEquals(1, callCounter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Example 77 with ComponentLog

use of org.apache.nifi.logging.ComponentLog in project nifi by apache.

the class JettyWebSocketClient method maintainSessions.

void maintainSessions() throws Exception {
    if (client == null) {
        return;
    }
    connectionLock.lock();
    final ComponentLog logger = getLogger();
    try {
        // Loop through existing sessions and reconnect.
        for (String clientId : activeSessions.keySet()) {
            final WebSocketMessageRouter router;
            try {
                router = routers.getRouterOrFail(clientId);
            } catch (final WebSocketConfigurationException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("The clientId {} is no longer active. Discarding the clientId.", new Object[] { clientId });
                }
                activeSessions.remove(clientId);
                continue;
            }
            final String sessionId = activeSessions.get(clientId);
            // If this session is still alive, do nothing.
            if (!router.containsSession(sessionId)) {
                // This session is no longer active, reconnect it.
                // If it fails, the sessionId will remain in activeSessions, and retries later.
                // This reconnect attempt is continued until user explicitly stops a processor or this controller service.
                connect(clientId, sessionId);
            }
        }
    } finally {
        connectionLock.unlock();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Session maintenance completed. activeSessions={}", new Object[] { activeSessions });
    }
}
Also used : WebSocketMessageRouter(org.apache.nifi.websocket.WebSocketMessageRouter) WebSocketConfigurationException(org.apache.nifi.websocket.WebSocketConfigurationException) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 78 with ComponentLog

use of org.apache.nifi.logging.ComponentLog in project nifi by apache.

the class CSVRecordLookupService method loadCache.

private void loadCache() throws IllegalStateException, IOException {
    if (lock.tryLock()) {
        try {
            final ComponentLog logger = getLogger();
            if (logger.isDebugEnabled()) {
                logger.debug("Loading lookup table from file: " + csvFile);
            }
            final FileReader reader = new FileReader(csvFile);
            final CSVParser records = csvFormat.withFirstRecordAsHeader().parse(reader);
            ConcurrentHashMap<String, Record> cache = new ConcurrentHashMap<>();
            RecordSchema lookupRecordSchema = null;
            for (final CSVRecord record : records) {
                final String key = record.get(lookupKeyColumn);
                if (StringUtils.isBlank(key)) {
                    throw new IllegalStateException("Empty lookup key encountered in: " + csvFile);
                } else if (!ignoreDuplicates && cache.containsKey(key)) {
                    throw new IllegalStateException("Duplicate lookup key encountered: " + key + " in " + csvFile);
                } else if (ignoreDuplicates && cache.containsKey(key)) {
                    logger.warn("Duplicate lookup key encountered: {} in {}", new Object[] { key, csvFile });
                }
                // Put each key/value pair (except the lookup) into the properties
                final Map<String, Object> properties = new HashMap<>();
                record.toMap().forEach((k, v) -> {
                    if (!lookupKeyColumn.equals(k)) {
                        properties.put(k, v);
                    }
                });
                if (lookupRecordSchema == null) {
                    List<RecordField> recordFields = new ArrayList<>(properties.size());
                    properties.forEach((k, v) -> recordFields.add(new RecordField(k, RecordFieldType.STRING.getDataType())));
                    lookupRecordSchema = new SimpleRecordSchema(recordFields);
                }
                cache.put(key, new MapRecord(lookupRecordSchema, properties));
            }
            this.cache = cache;
            if (cache.isEmpty()) {
                logger.warn("Lookup table is empty after reading file: " + csvFile);
            }
        } finally {
            lock.unlock();
        }
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) ComponentLog(org.apache.nifi.logging.ComponentLog) CSVParser(org.apache.commons.csv.CSVParser) FileReader(java.io.FileReader) CSVRecord(org.apache.commons.csv.CSVRecord) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) CSVRecord(org.apache.commons.csv.CSVRecord) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

Example 79 with ComponentLog

use of org.apache.nifi.logging.ComponentLog in project nifi by apache.

the class TestJsonTreeRowRecordReader method testPerformanceOnIndividualMessages.

@Test
@Ignore("Intended only for manual testing to determine performance before/after modifications")
public void testPerformanceOnIndividualMessages() throws IOException, MalformedRecordException {
    final RecordSchema schema = new SimpleRecordSchema(Collections.emptyList());
    final File file = new File("/devel/nifi/nifi-assembly/target/nifi-1.2.0-SNAPSHOT-bin/nifi-1.2.0-SNAPSHOT/1.prov.json");
    final byte[] data = Files.readAllBytes(file.toPath());
    final ComponentLog logger = Mockito.mock(ComponentLog.class);
    int recordCount = 0;
    final int iterations = 1_000_000;
    for (int j = 0; j < 5; j++) {
        final long start = System.nanoTime();
        for (int i = 0; i < iterations; i++) {
            try (final InputStream in = new ByteArrayInputStream(data);
                final JsonTreeRowRecordReader reader = new JsonTreeRowRecordReader(in, logger, schema, dateFormat, timeFormat, timestampFormat)) {
                while (reader.nextRecord() != null) {
                    recordCount++;
                }
            }
        }
        final long nanos = System.nanoTime() - start;
        final long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
        System.out.println("Took " + millis + " millis to read " + recordCount + " records");
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) ComponentLog(org.apache.nifi.logging.ComponentLog) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 80 with ComponentLog

use of org.apache.nifi.logging.ComponentLog in project nifi by apache.

the class UpdateAttribute method evaluateCriteria.

// Evaluates the specified Criteria on the specified flowfile. Clones the
// specified flow file for each rule that is applied.
private boolean evaluateCriteria(final ProcessSession session, final ProcessContext context, final Criteria criteria, final FlowFile flowfile, final Map<FlowFile, List<Rule>> matchedRules, final Map<String, String> statefulAttributes) {
    final ComponentLog logger = getLogger();
    final List<Rule> rules = criteria.getRules();
    // consider each rule and hold a copy of the flowfile for each matched rule
    for (final Rule rule : rules) {
        // evaluate the rule
        if (evaluateRule(context, rule, flowfile, statefulAttributes)) {
            final FlowFile flowfileToUse;
            // determine if we should use the original flow file or clone
            if (FlowFilePolicy.USE_ORIGINAL.equals(criteria.getFlowFilePolicy()) || matchedRules.isEmpty()) {
                flowfileToUse = flowfile;
            } else {
                // clone the original for this rule
                flowfileToUse = session.clone(flowfile);
            }
            // store the flow file to use when executing this rule
            List<Rule> rulesForFlowFile = matchedRules.get(flowfileToUse);
            if (rulesForFlowFile == null) {
                rulesForFlowFile = new ArrayList<>();
                matchedRules.put(flowfileToUse, rulesForFlowFile);
            }
            rulesForFlowFile.add(rule);
            // log if appropriate
            if (debugEnabled) {
                logger.debug(this + " all conditions met for rule '" + rule.getName() + "'. Using flow file - " + flowfileToUse);
            }
        }
    }
    return !matchedRules.isEmpty();
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Rule(org.apache.nifi.update.attributes.Rule) ComponentLog(org.apache.nifi.logging.ComponentLog)

Aggregations

ComponentLog (org.apache.nifi.logging.ComponentLog)211 FlowFile (org.apache.nifi.flowfile.FlowFile)111 ProcessException (org.apache.nifi.processor.exception.ProcessException)95 IOException (java.io.IOException)94 HashMap (java.util.HashMap)51 Map (java.util.Map)47 InputStream (java.io.InputStream)46 ArrayList (java.util.ArrayList)44 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)40 HashSet (java.util.HashSet)33 ProcessSession (org.apache.nifi.processor.ProcessSession)32 List (java.util.List)28 ProcessContext (org.apache.nifi.processor.ProcessContext)28 Relationship (org.apache.nifi.processor.Relationship)28 StopWatch (org.apache.nifi.util.StopWatch)28 OutputStream (java.io.OutputStream)27 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)27 Set (java.util.Set)23 Collections (java.util.Collections)21 AtomicReference (java.util.concurrent.atomic.AtomicReference)21