Search in sources :

Example 6 with FingerprintFactory

use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.

the class TestPopularVoteFlowElection method testDifferentPopulatedFlowsElection.

@Test
public void testDifferentPopulatedFlowsElection() throws IOException {
    final FingerprintFactory fingerprintFactory = new FingerprintFactory(StringEncryptor.createEncryptor(getNiFiProperties()));
    final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 4, fingerprintFactory);
    final byte[] nonEmptyCandidateA = Files.readAllBytes(Paths.get("src/test/resources/conf/controller-service-flow.xml"));
    final byte[] nonEmptyCandidateB = Files.readAllBytes(Paths.get("src/test/resources/conf/reporting-task-flow.xml"));
    for (int i = 0; i < 4; i++) {
        assertFalse(election.isElectionComplete());
        assertNull(election.getElectedDataFlow());
        final DataFlow dataFlow;
        if (i % 2 == 0) {
            dataFlow = createDataFlow(nonEmptyCandidateA);
        } else {
            dataFlow = createDataFlow(nonEmptyCandidateB);
        }
        final DataFlow electedDataFlow = election.castVote(dataFlow, createNodeId(i));
        if (i == 3) {
            assertNotNull(electedDataFlow);
            assertEquals(new String(nonEmptyCandidateA), new String(electedDataFlow.getFlow()));
        } else {
            assertNull(electedDataFlow);
        }
    }
}
Also used : FingerprintFactory(org.apache.nifi.fingerprint.FingerprintFactory) Matchers.anyString(org.mockito.Matchers.anyString) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) Test(org.junit.Test)

Example 7 with FingerprintFactory

use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.

the class Cluster method createNode.

public Node createNode() {
    final Map<String, String> addProps = new HashMap<>();
    addProps.put(NiFiProperties.ZOOKEEPER_CONNECT_STRING, getZooKeeperConnectString());
    addProps.put(NiFiProperties.CLUSTER_IS_NODE, "true");
    final NiFiProperties nifiProperties = NiFiProperties.createBasicNiFiProperties("src/test/resources/conf/nifi.properties", addProps);
    final FingerprintFactory fingerprintFactory = new FingerprintFactory(StringEncryptor.createEncryptor(nifiProperties));
    final FlowElection flowElection = new PopularVoteFlowElection(flowElectionTimeoutMillis, TimeUnit.MILLISECONDS, flowElectionMaxNodes, fingerprintFactory);
    final Node node = new Node(nifiProperties, flowElection);
    node.start();
    nodes.add(node);
    return node;
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) PopularVoteFlowElection(org.apache.nifi.cluster.coordination.flow.PopularVoteFlowElection) HashMap(java.util.HashMap) FingerprintFactory(org.apache.nifi.fingerprint.FingerprintFactory) PopularVoteFlowElection(org.apache.nifi.cluster.coordination.flow.PopularVoteFlowElection) FlowElection(org.apache.nifi.cluster.coordination.flow.FlowElection)

Example 8 with FingerprintFactory

use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.

the class StandardFlowSynchronizer method checkFlowInheritability.

private String checkFlowInheritability(final byte[] existingFlow, final byte[] proposedFlow, final FlowController controller) {
    if (existingFlow == null) {
        // no existing flow, so equivalent to proposed flow
        return null;
    }
    // check if the Flow is inheritable
    final FingerprintFactory fingerprintFactory = new FingerprintFactory(encryptor);
    final String existingFlowFingerprintBeforeHash = fingerprintFactory.createFingerprint(existingFlow, controller);
    if (existingFlowFingerprintBeforeHash.trim().isEmpty()) {
        // no existing flow, so equivalent to proposed flow
        return null;
    }
    if (proposedFlow == null || proposedFlow.length == 0) {
        // existing flow is not empty and proposed flow is empty (we could orphan flowfiles)
        return "Proposed Flow was empty but Current Flow is not";
    }
    final String proposedFlowFingerprintBeforeHash = fingerprintFactory.createFingerprint(proposedFlow, controller);
    if (proposedFlowFingerprintBeforeHash.trim().isEmpty()) {
        // existing flow is not empty and proposed flow is empty (we could orphan flowfiles)
        return "Proposed Flow was empty but Current Flow is not";
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Local Fingerprint Before Hash = {}", new Object[] { existingFlowFingerprintBeforeHash });
        logger.trace("Proposed Fingerprint Before Hash = {}", new Object[] { proposedFlowFingerprintBeforeHash });
    }
    final boolean inheritable = existingFlowFingerprintBeforeHash.equals(proposedFlowFingerprintBeforeHash);
    if (!inheritable) {
        return findFirstDiscrepancy(existingFlowFingerprintBeforeHash, proposedFlowFingerprintBeforeHash, "Flows");
    }
    return null;
}
Also used : FingerprintFactory(org.apache.nifi.fingerprint.FingerprintFactory)

Aggregations

FingerprintFactory (org.apache.nifi.fingerprint.FingerprintFactory)8 DataFlow (org.apache.nifi.cluster.protocol.DataFlow)5 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 HashMap (java.util.HashMap)1 FlowElection (org.apache.nifi.cluster.coordination.flow.FlowElection)1 PopularVoteFlowElection (org.apache.nifi.cluster.coordination.flow.PopularVoteFlowElection)1 StringEncryptor (org.apache.nifi.encrypt.StringEncryptor)1 NiFiProperties (org.apache.nifi.util.NiFiProperties)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1