Search in sources :

Example 6 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi by apache.

the class FlowController method createReportingTask.

public ReportingTaskNode createReportingTask(final String type, final String id, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded, final boolean register) throws ReportingTaskInstantiationException {
    if (type == null || id == null || bundleCoordinate == null) {
        throw new NullPointerException();
    }
    LoggableComponent<ReportingTask> task = null;
    boolean creationSuccessful = true;
    try {
        task = instantiateReportingTask(type, id, bundleCoordinate, additionalUrls);
    } catch (final Exception e) {
        LOG.error("Could not create Reporting Task of type " + type + " for ID " + id + "; creating \"Ghost\" implementation", e);
        final GhostReportingTask ghostTask = new GhostReportingTask();
        ghostTask.setIdentifier(id);
        ghostTask.setCanonicalClassName(type);
        task = new LoggableComponent<>(ghostTask, bundleCoordinate, null);
        creationSuccessful = false;
    }
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(controllerServiceProvider, componentVarRegistry);
    final ReportingTaskNode taskNode;
    if (creationSuccessful) {
        taskNode = new StandardReportingTaskNode(task, id, this, processScheduler, validationContextFactory, componentVarRegistry, this);
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;
        taskNode = new StandardReportingTaskNode(task, id, this, processScheduler, validationContextFactory, componentType, type, componentVarRegistry, this, true);
    }
    taskNode.setName(taskNode.getReportingTask().getClass().getSimpleName());
    if (firstTimeAdded) {
        final ReportingInitializationContext config = new StandardReportingInitializationContext(id, taskNode.getName(), SchedulingStrategy.TIMER_DRIVEN, "1 min", taskNode.getLogger(), this, nifiProperties, this);
        try {
            taskNode.getReportingTask().initialize(config);
        } catch (final InitializationException ie) {
            throw new ReportingTaskInstantiationException("Failed to initialize reporting task of type " + type, ie);
        }
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getReportingTask().getIdentifier())) {
            ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, taskNode.getReportingTask());
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, taskNode.getReportingTask());
        } catch (final Exception e) {
            throw new ComponentLifeCycleException("Failed to invoke On-Added Lifecycle methods of " + taskNode.getReportingTask(), e);
        }
    }
    if (register) {
        reportingTasks.put(id, taskNode);
        // 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 ReportingTaskLogObserver(getBulletinRepository(), taskNode));
    }
    return taskNode;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) LogRepository(org.apache.nifi.logging.LogRepository) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) InitializationException(org.apache.nifi.reporting.InitializationException) 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) GhostReportingTask(org.apache.nifi.reporting.GhostReportingTask) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) ReportingTaskLogObserver(org.apache.nifi.logging.ReportingTaskLogObserver) ReportingTask(org.apache.nifi.reporting.ReportingTask) GhostReportingTask(org.apache.nifi.reporting.GhostReportingTask)

Example 7 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi by apache.

the class ITReportLineageToAtlas method test.

private void test(TestConfiguration tc) throws InitializationException, IOException {
    final ReportLineageToAtlas reportingTask = new ReportLineageToAtlas();
    final MockComponentLog logger = new MockComponentLog("reporting-task-id", reportingTask);
    final ReportingInitializationContext initializationContext = mock(ReportingInitializationContext.class);
    when(initializationContext.getLogger()).thenReturn(logger);
    final ConfigurationContext configurationContext = new MockConfigurationContext(tc.properties, null);
    final ValidationContext validationContext = mock(ValidationContext.class);
    when(validationContext.getProperty(any())).then(invocation -> new MockPropertyValue(tc.properties.get(invocation.getArguments()[0])));
    final ReportingContext reportingContext = mock(ReportingContext.class);
    final MockStateManager stateManager = new MockStateManager(reportingTask);
    final EventAccess eventAccess = mock(EventAccess.class);
    when(reportingContext.getProperties()).thenReturn(tc.properties);
    when(reportingContext.getProperty(any())).then(invocation -> new MockPropertyValue(tc.properties.get(invocation.getArguments()[0])));
    when(reportingContext.getStateManager()).thenReturn(stateManager);
    when(reportingContext.getEventAccess()).thenReturn(eventAccess);
    when(eventAccess.getGroupStatus(eq("root"))).thenReturn(tc.rootPgStatus);
    final ProvenanceRepository provenanceRepository = mock(ProvenanceRepository.class);
    when(eventAccess.getControllerStatus()).thenReturn(tc.rootPgStatus);
    when(eventAccess.getProvenanceRepository()).thenReturn(provenanceRepository);
    when(eventAccess.getProvenanceEvents(eq(-1L), anyInt())).thenReturn(tc.provenanceRecords);
    when(provenanceRepository.getMaxEventId()).thenReturn((long) tc.provenanceRecords.size() - 1);
    when(provenanceRepository.getEvent(anyLong())).then(invocation -> tc.provenanceRecords.get(((Long) invocation.getArguments()[0]).intValue()));
    // To mock this async method invocations, keep the requested event ids in a stack.
    final ComputeLineageSubmission lineageComputationSubmission = mock(ComputeLineageSubmission.class);
    when(provenanceRepository.submitLineageComputation(anyLong(), any())).thenAnswer(invocation -> {
        requestedLineageComputationIds.push((Long) invocation.getArguments()[0]);
        return lineageComputationSubmission;
    });
    when(lineageComputationSubmission.getResult()).then(invocation -> tc.lineageResults.get(requestedLineageComputationIds.pop()));
    final ComputeLineageSubmission expandParentsSubmission = mock(ComputeLineageSubmission.class);
    when(provenanceRepository.submitExpandParents(anyLong(), any())).thenAnswer(invocation -> {
        requestedExpandParentsIds.push(((Long) invocation.getArguments()[0]));
        return expandParentsSubmission;
    });
    when(expandParentsSubmission.getResult()).then(invocation -> tc.parentLineageResults.get(requestedExpandParentsIds.pop()));
    tc.properties.put(ATLAS_NIFI_URL, "http://localhost:8080/nifi");
    tc.properties.put(ATLAS_URLS, TARGET_ATLAS_URL);
    tc.properties.put(ATLAS_USER, "admin");
    tc.properties.put(ATLAS_PASSWORD, "admin");
    tc.properties.put(new PropertyDescriptor.Builder().name("hostnamePattern.example").dynamic(true).build(), ".*");
    reportingTask.initialize(initializationContext);
    reportingTask.validate(validationContext);
    reportingTask.setup(configurationContext);
    reportingTask.onTrigger(reportingContext);
    reportingTask.onUnscheduled();
    reportingTask.onStopped();
}
Also used : EventAccess(org.apache.nifi.reporting.EventAccess) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) MockComponentLog(org.apache.nifi.util.MockComponentLog) ComputeLineageSubmission(org.apache.nifi.provenance.lineage.ComputeLineageSubmission) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ValidationContext(org.apache.nifi.components.ValidationContext) ReportingContext(org.apache.nifi.reporting.ReportingContext) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) MockStateManager(org.apache.nifi.state.MockStateManager) Matchers.anyLong(org.mockito.Matchers.anyLong) ProvenanceRepository(org.apache.nifi.provenance.ProvenanceRepository)

Aggregations

ReportingInitializationContext (org.apache.nifi.reporting.ReportingInitializationContext)7 ReportingTask (org.apache.nifi.reporting.ReportingTask)4 StandardReportingInitializationContext (org.apache.nifi.controller.reporting.StandardReportingInitializationContext)3 ComponentLog (org.apache.nifi.logging.ComponentLog)3 NarCloseable (org.apache.nifi.nar.NarCloseable)3 ConfigurationContext (org.apache.nifi.controller.ConfigurationContext)2 ReportingTaskInstantiationException (org.apache.nifi.controller.reporting.ReportingTaskInstantiationException)2 StandardReportingTaskNode (org.apache.nifi.controller.reporting.StandardReportingTaskNode)2 MockReportingInitializationContext (org.apache.nifi.mock.MockReportingInitializationContext)2 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 EventAccess (org.apache.nifi.reporting.EventAccess)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 ReportingContext (org.apache.nifi.reporting.ReportingContext)2 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Client (javax.ws.rs.client.Client)1 Entity (javax.ws.rs.client.Entity)1