2121import jakarta .ws .rs .core .Response .Status ;
2222import jakarta .ws .rs .core .Response .Status .Family ;
2323import jakarta .ws .rs .core .Response .StatusType ;
24+ import org .apache .nifi .cluster .coordination .ClusterCoordinator ;
25+ import org .apache .nifi .cluster .coordination .node .DisconnectionCode ;
26+ import org .apache .nifi .cluster .coordination .node .NodeConnectionState ;
27+ import org .apache .nifi .cluster .coordination .node .NodeConnectionStatus ;
2428import org .apache .nifi .cluster .manager .NodeResponse ;
29+ import org .apache .nifi .cluster .manager .exception .NoConnectedNodesException ;
30+ import org .apache .nifi .cluster .protocol .NodeIdentifier ;
2531import org .apache .nifi .components .connector .ConnectorRequestReplicator ;
2632import org .apache .nifi .components .connector .ConnectorState ;
33+ import org .apache .nifi .components .connector .FlowEvolutionStatus ;
2734import org .apache .nifi .controller .flow .FlowManager ;
2835import org .apache .nifi .web .api .entity .ConnectorEntity ;
2936import org .apache .nifi .web .api .entity .Entity ;
@@ -41,30 +48,64 @@ public class ClusteredConnectorRequestReplicator implements ConnectorRequestRepl
4148 private static final String GET = "GET" ;
4249
4350 private final Supplier <RequestReplicator > requestReplicatorSupplier ;
51+ private final Supplier <ClusterCoordinator > clusterCoordinatorSupplier ;
4452 private final String replicationScheme ;
4553
46- public ClusteredConnectorRequestReplicator (final Supplier <RequestReplicator > requestReplicatorSupplier , final boolean httpsEnabled ) {
54+ public ClusteredConnectorRequestReplicator (final Supplier <RequestReplicator > requestReplicatorSupplier , final Supplier <ClusterCoordinator > clusterCoordinatorSupplier ,
55+ final boolean httpsEnabled ) {
4756 this .requestReplicatorSupplier = Objects .requireNonNull (requestReplicatorSupplier , "Request Replicator Supplier required" );
57+ this .clusterCoordinatorSupplier = Objects .requireNonNull (clusterCoordinatorSupplier , "Cluster Coordinator Supplier required" );
4858 this .replicationScheme = httpsEnabled ? "https" : "http" ;
4959 }
5060
5161 @ Override
5262 public ConnectorState getState (final String connectorId ) throws IOException {
63+ final ConnectorEntity connectorEntity = getConnectorEntity (connectorId , false );
64+ final String stateName = connectorEntity .getComponent ().getState ();
65+ logger .debug ("getState: Connector [{}] — state from response: [{}]" , connectorId , stateName );
66+ try {
67+ return ConnectorState .valueOf (stateName );
68+ } catch (final IllegalArgumentException e ) {
69+ throw new IOException ("Received unknown Connector state '" + stateName + "' for Connector with ID " + connectorId );
70+ }
71+ }
72+
73+ @ Override
74+ public FlowEvolutionStatus getFlowEvolutionStatus (final String connectorId ) throws IOException {
75+ final ConnectorEntity connectorEntity = getConnectorEntity (connectorId , true );
76+ final String statusName = connectorEntity .getComponent ().getFlowEvolutionStatus ();
77+ if (statusName == null ) {
78+ return FlowEvolutionStatus .INACTIVE ;
79+ }
80+
81+ try {
82+ return FlowEvolutionStatus .valueOf (statusName );
83+ } catch (final IllegalArgumentException e ) {
84+ throw new IOException ("Received unknown flow evolution status '" + statusName + "' for Connector with ID " + connectorId );
85+ }
86+ }
87+
88+ private ConnectorEntity getConnectorEntity (final String connectorId , final boolean requireAllConnected ) throws IOException {
5389 final RequestReplicator requestReplicator = getRequestReplicator ();
90+ verifyClusterMembership (connectorId , requireAllConnected );
91+
5492 final URI uri = URI .create (replicationScheme + "://localhost/nifi-api/connectors/" + connectorId );
5593
56- logger .debug ("getState: Connector [{}] — replicating GET to URI [{}]" , connectorId , uri );
94+ logger .debug ("Connector [{}] — replicating GET to URI [{}]" , connectorId , uri );
5795
5896 // User is null. GET /connectors/{id} API endpoint has special logic for authorizing requests made by nifi cluster nodes based on mTLS cert SANs
5997 final AsyncClusterResponse asyncResponse = requestReplicator .replicate (null , GET , uri , Map .of (), Map .of ());
6098
6199 try {
62100 final NodeResponse mergedNodeResponse = asyncResponse .awaitMergedResponse ();
101+ if (mergedNodeResponse == null ) {
102+ throw new IOException ("No merged response received while retrieving Connector " + connectorId );
103+ }
63104 final Entity updatedEntity = mergedNodeResponse .getUpdatedEntity ();
64105 final Response response = mergedNodeResponse .getClientResponse ();
65106 if (response != null ) {
66107 final int statusCode = response .getStatusInfo ().getStatusCode ();
67- logger .debug ("getState: Connector [{}] — merged response status [{}] ({})" , connectorId , statusCode , response .getStatusInfo ().getReasonPhrase ());
108+ logger .debug ("Connector [{}] — merged response status [{}] ({})" , connectorId , statusCode , response .getStatusInfo ().getReasonPhrase ());
68109 verifyResponse (response .getStatusInfo (), connectorId );
69110 } else if (mergedNodeResponse .hasThrowable ()) {
70111 throw new IOException ("Failed to retrieve Connector state for " + connectorId , mergedNodeResponse .getThrowable ());
@@ -77,10 +118,10 @@ public ConnectorState getState(final String connectorId) throws IOException {
77118 // only return the state from whichever single node was selected as the "client response".
78119 final ConnectorEntity connectorEntity ;
79120 if (updatedEntity instanceof final ConnectorEntity mergedConnectorEntity ) {
80- logger .debug ("getState: Connector [{}] - using merged updatedEntity" , connectorId );
121+ logger .debug ("Connector [{}] - using merged updatedEntity" , connectorId );
81122 connectorEntity = mergedConnectorEntity ;
82123 } else {
83- logger .debug ("getState: Connector [{}] — updatedEntity is [{}], falling back to readEntity()" , connectorId ,
124+ logger .debug ("Connector [{}] — updatedEntity is [{}], falling back to readEntity()" , connectorId ,
84125 updatedEntity == null ? "null" : updatedEntity .getClass ().getSimpleName ());
85126 connectorEntity = response .readEntity (ConnectorEntity .class );
86127 }
@@ -93,19 +134,40 @@ public ConnectorState getState(final String connectorId) throws IOException {
93134 + ". Permissions canRead=" + (connectorEntity .getPermissions () != null ? connectorEntity .getPermissions ().getCanRead () : "null" ));
94135 }
95136
96- final String stateName = connectorEntity .getComponent ().getState ();
97- logger .debug ("getState: Connector [{}] — state from response: [{}]" , connectorId , stateName );
98- try {
99- return ConnectorState .valueOf (stateName );
100- } catch (final IllegalArgumentException e ) {
101- throw new IOException ("Received unknown Connector state '" + stateName + "' for Connector with ID " + connectorId );
137+ if (requireAllConnected ) {
138+ for (final NodeIdentifier node : asyncResponse .getNodesInvolved ()) {
139+ final NodeResponse nodeResponse = asyncResponse .getNodeResponse (node );
140+ if (nodeResponse == null || !nodeResponse .is2xx ()) {
141+ throw new IOException ("Failed to retrieve flow evolution status for Connector " + connectorId + " from node " + node );
142+ }
143+ }
102144 }
145+ return connectorEntity ;
103146 } catch (final InterruptedException e ) {
104147 Thread .currentThread ().interrupt ();
105148 throw new IOException ("Interrupted while waiting for response for status of Connector with ID " + connectorId , e );
106149 }
107150 }
108151
152+ private void verifyClusterMembership (final String connectorId , final boolean requireAllConnected ) throws IOException {
153+ final ClusterCoordinator clusterCoordinator = clusterCoordinatorSupplier .get ();
154+ if (clusterCoordinator == null || !clusterCoordinator .isConnected ()) {
155+ throw new NoConnectedNodesException ();
156+ }
157+
158+ for (final NodeConnectionStatus connectionStatus : clusterCoordinator .getConnectionStatuses ()) {
159+ final NodeConnectionState state = connectionStatus .getState ();
160+ if (state == NodeConnectionState .CONNECTING
161+ || (state == NodeConnectionState .DISCONNECTED && connectionStatus .getDisconnectCode () == DisconnectionCode .NOT_YET_CONNECTED )) {
162+ throw new NoConnectedNodesException ();
163+ }
164+ if (requireAllConnected && state != NodeConnectionState .CONNECTED && state != NodeConnectionState .REMOVED ) {
165+ throw new IOException ("Cannot retrieve flow evolution status for Connector " + connectorId
166+ + " because node " + connectionStatus .getNodeIdentifier () + " is " + state );
167+ }
168+ }
169+ }
170+
109171 @ Override
110172 public void setFlowManager (final FlowManager flowManager ) {
111173 }
0 commit comments