Java Code Examples for org.apache.nifi.components.state.StateMap#toMap()

The following examples show how to use org.apache.nifi.components.state.StateMap#toMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTestStateProvider.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testToMap() throws IOException {
    final String key = "testKeySet";
    final StateProvider provider = getProvider();
    Map<String, String> map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());

    provider.setState(Collections.singletonMap(key, "value"), componentId);
    map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertEquals(1, map.size());
    assertEquals("value", map.get(key));

    provider.setState(Collections.<String, String> emptyMap(), componentId);

    final StateMap stateMap = provider.getState(componentId);
    map = stateMap.toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    assertEquals(1, stateMap.getVersion());
}
 
Example 2
Source File: ITRedisStateProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testToMap() throws IOException {
    final String key = "testKeySet";
    final StateProvider provider = getProvider();
    Map<String, String> map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());

    provider.setState(Collections.singletonMap(key, "value"), componentId);
    map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertEquals(1, map.size());
    assertEquals("value", map.get(key));

    provider.setState(Collections.<String, String> emptyMap(), componentId);

    final StateMap stateMap = provider.getState(componentId);
    map = stateMap.toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    assertEquals(1, stateMap.getVersion());
}
 
Example 3
Source File: AbstractTestStateProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testToMap() throws IOException {
    final String key = "testKeySet";
    final StateProvider provider = getProvider();
    Map<String, String> map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());

    provider.setState(Collections.singletonMap(key, "value"), componentId);
    map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertEquals(1, map.size());
    assertEquals("value", map.get(key));

    provider.setState(Collections.<String, String> emptyMap(), componentId);

    final StateMap stateMap = provider.getState(componentId);
    map = stateMap.toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    assertEquals(1, stateMap.getVersion());
}
 
Example 4
Source File: DtoFactory.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a StateMapDTO for the given scope and state map.
 *
 * @param scope the scope
 * @param stateMap the state map
 * @return dto
 */
public StateMapDTO createStateMapDTO(final Scope scope, final StateMap stateMap) {
    if (stateMap == null) {
        return null;
    }

    final StateMapDTO dto = new StateMapDTO();
    dto.setScope(scope.toString());

    final TreeMap<String, String> sortedState = new TreeMap<>(SortedStateUtils.getKeyComparator());
    final Map<String, String> state = stateMap.toMap();
    sortedState.putAll(state);

    int count = 0;
    final List<StateEntryDTO> stateEntries = new ArrayList<>();
    final Set<Map.Entry<String, String>> entrySet = sortedState.entrySet();
    for (final Iterator<Entry<String, String>> iter = entrySet.iterator(); iter.hasNext() && count++ < SortedStateUtils.MAX_COMPONENT_STATE_ENTRIES;) {
        final Map.Entry<String, String> entry = iter.next();
        final StateEntryDTO entryDTO = new StateEntryDTO();
        entryDTO.setKey(entry.getKey());
        entryDTO.setValue(entry.getValue());
        stateEntries.add(entryDTO);
    }
    dto.setTotalEntryCount(state.size());
    dto.setState(stateEntries);

    return dto;
}
 
Example 5
Source File: StateMapSerDe.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeRecord(final StateMapUpdate record, final DataOutputStream out) throws IOException {
    out.writeUTF(record.getComponentId());
    out.writeUTF(record.getUpdateType().name());
    if (record.getUpdateType() == UpdateType.DELETE) {
        return;
    }

    final StateMap stateMap = record.getStateMap();
    final long recordVersion = stateMap.getVersion();
    out.writeLong(recordVersion);

    final Map<String, String> map = stateMap.toMap();
    out.writeInt(map.size());
    for (final Map.Entry<String, String> entry : map.entrySet()) {
        final boolean hasKey = entry.getKey() != null;
        final boolean hasValue = entry.getValue() != null;
        out.writeBoolean(hasKey);
        if (hasKey) {
            out.writeUTF(entry.getKey());
        }

        out.writeBoolean(hasValue);
        if (hasValue) {
            out.writeUTF(entry.getValue());
        }
    }
}
 
Example 6
Source File: AbstractTestStateProvider.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceSuccessful() throws IOException {
    final String key = "testReplaceSuccessful";
    final StateProvider provider = getProvider();

    StateMap map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(-1, map.getVersion());

    assertNotNull(map.toMap());
    assertTrue(map.toMap().isEmpty());
    provider.setState(Collections.singletonMap(key, "value1"), componentId);

    map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(0, map.getVersion());
    assertEquals("value1", map.get(key));
    assertEquals("value1", map.toMap().get(key));

    final Map<String, String> newMap = new HashMap<>(map.toMap());
    newMap.put(key, "value2");
    assertTrue(provider.replace(map, newMap, componentId));

    map = provider.getState(componentId);
    assertEquals("value2", map.get(key));
    assertEquals(1L, map.getVersion());
}
 
Example 7
Source File: StatePeerPersistence.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void save(final PeerStatusCache peerStatusCache) throws IOException {
    final StateMap state = stateManager.getState(Scope.LOCAL);
    final Map<String, String> stateMap = state.toMap();
    final Map<String, String> updatedStateMap = new HashMap<>(stateMap);
    final StringBuilder peers = new StringBuilder();
    write(peerStatusCache, peers::append);
    updatedStateMap.put(STATE_KEY_PEERS, peers.toString());
    updatedStateMap.put(STATE_KEY_PEERS_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
    stateManager.setState(updatedStateMap, Scope.LOCAL);
}
 
Example 8
Source File: ITRedisStateProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceSuccessful() throws IOException {
    final String key = "testReplaceSuccessful";
    final StateProvider provider = getProvider();

    StateMap map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(-1, map.getVersion());

    assertNotNull(map.toMap());
    assertTrue(map.toMap().isEmpty());
    provider.setState(Collections.singletonMap(key, "value1"), componentId);

    map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(0, map.getVersion());
    assertEquals("value1", map.get(key));
    assertEquals("value1", map.toMap().get(key));

    final Map<String, String> newMap = new HashMap<>(map.toMap());
    newMap.put(key, "value2");
    assertTrue(provider.replace(map, newMap, componentId));

    map = provider.getState(componentId);
    assertEquals("value2", map.get(key));
    assertEquals(1L, map.getVersion());
}
 
Example 9
Source File: StateMapSerDe.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeRecord(final StateMapUpdate record, final DataOutputStream out) throws IOException {
    out.writeUTF(record.getComponentId());
    out.writeUTF(record.getUpdateType().name());
    if (record.getUpdateType() == UpdateType.DELETE) {
        return;
    }

    final StateMap stateMap = record.getStateMap();
    final long recordVersion = stateMap.getVersion();
    out.writeLong(recordVersion);

    final Map<String, String> map = stateMap.toMap();
    out.writeInt(map.size());
    for (final Map.Entry<String, String> entry : map.entrySet()) {
        final boolean hasKey = entry.getKey() != null;
        final boolean hasValue = entry.getValue() != null;
        out.writeBoolean(hasKey);
        if (hasKey) {
            out.writeUTF(entry.getKey());
        }

        out.writeBoolean(hasValue);
        if (hasValue) {
            out.writeUTF(entry.getValue());
        }
    }
}
 
Example 10
Source File: AbstractTestStateProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceSuccessful() throws IOException {
    final String key = "testReplaceSuccessful";
    final StateProvider provider = getProvider();

    StateMap map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(-1, map.getVersion());

    assertNotNull(map.toMap());
    assertTrue(map.toMap().isEmpty());
    provider.setState(Collections.singletonMap(key, "value1"), componentId);

    map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(0, map.getVersion());
    assertEquals("value1", map.get(key));
    assertEquals("value1", map.toMap().get(key));

    final Map<String, String> newMap = new HashMap<>(map.toMap());
    newMap.put(key, "value2");
    assertTrue(provider.replace(map, newMap, componentId));

    map = provider.getState(componentId);
    assertEquals("value2", map.get(key));
    assertEquals(1L, map.getVersion());
}
 
Example 11
Source File: TestAbstractListProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnlyNewStateStored() throws Exception {
    final ConcreteListProcessor proc = new ConcreteListProcessor();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.run();

    final long initialTimestamp = System.nanoTime();

    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 0);
    proc.addEntity("name", "id", initialTimestamp);
    proc.addEntity("name", "id2", initialTimestamp);

    runner.run();
    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 0);
    runner.clearTransferState();

    Thread.sleep(DEFAULT_SLEEP_MILLIS);

    runner.run();
    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 2);
    runner.clearTransferState();

    final StateMap stateMap = runner.getStateManager().getState(Scope.CLUSTER);
    assertEquals(2, stateMap.getVersion());

    final Map<String, String> map = stateMap.toMap();
    // Ensure only timestamp is migrated
    assertEquals(2, map.size());
    assertEquals(Long.toString(initialTimestamp), map.get(AbstractListProcessor.LISTING_TIMESTAMP_KEY));
    assertEquals(Long.toString(initialTimestamp), map.get(AbstractListProcessor.PROCESSED_TIMESTAMP_KEY));

    proc.addEntity("new name", "new id", initialTimestamp + 1);
    runner.run();

    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 1);
    runner.clearTransferState();

    StateMap updatedStateMap = runner.getStateManager().getState(Scope.CLUSTER);
    assertEquals(3, updatedStateMap.getVersion());

    assertEquals(2, updatedStateMap.toMap().size());
    assertEquals(Long.toString(initialTimestamp + 1), updatedStateMap.get(AbstractListProcessor.LISTING_TIMESTAMP_KEY));
    // Processed timestamp is now caught up
    assertEquals(Long.toString(initialTimestamp + 1), updatedStateMap.get(AbstractListProcessor.PROCESSED_TIMESTAMP_KEY));
}
 
Example 12
Source File: ITAbstractListProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnlyNewStateStored() throws Exception {

    runner.run();

    final long initialTimestamp = System.currentTimeMillis();

    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 0);
    proc.addEntity("name", "id", initialTimestamp);
    proc.addEntity("name", "id2", initialTimestamp);

    runner.run();
    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 0);
    runner.clearTransferState();

    Thread.sleep(DEFAULT_SLEEP_MILLIS);

    runner.run();
    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 2);
    runner.clearTransferState();

    final StateMap stateMap = runner.getStateManager().getState(Scope.CLUSTER);
    assertEquals(2, stateMap.getVersion());

    final Map<String, String> map = stateMap.toMap();
    // Ensure timestamp and identifiers are migrated
    assertEquals(4, map.size());
    assertEquals(Long.toString(initialTimestamp), map.get(AbstractListProcessor.LATEST_LISTED_ENTRY_TIMESTAMP_KEY));
    assertEquals(Long.toString(initialTimestamp), map.get(AbstractListProcessor.LAST_PROCESSED_LATEST_ENTRY_TIMESTAMP_KEY));
    assertEquals("id", map.get(AbstractListProcessor.IDENTIFIER_PREFIX + ".0"));
    assertEquals("id2", map.get(AbstractListProcessor.IDENTIFIER_PREFIX + ".1"));

    proc.addEntity("new name", "new id", initialTimestamp + 1);
    runner.run();

    runner.assertAllFlowFilesTransferred(ConcreteListProcessor.REL_SUCCESS, 1);
    runner.clearTransferState();

    StateMap updatedStateMap = runner.getStateManager().getState(Scope.CLUSTER);
    assertEquals(3, updatedStateMap.getVersion());

    assertEquals(3, updatedStateMap.toMap().size());
    assertEquals(Long.toString(initialTimestamp + 1), updatedStateMap.get(AbstractListProcessor.LATEST_LISTED_ENTRY_TIMESTAMP_KEY));
    // Processed timestamp is now caught up
    assertEquals(Long.toString(initialTimestamp + 1), updatedStateMap.get(AbstractListProcessor.LAST_PROCESSED_LATEST_ENTRY_TIMESTAMP_KEY));
    assertEquals("new id", updatedStateMap.get(AbstractListProcessor.IDENTIFIER_PREFIX + ".0"));
}
 
Example 13
Source File: NodeClusterCoordinator.java    From nifi with Apache License 2.0 4 votes vote down vote up
private void recoverState() throws IOException {
    final StateMap stateMap = stateManager.getState(Scope.LOCAL);
    if (stateMap == null) {
        logger.debug("No state to restore");
        return;
    }

    final ObjectMapper mapper = new ObjectMapper();
    final JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.setCodec(mapper);

    final Map<NodeIdentifier, NodeConnectionStatus> connectionStatusMap = new HashMap<>();
    NodeIdentifier localNodeId = null;

    final Map<String, String> state = stateMap.toMap();
    for (final Map.Entry<String, String> entry : state.entrySet()) {
        final String nodeUuid = entry.getKey();
        final String nodeIdentifierJson = entry.getValue();
        logger.debug("Recovering state for {} = {}", nodeUuid, nodeIdentifierJson);

        try (final JsonParser jsonParser = jsonFactory.createParser(nodeIdentifierJson)) {
            final NodeIdentifierDescriptor nodeIdDesc = jsonParser.readValueAs(NodeIdentifierDescriptor.class);
            final NodeIdentifier nodeId = nodeIdDesc.toNodeIdentifier();

            connectionStatusMap.put(nodeId, new NodeConnectionStatus(nodeId, DisconnectionCode.NOT_YET_CONNECTED));
            if (nodeIdDesc.isLocalNodeIdentifier()) {
                if (localNodeId == null) {
                    localNodeId = nodeId;
                } else {
                    logger.warn("When recovering state, determined that two Node Identifiers claim to be the local Node Identifier: {} and {}. Will ignore both of these and wait until " +
                        "connecting to cluster to determine which Node Identiifer is the local Node Identifier", localNodeId.getFullDescription(), nodeId.getFullDescription());
                    localNodeId = null;
                }
            }
        }
    }

    if (!connectionStatusMap.isEmpty()) {
        resetNodeStatuses(connectionStatusMap);
    }

    if (localNodeId != null) {
        logger.debug("Recovered state indicating that Local Node Identifier is {}", localNodeId);
        setLocalNodeIdentifier(localNodeId);
    }
}