Search in sources :

Example 6 with StandardDataFlow

use of org.apache.nifi.cluster.protocol.StandardDataFlow 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 7 with StandardDataFlow

use of org.apache.nifi.cluster.protocol.StandardDataFlow in project nifi by apache.

the class StandardFlowService method createDataFlow.

@Override
public StandardDataFlow createDataFlow() throws IOException {
    // Load the flow from disk if the file exists.
    if (dao.isFlowPresent()) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        dao.load(baos);
        final byte[] bytes = baos.toByteArray();
        final byte[] snippetBytes = controller.getSnippetManager().export();
        final byte[] authorizerFingerprint = getAuthorizerFingerprint();
        final StandardDataFlow fromDisk = new StandardDataFlow(bytes, snippetBytes, authorizerFingerprint, new HashSet<>());
        return fromDisk;
    }
    // end up with the same ID for the root Process Group.
    return createDataFlowFromController();
}
Also used : StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with StandardDataFlow

use of org.apache.nifi.cluster.protocol.StandardDataFlow in project nifi by apache.

the class StandardFlowService method createDataFlowFromController.

@Override
public StandardDataFlow createDataFlowFromController() throws IOException {
    final byte[] snippetBytes = controller.getSnippetManager().export();
    final byte[] authorizerFingerprint = getAuthorizerFingerprint();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    dao.save(controller, baos);
    final byte[] flowBytes = baos.toByteArray();
    baos.reset();
    final Set<String> missingComponents = new HashSet<>();
    controller.getRootGroup().findAllProcessors().stream().filter(p -> p.isExtensionMissing()).forEach(p -> missingComponents.add(p.getIdentifier()));
    controller.getAllControllerServices().stream().filter(cs -> cs.isExtensionMissing()).forEach(cs -> missingComponents.add(cs.getIdentifier()));
    controller.getAllReportingTasks().stream().filter(r -> r.isExtensionMissing()).forEach(r -> missingComponents.add(r.getIdentifier()));
    return new StandardDataFlow(flowBytes, snippetBytes, authorizerFingerprint, missingComponents);
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) GZIPInputStream(java.util.zip.GZIPInputStream) DisconnectionCode(org.apache.nifi.cluster.coordination.node.DisconnectionCode) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) BufferedInputStream(java.io.BufferedInputStream) ProcessGroup(org.apache.nifi.groups.ProcessGroup) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) FlowService(org.apache.nifi.services.FlowService) StringUtils(org.apache.commons.lang3.StringUtils) Scope(org.apache.nifi.components.state.Scope) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) Map(java.util.Map) Bulletin(org.apache.nifi.reporting.Bulletin) ReconnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage) Path(java.nio.file.Path) ConnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage) ProtocolHandler(org.apache.nifi.cluster.protocol.ProtocolHandler) NarClassLoaders(org.apache.nifi.nar.NarClassLoaders) FileUtils(org.apache.nifi.util.file.FileUtils) ConnectionException(org.apache.nifi.cluster.ConnectionException) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) RevisionManager(org.apache.nifi.web.revision.RevisionManager) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) AuthorizerCapabilityDetection(org.apache.nifi.authorization.AuthorizerCapabilityDetection) FlowResponseMessage(org.apache.nifi.cluster.protocol.message.FlowResponseMessage) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) ConnectionRequest(org.apache.nifi.cluster.protocol.ConnectionRequest) FlowRequestMessage(org.apache.nifi.cluster.protocol.message.FlowRequestMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) StandardXMLFlowConfigurationDAO(org.apache.nifi.persistence.StandardXMLFlowConfigurationDAO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReconnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) Calendar(java.util.Calendar) NodeProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.NodeProtocolSenderListener) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) LogLevel(org.apache.nifi.logging.LogLevel) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) ManagedAuthorizer(org.apache.nifi.authorization.ManagedAuthorizer) OutputStream(java.io.OutputStream) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) Logger(org.slf4j.Logger) Files(java.nio.file.Files) StateManager(org.apache.nifi.components.state.StateManager) FlowEngine(org.apache.nifi.engine.FlowEngine) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) TemplateDeserializer(org.apache.nifi.persistence.TemplateDeserializer) TimeUnit(java.util.concurrent.TimeUnit) DisconnectMessage(org.apache.nifi.cluster.protocol.message.DisconnectMessage) BulletinFactory(org.apache.nifi.events.BulletinFactory) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) Lock(java.util.concurrent.locks.Lock) Authorizer(org.apache.nifi.authorization.Authorizer) FormatUtils(org.apache.nifi.util.FormatUtils) NiFiProperties(org.apache.nifi.util.NiFiProperties) Paths(java.nio.file.Paths) FlowConfigurationDAO(org.apache.nifi.persistence.FlowConfigurationDAO) Collections(java.util.Collections) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) InputStream(java.io.InputStream) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashSet(java.util.HashSet)

Example 9 with StandardDataFlow

use of org.apache.nifi.cluster.protocol.StandardDataFlow in project nifi by apache.

the class StandardFlowServiceTest method testLoadWithFlow.

@Test
public void testLoadWithFlow() throws IOException {
    byte[] flowBytes = IOUtils.toByteArray(StandardFlowServiceTest.class.getResourceAsStream("/conf/all-flow.xml"));
    flowService.load(new StandardDataFlow(flowBytes, null, null, new HashSet<>()));
    FlowSerializer serializer = new StandardFlowSerializer(mockEncryptor);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(flowController, baos, ScheduledStateLookup.IDENTITY_LOOKUP);
    String expectedFlow = new String(flowBytes).trim();
    String actualFlow = new String(baos.toByteArray()).trim();
    Assert.assertEquals(expectedFlow, actualFlow);
}
Also used : StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) StandardFlowSerializer(org.apache.nifi.controller.serialization.StandardFlowSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StandardFlowSerializer(org.apache.nifi.controller.serialization.StandardFlowSerializer) FlowSerializer(org.apache.nifi.controller.serialization.FlowSerializer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with StandardDataFlow

use of org.apache.nifi.cluster.protocol.StandardDataFlow in project nifi by apache.

the class StandardFlowServiceTest method testLoadExistingFlowWithUninheritableFlow.

@Test
public void testLoadExistingFlowWithUninheritableFlow() throws IOException {
    byte[] originalBytes = IOUtils.toByteArray(StandardFlowServiceTest.class.getResourceAsStream("/conf/all-flow.xml"));
    flowService.load(new StandardDataFlow(originalBytes, null, null, new HashSet<>()));
    try {
        byte[] updatedBytes = IOUtils.toByteArray(StandardFlowServiceTest.class.getResourceAsStream("/conf/all-flow-uninheritable.xml"));
        flowService.load(new StandardDataFlow(updatedBytes, null, null, new HashSet<>()));
        fail("should have thrown " + UninheritableFlowException.class);
    } catch (UninheritableFlowException ufe) {
        FlowSerializer serializer = new StandardFlowSerializer(mockEncryptor);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serializer.serialize(flowController, baos, ScheduledStateLookup.IDENTITY_LOOKUP);
        String expectedFlow = new String(originalBytes).trim();
        String actualFlow = new String(baos.toByteArray()).trim();
        Assert.assertEquals(expectedFlow, actualFlow);
    }
}
Also used : StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) StandardFlowSerializer(org.apache.nifi.controller.serialization.StandardFlowSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StandardFlowSerializer(org.apache.nifi.controller.serialization.StandardFlowSerializer) FlowSerializer(org.apache.nifi.controller.serialization.FlowSerializer) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)15 HashSet (java.util.HashSet)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Test (org.junit.Test)9 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)5 StandardFlowSerializer (org.apache.nifi.controller.serialization.StandardFlowSerializer)5 FlowSerializer (org.apache.nifi.controller.serialization.FlowSerializer)4 ArrayList (java.util.ArrayList)3 ConnectionRequest (org.apache.nifi.cluster.protocol.ConnectionRequest)3 DataFlow (org.apache.nifi.cluster.protocol.DataFlow)3 ConnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage)3 RevisionManager (org.apache.nifi.web.revision.RevisionManager)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 StandardOpenOption (java.nio.file.StandardOpenOption)2 Collections (java.util.Collections)2