exceptionfactory commented on code in PR #11914:
URL: https://github.com/apache/nifi/pull/11914#discussion_r4606914778
##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/configuration/WebApplicationConfiguration.java:
##########
@@ -220,6 +221,11 @@ public ClusterReplicationComponentLifecycle
clusterComponentLifecycle() {
return lifecycle;
}
+ @Bean
Review Comment:
This needs to include `shutdown` as the destroy method to ensure that the
Executor is stopped
##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/ProcessGroupResource.java:
##########
@@ -925,6 +936,230 @@ private void
authorizeHandleDropAllFlowFilesRequest(String processGroupId, Autho
.forEach(connectionSourceData ->
connectionSourceData.authorize(authorizer, RequestAction.WRITE,
NiFiUserUtils.getNiFiUser()));
}
+ /**
+ * Creates a request to drain the data that is queued in a process group.
+ *
+ * @param processGroupId The id of the process group to drain
+ * @return A processGroupDrainRequestEntity
+ */
+ @POST
+ @Consumes(MediaType.WILDCARD)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{id}/drain-requests")
+ @Operation(
+ summary = "Begins draining a process group",
+ description = "Stops the components that bring data into the
process group and starts every other component so that the queued data is
processed. "
+ + "NiFi ends the drain once those components have stopped
and no FlowFiles remain queued, or when the request is deleted. Ending the
drain "
+ + "leaves every component in its current run state.
FlowFiles received over HTTP Site-to-Site before the drain began but not yet "
+ + "confirmed by the sender may still be added after the
drain ends.",
+ responses = {
+ @ApiResponse(
+ responseCode = "202", description = "The request
has been accepted. An HTTP response header will contain the URI where the
status can be polled.",
+ content = @Content(schema = @Schema(implementation
= ProcessGroupDrainRequestEntity.class))
+ ),
+ @ApiResponse(responseCode = "400", description = "NiFi was
unable to complete the request because it was invalid. The request should not
be retried without modification."),
+ @ApiResponse(responseCode = "401", description = "Client
could not be authenticated."),
+ @ApiResponse(responseCode = "403", description = "Client
is not authorized to make this request."),
+ @ApiResponse(responseCode = "404", description = "The
specified resource could not be found."),
+ @ApiResponse(responseCode = "409", description = "The
request was valid but NiFi was not in the appropriate state to process it.")
+ },
+ security = {
+ @SecurityRequirement(name = "Write -
/process-groups/{uuid} - For this and all encapsulated components"),
+ @SecurityRequirement(name = "Write -
/controller-services/{uuid} - For all encapsulated Controller Services"),
+ @SecurityRequirement(name = "Write -
/controller-services/{uuid} - For all referenced Controller Services that are
not enabled, "
+ + "including the services they require, because
the drain enables them"),
+ @SecurityRequirement(name = "Read -
/controller-services/{uuid} - For all other referenced Controller Services")
+ }
+ )
+ public Response createProcessGroupDrainRequest(
+ @Parameter(description = "The process group id.", required = true)
+ @PathParam("id") final String processGroupId
+ ) {
+ if (isReplicateRequest()) {
+ final NodeResponse nodeResponse;
+ try {
+ nodeResponse = replicateNodeResponse(HttpMethod.POST);
+ } catch (final InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return
Response.status(Status.INTERNAL_SERVER_ERROR).entity("Request to drain Process
Group " + processGroupId + " was interrupted").type("text/plain").build();
+ }
+
+ if (nodeResponse.getStatus() != Status.ACCEPTED.getStatusCode()) {
+ return nodeResponse.getResponse();
+ }
+
+ final ProcessGroupDrainRequestEntity entity =
getResponseEntity(nodeResponse, ProcessGroupDrainRequestEntity.class);
+ final ProcessGroupDrainRequestDTO drainRequest =
entity.getDrainRequest();
+ // A request that another node forwarded to the Cluster
Coordinator is watched by the node that forwarded it
+ if
(httpServletRequest.getHeader(ReplicationHeader.REQUEST_FORWARDED_TO_COORDINATOR.getHeader())
== null) {
+ final URI drainRequestUri =
UriBuilder.fromUri(getAbsolutePath()).path(drainRequest.getId()).build();
+ processGroupDrainMonitor.watch(drainRequestUri,
processGroupId, drainRequest.getId(), NiFiUserUtils.getNiFiUser());
+ }
+ return
Response.status(Status.ACCEPTED).location(URI.create(drainRequest.getUri())).entity(entity).build();
+ }
+
+ final ProcessGroupEntity requestProcessGroupEntity = new
ProcessGroupEntity();
+ requestProcessGroupEntity.setId(processGroupId);
+
+ return withWriteLock(
+ serviceFacade,
+ requestProcessGroupEntity,
+ lookup -> {
+ final ProcessGroupAuthorizable processGroup =
lookup.getProcessGroup(processGroupId);
+ authorizeProcessGroup(processGroup, authorizer, lookup,
RequestAction.WRITE, true, true, true, false, false);
+
+ // The drain enables each of these services that is not
already enabled, which requires permission to modify the service
+ final NiFiUser user = NiFiUserUtils.getNiFiUser();
+ for (final ControllerServiceNode service :
processGroup.getProcessGroup().findControllerServicesForDrain()) {
+ final RequestAction action = service.isActive() ?
RequestAction.READ : RequestAction.WRITE;
+
lookup.getControllerService(service.getIdentifier()).getAuthorizable().authorize(authorizer,
action, user);
+ }
+ },
+ () -> {
+ serviceFacade.verifyBeginProcessGroupDrain(processGroupId);
+
+ final ClusterCoordinator clusterCoordinator =
getClusterCoordinator();
+ if (clusterCoordinator != null) {
+ final Set<NodeIdentifier> disconnectedNodes =
clusterCoordinator.getNodeIdentifiers(NodeConnectionState.CONNECTING,
NodeConnectionState.DISCONNECTED,
+ NodeConnectionState.DISCONNECTING);
+ if (!disconnectedNodes.isEmpty()) {
+ throw new IllegalClusterStateException("Cannot
drain Process Group %s because the following %s nodes are not currently
connected: %s"
+ .formatted(processGroupId,
disconnectedNodes.size(), disconnectedNodes));
+ }
+ }
+ },
+ processGroupEntity -> {
+ // ensure the id is the same across the cluster
+ final String drainRequestId = generateUuid();
+
+ final ProcessGroupDrainRequestDTO drainRequest =
serviceFacade.beginProcessGroupDrain(processGroupEntity.getId(),
drainRequestId);
+ drainRequest.setUri(generateResourceUri("process-groups",
processGroupEntity.getId(), "drain-requests", drainRequest.getId()));
+
+ // A replicated drain is watched by the node that
replicated the request
+ if
(httpServletRequest.getHeader(ReplicationHeader.REQUEST_REPLICATED.getHeader())
== null) {
+
processGroupDrainMonitor.watch(processGroupEntity.getId(), drainRequestId,
NiFiUserUtils.getNiFiUser());
+ }
+
+ final ProcessGroupDrainRequestEntity entity = new
ProcessGroupDrainRequestEntity();
+ entity.setDrainRequest(drainRequest);
+
+ final URI location = URI.create(drainRequest.getUri());
+ return
Response.status(Status.ACCEPTED).location(location).entity(entity).build();
+ }
+ );
+ }
+
+ /**
+ * Gets the current status of a request to drain a process group.
+ *
+ * @param processGroupId The id of the process group
+ * @param drainRequestId The id of the drain request
+ * @return A processGroupDrainRequestEntity. The drain request is empty if
this node has no drain request with the given id.
+ */
+ @GET
+ @Consumes(MediaType.WILDCARD)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{id}/drain-requests/{drain-request-id}")
+ @Operation(
+ summary = "Gets the current status of a request to drain a process
group",
+ description = "The response does not contain a drain request if
the process group has no drain request with the given id.",
+ responses = {
+ @ApiResponse(responseCode = "200", content =
@Content(schema = @Schema(implementation =
ProcessGroupDrainRequestEntity.class))),
+ @ApiResponse(responseCode = "400", description = "NiFi was
unable to complete the request because it was invalid. The request should not
be retried without modification."),
+ @ApiResponse(responseCode = "401", description = "Client
could not be authenticated."),
+ @ApiResponse(responseCode = "403", description = "Client
is not authorized to make this request."),
+ @ApiResponse(responseCode = "404", description = "The
specified process group could not be found."),
+ @ApiResponse(responseCode = "409", description = "The
request was valid but NiFi was not in the appropriate state to process it.")
+ },
+ security = {
+ @SecurityRequirement(name = "Read -
/process-groups/{uuid}")
+ }
+ )
+ public Response getProcessGroupDrainRequest(
+ @Parameter(description = "The process group id.", required = true)
+ @PathParam("id") final String processGroupId,
+ @Parameter(description = "The drain request id.", required = true)
+ @PathParam("drain-request-id") final String drainRequestId
+ ) {
+ if (isReplicateRequest()) {
+ return replicate(HttpMethod.GET);
+ }
+
+ serviceFacade.authorizeAccess(lookup -> {
+ final Authorizable processGroup =
lookup.getProcessGroup(processGroupId).getAuthorizable();
+ processGroup.authorize(authorizer, RequestAction.READ,
NiFiUserUtils.getNiFiUser());
+ });
+
+ final ProcessGroupDrainRequestEntity entity = new
ProcessGroupDrainRequestEntity();
+ final Optional<ProcessGroupDrainRequestDTO> drainRequest =
serviceFacade.getProcessGroupDrainRequest(processGroupId, drainRequestId);
+ if (drainRequest.isPresent()) {
+ drainRequest.get().setUri(generateResourceUri("process-groups",
processGroupId, "drain-requests", drainRequestId));
+ entity.setDrainRequest(drainRequest.get());
+ }
+
+ return generateOkResponse(entity).build();
+ }
+
+ /**
+ * Ends the specified request to drain a process group. The run state of
each component is left unchanged.
+ *
+ * @param processGroupId The id of the process group
+ * @param drainRequestId The id of the drain request
+ * @return A processGroupDrainRequestEntity. The drain request is empty if
this node has no drain request with the given id.
+ */
+ @DELETE
+ @Consumes(MediaType.WILDCARD)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{id}/drain-requests/{drain-request-id}")
+ @Operation(
+ summary = "Ends a request to drain a process group",
+ description = "Ends the drain and leaves every component in its
current run state. The response contains the final status of the drain.",
+ responses = {
+ @ApiResponse(responseCode = "200", content =
@Content(schema = @Schema(implementation =
ProcessGroupDrainRequestEntity.class))),
+ @ApiResponse(responseCode = "400", description = "NiFi was
unable to complete the request because it was invalid. The request should not
be retried without modification."),
+ @ApiResponse(responseCode = "401", description = "Client
could not be authenticated."),
+ @ApiResponse(responseCode = "403", description = "Client
is not authorized to make this request."),
+ @ApiResponse(responseCode = "404", description = "The
specified resource could not be found."),
+ @ApiResponse(responseCode = "409", description = "The
request was valid but NiFi was not in the appropriate state to process it.")
+ },
+ security = {
+ @SecurityRequirement(name = "Write -
/process-groups/{uuid}")
Review Comment:
This requirement is not as strict as the begin request, which requires write
on every encapsulated component. For consistency, it seems like this should
also require write on every referenced component
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]