Search in sources :

Example 1 with FlowService

use of org.apache.nifi.services.FlowService in project nifi by apache.

the class NiFiTestServer method loadFlow.

public void loadFlow() throws Exception {
    logger.info("Loading Flow...");
    // start and load the flow
    FlowService flowService = getSpringBean("flowService", FlowService.class);
    flowService.load(null);
    logger.info("Flow loaded successfully.");
}
Also used : FlowService(org.apache.nifi.services.FlowService)

Example 2 with FlowService

use of org.apache.nifi.services.FlowService in project nifi by apache.

the class TestNodeClusterCoordinator method testUnknownNodeAskedToConnectOnAttemptedConnectionComplete.

@Test(timeout = 5000)
public void testUnknownNodeAskedToConnectOnAttemptedConnectionComplete() throws IOException, InterruptedException {
    final ClusterCoordinationProtocolSenderListener senderListener = Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
    final AtomicReference<ReconnectionRequestMessage> requestRef = new AtomicReference<>();
    Mockito.when(senderListener.requestReconnection(Mockito.any(ReconnectionRequestMessage.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final ReconnectionRequestMessage msg = invocation.getArgumentAt(0, ReconnectionRequestMessage.class);
            requestRef.set(msg);
            return null;
        }
    });
    final EventReporter eventReporter = Mockito.mock(EventReporter.class);
    final RevisionManager revisionManager = Mockito.mock(RevisionManager.class);
    Mockito.when(revisionManager.getAllRevisions()).thenReturn(Collections.emptyList());
    final NodeClusterCoordinator coordinator = new NodeClusterCoordinator(senderListener, eventReporter, null, new FirstVoteWinsFlowElection(), null, revisionManager, createProperties(), null) {

        @Override
        void notifyOthersOfNodeStatusChange(NodeConnectionStatus updatedStatus, boolean notifyAllNodes, boolean waitForCoordinator) {
        }
    };
    final FlowService flowService = Mockito.mock(FlowService.class);
    final StandardDataFlow dataFlow = new StandardDataFlow(new byte[50], new byte[50], new byte[50], new HashSet<>());
    Mockito.when(flowService.createDataFlowFromController()).thenReturn(dataFlow);
    coordinator.setFlowService(flowService);
    coordinator.setConnected(true);
    final NodeIdentifier nodeId = createNodeId(1);
    coordinator.finishNodeConnection(nodeId);
    while (requestRef.get() == null) {
        Thread.sleep(10L);
    }
    final ReconnectionRequestMessage msg = requestRef.get();
    assertEquals(nodeId, msg.getNodeId());
    final StandardDataFlow df = msg.getDataFlow();
    assertNotNull(df);
    assertTrue(Arrays.equals(dataFlow.getFlow(), df.getFlow()));
    assertTrue(Arrays.equals(dataFlow.getSnippets(), df.getSnippets()));
}
Also used : RevisionManager(org.apache.nifi.web.revision.RevisionManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ReconnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) EventReporter(org.apache.nifi.events.EventReporter) FlowService(org.apache.nifi.services.FlowService) Test(org.junit.Test)

Example 3 with FlowService

use of org.apache.nifi.services.FlowService in project nifi by apache.

the class TestNodeClusterCoordinator method setup.

@Before
public void setup() throws IOException {
    System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, "src/test/resources/conf/nifi.properties");
    senderListener = Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
    nodeStatuses = Collections.synchronizedList(new ArrayList<>());
    final EventReporter eventReporter = Mockito.mock(EventReporter.class);
    final RevisionManager revisionManager = Mockito.mock(RevisionManager.class);
    Mockito.when(revisionManager.getAllRevisions()).thenReturn(Collections.emptyList());
    coordinator = new NodeClusterCoordinator(senderListener, eventReporter, null, new FirstVoteWinsFlowElection(), null, revisionManager, createProperties(), null) {

        @Override
        void notifyOthersOfNodeStatusChange(NodeConnectionStatus updatedStatus, boolean notifyAllNodes, boolean waitForCoordinator) {
            nodeStatuses.add(updatedStatus);
        }
    };
    final FlowService flowService = Mockito.mock(FlowService.class);
    final StandardDataFlow dataFlow = new StandardDataFlow(new byte[50], new byte[50], new byte[50], new HashSet<>());
    Mockito.when(flowService.createDataFlow()).thenReturn(dataFlow);
    coordinator.setFlowService(flowService);
}
Also used : StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) RevisionManager(org.apache.nifi.web.revision.RevisionManager) ArrayList(java.util.ArrayList) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) EventReporter(org.apache.nifi.events.EventReporter) FlowService(org.apache.nifi.services.FlowService) Before(org.junit.Before)

Example 4 with FlowService

use of org.apache.nifi.services.FlowService in project nifi by apache.

the class JettyServer method start.

@Override
public void start() {
    try {
        ExtensionManager.discoverExtensions(systemBundle, bundles);
        ExtensionManager.logClassLoaderMapping();
        DocGenerator.generate(props, extensionMapping);
        // start the server
        server.start();
        // ensure everything started successfully
        for (Handler handler : server.getChildHandlers()) {
            // see if the handler is a web app
            if (handler instanceof WebAppContext) {
                WebAppContext context = (WebAppContext) handler;
                // cause it to be unavailable
                if (context.getUnavailableException() != null) {
                    startUpFailure(context.getUnavailableException());
                }
            }
        }
        // this must be done after starting the server (and ensuring there were no start up failures)
        if (webApiContext != null) {
            // give the web api the component ui extensions
            final ServletContext webApiServletContext = webApiContext.getServletHandler().getServletContext();
            webApiServletContext.setAttribute("nifi-ui-extensions", componentUiExtensions);
            // get the application context
            final WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(webApiServletContext);
            // component ui extensions
            if (CollectionUtils.isNotEmpty(componentUiExtensionWebContexts)) {
                final NiFiWebConfigurationContext configurationContext = webApplicationContext.getBean("nifiWebConfigurationContext", NiFiWebConfigurationContext.class);
                for (final WebAppContext customUiContext : componentUiExtensionWebContexts) {
                    // set the NiFi context in each custom ui servlet context
                    final ServletContext customUiServletContext = customUiContext.getServletHandler().getServletContext();
                    customUiServletContext.setAttribute("nifi-web-configuration-context", configurationContext);
                    // add the security filter to any ui extensions wars
                    final FilterHolder securityFilter = webApiContext.getServletHandler().getFilter("springSecurityFilterChain");
                    if (securityFilter != null) {
                        customUiContext.addFilter(securityFilter, "/*", EnumSet.allOf(DispatcherType.class));
                    }
                }
            }
            // content viewer extensions
            if (CollectionUtils.isNotEmpty(contentViewerWebContexts)) {
                for (final WebAppContext contentViewerContext : contentViewerWebContexts) {
                    // add the security filter to any content viewer  wars
                    final FilterHolder securityFilter = webApiContext.getServletHandler().getFilter("springSecurityFilterChain");
                    if (securityFilter != null) {
                        contentViewerContext.addFilter(securityFilter, "/*", EnumSet.allOf(DispatcherType.class));
                    }
                }
            }
            // content viewer controller
            if (webContentViewerContext != null) {
                final ContentAccess contentAccess = webApplicationContext.getBean("contentAccess", ContentAccess.class);
                // add the content access
                final ServletContext webContentViewerServletContext = webContentViewerContext.getServletHandler().getServletContext();
                webContentViewerServletContext.setAttribute("nifi-content-access", contentAccess);
                final FilterHolder securityFilter = webApiContext.getServletHandler().getFilter("springSecurityFilterChain");
                if (securityFilter != null) {
                    webContentViewerContext.addFilter(securityFilter, "/*", EnumSet.allOf(DispatcherType.class));
                }
            }
        }
        // ensure the web document war was loaded and provide the extension mapping
        if (webDocsContext != null) {
            final ServletContext webDocsServletContext = webDocsContext.getServletHandler().getServletContext();
            webDocsServletContext.setAttribute("nifi-extension-mapping", extensionMapping);
        }
        // in a cluster
        if (props.isNode()) {
            FlowService flowService = null;
            try {
                logger.info("Loading Flow...");
                ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(webApiContext.getServletContext());
                flowService = ctx.getBean("flowService", FlowService.class);
                // start and load the flow
                flowService.start();
                flowService.load(null);
                logger.info("Flow loaded successfully.");
            } catch (BeansException | LifeCycleStartException | IOException | FlowSerializationException | FlowSynchronizationException | UninheritableFlowException e) {
                // ensure the flow service is terminated
                if (flowService != null && flowService.isRunning()) {
                    flowService.stop(false);
                }
                logger.error("Unable to load flow due to: " + e, e);
                // cannot wrap the exception as they are not defined in a classloader accessible to the caller
                throw new Exception("Unable to load flow due to: " + e);
            }
        }
        // dump the application url after confirming everything started successfully
        dumpUrls();
    } catch (Exception ex) {
        startUpFailure(ex);
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ContentAccess(org.apache.nifi.web.ContentAccess) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) SocketException(java.net.SocketException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) WebApplicationContext(org.springframework.web.context.WebApplicationContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) ServletContext(javax.servlet.ServletContext) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) DispatcherType(javax.servlet.DispatcherType) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) FlowService(org.apache.nifi.services.FlowService) BeansException(org.springframework.beans.BeansException)

Example 5 with FlowService

use of org.apache.nifi.services.FlowService in project nifi by apache.

the class ApplicationStartupContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    final NiFiProperties properties = ctx.getBean("nifiProperties", NiFiProperties.class);
    try {
        flowService = ctx.getBean("flowService", FlowService.class);
        flowController = ctx.getBean("flowController", FlowController.class);
        requestReplicator = ctx.getBean("requestReplicator", RequestReplicator.class);
        // the flow loading here when not clustered resolves this.
        if (!properties.isNode()) {
            logger.info("Starting Flow Controller...");
            // start and load the flow
            flowService.start();
            flowService.load(null);
            /*
                 * Start up all processors if specified.
                 *
                 * When operating as the node, the cluster manager controls
                 * which processors should start. As part of the flow
                 * reloading actions, the node will start the necessary
                 * processors.
                 */
            flowController.onFlowInitialized(properties.getAutoResumeState());
            logger.info("Flow Controller started successfully.");
        }
    } catch (BeansException | RepositoryPurgeException | IOException e) {
        shutdown();
        throw new NiFiCoreException("Unable to start Flow Controller.", e);
    }
    try {
        // attempt to get a few beans that we want to to ensure properly created since they are lazily initialized
        ctx.getBean("loginIdentityProvider", LoginIdentityProvider.class);
        ctx.getBean("authorizer", Authorizer.class);
    } catch (final BeansException e) {
        shutdown();
        throw new NiFiCoreException("Unable to start Flow Controller.", e);
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) ApplicationContext(org.springframework.context.ApplicationContext) NiFiCoreException(org.apache.nifi.web.NiFiCoreException) RequestReplicator(org.apache.nifi.cluster.coordination.http.replication.RequestReplicator) FlowController(org.apache.nifi.controller.FlowController) IOException(java.io.IOException) RepositoryPurgeException(org.apache.nifi.controller.repository.RepositoryPurgeException) FlowService(org.apache.nifi.services.FlowService) BeansException(org.springframework.beans.BeansException)

Aggregations

FlowService (org.apache.nifi.services.FlowService)5 IOException (java.io.IOException)2 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)2 ClusterCoordinationProtocolSenderListener (org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener)2 EventReporter (org.apache.nifi.events.EventReporter)2 RevisionManager (org.apache.nifi.web.revision.RevisionManager)2 BeansException (org.springframework.beans.BeansException)2 ApplicationContext (org.springframework.context.ApplicationContext)2 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DispatcherType (javax.servlet.DispatcherType)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 RequestReplicator (org.apache.nifi.cluster.coordination.http.replication.RequestReplicator)1 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)1 ReconnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage)1 FlowController (org.apache.nifi.controller.FlowController)1 UninheritableFlowException (org.apache.nifi.controller.UninheritableFlowException)1 RepositoryPurgeException (org.apache.nifi.controller.repository.RepositoryPurgeException)1