Search in sources :

Example 31 with ScheduledState

use of org.apache.nifi.controller.ScheduledState in project nifi by apache.

the class StandardProcessorDAO method updateProcessor.

@Override
public ProcessorNode updateProcessor(ProcessorDTO processorDTO) {
    ProcessorNode processor = locateProcessor(processorDTO.getId());
    ProcessGroup parentGroup = processor.getProcessGroup();
    // ensure we can perform the update
    verifyUpdate(processor, processorDTO);
    // configure the processor
    configureProcessor(processor, processorDTO);
    parentGroup.onComponentModified();
    // attempt to change the underlying processor if an updated bundle is specified
    // updating the bundle must happen after configuring so that any additional classpath resources are set first
    updateBundle(processor, processorDTO);
    // see if an update is necessary
    if (isNotNull(processorDTO.getState())) {
        final ScheduledState purposedScheduledState = ScheduledState.valueOf(processorDTO.getState());
        // only attempt an action if it is changing
        if (!purposedScheduledState.equals(processor.getScheduledState())) {
            try {
                // perform the appropriate action
                switch(purposedScheduledState) {
                    case RUNNING:
                        parentGroup.startProcessor(processor, true);
                        break;
                    case STOPPED:
                        switch(processor.getScheduledState()) {
                            case RUNNING:
                                parentGroup.stopProcessor(processor);
                                break;
                            case DISABLED:
                                parentGroup.enableProcessor(processor);
                                break;
                        }
                        break;
                    case DISABLED:
                        parentGroup.disableProcessor(processor);
                        break;
                }
            } catch (IllegalStateException | ComponentLifeCycleException ise) {
                throw new NiFiCoreException(ise.getMessage(), ise);
            } catch (RejectedExecutionException ree) {
                throw new NiFiCoreException("Unable to schedule all tasks for the specified processor.", ree);
            } catch (NullPointerException npe) {
                throw new NiFiCoreException("Unable to update processor run state.", npe);
            } catch (Exception e) {
                throw new NiFiCoreException("Unable to update processor run state: " + e, e);
            }
        }
    }
    return processor;
}
Also used : NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ScheduledState(org.apache.nifi.controller.ScheduledState) ProcessGroup(org.apache.nifi.groups.ProcessGroup) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) ParseException(java.text.ParseException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ValidationException(org.apache.nifi.controller.exception.ValidationException)

Example 32 with ScheduledState

use of org.apache.nifi.controller.ScheduledState in project nifi by apache.

the class StandardReportingTaskDAO method verifyUpdate.

private void verifyUpdate(final ReportingTaskNode reportingTask, final ReportingTaskDTO reportingTaskDTO) {
    // ensure the state, if specified, is valid
    if (isNotNull(reportingTaskDTO.getState())) {
        try {
            final ScheduledState purposedScheduledState = ScheduledState.valueOf(reportingTaskDTO.getState());
            // only attempt an action if it is changing
            if (!purposedScheduledState.equals(reportingTask.getScheduledState())) {
                // perform the appropriate action
                switch(purposedScheduledState) {
                    case RUNNING:
                        reportingTask.verifyCanStart();
                        break;
                    case STOPPED:
                        switch(reportingTask.getScheduledState()) {
                            case RUNNING:
                                reportingTask.verifyCanStop();
                                break;
                            case DISABLED:
                                reportingTask.verifyCanEnable();
                                break;
                        }
                        break;
                    case DISABLED:
                        reportingTask.verifyCanDisable();
                        break;
                }
            }
        } catch (IllegalArgumentException iae) {
            throw new IllegalArgumentException(String.format("The specified reporting task state (%s) is not valid. Valid options are 'RUNNING', 'STOPPED', and 'DISABLED'.", reportingTaskDTO.getState()));
        }
    }
    boolean modificationRequest = false;
    if (isAnyNotNull(reportingTaskDTO.getName(), reportingTaskDTO.getSchedulingStrategy(), reportingTaskDTO.getSchedulingPeriod(), reportingTaskDTO.getAnnotationData(), reportingTaskDTO.getProperties(), reportingTaskDTO.getBundle())) {
        modificationRequest = true;
        // validate the request
        final List<String> requestValidation = validateProposedConfiguration(reportingTask, reportingTaskDTO);
        // ensure there was no validation errors
        if (!requestValidation.isEmpty()) {
            throw new ValidationException(requestValidation);
        }
    }
    final BundleDTO bundleDTO = reportingTaskDTO.getBundle();
    if (bundleDTO != null) {
        // ensures all nodes in a cluster have the bundle, throws exception if bundle not found for the given type
        final BundleCoordinate bundleCoordinate = BundleUtils.getBundle(reportingTask.getCanonicalClassName(), bundleDTO);
        // ensure we are only changing to a bundle with the same group and id, but different version
        reportingTask.verifyCanUpdateBundle(bundleCoordinate);
    }
    if (modificationRequest) {
        reportingTask.verifyCanUpdate();
    }
}
Also used : ValidationException(org.apache.nifi.controller.exception.ValidationException) ScheduledState(org.apache.nifi.controller.ScheduledState) BundleDTO(org.apache.nifi.web.api.dto.BundleDTO) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate)

Example 33 with ScheduledState

use of org.apache.nifi.controller.ScheduledState in project nifi by apache.

the class ClusterReplicationComponentLifecycle method scheduleComponents.

@Override
public Set<AffectedComponentEntity> scheduleComponents(final URI exampleUri, final NiFiUser user, final String groupId, final Set<AffectedComponentEntity> components, final ScheduledState desiredState, final Pause pause) throws LifecycleManagementException {
    final Set<String> componentIds = components.stream().map(component -> component.getId()).collect(Collectors.toSet());
    final Map<String, AffectedComponentEntity> componentMap = components.stream().collect(Collectors.toMap(AffectedComponentEntity::getId, Function.identity()));
    final Map<String, Revision> componentRevisionMap = getRevisions(groupId, componentIds);
    final Map<String, RevisionDTO> componentRevisionDtoMap = componentRevisionMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> dtoFactory.createRevisionDTO(entry.getValue())));
    final ScheduleComponentsEntity scheduleProcessorsEntity = new ScheduleComponentsEntity();
    scheduleProcessorsEntity.setComponents(componentRevisionDtoMap);
    scheduleProcessorsEntity.setId(groupId);
    scheduleProcessorsEntity.setState(desiredState.name());
    URI scheduleGroupUri;
    try {
        scheduleGroupUri = new URI(exampleUri.getScheme(), exampleUri.getUserInfo(), exampleUri.getHost(), exampleUri.getPort(), "/nifi-api/flow/process-groups/" + groupId, null, exampleUri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    headers.put("content-type", MediaType.APPLICATION_JSON);
    // Determine whether we should replicate only to the cluster coordinator, or if we should replicate directly to the cluster nodes themselves.
    try {
        final NodeResponse clusterResponse;
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            clusterResponse = getRequestReplicator().replicate(user, HttpMethod.PUT, scheduleGroupUri, scheduleProcessorsEntity, headers).awaitMergedResponse();
        } else {
            clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), user, HttpMethod.PUT, scheduleGroupUri, scheduleProcessorsEntity, headers).awaitMergedResponse();
        }
        final int scheduleComponentStatus = clusterResponse.getStatus();
        if (scheduleComponentStatus != Status.OK.getStatusCode()) {
            final String explanation = getResponseEntity(clusterResponse, String.class);
            throw new LifecycleManagementException("Failed to transition components to a state of " + desiredState + " due to " + explanation);
        }
        final boolean processorsTransitioned = waitForProcessorStatus(user, exampleUri, groupId, componentMap, desiredState, pause);
        if (!processorsTransitioned) {
            throw new LifecycleManagementException("Failed while waiting for components to transition to state of " + desiredState);
        }
    } catch (final InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new LifecycleManagementException("Interrupted while attempting to transition components to state of " + desiredState);
    }
    final Set<AffectedComponentEntity> updatedEntities = components.stream().map(component -> AffectedComponentUtils.updateEntity(component, serviceFacade, dtoFactory, user)).collect(Collectors.toSet());
    return updatedEntities;
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Revision(org.apache.nifi.web.Revision) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) HashMap(java.util.HashMap) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Function(java.util.function.Function) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) HttpMethod(javax.ws.rs.HttpMethod) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) MediaType(javax.ws.rs.core.MediaType) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ActivateControllerServicesEntity(org.apache.nifi.web.api.entity.ActivateControllerServicesEntity) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) RequestReplicator(org.apache.nifi.cluster.coordination.http.replication.RequestReplicator) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) Logger(org.slf4j.Logger) ControllerServicesEntity(org.apache.nifi.web.api.entity.ControllerServicesEntity) Set(java.util.Set) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Collectors(java.util.stream.Collectors) ReplicationTarget(org.apache.nifi.web.api.ApplicationResource.ReplicationTarget) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ScheduledState(org.apache.nifi.controller.ScheduledState) ControllerServiceState(org.apache.nifi.controller.service.ControllerServiceState) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Revision(org.apache.nifi.web.Revision) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 34 with ScheduledState

use of org.apache.nifi.controller.ScheduledState in project nifi by apache.

the class ClusterReplicationComponentLifecycle method isProcessorActionComplete.

private boolean isProcessorActionComplete(final Set<ProcessorEntity> processorEntities, final Map<String, AffectedComponentEntity> affectedComponents, final ScheduledState desiredState) {
    final String desiredStateName = desiredState.name();
    // update the affected processors
    processorEntities.stream().filter(entity -> affectedComponents.containsKey(entity.getId())).forEach(entity -> {
        final AffectedComponentEntity affectedComponentEntity = affectedComponents.get(entity.getId());
        affectedComponentEntity.setRevision(entity.getRevision());
        // only consider update this component if the user had permissions to it
        if (Boolean.TRUE.equals(affectedComponentEntity.getPermissions().getCanRead())) {
            final AffectedComponentDTO affectedComponent = affectedComponentEntity.getComponent();
            affectedComponent.setState(entity.getStatus().getAggregateSnapshot().getRunStatus());
            affectedComponent.setActiveThreadCount(entity.getStatus().getAggregateSnapshot().getActiveThreadCount());
            if (Boolean.TRUE.equals(entity.getPermissions().getCanRead())) {
                affectedComponent.setValidationErrors(entity.getComponent().getValidationErrors());
            }
        }
    });
    final boolean allProcessorsMatch = processorEntities.stream().filter(entity -> affectedComponents.containsKey(entity.getId())).allMatch(entity -> {
        final ProcessorStatusDTO status = entity.getStatus();
        final String runStatus = status.getAggregateSnapshot().getRunStatus();
        final boolean stateMatches = desiredStateName.equalsIgnoreCase(runStatus);
        if (!stateMatches) {
            return false;
        }
        if (desiredState == ScheduledState.STOPPED && status.getAggregateSnapshot().getActiveThreadCount() != 0) {
            return false;
        }
        return true;
    });
    if (!allProcessorsMatch) {
        return false;
    }
    return true;
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Revision(org.apache.nifi.web.Revision) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) HashMap(java.util.HashMap) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Function(java.util.function.Function) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) HttpMethod(javax.ws.rs.HttpMethod) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) MediaType(javax.ws.rs.core.MediaType) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ActivateControllerServicesEntity(org.apache.nifi.web.api.entity.ActivateControllerServicesEntity) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) RequestReplicator(org.apache.nifi.cluster.coordination.http.replication.RequestReplicator) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) Logger(org.slf4j.Logger) ControllerServicesEntity(org.apache.nifi.web.api.entity.ControllerServicesEntity) Set(java.util.Set) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Collectors(java.util.stream.Collectors) ReplicationTarget(org.apache.nifi.web.api.ApplicationResource.ReplicationTarget) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ScheduledState(org.apache.nifi.controller.ScheduledState) ControllerServiceState(org.apache.nifi.controller.service.ControllerServiceState) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity)

Example 35 with ScheduledState

use of org.apache.nifi.controller.ScheduledState in project nifi by apache.

the class LocalComponentLifecycle method isProcessorActionComplete.

private boolean isProcessorActionComplete(final Set<ProcessorEntity> processorEntities, final Map<String, AffectedComponentEntity> affectedComponents, final ScheduledState desiredState) {
    final String desiredStateName = desiredState.name();
    // update the affected processors
    processorEntities.stream().filter(entity -> affectedComponents.containsKey(entity.getId())).forEach(entity -> {
        final AffectedComponentEntity affectedComponentEntity = affectedComponents.get(entity.getId());
        affectedComponentEntity.setRevision(entity.getRevision());
        // only consider updating this component if the user had permissions to it
        if (Boolean.TRUE.equals(affectedComponentEntity.getPermissions().getCanRead())) {
            final AffectedComponentDTO affectedComponent = affectedComponentEntity.getComponent();
            affectedComponent.setState(entity.getStatus().getAggregateSnapshot().getRunStatus());
            affectedComponent.setActiveThreadCount(entity.getStatus().getAggregateSnapshot().getActiveThreadCount());
            if (Boolean.TRUE.equals(entity.getPermissions().getCanRead())) {
                affectedComponent.setValidationErrors(entity.getComponent().getValidationErrors());
            }
        }
    });
    final boolean allProcessorsMatch = processorEntities.stream().filter(entity -> affectedComponents.containsKey(entity.getId())).allMatch(entity -> {
        final ProcessorStatusDTO status = entity.getStatus();
        final String runStatus = status.getAggregateSnapshot().getRunStatus();
        final boolean stateMatches = desiredStateName.equalsIgnoreCase(runStatus);
        if (!stateMatches) {
            return false;
        }
        if (desiredState == ScheduledState.STOPPED && status.getAggregateSnapshot().getActiveThreadCount() != 0) {
            return false;
        }
        return true;
    });
    if (!allProcessorsMatch) {
        return false;
    }
    return true;
}
Also used : NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) Revision(org.apache.nifi.web.Revision) Logger(org.slf4j.Logger) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) LoggerFactory(org.slf4j.LoggerFactory) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) Set(java.util.Set) RevisionManager(org.apache.nifi.web.revision.RevisionManager) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ScheduledState(org.apache.nifi.controller.ScheduledState) Map(java.util.Map) ControllerServiceState(org.apache.nifi.controller.service.ControllerServiceState) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) URI(java.net.URI) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity)

Aggregations

ScheduledState (org.apache.nifi.controller.ScheduledState)35 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)9 Map (java.util.Map)7 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 ControllerServiceState (org.apache.nifi.controller.service.ControllerServiceState)7 Date (java.util.Date)6 HttpMethod (javax.ws.rs.HttpMethod)6 MediaType (javax.ws.rs.core.MediaType)6 ValidationException (org.apache.nifi.controller.exception.ValidationException)6 List (java.util.List)5 HashSet (java.util.HashSet)4 NiFiServiceFacade (org.apache.nifi.web.NiFiServiceFacade)4 Revision (org.apache.nifi.web.Revision)4 BundleDTO (org.apache.nifi.web.api.dto.BundleDTO)4 ProcessorConfigDTO (org.apache.nifi.web.api.dto.ProcessorConfigDTO)4 ControllerServiceEntity (org.apache.nifi.web.api.entity.ControllerServiceEntity)4 Api (io.swagger.annotations.Api)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiParam (io.swagger.annotations.ApiParam)3