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;
}
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);
}
}
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);
}
}
Aggregations