Search in sources :

Example 1 with FingerprintFactory

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

the class TestPopularVoteFlowElection method testDifferentEmptyFlows.

@Test
public void testDifferentEmptyFlows() throws IOException {
    final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
    Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenAnswer(new Answer<String>() {

        @Override
        public String answer(final InvocationOnMock invocation) throws Throwable {
            final byte[] flow = invocation.getArgumentAt(0, byte[].class);
            final String xml = new String(flow);
            // Return the ID of the root group as the fingerprint.
            final String fingerprint = xml.replaceAll("(?s:(.*<id>)(.*?)(</id>.*))", "$2");
            return fingerprint;
        }
    });
    final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 3, fingerprintFactory);
    final byte[] flow1 = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
    final byte[] flow2 = Files.readAllBytes(Paths.get("src/test/resources/conf/different-empty-flow.xml"));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    assertNull(election.castVote(createDataFlow(flow1), createNodeId(1)));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    assertNull(election.castVote(createDataFlow(flow1), createNodeId(2)));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    final DataFlow electedDataFlow = election.castVote(createDataFlow(flow2), createNodeId(3));
    assertNotNull(electedDataFlow);
    final String electedFlowXml = new String(electedDataFlow.getFlow());
    assertTrue(new String(flow1).equals(electedFlowXml) || new String(flow2).equals(electedFlowXml));
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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 2 with FingerprintFactory

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

the class TestPopularVoteFlowElection method testOnlyEmptyFlows.

@Test
public void testOnlyEmptyFlows() throws IOException {
    final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
    Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenReturn("fingerprint");
    final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 3, fingerprintFactory);
    final byte[] flow = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    assertNull(election.castVote(createDataFlow(flow), createNodeId(1)));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    assertNull(election.castVote(createDataFlow(flow), createNodeId(2)));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    final DataFlow electedDataFlow = election.castVote(createDataFlow(flow), createNodeId(3));
    assertNotNull(electedDataFlow);
    assertEquals(new String(flow), new String(electedDataFlow.getFlow()));
}
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 3 with FingerprintFactory

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

the class TestPopularVoteFlowElection method testEmptyFlowIgnoredIfNonEmptyFlowExists.

@Test
public void testEmptyFlowIgnoredIfNonEmptyFlowExists() throws IOException {
    final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
    Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenReturn("fingerprint");
    final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 8, fingerprintFactory);
    final byte[] emptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
    final byte[] nonEmptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/non-empty-flow.xml"));
    for (int i = 0; i < 8; i++) {
        assertFalse(election.isElectionComplete());
        assertNull(election.getElectedDataFlow());
        final DataFlow dataFlow;
        if (i % 4 == 0) {
            dataFlow = createDataFlow(nonEmptyFlow);
        } else {
            dataFlow = createDataFlow(emptyFlow);
        }
        final DataFlow electedDataFlow = election.castVote(dataFlow, createNodeId(i));
        if (i == 7) {
            assertNotNull(electedDataFlow);
            assertEquals(new String(nonEmptyFlow), 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 4 with FingerprintFactory

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

the class PopularVoteFlowElectionFactoryBean method getObject.

@Override
public PopularVoteFlowElection getObject() throws Exception {
    final String maxWaitTime = properties.getFlowElectionMaxWaitTime();
    long maxWaitMillis;
    try {
        maxWaitMillis = FormatUtils.getTimeDuration(maxWaitTime, TimeUnit.MILLISECONDS);
    } catch (final Exception e) {
        logger.warn("Failed to parse value of property '{}' as a valid time period. Value was '{}'. Ignoring this value and using the default value of '{}'", NiFiProperties.FLOW_ELECTION_MAX_WAIT_TIME, maxWaitTime, NiFiProperties.DEFAULT_FLOW_ELECTION_MAX_WAIT_TIME);
        maxWaitMillis = FormatUtils.getTimeDuration(NiFiProperties.DEFAULT_FLOW_ELECTION_MAX_WAIT_TIME, TimeUnit.MILLISECONDS);
    }
    final Integer maxNodes = properties.getFlowElectionMaxCandidates();
    final StringEncryptor encryptor = StringEncryptor.createEncryptor(properties);
    final FingerprintFactory fingerprintFactory = new FingerprintFactory(encryptor);
    return new PopularVoteFlowElection(maxWaitMillis, TimeUnit.MILLISECONDS, maxNodes, fingerprintFactory);
}
Also used : FingerprintFactory(org.apache.nifi.fingerprint.FingerprintFactory) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor)

Example 5 with FingerprintFactory

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

the class TestPopularVoteFlowElection method testAutoGeneratedVsPopulatedFlowElection.

@Test
public void testAutoGeneratedVsPopulatedFlowElection() throws IOException {
    final FingerprintFactory fingerprintFactory = new FingerprintFactory(StringEncryptor.createEncryptor(getNiFiProperties()));
    final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 4, fingerprintFactory);
    final byte[] emptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/auto-generated-empty-flow.xml"));
    final byte[] nonEmptyFlow = 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(emptyFlow);
        } else {
            dataFlow = createDataFlow(nonEmptyFlow);
        }
        final DataFlow electedDataFlow = election.castVote(dataFlow, createNodeId(i));
        if (i == 3) {
            assertNotNull(electedDataFlow);
            assertEquals(new String(nonEmptyFlow), 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)

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