Search in sources :

Example 1 with ProcessorsEntity

use of org.apache.nifi.web.api.entity.ProcessorsEntity in project nifi by apache.

the class ClusterReplicationComponentLifecycle method waitForProcessorStatus.

/**
 * Periodically polls the process group with the given ID, waiting for all processors whose ID's are given to have the given Scheduled State.
 *
 * @param user the user making the request
 * @param originalUri the original uri
 * @param groupId the ID of the Process Group to poll
 * @param processors the Processors whose state should be equal to the given desired state
 * @param desiredState the desired state for all processors with the ID's given
 * @param pause the Pause that can be used to wait between polling
 * @return <code>true</code> if successful, <code>false</code> if unable to wait for processors to reach the desired state
 */
private boolean waitForProcessorStatus(final NiFiUser user, final URI originalUri, final String groupId, final Map<String, AffectedComponentEntity> processors, final ScheduledState desiredState, final Pause pause) throws InterruptedException {
    URI groupUri;
    try {
        groupUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/process-groups/" + groupId + "/processors", "includeDescendantGroups=true", originalUri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    final MultivaluedMap<String, String> requestEntity = new MultivaluedHashMap<>();
    boolean continuePolling = true;
    while (continuePolling) {
        // Determine whether we should replicate only to the cluster coordinator, or if we should replicate directly to the cluster nodes themselves.
        final NodeResponse clusterResponse;
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            clusterResponse = getRequestReplicator().replicate(user, HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        } else {
            clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), user, HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        }
        if (clusterResponse.getStatus() != Status.OK.getStatusCode()) {
            return false;
        }
        final ProcessorsEntity processorsEntity = getResponseEntity(clusterResponse, ProcessorsEntity.class);
        final Set<ProcessorEntity> processorEntities = processorsEntity.getProcessors();
        if (isProcessorActionComplete(processorEntities, processors, desiredState)) {
            logger.debug("All {} processors of interest now have the desired state of {}", processors.size(), desiredState);
            return true;
        }
        // Not all of the processors are in the desired state. Pause for a bit and poll again.
        continuePolling = pause.pause();
    }
    return false;
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) URISyntaxException(java.net.URISyntaxException) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity)

Example 2 with ProcessorsEntity

use of org.apache.nifi.web.api.entity.ProcessorsEntity in project nifi by apache.

the class ProcessorsEndpointMerger method merge.

@Override
public final NodeResponse merge(final URI uri, final String method, final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses, final NodeResponse clientResponse) {
    if (!canHandle(uri, method)) {
        throw new IllegalArgumentException("Cannot use Endpoint Mapper of type " + getClass().getSimpleName() + " to map responses for URI " + uri + ", HTTP Method " + method);
    }
    final ProcessorsEntity responseEntity = clientResponse.getClientResponse().readEntity(ProcessorsEntity.class);
    final Set<ProcessorEntity> processorEntities = responseEntity.getProcessors();
    final Map<String, Map<NodeIdentifier, ProcessorEntity>> entityMap = new HashMap<>();
    for (final NodeResponse nodeResponse : successfulResponses) {
        final ProcessorsEntity nodeResponseEntity = nodeResponse == clientResponse ? responseEntity : nodeResponse.getClientResponse().readEntity(ProcessorsEntity.class);
        final Set<ProcessorEntity> nodeProcessorEntities = nodeResponseEntity.getProcessors();
        for (final ProcessorEntity nodeProcessorEntity : nodeProcessorEntities) {
            final NodeIdentifier nodeId = nodeResponse.getNodeId();
            Map<NodeIdentifier, ProcessorEntity> innerMap = entityMap.get(nodeId);
            if (innerMap == null) {
                innerMap = new HashMap<>();
                entityMap.put(nodeProcessorEntity.getId(), innerMap);
            }
            innerMap.put(nodeResponse.getNodeId(), nodeProcessorEntity);
        }
    }
    ProcessorsEntityMerger.mergeProcessors(processorEntities, entityMap);
    // create a new client response
    return new NodeResponse(clientResponse, responseEntity);
}
Also used : HashMap(java.util.HashMap) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ProcessorsEntity

use of org.apache.nifi.web.api.entity.ProcessorsEntity in project nifi by apache.

the class ProcessGroupResource method getProcessors.

/**
 * Retrieves all the processors in this NiFi.
 *
 * @param groupId group id
 * @return A processorsEntity.
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/processors")
@ApiOperation(value = "Gets all processors", response = ProcessorsEntity.class, authorizations = { @Authorization(value = "Read - /process-groups/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response getProcessors(@ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, @ApiParam("Whether or not to include processors from descendant process groups") @QueryParam("includeDescendantGroups") @DefaultValue("false") boolean includeDescendantGroups) {
    if (isReplicateRequest()) {
        return replicate(HttpMethod.GET);
    }
    // authorize access
    serviceFacade.authorizeAccess(lookup -> {
        final Authorizable processGroup = lookup.getProcessGroup(groupId).getAuthorizable();
        processGroup.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
    });
    // get the processors
    final Set<ProcessorEntity> processors = serviceFacade.getProcessors(groupId, includeDescendantGroups);
    // create the response entity
    final ProcessorsEntity entity = new ProcessorsEntity();
    entity.setProcessors(processorResource.populateRemainingProcessorEntitiesContent(processors));
    // generate the response
    return generateOkResponse(entity).build();
}
Also used : ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) SnippetAuthorizable(org.apache.nifi.authorization.SnippetAuthorizable) TemplateContentsAuthorizable(org.apache.nifi.authorization.TemplateContentsAuthorizable) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with ProcessorsEntity

use of org.apache.nifi.web.api.entity.ProcessorsEntity in project nifi by apache.

the class ProcessGroupResource method waitForProcessorStatus.

/**
 * Periodically polls the process group with the given ID, waiting for all processors whose ID's are given to have the given Scheduled State.
 *
 * @param groupId the ID of the Process Group to poll
 * @param processorIds the ID of all Processors whose state should be equal to the given desired state
 * @param desiredState the desired state for all processors with the ID's given
 * @param pause the Pause that can be used to wait between polling
 * @return <code>true</code> if successful, <code>false</code> if unable to wait for processors to reach the desired state
 */
private boolean waitForProcessorStatus(final URI originalUri, final String groupId, final Set<String> processorIds, final ScheduledState desiredState, final VariableRegistryUpdateRequest updateRequest, final Pause pause) throws InterruptedException {
    URI groupUri;
    try {
        groupUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/process-groups/" + groupId + "/processors", "includeDescendantGroups=true", originalUri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    final MultivaluedMap<String, String> requestEntity = new MultivaluedHashMap<>();
    boolean continuePolling = true;
    while (continuePolling) {
        // Determine whether we should replicate only to the cluster coordinator, or if we should replicate directly to the cluster nodes themselves.
        final NodeResponse clusterResponse;
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            clusterResponse = getRequestReplicator().replicate(HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        } else {
            clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        }
        if (clusterResponse.getStatus() != Status.OK.getStatusCode()) {
            return false;
        }
        final ProcessorsEntity processorsEntity = getResponseEntity(clusterResponse, ProcessorsEntity.class);
        final Set<ProcessorEntity> processorEntities = processorsEntity.getProcessors();
        if (isProcessorActionComplete(processorEntities, updateRequest, processorIds, desiredState)) {
            logger.debug("All {} processors of interest now have the desired state of {}", processorIds.size(), desiredState);
            return true;
        }
        // Not all of the processors are in the desired state. Pause for a bit and poll again.
        continuePolling = pause.pause();
    }
    return false;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) URISyntaxException(java.net.URISyntaxException) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity)

Aggregations

ProcessorEntity (org.apache.nifi.web.api.entity.ProcessorEntity)4 ProcessorsEntity (org.apache.nifi.web.api.entity.ProcessorsEntity)4 HashMap (java.util.HashMap)3 NodeResponse (org.apache.nifi.cluster.manager.NodeResponse)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ComponentAuthorizable (org.apache.nifi.authorization.ComponentAuthorizable)1 ProcessGroupAuthorizable (org.apache.nifi.authorization.ProcessGroupAuthorizable)1 SnippetAuthorizable (org.apache.nifi.authorization.SnippetAuthorizable)1 TemplateContentsAuthorizable (org.apache.nifi.authorization.TemplateContentsAuthorizable)1 Authorizable (org.apache.nifi.authorization.resource.Authorizable)1