Search in sources :

Example 1 with StateMap

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

the class StandardNiFiServiceFacade method getProcessorState.

@Override
public ComponentStateDTO getProcessorState(final String processorId) {
    final StateMap clusterState = isClustered() ? processorDAO.getState(processorId, Scope.CLUSTER) : null;
    final StateMap localState = processorDAO.getState(processorId, Scope.LOCAL);
    // processor will be non null as it was already found when getting the state
    final ProcessorNode processor = processorDAO.getProcessor(processorId);
    return dtoFactory.createComponentStateDTO(processorId, processor.getProcessor().getClass(), localState, clusterState);
}
Also used : ProcessorNode(org.apache.nifi.controller.ProcessorNode) StateMap(org.apache.nifi.components.state.StateMap)

Example 2 with StateMap

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

the class TestStateMapSerDe method testCreateRoundTrip.

@Test
public void testCreateRoundTrip() throws IOException {
    final String componentId = "1234";
    final StateMapSerDe serde = new StateMapSerDe();
    final Map<String, String> stateValues = new HashMap<>();
    stateValues.put("abc", "xyz");
    stateValues.put("cba", "zyx");
    final StateMap stateMap = new StandardStateMap(stateValues, 3L);
    final StateMapUpdate record = new StateMapUpdate(stateMap, componentId, UpdateType.CREATE);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final DataOutputStream out = new DataOutputStream(baos)) {
        serde.serializeRecord(record, out);
    }
    final StateMapUpdate update;
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try (final DataInputStream in = new DataInputStream(bais)) {
        update = serde.deserializeRecord(in, serde.getVersion());
    }
    assertNotNull(update);
    assertEquals(componentId, update.getComponentId());
    assertEquals(UpdateType.CREATE, update.getUpdateType());
    final StateMap recoveredStateMap = update.getStateMap();
    assertEquals(3L, recoveredStateMap.getVersion());
    assertEquals(stateValues, recoveredStateMap.toMap());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) StateMap(org.apache.nifi.components.state.StateMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 3 with StateMap

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

the class AbstractTestStateProvider method testReplaceWithNonExistingValue.

@Test
public void testReplaceWithNonExistingValue() throws Exception {
    final StateProvider provider = getProvider();
    StateMap stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    final Map<String, String> newValue = new HashMap<>();
    newValue.put("value", "value");
    final boolean replaced = provider.replace(stateMap, newValue, componentId);
    assertFalse(replaced);
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) HashMap(java.util.HashMap) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 4 with StateMap

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

the class AbstractTestStateProvider method testClear.

@Test
public void testClear() throws IOException {
    final StateProvider provider = getProvider();
    StateMap stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    assertEquals(-1L, stateMap.getVersion());
    assertTrue(stateMap.toMap().isEmpty());
    provider.setState(Collections.singletonMap("testClear", "value"), componentId);
    stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    assertEquals(0, stateMap.getVersion());
    assertEquals("value", stateMap.get("testClear"));
    provider.clear(componentId);
    stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    assertEquals(1L, stateMap.getVersion());
    assertTrue(stateMap.toMap().isEmpty());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 5 with StateMap

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

the class AbstractTestStateProvider method testReplaceWithWrongVersion.

@Test
public void testReplaceWithWrongVersion() throws IOException {
    final String key = "testReplaceWithWrongVersion";
    final StateProvider provider = getProvider();
    provider.setState(Collections.singletonMap(key, "value1"), componentId);
    StateMap stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    assertEquals("value1", stateMap.get(key));
    assertEquals(0, stateMap.getVersion());
    provider.setState(Collections.singletonMap(key, "intermediate value"), componentId);
    assertFalse(provider.replace(stateMap, Collections.singletonMap(key, "value2"), componentId));
    stateMap = provider.getState(componentId);
    assertEquals(key, stateMap.toMap().keySet().iterator().next());
    assertEquals(1, stateMap.toMap().size());
    assertEquals("intermediate value", stateMap.get(key));
    assertEquals(1, stateMap.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) 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