Java Code Examples for org.apache.nifi.logging.ComponentLog#error()

The following examples show how to use org.apache.nifi.logging.ComponentLog#error() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashAttribute.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final Map<String, Pattern> patterns = regexMapRef.get();
    final ComponentLog logger = getLogger();

    final SortedMap<String, String> attributes = getRelevantAttributes(flowFile, patterns);
    if (attributes.size() != patterns.size()) {
        final Set<String> wantedKeys = patterns.keySet();
        final Set<String> foundKeys = attributes.keySet();
        final StringBuilder missingKeys = new StringBuilder();
        for (final String wantedKey : wantedKeys) {
            if (!foundKeys.contains(wantedKey)) {
                missingKeys.append(wantedKey).append(" ");
            }
        }

        logger.error("routing {} to 'failure' because of missing attributes: {}", new Object[]{flowFile, missingKeys.toString()});
        session.transfer(flowFile, REL_FAILURE);
    } else {
        // create single string of attribute key/value pairs to use for group ID hash
        final StringBuilder hashableValue = new StringBuilder();
        for (final Map.Entry<String, String> entry : attributes.entrySet()) {
            hashableValue.append(entry.getKey());
            if (StringUtils.isBlank(entry.getValue())) {
                hashableValue.append("EMPTY");
            } else {
                hashableValue.append(entry.getValue());
            }
        }

        // create group ID
        final String hashValue = DigestUtils.md5Hex(hashableValue.toString());

        logger.info("adding Hash Value {} to attributes for {} and routing to success", new Object[]{hashValue, flowFile});
        flowFile = session.putAttribute(flowFile, context.getProperty(HASH_VALUE_ATTRIBUTE).getValue(), hashValue);
        session.getProvenanceReporter().modifyAttributes(flowFile);
        session.transfer(flowFile, REL_SUCCESS);
    }
}
 
Example 2
Source File: PutHiveStreaming.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private HiveWriter getOrCreateWriter(HiveEndPoint endPoint) throws HiveWriter.ConnectFailure, InterruptedException {
    ComponentLog log = getLogger();
    try {
        HiveWriter writer = allWriters.get(endPoint);
        if (writer == null) {
            log.debug("Creating Writer to Hive end point : " + endPoint);
            writer = makeHiveWriter(endPoint, callTimeoutPool, ugi, options);
            if (allWriters.size() > (options.getMaxOpenConnections() - 1)) {
                log.info("cached HiveEndPoint size {} exceeded maxOpenConnections {} ", new Object[]{allWriters.size(), options.getMaxOpenConnections()});
                int retired = retireIdleWriters();
                if (retired == 0) {
                    retireEldestWriter();
                }
            }
            allWriters.put(endPoint, writer);
            HiveUtils.logAllHiveEndPoints(allWriters);
        }
        return writer;
    } catch (HiveWriter.ConnectFailure e) {
        log.error("Failed to create HiveWriter for endpoint: " + endPoint, e);
        throw e;
    }
}
 
Example 3
Source File: DruidTranquilityController.java    From nifi with Apache License 2.0 6 votes vote down vote up
private List<AggregatorFactory> getAggregatorList(String aggregatorJSON) {
    ComponentLog log = getLogger();
    ObjectMapper mapper = new ObjectMapper(null);
    mapper.registerModule(new AggregatorsModule());
    mapper.registerModules(Lists.newArrayList(new SketchModule().getJacksonModules()));
    mapper.registerModules(Lists.newArrayList(new ApproximateHistogramDruidModule().getJacksonModules()));

    try {
        return mapper.readValue(
            aggregatorJSON,
            new TypeReference<List<AggregatorFactory>>() {
            }
        );
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        return null;
    }
}
 
Example 4
Source File: InvokeScriptedProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Reloads the script defined by the given string
 *
 * @param scriptBody the contents of the script to be loaded
 * @return true if the script was loaded successfully; false otherwise
 */
private boolean reloadScriptBody(final String scriptBody) {
    final Collection<ValidationResult> results = new HashSet<>();
    try {
        return reloadScript(scriptBody);

    } catch (final Exception e) {
        final ComponentLog logger = getLogger();
        final String message = "Unable to load script: " + e;

        logger.error(message, e);
        results.add(new ValidationResult.Builder()
                .subject("ScriptValidation")
                .valid(false)
                .explanation("Unable to load script due to " + e)
                .input(scriptingComponentHelper.getScriptPath())
                .build());
    }

    // store the updated validation results
    validationResults.set(results);

    // return whether there was any issues loading the configured script
    return results.isEmpty();
}
 
Example 5
Source File: ProvenanceEnumerator.java    From nifi with Apache License 2.0 6 votes vote down vote up
public ProvenanceEnumerator(final ReportingContext context, final ComponentLog logger, final int[] fields) {
    this.logger = logger;
    this.fields = fields;
    final EventAccess eventAccess = context.getEventAccess();
    this.provenanceEventRepository = eventAccess.getProvenanceRepository();
    final ProcessGroupStatus procGroupStatus = eventAccess.getControllerStatus();
    this.componentMapHolder = ComponentMapHolder.createComponentMap(procGroupStatus);

    final boolean isClustered = context.isClustered();
    nodeIdentifier = context.getClusterNodeIdentifier();
    if (nodeIdentifier == null && isClustered) {
        logger.warn("This instance of NiFi is configured for clustering, but the Cluster Node Identifier is not yet available. "
                + "The contentPath and previousContentPath fields will be null for all rows in this query");
    }

    try {
        this.provenanceEvents = provenanceEventRepository.getEvents(0, FETCH_SIZE);
    } catch (IOException ioe) {
        logger.error("Error retrieving provenance events, queries will return no rows");
    }
    reset();
}
 
Example 6
Source File: ScanAttribute.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final List<FlowFile> flowFiles = session.get(50);
    if (flowFiles.isEmpty()) {
        return;
    }

    final ComponentLog logger = getLogger();
    try {
        if (fileWatcher.checkAndReset()) {
            this.dictionaryTerms = createDictionary(context);
        }
    } catch (final IOException e) {
        logger.error("Unable to reload dictionary due to {}", e);
    }

    final boolean matchAll = context.getProperty(MATCHING_CRITERIA).getValue().equals(MATCH_CRITERIA_ALL);

    for (final FlowFile flowFile : flowFiles) {
        final boolean matched = matchAll ? allMatch(flowFile, attributePattern, dictionaryTerms) : anyMatch(flowFile, attributePattern, dictionaryTerms);
        final Relationship relationship = matched ? REL_MATCHED : REL_UNMATCHED;
        session.getProvenanceReporter().route(flowFile, relationship);
        session.transfer(flowFile, relationship);
        logger.info("Transferred {} to {}", new Object[]{flowFile, relationship});
    }
}
 
Example 7
Source File: ExtractImageMetadata.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowfile = session.get();
    if (flowfile == null) {
        return;
    }

    final ComponentLog logger = this.getLogger();
    final AtomicReference<Metadata> value = new AtomicReference<>(null);
    final Integer max = context.getProperty(MAX_NUMBER_OF_ATTRIBUTES).asInteger();

    try {
        session.read(flowfile, new InputStreamCallback() {
            @Override
            public void process(InputStream in) throws IOException {
                try {
                    Metadata imageMetadata = ImageMetadataReader.readMetadata(in);
                    value.set(imageMetadata);
                } catch (ImageProcessingException ex) {
                    throw new ProcessException(ex);
                }
            }
        });

        Metadata metadata = value.get();
        Map<String, String> results = getTags(max, metadata);

        // Write the results to an attribute
        if (!results.isEmpty()) {
            flowfile = session.putAllAttributes(flowfile, results);
        }

        session.transfer(flowfile, SUCCESS);
    } catch (ProcessException e) {
        logger.error("Failed to extract image metadata from {} due to {}", new Object[]{flowfile, e});
        session.transfer(flowfile, FAILURE);
    }
}
 
Example 8
Source File: SiteToSiteUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static SiteToSiteClient getClient(PropertyContext reportContext, ComponentLog logger, StateManager stateManager) {
    final SSLContextService sslContextService = reportContext.getProperty(SiteToSiteUtils.SSL_CONTEXT).asControllerService(SSLContextService.class);
    final SSLContext sslContext = sslContextService == null ? null : sslContextService.createSSLContext(SslContextFactory.ClientAuth.REQUIRED);
    final EventReporter eventReporter = (EventReporter) (severity, category, message) -> {
        switch (severity) {
            case WARNING:
                logger.warn(message);
                break;
            case ERROR:
                logger.error(message);
                break;
            default:
                break;
        }
    };
    final String destinationUrl = reportContext.getProperty(SiteToSiteUtils.DESTINATION_URL).evaluateAttributeExpressions().getValue();

    final SiteToSiteTransportProtocol mode = SiteToSiteTransportProtocol.valueOf(reportContext.getProperty(SiteToSiteUtils.TRANSPORT_PROTOCOL).getValue());
    final HttpProxy httpProxy = mode.equals(SiteToSiteTransportProtocol.RAW) || StringUtils.isEmpty(reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_HOSTNAME).getValue()) ? null
            : new HttpProxy(reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_HOSTNAME).getValue(), reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_PORT).asInteger(),
            reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_USERNAME).getValue(), reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_PASSWORD).getValue());

    // If no state manager was provided and this context supports retrieving it, do so
    if (stateManager == null && reportContext instanceof ReportingContext) {
        stateManager = ((ReportingContext) reportContext).getStateManager();
    }
    return new SiteToSiteClient.Builder()
            .urls(SiteToSiteRestApiClient.parseClusterUrls(destinationUrl))
            .portName(reportContext.getProperty(SiteToSiteUtils.PORT_NAME).getValue())
            .useCompression(reportContext.getProperty(SiteToSiteUtils.COMPRESS).asBoolean())
            .eventReporter(eventReporter)
            .sslContext(sslContext)
            .timeout(reportContext.getProperty(SiteToSiteUtils.TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS)
            .transportProtocol(mode)
            .httpProxy(httpProxy)
            .stateManager(stateManager)
            .build();
}
 
Example 9
Source File: InvokeScriptedProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes the onTrigger() method of the scripted processor. If the script
 * failed to reload, the processor yields until the script can be reloaded
 * successfully. If the scripted processor's onTrigger() method throws an
 * exception, a ProcessException will be thrown. If no processor is defined
 * by the script, an error is logged with the system.
 *
 * @param context provides access to convenience methods for obtaining
 * property values, delaying the scheduling of the processor, provides
 * access to Controller Services, etc.
 * @param sessionFactory provides access to a {@link ProcessSessionFactory},
 * which can be used for accessing FlowFiles, etc.
 * @throws ProcessException if the scripted processor's onTrigger() method
 * throws an exception
 */
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {

    // Initialize the rest of the processor resources if we have not already done so
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }

    ComponentLog log = getLogger();

    // ensure the processor (if it exists) is loaded
    final Processor instance = processor.get();

    // ensure the processor did not fail to reload at some point
    final Collection<ValidationResult> results = validationResults.get();
    if (!results.isEmpty()) {
        log.error(String.format("Unable to run because the Processor is not valid: [%s]",
                StringUtils.join(results, ", ")));
        context.yield();
        return;
    }
    if (instance != null) {
        try {
            // run the processor
            instance.onTrigger(context, sessionFactory);
        } catch (final ProcessException e) {
            final String message = String.format("An error occurred executing the configured Processor [%s]: %s",
                    context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue(), e);
            log.error(message);
            throw e;
        }
    } else {
        log.error("There is no processor defined by the script");
    }
}
 
Example 10
Source File: GetJMSTopic.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();

    WrappedMessageConsumer consumer = this.wrappedConsumer;
    if (consumer == null || consumer.isClosed()) {
        try {
            Properties props = null;
            try {
                props = getSubscriptionPropertiesFromFile();
            } catch (IOException ignore) {
            }
            if (props == null) {
                props = getSubscriptionPropertiesFromContext(context);
            }
            String subscriptionName = props.getProperty(SUBSCRIPTION_NAME_PROPERTY);
            consumer = JmsFactory.createTopicMessageConsumer(context, subscriptionName);
            this.wrappedConsumer = consumer;
        } catch (final JMSException e) {
            logger.error("Failed to connect to JMS Server due to {}", new Object[]{e});
            context.yield();
            return;
        }
    }

    super.consume(context, session, consumer);
}
 
Example 11
Source File: LivySessionController.java    From nifi with Apache License 2.0 5 votes vote down vote up
@OnDisabled
public void shutdown() {
    ComponentLog log = getLogger();
    try {
        enabled = false;
        livySessionManagerThread.interrupt();
        livySessionManagerThread.join();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        log.error("Livy Session Manager Thread interrupted");
    }
}
 
Example 12
Source File: UnpackContent.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    PackageFormat packagingFormat = PackageFormat.getFormat(context.getProperty(PACKAGING_FORMAT).getValue().toLowerCase());
    if (packagingFormat == PackageFormat.AUTO_DETECT_FORMAT) {
        packagingFormat = null;
        final String mimeType = flowFile.getAttribute(CoreAttributes.MIME_TYPE.key());
        if (mimeType == null) {
            logger.error("No mime.type attribute set for {}; routing to failure", new Object[]{flowFile});
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        for (PackageFormat format: PackageFormat.values()) {
            if (mimeType.toLowerCase().equals(format.getMimeType())) {
                packagingFormat = format;
            }
        }
        if (packagingFormat == null) {
            logger.info("Cannot unpack {} because its mime.type attribute is set to '{}', which is not a format that can be unpacked; routing to 'success'", new Object[]{flowFile, mimeType});
            session.transfer(flowFile, REL_SUCCESS);
            return;
        }
    }

    // set the Unpacker to use for this FlowFile.  FlowFileUnpackager objects maintain state and are not reusable.
    final Unpacker unpacker;
    final boolean addFragmentAttrs;
    switch (packagingFormat) {
    case TAR_FORMAT:
    case X_TAR_FORMAT:
        unpacker = tarUnpacker;
        addFragmentAttrs = true;
        break;
    case ZIP_FORMAT:
        unpacker = zipUnpacker;
        addFragmentAttrs = true;
        break;
    case FLOWFILE_STREAM_FORMAT_V2:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV2());
        addFragmentAttrs = false;
        break;
    case FLOWFILE_STREAM_FORMAT_V3:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV3());
        addFragmentAttrs = false;
        break;
    case FLOWFILE_TAR_FORMAT:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV1());
        addFragmentAttrs = false;
        break;
    case AUTO_DETECT_FORMAT:
    default:
        // The format of the unpacker should be known before initialization
        throw new ProcessException(packagingFormat + " is not a valid packaging format");
    }

    final List<FlowFile> unpacked = new ArrayList<>();
    try {
        unpacker.unpack(session, flowFile, unpacked);
        if (unpacked.isEmpty()) {
            logger.error("Unable to unpack {} because it does not appear to have any entries; routing to failure", new Object[]{flowFile});
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        if (addFragmentAttrs) {
            finishFragmentAttributes(session, flowFile, unpacked);
        }
        session.transfer(unpacked, REL_SUCCESS);
        final String fragmentId = unpacked.size() > 0 ? unpacked.get(0).getAttribute(FRAGMENT_ID) : null;
        flowFile = FragmentAttributes.copyAttributesToOriginal(session, flowFile, fragmentId, unpacked.size());
        session.transfer(flowFile, REL_ORIGINAL);
        session.getProvenanceReporter().fork(flowFile, unpacked);
        logger.info("Unpacked {} into {} and transferred to success", new Object[]{flowFile, unpacked});
    } catch (final Exception e) {
        logger.error("Unable to unpack {} due to {}; routing to failure", new Object[]{flowFile, e});
        session.transfer(flowFile, REL_FAILURE);
        session.remove(unpacked);
    }
}
 
Example 13
Source File: DetectDuplicate.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    final String cacheKey = context.getProperty(CACHE_ENTRY_IDENTIFIER).evaluateAttributeExpressions(flowFile).getValue();
    if (StringUtils.isBlank(cacheKey)) {
        logger.error("FlowFile {} has no attribute for given Cache Entry Identifier", new Object[]{flowFile});
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    final DistributedMapCacheClient cache = context.getProperty(DISTRIBUTED_CACHE_SERVICE).asControllerService(DistributedMapCacheClient.class);
    final Long durationMS = context.getProperty(AGE_OFF_DURATION).asTimePeriod(TimeUnit.MILLISECONDS);
    final long now = System.currentTimeMillis();

    try {
        final String flowFileDescription = context.getProperty(FLOWFILE_DESCRIPTION).evaluateAttributeExpressions(flowFile).getValue();
        final CacheValue cacheValue = new CacheValue(flowFileDescription, now);
        final CacheValue originalCacheValue;

        final boolean shouldCacheIdentifier = context.getProperty(CACHE_IDENTIFIER).asBoolean();
        if (shouldCacheIdentifier) {
            originalCacheValue = cache.getAndPutIfAbsent(cacheKey, cacheValue, keySerializer, valueSerializer, valueDeserializer);
        } else {
            originalCacheValue = cache.get(cacheKey, keySerializer, valueDeserializer);
        }

        boolean duplicate = originalCacheValue != null;
        if (duplicate && durationMS != null && (now >= originalCacheValue.getEntryTimeMS() + durationMS)) {
            boolean status = cache.remove(cacheKey, keySerializer);
            logger.debug("Removal of expired cached entry with key {} returned {}", new Object[]{cacheKey, status});

            // both should typically result in duplicate being false...but, better safe than sorry
            if (shouldCacheIdentifier) {
                duplicate = !cache.putIfAbsent(cacheKey, cacheValue, keySerializer, valueSerializer);
            } else {
                duplicate = cache.containsKey(cacheKey, keySerializer);
            }
        }

        if (duplicate) {
            session.getProvenanceReporter().route(flowFile, REL_DUPLICATE, "Duplicate of: " + ORIGINAL_DESCRIPTION_ATTRIBUTE_NAME);
            String originalFlowFileDescription = originalCacheValue.getDescription();
            flowFile = session.putAttribute(flowFile, ORIGINAL_DESCRIPTION_ATTRIBUTE_NAME, originalFlowFileDescription);
            session.transfer(flowFile, REL_DUPLICATE);
            logger.info("Found {} to be a duplicate of FlowFile with description {}", new Object[]{flowFile, originalFlowFileDescription});
            session.adjustCounter("Duplicates Detected", 1L, false);
        } else {
            session.getProvenanceReporter().route(flowFile, REL_NON_DUPLICATE);
            session.transfer(flowFile, REL_NON_DUPLICATE);
            logger.info("Could not find a duplicate entry in cache for {}; routing to non-duplicate", new Object[]{flowFile});
            session.adjustCounter("Non-Duplicate Files Processed", 1L, false);
        }
    } catch (final IOException e) {
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        logger.error("Unable to communicate with cache when processing {} due to {}", new Object[]{flowFile, e});
    }
}
 
Example 14
Source File: BaseScriptedLookupService.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a list of property descriptors supported by this processor. The
 * list always includes properties such as script engine name, script file
 * name, script body name, script arguments, and an external module path. If
 * the scripted processor also defines supported properties, those are added
 * to the list as well.
 *
 * @return a List of PropertyDescriptor objects supported by this processor
 */
@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {

    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    List<PropertyDescriptor> supportedPropertyDescriptors = new ArrayList<>();
    List<PropertyDescriptor> _temp = new ArrayList<>();
    _temp.addAll(scriptingComponentHelper.getDescriptors());
    _temp.remove(scriptingComponentHelper.SCRIPT_ENGINE);

    PropertyDescriptor.Builder jythonLessEngineProp = new PropertyDescriptor
            .Builder().fromPropertyDescriptor(scriptingComponentHelper.SCRIPT_ENGINE);
    List<AllowableValue> filtered = scriptingComponentHelper.getScriptEngineAllowableValues()
            .stream().filter(allowableValue -> !allowableValue.getValue().contains("ython"))
            .collect(Collectors.toList());
    jythonLessEngineProp.allowableValues(filtered.toArray(new AllowableValue[filtered.size()]));

    supportedPropertyDescriptors.add(jythonLessEngineProp.build());
    supportedPropertyDescriptors.addAll(_temp);

    final ConfigurableComponent instance = lookupService.get();
    if (instance != null) {
        try {
            final List<PropertyDescriptor> instanceDescriptors = instance.getPropertyDescriptors();
            if (instanceDescriptors != null) {
                supportedPropertyDescriptors.addAll(instanceDescriptors);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get property descriptors from Processor: " + t;

            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }

    return Collections.unmodifiableList(supportedPropertyDescriptors);
}
 
Example 15
Source File: FetchDistributedMapCache.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    final String cacheKey = context.getProperty(PROP_CACHE_ENTRY_IDENTIFIER).evaluateAttributeExpressions(flowFile).getValue();
    // This block retains the previous behavior when only one Cache Entry Identifier was allowed, so as not to change the expected error message
    if (StringUtils.isBlank(cacheKey)) {
        logger.error("FlowFile {} has no attribute for given Cache Entry Identifier", new Object[]{flowFile});
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    List<String> cacheKeys = Arrays.stream(cacheKey.split(",")).filter(path -> !StringUtils.isEmpty(path)).map(String::trim).collect(Collectors.toList());
    for (int i = 0; i < cacheKeys.size(); i++) {
        if (StringUtils.isBlank(cacheKeys.get(i))) {
            // Log first missing identifier, route to failure, and return
            logger.error("FlowFile {} has no attribute for Cache Entry Identifier in position {}", new Object[]{flowFile, i});
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_FAILURE);
            return;
        }
    }

    final DistributedMapCacheClient cache = context.getProperty(PROP_DISTRIBUTED_CACHE_SERVICE).asControllerService(DistributedMapCacheClient.class);

    try {
        final Map<String, byte[]> cacheValues;
        final boolean singleKey = cacheKeys.size() == 1;
        if (singleKey) {
            cacheValues = new HashMap<>(1);
            cacheValues.put(cacheKeys.get(0), cache.get(cacheKey, keySerializer, valueDeserializer));
        } else {
            cacheValues = cache.subMap(new HashSet<>(cacheKeys), keySerializer, valueDeserializer);
        }
        boolean notFound = false;
        for(Map.Entry<String,byte[]> cacheValueEntry : cacheValues.entrySet()) {
            final byte[] cacheValue = cacheValueEntry.getValue();

            if (cacheValue == null) {
                logger.info("Could not find an entry in cache for {}; routing to not-found", new Object[]{flowFile});
                notFound = true;
                break;
            } else {
                boolean putInAttribute = context.getProperty(PROP_PUT_CACHE_VALUE_IN_ATTRIBUTE).isSet();
                if (putInAttribute) {
                    String attributeName = context.getProperty(PROP_PUT_CACHE_VALUE_IN_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
                    if (!singleKey) {
                        // Append key to attribute name if multiple keys
                        attributeName += "." + cacheValueEntry.getKey();
                    }
                    String attributeValue = new String(cacheValue, context.getProperty(PROP_CHARACTER_SET).getValue());

                    int maxLength = context.getProperty(PROP_PUT_ATTRIBUTE_MAX_LENGTH).asInteger();
                    if (maxLength < attributeValue.length()) {
                        attributeValue = attributeValue.substring(0, maxLength);
                    }

                    flowFile = session.putAttribute(flowFile, attributeName, attributeValue);

                } else if (cacheKeys.size() > 1) {
                    throw new IOException("Multiple Cache Value Identifiers specified without Put Cache Value In Attribute set");
                } else {
                    // Write single value to content
                    flowFile = session.write(flowFile, out -> out.write(cacheValue));
                }

                if (putInAttribute) {
                    logger.info("Found a cache key of {} and added an attribute to {} with it's value.", new Object[]{cacheKey, flowFile});
                } else {
                    logger.info("Found a cache key of {} and replaced the contents of {} with it's value.", new Object[]{cacheKey, flowFile});
                }
            }
        }
        // If the loop was exited because a cache entry was not found, route to REL_NOT_FOUND; otherwise route to REL_SUCCESS
        if (notFound) {
            session.transfer(flowFile, REL_NOT_FOUND);
        } else {
            session.transfer(flowFile, REL_SUCCESS);
        }
    } catch (final IOException e) {
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        logger.error("Unable to communicate with cache when processing {} due to {}", new Object[]{flowFile, e});
    }
}
 
Example 16
Source File: AbstractElasticsearch5TransportClientProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiate ElasticSearch Client. This should be called by subclasses' @OnScheduled method to create a client
 * if one does not yet exist. If called when scheduled, closeClient() should be called by the subclasses' @OnStopped
 * method so the client will be destroyed when the processor is stopped.
 *
 * @param context The context for this processor
 * @throws ProcessException if an error occurs while creating an Elasticsearch client
 */
@Override
protected void createElasticsearchClient(ProcessContext context) throws ProcessException {

    ComponentLog log = getLogger();
    if (esClient.get() != null) {
        return;
    }

    log.debug("Creating ElasticSearch Client");
    try {
        final String clusterName = context.getProperty(CLUSTER_NAME).evaluateAttributeExpressions().getValue();
        final String pingTimeout = context.getProperty(PING_TIMEOUT).evaluateAttributeExpressions().getValue();
        final String samplerInterval = context.getProperty(SAMPLER_INTERVAL).evaluateAttributeExpressions().getValue();
        final String username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue();
        final String password = context.getProperty(PASSWORD).getValue();

        final SSLContextService sslService =
                context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);

        Settings.Builder settingsBuilder = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.ping_timeout", pingTimeout)
                .put("client.transport.nodes_sampler_interval", samplerInterval);

        String xPackUrl = context.getProperty(PROP_XPACK_LOCATION).evaluateAttributeExpressions().getValue();
        if (sslService != null) {
            settingsBuilder.put("xpack.security.transport.ssl.enabled", "true");
            if (!StringUtils.isEmpty(sslService.getKeyStoreFile())) {
                settingsBuilder.put("xpack.ssl.keystore.path", sslService.getKeyStoreFile());
            }
            if (!StringUtils.isEmpty(sslService.getKeyStorePassword())) {
                settingsBuilder.put("xpack.ssl.keystore.password", sslService.getKeyStorePassword());
            }
            if (!StringUtils.isEmpty(sslService.getKeyPassword())) {
                settingsBuilder.put("xpack.ssl.keystore.key_password", sslService.getKeyPassword());
            }
            if (!StringUtils.isEmpty(sslService.getTrustStoreFile())) {
                settingsBuilder.put("xpack.ssl.truststore.path", sslService.getTrustStoreFile());
            }
            if (!StringUtils.isEmpty(sslService.getTrustStorePassword())) {
                settingsBuilder.put("xpack.ssl.truststore.password", sslService.getTrustStorePassword());
            }
        }

        // Set username and password for X-Pack
        if (!StringUtils.isEmpty(username)) {
            StringBuffer secureUser = new StringBuffer(username);
            if (!StringUtils.isEmpty(password)) {
                secureUser.append(":");
                secureUser.append(password);
            }
            settingsBuilder.put("xpack.security.user", secureUser);
        }

        final String hosts = context.getProperty(HOSTS).evaluateAttributeExpressions().getValue();
        esHosts = getEsHosts(hosts);
        Client transportClient = getTransportClient(settingsBuilder, xPackUrl, username, password, esHosts, log);
        esClient.set(transportClient);

    } catch (Exception e) {
        log.error("Failed to create Elasticsearch client due to {}", new Object[]{e}, e);
        throw new ProcessException(e);
    }
}
 
Example 17
Source File: StandardProcessScheduler.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void schedule(final ReportingTaskNode taskNode) {
    final LifecycleState lifecycleState = getLifecycleState(requireNonNull(taskNode), true);
    if (lifecycleState.isScheduled()) {
        return;
    }

    final int activeThreadCount = lifecycleState.getActiveThreadCount();
    if (activeThreadCount > 0) {
        throw new IllegalStateException("Reporting Task " + taskNode.getName() + " cannot be started because it has " + activeThreadCount + " threads still running");
    }

    final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
    lifecycleState.setScheduled(true);

    final Runnable startReportingTaskRunnable = new Runnable() {
        @Override
        public void run() {
            final long lastStopTime = lifecycleState.getLastStopTime();
            final ReportingTask reportingTask = taskNode.getReportingTask();

            // Attempt to start the Reporting Task, and if we fail re-schedule the task again after #administrativeYielMillis milliseconds
            try {
                synchronized (lifecycleState) {
                    // if no longer scheduled to run, then we're finished. This can happen, for example,
                    // if the @OnScheduled method throws an Exception and the user stops the reporting task
                    // while we're administratively yielded.
                    // we also check if the schedule state's last start time is equal to what it was before.
                    // if not, then means that the reporting task has been stopped and started again, so we should just
                    // bail; another thread will be responsible for invoking the @OnScheduled methods.
                    if (!lifecycleState.isScheduled() || lifecycleState.getLastStopTime() != lastStopTime) {
                        LOG.debug("Did not complete invocation of @OnScheduled task for {} but Lifecycle State is no longer scheduled. Will not attempt to invoke task anymore", reportingTask);
                        return;
                    }

                    final ValidationStatus validationStatus = taskNode.getValidationStatus();
                    if (validationStatus != ValidationStatus.VALID) {
                        LOG.debug("Cannot schedule {} to run because it is currently invalid. Will try again in 5 seconds", taskNode);
                        componentLifeCycleThreadPool.schedule(this, 5, TimeUnit.SECONDS);
                        return;
                    }

                    try (final NarCloseable x = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), reportingTask.getClass(), reportingTask.getIdentifier())) {
                        ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, reportingTask, taskNode.getConfigurationContext());
                    }

                    agent.schedule(taskNode, lifecycleState);
                }
            } catch (final Exception e) {
                final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
                componentLog.error("Failed to invoke @OnScheduled method due to {}", cause);

                LOG.error("Failed to invoke the On-Scheduled Lifecycle methods of {} due to {}; administratively yielding this "
                        + "ReportingTask and will attempt to schedule it again after {}",
                        new Object[]{reportingTask, e.toString(), administrativeYieldDuration}, e);


                try (final NarCloseable x = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), reportingTask.getClass(), reportingTask.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, taskNode.getConfigurationContext());
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, taskNode.getConfigurationContext());
                }

                componentLifeCycleThreadPool.schedule(this, administrativeYieldMillis, TimeUnit.MILLISECONDS);
            }
        }
    };

    componentLifeCycleThreadPool.execute(startReportingTaskRunnable);
    taskNode.setScheduledState(ScheduledState.RUNNING);
}
 
Example 18
Source File: LogAttribute.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
protected String processFlowFile(final ComponentLog logger, final DebugLevels logLevel, final FlowFile flowFile, final ProcessSession session, final ProcessContext context) {
    final Set<String> attributeKeys = getAttributesToLog(flowFile.getAttributes().keySet(), context);
    final ComponentLog LOG = getLogger();
    final String dashedLine;

    String logPrefix = context.getProperty(LOG_PREFIX).evaluateAttributeExpressions(flowFile).getValue();

    if (StringUtil.isBlank(logPrefix)) {
        dashedLine = StringUtils.repeat('-', 50);
    } else {
        // abbreviate long lines
        logPrefix = StringUtils.abbreviate(logPrefix, 40);
        // center the logPrefix and pad with dashes
        logPrefix = StringUtils.center(logPrefix, 40, '-');
        // five dashes on the left and right side, plus the dashed logPrefix
        dashedLine = StringUtils.repeat('-', 5) + logPrefix + StringUtils.repeat('-', 5);
    }

    // Pretty print metadata
    final StringBuilder message = new StringBuilder();
    message.append("logging for flow file ").append(flowFile);
    message.append("\n");
    message.append(dashedLine);
    message.append("\nStandard FlowFile Attributes");
    message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", "entryDate", new Date(flowFile.getEntryDate())));
    message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", "lineageStartDate", new Date(flowFile.getLineageStartDate())));
    message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", "fileSize", flowFile.getSize()));
    message.append("\nFlowFile Attribute Map Content");
    for (final String key : attributeKeys) {
        message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", key, flowFile.getAttribute(key)));
    }
    message.append("\n");
    message.append(dashedLine);

    // The user can request to log the payload
    final boolean logPayload = context.getProperty(LOG_PAYLOAD).asBoolean();
    if (logPayload) {
        message.append("\n");
        if (flowFile.getSize() < ONE_MB) {
            final FlowFilePayloadCallback callback = new FlowFilePayloadCallback();
            session.read(flowFile, callback);
            message.append(callback.getContents());
        } else {
            message.append("\n Not including payload since it is larger than one mb.");
        }
    }
    final String outputMessage = message.toString().trim();
    // Uses optional property to specify logging level
    switch (logLevel) {
        case info:
            LOG.info(outputMessage);
            break;
        case debug:
            LOG.debug(outputMessage);
            break;
        case warn:
            LOG.warn(outputMessage);
            break;
        case trace:
            LOG.trace(outputMessage);
            break;
        case error:
            LOG.error(outputMessage);
            break;
        default:
            LOG.debug(outputMessage);
    }

    return outputMessage;

}
 
Example 19
Source File: ScriptingComponentHelper.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the specified script engine. First, the engine is loaded and instantiated using the JSR-223
 * javax.script APIs. Then, if any script configurators have been defined for this engine, their init() method is
 * called, and the configurator is saved for future calls.
 *
 * @param numberOfScriptEngines number of engines to setup
 * @see org.apache.nifi.processors.script.ScriptEngineConfigurator
 */
protected void setupEngines(int numberOfScriptEngines, ComponentLog log) {
    engineQ = new LinkedBlockingQueue<>(numberOfScriptEngines);
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (StringUtils.isBlank(scriptEngineName)) {
            throw new IllegalArgumentException("The script engine name cannot be null");
        }

        ScriptEngineConfigurator configurator = scriptEngineConfiguratorMap.get(scriptEngineName.toLowerCase());

        // Get a list of URLs from the configurator (if present), or just convert modules from Strings to URLs
        URL[] additionalClasspathURLs = null;
        if (configurator != null) {
            additionalClasspathURLs = configurator.getModuleURLsForClasspath(modules, log);
        } else {
            if (modules != null) {
                List<URL> urls = new LinkedList<>();
                for (String modulePathString : modules) {
                    try {
                        urls.add(new File(modulePathString).toURI().toURL());
                    } catch (MalformedURLException mue) {
                        log.error("{} is not a valid file, ignoring", new Object[]{modulePathString}, mue);
                    }
                }
                additionalClasspathURLs = urls.toArray(new URL[urls.size()]);
            }
        }

        // Need the right classloader when the engine is created. This ensures the NAR's execution class loader
        // (plus the module path) becomes the parent for the script engine
        ClassLoader scriptEngineModuleClassLoader = additionalClasspathURLs != null
                ? new URLClassLoader(additionalClasspathURLs, originalContextClassLoader)
                : originalContextClassLoader;
        if (scriptEngineModuleClassLoader != null) {
            Thread.currentThread().setContextClassLoader(scriptEngineModuleClassLoader);
        }

        for (int i = 0; i < numberOfScriptEngines; i++) {
            ScriptEngine scriptEngine = createScriptEngine();
            try {
                if (configurator != null) {
                    configurator.init(scriptEngine, modules);
                }
                if (!engineQ.offer(scriptEngine)) {
                    log.error("Error adding script engine {}", new Object[]{scriptEngine.getFactory().getEngineName()});
                }

            } catch (ScriptException se) {
                log.error("Error initializing script engine configurator {}", new Object[]{scriptEngineName});
                if (log.isDebugEnabled()) {
                    log.error("Error initializing script engine configurator", se);
                }
            }
        }
    } finally {
        // Restore original context class loader
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}
 
Example 20
Source File: ReflectionUtils.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Invokes all methods on the given instance that have been annotated with
 * the given annotation. If the signature of the method that is defined in
 * <code>instance</code> uses 1 or more parameters, those parameters must be
 * specified by the <code>args</code> parameter. However, if more arguments
 * are supplied by the <code>args</code> parameter than needed, the extra
 * arguments will be ignored.
 *
 * @param annotation annotation
 * @param instance instance
 * @param logger the ComponentLog to use for logging any errors. If null,
 * will use own logger, but that will not generate bulletins or easily tie
 * to the Processor's log messages.
 * @param args args
 * @return <code>true</code> if all appropriate methods were invoked and
 * returned without throwing an Exception, <code>false</code> if one of the
 * methods threw an Exception or could not be invoked; if <code>false</code>
 * is returned, an error will have been logged.
 */
public static boolean quietlyInvokeMethodsWithAnnotation(
        final Class<? extends Annotation> annotation, final Object instance, final ComponentLog logger, final Object... args) {

    for (final Method method : instance.getClass().getMethods()) {
        if (method.isAnnotationPresent(annotation)) {

            final boolean isAccessible = method.isAccessible();
            method.setAccessible(true);

            try {
                final Class<?>[] argumentTypes = method.getParameterTypes();
                if (argumentTypes.length > args.length) {
                    if (logger == null) {
                        LOG.error("Unable to invoke method {} on {} because method expects {} parameters but only {} were given",
                                new Object[]{method.getName(), instance, argumentTypes.length, args.length});
                    } else {
                        logger.error("Unable to invoke method {} on {} because method expects {} parameters but only {} were given",
                                new Object[]{method.getName(), instance, argumentTypes.length, args.length});
                    }

                    return false;
                }

                for (int i = 0; i < argumentTypes.length; i++) {
                    final Class<?> argType = argumentTypes[i];
                    if (!argType.isAssignableFrom(args[i].getClass())) {
                        if (logger == null) {
                            LOG.error("Unable to invoke method {} on {} because method parameter {} is expected to be of type {} but argument passed was of type {}",
                                    new Object[]{method.getName(), instance, i, argType, args[i].getClass()});
                        } else {
                            logger.error("Unable to invoke method {} on {} because method parameter {} is expected to be of type {} but argument passed was of type {}",
                                    new Object[]{method.getName(), instance, i, argType, args[i].getClass()});
                        }

                        return false;
                    }
                }

                try {
                    if (argumentTypes.length == args.length) {
                        method.invoke(instance, args);
                    } else {
                        final Object[] argsToPass = new Object[argumentTypes.length];
                        for (int i = 0; i < argsToPass.length; i++) {
                            argsToPass[i] = args[i];
                        }

                        method.invoke(instance, argsToPass);
                    }
                } catch (final InvocationTargetException ite) {
                    if (logger == null) {
                        LOG.error("Unable to invoke method {} on {} due to {}", new Object[]{method.getName(), instance, ite.getCause()});
                        LOG.error("", ite.getCause());
                    } else {
                        logger.error("Unable to invoke method {} on {} due to {}", new Object[]{method.getName(), instance, ite.getCause()});
                    }
                } catch (final IllegalAccessException | IllegalArgumentException t) {
                    if (logger == null) {
                        LOG.error("Unable to invoke method {} on {} due to {}", new Object[]{method.getName(), instance, t});
                        LOG.error("", t);
                    } else {
                        logger.error("Unable to invoke method {} on {} due to {}", new Object[]{method.getName(), instance, t});
                    }

                    return false;
                }
            } finally {
                if (!isAccessible) {
                    method.setAccessible(false);
                }
            }
        }
    }

    return true;
}