Search in sources :

Example 21 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class TestMonitorActivity method testClusterMonitorActivityRestoredBySelfOnPrimaryNodeFallbackToNodeScope.

@Test
public void testClusterMonitorActivityRestoredBySelfOnPrimaryNodeFallbackToNodeScope() throws Exception {
    final TestableProcessor processor = new TestableProcessor(10000);
    final TestRunner runner = TestRunners.newTestRunner(processor);
    runner.setClustered(false);
    runner.setPrimaryNode(false);
    runner.setProperty(MonitorActivity.MONITORING_SCOPE, MonitorActivity.SCOPE_CLUSTER);
    runner.setProperty(MonitorActivity.REPORTING_NODE, MonitorActivity.REPORT_NODE_PRIMARY);
    runner.setProperty(MonitorActivity.THRESHOLD, "100 ms");
    runner.setProperty(MonitorActivity.COPY_ATTRIBUTES, "true");
    // Becomes inactive
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_INACTIVE);
    runner.clearTransferState();
    // Activity restored
    final HashMap<String, String> attributes = new HashMap<>();
    attributes.put("key1", "value1");
    attributes.put("key2", "value2");
    runner.enqueue("Incoming data", attributes);
    runNext(runner);
    final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(MonitorActivity.REL_SUCCESS);
    final List<MockFlowFile> activityRestoredFiles = runner.getFlowFilesForRelationship(MonitorActivity.REL_ACTIVITY_RESTORED);
    assertEquals(1, successFiles.size());
    assertEquals(1, activityRestoredFiles.size());
    assertEquals("value1", activityRestoredFiles.get(0).getAttribute("key1"));
    assertEquals("value2", activityRestoredFiles.get(0).getAttribute("key2"));
    // Latest activity should NOT be persisted
    final StateMap updatedState = runner.getStateManager().getState(Scope.CLUSTER);
    assertNull("Latest timestamp should NOT be persisted", updatedState.get(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER));
    runner.clearTransferState();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 22 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class TestMonitorActivity method testClusterMonitorActiveMoreRecentTimestampExisted.

@Test
public void testClusterMonitorActiveMoreRecentTimestampExisted() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new TestableProcessor(100));
    runner.setClustered(true);
    runner.setPrimaryNode(false);
    runner.setProperty(MonitorActivity.MONITORING_SCOPE, MonitorActivity.SCOPE_CLUSTER);
    // This has to be very small threshold, otherwise, MonitorActivity skip persisting state.
    runner.setProperty(MonitorActivity.THRESHOLD, "1 ms");
    runner.enqueue("Incoming data");
    // Set future timestamp in state
    final HashMap<String, String> existingState = new HashMap<>();
    final long existingTimestamp = System.currentTimeMillis() + 10_000;
    existingState.put(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER, String.valueOf(existingTimestamp));
    existingState.put("key1", "value1");
    existingState.put("key2", "value2");
    runner.getStateManager().setState(existingState, Scope.CLUSTER);
    runner.getStateManager().replace(runner.getStateManager().getState(Scope.CLUSTER), existingState, Scope.CLUSTER);
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_SUCCESS);
    final StateMap postProcessedState = runner.getStateManager().getState(Scope.CLUSTER);
    assertEquals("Existing timestamp should NOT be updated", String.valueOf(existingTimestamp), postProcessedState.get(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER));
    // State should stay the same.
    assertEquals(postProcessedState.get("key1"), existingState.get("key1"));
    assertEquals(postProcessedState.get("key2"), existingState.get("key2"));
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 23 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class TestMonitorActivity method testClusterMonitorActiveWithLatestTimestamp.

@Test
public void testClusterMonitorActiveWithLatestTimestamp() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new TestableProcessor(100));
    runner.setClustered(true);
    runner.setPrimaryNode(false);
    runner.setProperty(MonitorActivity.MONITORING_SCOPE, MonitorActivity.SCOPE_CLUSTER);
    // This has to be very small threshold, otherwise, MonitorActivity skip persisting state.
    runner.setProperty(MonitorActivity.THRESHOLD, "1 ms");
    runner.enqueue("Incoming data");
    // Set future timestamp in state
    final HashMap<String, String> existingState = new HashMap<>();
    final long existingTimestamp = System.currentTimeMillis() - 1_000;
    existingState.put(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER, String.valueOf(existingTimestamp));
    existingState.put("key1", "value1");
    existingState.put("key2", "value2");
    runner.getStateManager().setState(existingState, Scope.CLUSTER);
    runner.getStateManager().replace(runner.getStateManager().getState(Scope.CLUSTER), existingState, Scope.CLUSTER);
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_SUCCESS);
    final StateMap postProcessedState = runner.getStateManager().getState(Scope.CLUSTER);
    assertTrue("Existing timestamp should be updated", existingTimestamp < Long.parseLong(postProcessedState.get(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER)));
    // State should be updated. Null in this case.
    assertNull(postProcessedState.get("key1"));
    assertNull(postProcessedState.get("key2"));
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 24 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class TestMonitorActivity method testClusterMonitorActivityRestoredBySelfOnPrimaryNode.

@Test
public void testClusterMonitorActivityRestoredBySelfOnPrimaryNode() throws Exception {
    final TestableProcessor processor = new TestableProcessor(10000);
    final TestRunner runner = TestRunners.newTestRunner(processor);
    runner.setClustered(true);
    runner.setPrimaryNode(true);
    runner.setProperty(MonitorActivity.MONITORING_SCOPE, MonitorActivity.SCOPE_CLUSTER);
    runner.setProperty(MonitorActivity.REPORTING_NODE, MonitorActivity.REPORT_NODE_PRIMARY);
    runner.setProperty(MonitorActivity.THRESHOLD, "100 ms");
    runner.setProperty(MonitorActivity.COPY_ATTRIBUTES, "true");
    // Becomes inactive
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_INACTIVE);
    runner.clearTransferState();
    // Activity restored
    final HashMap<String, String> attributes = new HashMap<>();
    attributes.put("key1", "value1");
    attributes.put("key2", "value2");
    runner.enqueue("Incoming data", attributes);
    runNext(runner);
    final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(MonitorActivity.REL_SUCCESS);
    final List<MockFlowFile> activityRestoredFiles = runner.getFlowFilesForRelationship(MonitorActivity.REL_ACTIVITY_RESTORED);
    assertEquals(1, successFiles.size());
    assertEquals(1, activityRestoredFiles.size());
    assertEquals("value1", activityRestoredFiles.get(0).getAttribute("key1"));
    assertEquals("value2", activityRestoredFiles.get(0).getAttribute("key2"));
    // Latest activity should be persisted
    final StateMap updatedState = runner.getStateManager().getState(Scope.CLUSTER);
    assertNotNull("Latest timestamp should be persisted", updatedState.get(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER));
    assertEquals("value1", updatedState.get("key1"));
    assertEquals("value2", updatedState.get("key2"));
    runner.clearTransferState();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 25 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class TestMonitorActivity method testClusterMonitorActivityRestoredBySelf.

@Test
public void testClusterMonitorActivityRestoredBySelf() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new TestableProcessor(10000));
    runner.setClustered(true);
    runner.setPrimaryNode(false);
    runner.setProperty(MonitorActivity.MONITORING_SCOPE, MonitorActivity.SCOPE_CLUSTER);
    runner.setProperty(MonitorActivity.THRESHOLD, "100 ms");
    runner.setProperty(MonitorActivity.COPY_ATTRIBUTES, "true");
    // Becomes inactive
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_INACTIVE);
    runner.clearTransferState();
    // Activity restored
    final HashMap<String, String> attributes = new HashMap<>();
    attributes.put("key1", "value1");
    attributes.put("key2", "value2");
    runner.enqueue("Incoming data", attributes);
    runNext(runner);
    final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(MonitorActivity.REL_SUCCESS);
    final List<MockFlowFile> activityRestoredFiles = runner.getFlowFilesForRelationship(MonitorActivity.REL_ACTIVITY_RESTORED);
    assertEquals(1, successFiles.size());
    assertEquals(1, activityRestoredFiles.size());
    assertEquals("value1", activityRestoredFiles.get(0).getAttribute("key1"));
    assertEquals("value2", activityRestoredFiles.get(0).getAttribute("key2"));
    // Latest activity should be persisted
    final StateMap updatedState = runner.getStateManager().getState(Scope.CLUSTER);
    assertNotNull("Latest timestamp should be persisted", updatedState.get(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER));
    assertEquals("value1", updatedState.get("key1"));
    assertEquals("value2", updatedState.get("key2"));
    runner.clearTransferState();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Aggregations

StateMap (org.apache.nifi.components.state.StateMap)70 HashMap (java.util.HashMap)31 Test (org.junit.Test)29 IOException (java.io.IOException)18 StateProvider (org.apache.nifi.components.state.StateProvider)14 ArrayList (java.util.ArrayList)11 StateManager (org.apache.nifi.components.state.StateManager)11 FlowFile (org.apache.nifi.flowfile.FlowFile)10 TestRunner (org.apache.nifi.util.TestRunner)10 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)9 ComponentLog (org.apache.nifi.logging.ComponentLog)8 Map (java.util.Map)7 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)7 ProcessException (org.apache.nifi.processor.exception.ProcessException)7 Date (java.util.Date)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 Scope (org.apache.nifi.components.state.Scope)6 ProcessSession (org.apache.nifi.processor.ProcessSession)6 Collections (java.util.Collections)5