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