Search in sources :

Example 31 with StateMap

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

the class StateMapSerDe method serializeRecord.

@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());
        }
    }
}
Also used : StateMap(org.apache.nifi.components.state.StateMap) Map(java.util.Map) HashMap(java.util.HashMap) StateMap(org.apache.nifi.components.state.StateMap)

Example 32 with StateMap

use of org.apache.nifi.components.state.StateMap 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 33 with StateMap

use of org.apache.nifi.components.state.StateMap 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)

Example 34 with StateMap

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

the class StandardNiFiServiceFacade method getControllerServiceState.

@Override
public ComponentStateDTO getControllerServiceState(final String controllerServiceId) {
    final StateMap clusterState = isClustered() ? controllerServiceDAO.getState(controllerServiceId, Scope.CLUSTER) : null;
    final StateMap localState = controllerServiceDAO.getState(controllerServiceId, Scope.LOCAL);
    // controller service will be non null as it was already found when getting the state
    final ControllerServiceNode controllerService = controllerServiceDAO.getControllerService(controllerServiceId);
    return dtoFactory.createComponentStateDTO(controllerServiceId, controllerService.getControllerServiceImplementation().getClass(), localState, clusterState);
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StateMap(org.apache.nifi.components.state.StateMap)

Example 35 with StateMap

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

the class ComponentStateAuditor method clearReportingTaskStateAdvice.

/**
 * Audits clearing of state from a Processor.
 *
 * @param proceedingJoinPoint join point
 * @param reportingTask the reporting task
 * @throws java.lang.Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.ComponentStateDAO+) && " + "execution(void clearState(org.apache.nifi.controller.ReportingTaskNode)) && " + "args(reportingTask)")
public StateMap clearReportingTaskStateAdvice(ProceedingJoinPoint proceedingJoinPoint, ReportingTaskNode reportingTask) throws Throwable {
    // update the reporting task state
    final StateMap stateMap = (StateMap) proceedingJoinPoint.proceed();
    // if no exception were thrown, add the clear action...
    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();
    // ensure the user was found
    if (user != null) {
        Collection<Action> actions = new ArrayList<>();
        // create the reporting task details
        FlowChangeExtensionDetails reportingTaskDetails = new FlowChangeExtensionDetails();
        reportingTaskDetails.setType(reportingTask.getReportingTask().getClass().getSimpleName());
        // create the clear action
        FlowChangeAction configAction = new FlowChangeAction();
        configAction.setUserIdentity(user.getIdentity());
        configAction.setOperation(Operation.ClearState);
        configAction.setTimestamp(new Date());
        configAction.setSourceId(reportingTask.getIdentifier());
        configAction.setSourceName(reportingTask.getName());
        configAction.setSourceType(Component.ReportingTask);
        configAction.setComponentDetails(reportingTaskDetails);
        actions.add(configAction);
        // record the action
        saveActions(actions, logger);
    }
    return stateMap;
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) StateMap(org.apache.nifi.components.state.StateMap) FlowChangeExtensionDetails(org.apache.nifi.action.component.details.FlowChangeExtensionDetails) ArrayList(java.util.ArrayList) Date(java.util.Date) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Around(org.aspectj.lang.annotation.Around)

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