Search in sources :

Example 1 with LogRepository

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

the class TestFlowController method testSynchronizeFlowWhenBundlesAreDifferent.

@Test
public void testSynchronizeFlowWhenBundlesAreDifferent() throws IOException {
    final FlowSynchronizer standardFlowSynchronizer = new StandardFlowSynchronizer(StringEncryptor.createEncryptor(nifiProperties), nifiProperties);
    final LogRepository logRepository = LogRepositoryFactory.getRepository("d89ada5d-35fb-44ff-83f1-4cc00b48b2df");
    logRepository.removeAllObservers();
    // first sync should work because we are syncing to an empty flow controller
    syncFlow("src/test/resources/nifi/fingerprint/flow4.xml", standardFlowSynchronizer);
    // second sync should fail because the bundle of the processor is different
    try {
        syncFlow("src/test/resources/nifi/fingerprint/flow4-with-different-bundle.xml", standardFlowSynchronizer);
        Assert.fail("Should have thrown UninheritableFlowException");
    } catch (UninheritableFlowException e) {
    // e.printStackTrace();
    }
}
Also used : FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) LogRepository(org.apache.nifi.logging.LogRepository) Test(org.junit.Test)

Example 2 with LogRepository

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

the class StandardStateManager method getLogger.

private ComponentLog getLogger(final String componentId) {
    final LogRepository repo = LogRepositoryFactory.getRepository(componentId);
    final ComponentLog logger = (repo == null) ? null : repo.getLogger();
    if (repo == null || logger == null) {
        return new SimpleProcessLogger(componentId, this);
    }
    return logger;
}
Also used : LogRepository(org.apache.nifi.logging.LogRepository) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 3 with LogRepository

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

the class FlowController method createProcessor.

/**
 * <p>
 * Creates a new ProcessorNode with the given type and identifier and
 * optionally initializes it.
 * </p>
 *
 * @param type the fully qualified Processor class name
 * @param id the unique ID of the Processor
 * @param coordinate the bundle coordinate for this processor
 * @param firstTimeAdded whether or not this is the first time this
 * Processor is added to the graph. If {@code true}, will invoke methods
 * annotated with the {@link OnAdded} annotation.
 * @return new processor node
 * @throws NullPointerException if either arg is null
 * @throws ProcessorInstantiationException if the processor cannot be
 * instantiated for any reason
 */
public ProcessorNode createProcessor(final String type, String id, final BundleCoordinate coordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded, final boolean registerLogObserver) throws ProcessorInstantiationException {
    id = id.intern();
    boolean creationSuccessful;
    LoggableComponent<Processor> processor;
    try {
        processor = instantiateProcessor(type, id, coordinate, additionalUrls);
        creationSuccessful = true;
    } catch (final ProcessorInstantiationException pie) {
        LOG.error("Could not create Processor of type " + type + " for ID " + id + "; creating \"Ghost\" implementation", pie);
        final GhostProcessor ghostProc = new GhostProcessor();
        ghostProc.setIdentifier(id);
        ghostProc.setCanonicalClassName(type);
        processor = new LoggableComponent<>(ghostProc, coordinate, null);
        creationSuccessful = false;
    }
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(controllerServiceProvider, componentVarRegistry);
    final ProcessorNode procNode;
    if (creationSuccessful) {
        procNode = new StandardProcessorNode(processor, id, validationContextFactory, processScheduler, controllerServiceProvider, nifiProperties, componentVarRegistry, this);
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;
        procNode = new StandardProcessorNode(processor, id, validationContextFactory, processScheduler, controllerServiceProvider, componentType, type, nifiProperties, componentVarRegistry, this, true);
    }
    final LogRepository logRepository = LogRepositoryFactory.getRepository(id);
    if (registerLogObserver) {
        logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN, new ProcessorLogObserver(getBulletinRepository(), procNode));
    }
    try {
        final Class<?> procClass = procNode.getProcessor().getClass();
        if (procClass.isAnnotationPresent(DefaultSettings.class)) {
            DefaultSettings ds = procClass.getAnnotation(DefaultSettings.class);
            try {
                procNode.setYieldPeriod(ds.yieldDuration());
            } catch (Throwable ex) {
                LOG.error(String.format("Error while setting yield period from DefaultSettings annotation:%s", ex.getMessage()), ex);
            }
            try {
                procNode.setPenalizationPeriod(ds.penaltyDuration());
            } catch (Throwable ex) {
                LOG.error(String.format("Error while setting penalty duration from DefaultSettings annotation:%s", ex.getMessage()), ex);
            }
            // the caller said to register the log observer, otherwise we could be changing the level when we didn't mean to
            if (registerLogObserver) {
                try {
                    procNode.setBulletinLevel(ds.bulletinLevel());
                } catch (Throwable ex) {
                    LOG.error(String.format("Error while setting bulletin level from DefaultSettings annotation:%s", ex.getMessage()), ex);
                }
            }
        }
    } catch (Throwable ex) {
        LOG.error(String.format("Error while setting default settings from DefaultSettings annotation: %s", ex.getMessage()), ex);
    }
    if (firstTimeAdded) {
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getProcessor().getIdentifier())) {
            ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, procNode.getProcessor());
        } catch (final Exception e) {
            if (registerLogObserver) {
                logRepository.removeObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID);
            }
            throw new ComponentLifeCycleException("Failed to invoke @OnAdded methods of " + procNode.getProcessor(), e);
        }
        if (firstTimeAdded) {
            try (final NarCloseable nc = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getProcessor().getIdentifier())) {
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, procNode.getProcessor());
            }
        }
    }
    return procNode;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) GhostProcessor(org.apache.nifi.processor.GhostProcessor) Processor(org.apache.nifi.processor.Processor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) LogRepository(org.apache.nifi.logging.LogRepository) DefaultSettings(org.apache.nifi.annotation.configuration.DefaultSettings) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) UnknownServiceAddressException(org.apache.nifi.cluster.protocol.UnknownServiceAddressException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) InitializationException(org.apache.nifi.reporting.InitializationException) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) CommunicationsException(org.apache.nifi.controller.exception.CommunicationsException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) GhostProcessor(org.apache.nifi.processor.GhostProcessor) ProcessorLogObserver(org.apache.nifi.logging.ProcessorLogObserver)

Example 4 with LogRepository

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

the class TestFlowController method testSynchronizeFlowWhenBundlesAreSame.

@Test
public void testSynchronizeFlowWhenBundlesAreSame() throws IOException {
    final FlowSynchronizer standardFlowSynchronizer = new StandardFlowSynchronizer(StringEncryptor.createEncryptor(nifiProperties), nifiProperties);
    final LogRepository logRepository = LogRepositoryFactory.getRepository("d89ada5d-35fb-44ff-83f1-4cc00b48b2df");
    logRepository.removeAllObservers();
    syncFlow("src/test/resources/nifi/fingerprint/flow4.xml", standardFlowSynchronizer);
    syncFlow("src/test/resources/nifi/fingerprint/flow4.xml", standardFlowSynchronizer);
}
Also used : FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) LogRepository(org.apache.nifi.logging.LogRepository) Test(org.junit.Test)

Example 5 with LogRepository

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

the class FlowController method createControllerService.

@Override
public ControllerServiceNode createControllerService(final String type, final String id, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded) {
    final ControllerServiceNode serviceNode = controllerServiceProvider.createControllerService(type, id, bundleCoordinate, additionalUrls, firstTimeAdded);
    // Register log observer to provide bulletins when reporting task logs anything at WARN level or above
    final LogRepository logRepository = LogRepositoryFactory.getRepository(id);
    logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN, new ControllerServiceLogObserver(getBulletinRepository(), serviceNode));
    if (firstTimeAdded) {
        final ControllerService service = serviceNode.getControllerServiceImplementation();
        try (final NarCloseable nc = NarCloseable.withComponentNarLoader(service.getClass(), service.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, service);
        }
    }
    return serviceNode;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ControllerServiceLogObserver(org.apache.nifi.logging.ControllerServiceLogObserver) LogRepository(org.apache.nifi.logging.LogRepository)

Aggregations

LogRepository (org.apache.nifi.logging.LogRepository)6 NarCloseable (org.apache.nifi.nar.NarCloseable)3 IOException (java.io.IOException)2 UnknownServiceAddressException (org.apache.nifi.cluster.protocol.UnknownServiceAddressException)2 CommunicationsException (org.apache.nifi.controller.exception.CommunicationsException)2 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)2 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)2 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)2 ReportingTaskInstantiationException (org.apache.nifi.controller.reporting.ReportingTaskInstantiationException)2 FlowSerializationException (org.apache.nifi.controller.serialization.FlowSerializationException)2 FlowSynchronizationException (org.apache.nifi.controller.serialization.FlowSynchronizationException)2 FlowSynchronizer (org.apache.nifi.controller.serialization.FlowSynchronizer)2 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)2 ComponentVariableRegistry (org.apache.nifi.registry.ComponentVariableRegistry)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 ResourceNotFoundException (org.apache.nifi.web.ResourceNotFoundException)2 ConfigException (org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)2 Test (org.junit.Test)2 DefaultSettings (org.apache.nifi.annotation.configuration.DefaultSettings)1