Search in sources :

Example 61 with StateMap

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

the class TestMonitorActivity method testClusterMonitorActive.

@Test
public void testClusterMonitorActive() 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");
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_SUCCESS);
    final StateMap updatedState = runner.getStateManager().getState(Scope.CLUSTER);
    assertNotNull("Latest timestamp should be persisted", updatedState.get(MonitorActivity.STATE_KEY_LATEST_SUCCESS_TRANSFER));
    // Should be null because COPY_ATTRIBUTES is null.
    assertNull(updatedState.get("key1"));
    assertNull(updatedState.get("key2"));
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 62 with StateMap

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

the class ListS3 method restoreState.

private void restoreState(final ProcessContext context) throws IOException {
    final StateMap stateMap = context.getStateManager().getState(Scope.CLUSTER);
    if (stateMap.getVersion() == -1L || stateMap.get(CURRENT_TIMESTAMP) == null || stateMap.get(CURRENT_KEY_PREFIX + "0") == null) {
        currentTimestamp = 0L;
        currentKeys = new HashSet<>();
    } else {
        currentTimestamp = Long.parseLong(stateMap.get(CURRENT_TIMESTAMP));
        currentKeys = extractKeys(stateMap);
    }
}
Also used : StateMap(org.apache.nifi.components.state.StateMap)

Example 63 with StateMap

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

the class MockStateManager method assertStateNotSet.

/**
 * Ensures that the state was not set for the given scope
 *
 * @param scope the scope
 */
public void assertStateNotSet(final Scope scope) {
    final StateMap stateMap = (scope == Scope.CLUSTER) ? clusterStateMap : localStateMap;
    Assert.assertEquals("Expected state not to be set for Scope " + scope + ", but it was set", -1L, stateMap.getVersion());
}
Also used : StateMap(org.apache.nifi.components.state.StateMap)

Example 64 with StateMap

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

the class MockStateManager method assertStateEquals.

/**
 * Ensures that the state is equal to the given values
 *
 * @param stateValues the values expected
 * @param scope the scope to compare the stateValues against
 */
public void assertStateEquals(final Map<String, String> stateValues, final Scope scope) {
    final StateMap stateMap = retrieveState(scope);
    Assert.assertEquals(stateValues, stateMap.toMap());
}
Also used : StateMap(org.apache.nifi.components.state.StateMap)

Example 65 with StateMap

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

the class MockStateManager method setState.

@Override
public synchronized void setState(final Map<String, String> state, final Scope scope) throws IOException {
    verifyAnnotation(scope);
    verifyCanSet(scope);
    final StateMap stateMap = new MockStateMap(state, versionIndex.incrementAndGet());
    if (scope == Scope.CLUSTER) {
        clusterStateMap = stateMap;
    } else {
        localStateMap = stateMap;
    }
}
Also used : StateMap(org.apache.nifi.components.state.StateMap)

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