Search in sources :

Example 1 with StandardStateMap

use of org.apache.nifi.controller.state.StandardStateMap in project nifi by apache.

the class WriteAheadLocalStateProvider method getProvider.

private ComponentProvider getProvider(final String componentId) {
    ComponentProvider componentProvider = componentProviders.get(componentId);
    if (componentProvider == null) {
        final StateMap stateMap = new StandardStateMap(Collections.<String, String>emptyMap(), -1L);
        componentProvider = new ComponentProvider(writeAheadLog, versionGenerator, componentId, stateMap, alwaysSync);
        final ComponentProvider existingComponentProvider = componentProviders.putIfAbsent(componentId, componentProvider);
        if (existingComponentProvider != null) {
            componentProvider = existingComponentProvider;
        }
    }
    return componentProvider;
}
Also used : StateMap(org.apache.nifi.components.state.StateMap) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap)

Example 2 with StandardStateMap

use of org.apache.nifi.controller.state.StandardStateMap in project nifi by apache.

the class ZooKeeperStateProvider method deserialize.

private StateMap deserialize(final byte[] data, final int recordVersion, final String componentId) throws IOException {
    try (final ByteArrayInputStream bais = new ByteArrayInputStream(data);
        final DataInputStream dis = new DataInputStream(bais)) {
        final byte encodingVersion = dis.readByte();
        if (encodingVersion > ENCODING_VERSION) {
            throw new IOException("Retrieved a response from ZooKeeper when retrieving state for component with ID " + componentId + ", but the response was encoded using the ZooKeeperStateProvider Encoding Version of " + encodingVersion + " but this instance can only decode versions up to " + ENCODING_VERSION + "; it appears that the state was encoded using a newer version of NiFi than is currently running. This information cannot be decoded.");
        }
        final int numEntries = dis.readInt();
        final Map<String, String> stateValues = new HashMap<>(numEntries);
        for (int i = 0; i < numEntries; i++) {
            final boolean hasKey = dis.readBoolean();
            final String key = hasKey ? dis.readUTF() : null;
            final boolean hasValue = dis.readBoolean();
            final String value = hasValue ? dis.readUTF() : null;
            stateValues.put(key, value);
        }
        return new StandardStateMap(stateValues, recordVersion);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap)

Example 3 with StandardStateMap

use of org.apache.nifi.controller.state.StandardStateMap in project nifi by apache.

the class ZooKeeperStateProvider method getState.

@Override
public StateMap getState(final String componentId) throws IOException {
    verifyEnabled();
    try {
        final Stat stat = new Stat();
        final String path = getComponentPath(componentId);
        final byte[] data = getZooKeeper().getData(path, false, stat);
        final StateMap stateMap = deserialize(data, stat.getVersion(), componentId);
        return stateMap;
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId + ", due to interruption", e);
    } catch (final KeeperException ke) {
        final Code exceptionCode = ke.code();
        if (Code.NONODE == exceptionCode) {
            return new StandardStateMap(null, -1L);
        }
        if (Code.SESSIONEXPIRED == exceptionCode) {
            invalidateClient();
            return getState(componentId);
        }
        throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId + " with exception code " + exceptionCode, ke);
    } catch (final IOException ioe) {
        // provide more context in the error message
        throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId, ioe);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) StateMap(org.apache.nifi.components.state.StateMap) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap) IOException(java.io.IOException) Code(org.apache.zookeeper.KeeperException.Code) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

StandardStateMap (org.apache.nifi.controller.state.StandardStateMap)3 IOException (java.io.IOException)2 StateMap (org.apache.nifi.components.state.StateMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 HashMap (java.util.HashMap)1 KeeperException (org.apache.zookeeper.KeeperException)1 Code (org.apache.zookeeper.KeeperException.Code)1 Stat (org.apache.zookeeper.data.Stat)1