This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new f10a689ddd4 NIFI-16594 Preserve node statuses received during flow 
synchronization on reconnection (#11932)
f10a689ddd4 is described below

commit f10a689ddd4f6d891c17150a4f731d73f04be951
Author: Pierre Villard <[email protected]>
AuthorDate: Mon Nov 23 22:06:26 2026 +0100

    NIFI-16594 Preserve node statuses received during flow synchronization on 
reconnection (#11932)
    
    Signed-off-by: Pierre Villard <[email protected]>
    Signed-off-by: David Handermann <[email protected]>
---
 .../cluster/coordination/ClusterCoordinator.java   |  6 ++
 .../coordination/node/NodeClusterCoordinator.java  | 11 +++-
 .../heartbeat/TestAbstractHeartbeatMonitor.java    |  4 ++
 .../node/TestNodeClusterCoordinator.java           | 65 ++++++++++++++++------
 .../nifi/controller/StandardFlowService.java       |  8 ++-
 .../nifi/controller/TestStandardFlowService.java   | 63 +++++++++++++++++++++
 .../nifi/tests/system/clustering/OffloadIT.java    | 44 +++++++++++++++
 7 files changed, 180 insertions(+), 21 deletions(-)

diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/coordination/ClusterCoordinator.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/coordination/ClusterCoordinator.java
index a9c3979c215..67af688fed3 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/coordination/ClusterCoordinator.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/coordination/ClusterCoordinator.java
@@ -248,6 +248,12 @@ public interface ClusterCoordinator {
      */
     void resetNodeStatuses(Map<NodeIdentifier, NodeConnectionStatus> 
statusMap);
 
+    /**
+     * Notifies all registered event listeners of the current connection state 
of every known node, without changing any
+     * node status. This allows components created since the last status 
change to align with the current cluster topology.
+     */
+    void notifyNodeStatusListeners();
+
     /**
      * Resets the status of the node to be in accordance with the given 
NodeConnectionStatus if and only if the
      * currently held status for this node has an Update ID equal to the given 
<code>qualifyingUpdateId</code>
diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/node/NodeClusterCoordinator.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/node/NodeClusterCoordinator.java
index 78aa701ef93..e5b7a3de4eb 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/node/NodeClusterCoordinator.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/node/NodeClusterCoordinator.java
@@ -312,8 +312,8 @@ public class NodeClusterCoordinator implements 
ClusterCoordinator, ProtocolHandl
     public void resetNodeStatuses(final Map<NodeIdentifier, 
NodeConnectionStatus> statusMap) {
         logger.info("Resetting cluster node statuses from {} to {}", 
nodeStatuses, statusMap);
 
-        // For each proposed replacement, update the nodeStatuses map if and 
only if the replacement
-        // has a larger update id than the current value.
+        // Replace each provided status unconditionally: update identifiers 
are assigned by the elected cluster coordinator
+        // and restart when a different node is elected, so they cannot be 
compared against the currently held values.
         for (final Map.Entry<NodeIdentifier, NodeConnectionStatus> entry : 
statusMap.entrySet()) {
             final NodeIdentifier nodeId = entry.getKey();
             final NodeConnectionStatus proposedStatus = entry.getValue();
@@ -328,6 +328,13 @@ public class NodeClusterCoordinator implements 
ClusterCoordinator, ProtocolHandl
         storeState();
     }
 
+    @Override
+    public void notifyNodeStatusListeners() {
+        for (final NodeConnectionStatus status : nodeStatuses.values()) {
+            onNodeStateChange(status.getNodeIdentifier(), status.getState());
+        }
+    }
+
     private NodeConnectionStatus removeNode(final NodeIdentifier nodeId) {
         final NodeConnectionStatus status = 
nodeStatuses.remove(nodeId.getId());
         nodeEvents.remove(nodeId.getId());
diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/heartbeat/TestAbstractHeartbeatMonitor.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/heartbeat/TestAbstractHeartbeatMonitor.java
index 60b030aff98..ecf00553eec 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/heartbeat/TestAbstractHeartbeatMonitor.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/heartbeat/TestAbstractHeartbeatMonitor.java
@@ -338,6 +338,10 @@ public class TestAbstractHeartbeatMonitor {
         public void resetNodeStatuses(Map<NodeIdentifier, 
NodeConnectionStatus> statusMap) {
         }
 
+        @Override
+        public void notifyNodeStatusListeners() {
+        }
+
         @Override
         public void shutdown() {
         }
diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/node/TestNodeClusterCoordinator.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/node/TestNodeClusterCoordinator.java
index 80814cbb00a..97d112a8a89 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/node/TestNodeClusterCoordinator.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/test/java/org/apache/nifi/cluster/coordination/node/TestNodeClusterCoordinator.java
@@ -16,6 +16,7 @@
  */
 package org.apache.nifi.cluster.coordination.node;
 
+import org.apache.nifi.cluster.coordination.ClusterTopologyEventListener;
 import org.apache.nifi.cluster.coordination.flow.FlowElection;
 import 
org.apache.nifi.cluster.manager.exception.IllegalNodeDisconnectionException;
 import org.apache.nifi.cluster.protocol.ConnectionRequest;
@@ -42,7 +43,6 @@ import org.apache.nifi.web.revision.RevisionSnapshot;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Timeout;
-import org.mockito.Mockito;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -64,6 +64,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 public class TestNodeClusterCoordinator {
@@ -82,16 +87,16 @@ public class TestNodeClusterCoordinator {
     public void setup() throws IOException {
         System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, 
"src/test/resources/conf/nifi.properties");
 
-        senderListener = 
Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
+        senderListener = mock(ClusterCoordinationProtocolSenderListener.class);
         nodeStatuses = Collections.synchronizedList(new ArrayList<>());
-        stateManagerProvider = Mockito.mock(StateManagerProvider.class);
+        stateManagerProvider = mock(StateManagerProvider.class);
 
-        final StateManager stateManager = Mockito.mock(StateManager.class);
+        final StateManager stateManager = mock(StateManager.class);
         when(stateManager.getState(any(Scope.class))).thenReturn(new 
MockStateMap(Collections.emptyMap(), 1));
         
when(stateManagerProvider.getStateManager(anyString())).thenReturn(stateManager);
 
-        final EventReporter eventReporter = Mockito.mock(EventReporter.class);
-        final RevisionManager revisionManager = 
Mockito.mock(RevisionManager.class);
+        final EventReporter eventReporter = mock(EventReporter.class);
+        final RevisionManager revisionManager = mock(RevisionManager.class);
         
when(revisionManager.getAllRevisions()).thenReturn(emptyRevisionSnapshot);
 
         coordinator = new NodeClusterCoordinator(senderListener, 
eventReporter, null, new FirstVoteWinsFlowElection(), null, revisionManager, 
createProperties(), null, stateManagerProvider) {
@@ -101,7 +106,7 @@ public class TestNodeClusterCoordinator {
             }
         };
 
-        final FlowService flowService = Mockito.mock(FlowService.class);
+        final FlowService flowService = mock(FlowService.class);
         final StandardDataFlow dataFlow = new StandardDataFlow(new byte[50], 
new byte[50], new byte[50], new HashSet<>());
         when(flowService.createDataFlow()).thenReturn(dataFlow);
         coordinator.setFlowService(flowService);
@@ -144,9 +149,9 @@ public class TestNodeClusterCoordinator {
 
     @Test
     public void testTryAgainIfNoFlowServiceSet() throws IOException {
-        final ClusterCoordinationProtocolSenderListener senderListener = 
Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
-        final EventReporter eventReporter = Mockito.mock(EventReporter.class);
-        final RevisionManager revisionManager = 
Mockito.mock(RevisionManager.class);
+        final ClusterCoordinationProtocolSenderListener senderListener = 
mock(ClusterCoordinationProtocolSenderListener.class);
+        final EventReporter eventReporter = mock(EventReporter.class);
+        final RevisionManager revisionManager = mock(RevisionManager.class);
         
when(revisionManager.getAllRevisions()).thenReturn(emptyRevisionSnapshot);
 
         final NodeClusterCoordinator coordinator = new 
NodeClusterCoordinator(senderListener, eventReporter, null, new 
FirstVoteWinsFlowElection(),
@@ -175,7 +180,7 @@ public class TestNodeClusterCoordinator {
     @Test
     @Timeout(value = 5)
     public void testUnknownNodeAskedToConnectOnAttemptedConnectionComplete() 
throws IOException, InterruptedException {
-        final ClusterCoordinationProtocolSenderListener senderListener = 
Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
+        final ClusterCoordinationProtocolSenderListener senderListener = 
mock(ClusterCoordinationProtocolSenderListener.class);
         final AtomicReference<ReconnectionRequestMessage> requestRef = new 
AtomicReference<>();
 
         
when(senderListener.requestReconnection(any(ReconnectionRequestMessage.class))).thenAnswer(invocation
 -> {
@@ -184,8 +189,8 @@ public class TestNodeClusterCoordinator {
             return null;
         });
 
-        final EventReporter eventReporter = Mockito.mock(EventReporter.class);
-        final RevisionManager revisionManager = 
Mockito.mock(RevisionManager.class);
+        final EventReporter eventReporter = mock(EventReporter.class);
+        final RevisionManager revisionManager = mock(RevisionManager.class);
         
when(revisionManager.getAllRevisions()).thenReturn(emptyRevisionSnapshot);
 
         final NodeClusterCoordinator coordinator = new 
NodeClusterCoordinator(senderListener, eventReporter, null, new 
FirstVoteWinsFlowElection(),
@@ -195,7 +200,7 @@ public class TestNodeClusterCoordinator {
             }
         };
 
-        final FlowService flowService = Mockito.mock(FlowService.class);
+        final FlowService flowService = mock(FlowService.class);
         final StandardDataFlow dataFlow = new StandardDataFlow(new byte[50], 
new byte[50], new byte[50], new HashSet<>());
         when(flowService.createDataFlowFromController()).thenReturn(dataFlow);
         coordinator.setFlowService(flowService);
@@ -244,7 +249,7 @@ public class TestNodeClusterCoordinator {
     @Test
     @Timeout(value = 5)
     public void testStatusChangesReplicated() throws InterruptedException {
-        final RevisionManager revisionManager = 
Mockito.mock(RevisionManager.class);
+        final RevisionManager revisionManager = mock(RevisionManager.class);
         
when(revisionManager.getAllRevisions()).thenReturn(emptyRevisionSnapshot);
 
         // Create a connection request message and send to the coordinator
@@ -508,7 +513,7 @@ public class TestNodeClusterCoordinator {
         }
         nodeStatuses.clear();
 
-        Mockito.doThrow(new 
ProtocolException("Simulated")).when(senderListener).disconnect(any());
+        doThrow(new 
ProtocolException("Simulated")).when(senderListener).disconnect(any());
         coordinator.requestNodeDisconnect(nodeId2, 
DisconnectionCode.USER_DISCONNECTED, "Unit Test");
 
         final Thread disconnectThread = 
coordinator.getPendingDisconnectionThread(nodeId2);
@@ -522,6 +527,34 @@ public class TestNodeClusterCoordinator {
         assertFalse(disconnectThread.isAlive());
     }
 
+    @Test
+    public void 
testNotifyNodeStatusListenersReportsCurrentStatesWithoutChangingThem() {
+        final NodeIdentifier connectedNodeId = createNodeId(1);
+        final NodeIdentifier offloadingNodeId = createNodeId(2);
+        coordinator.updateNodeStatus(new NodeConnectionStatus(connectedNodeId, 
NodeConnectionState.CONNECTED));
+        coordinator.updateNodeStatus(new 
NodeConnectionStatus(offloadingNodeId, NodeConnectionState.OFFLOADING));
+        final NodeConnectionStatus connectedStatusBefore = 
coordinator.getConnectionStatus(connectedNodeId);
+        final NodeConnectionStatus offloadingStatusBefore = 
coordinator.getConnectionStatus(offloadingNodeId);
+
+        final ClusterTopologyEventListener listener = 
mock(ClusterTopologyEventListener.class);
+        coordinator.registerEventListener(listener);
+
+        coordinator.notifyNodeStatusListeners();
+
+        verify(listener, times(1)).onNodeStateChange(connectedNodeId, 
NodeConnectionState.CONNECTED);
+        verify(listener, times(1)).onNodeStateChange(offloadingNodeId, 
NodeConnectionState.OFFLOADING);
+        verify(listener, times(2)).onNodeStateChange(any(), any());
+        verify(listener, never()).onNodeAdded(any());
+        verify(listener, never()).onNodeRemoved(any());
+
+        final NodeConnectionStatus connectedStatusAfter = 
coordinator.getConnectionStatus(connectedNodeId);
+        final NodeConnectionStatus offloadingStatusAfter = 
coordinator.getConnectionStatus(offloadingNodeId);
+        assertEquals(NodeConnectionState.CONNECTED, 
connectedStatusAfter.getState());
+        assertEquals(connectedStatusBefore.getUpdateIdentifier(), 
connectedStatusAfter.getUpdateIdentifier());
+        assertEquals(NodeConnectionState.OFFLOADING, 
offloadingStatusAfter.getState());
+        assertEquals(offloadingStatusBefore.getUpdateIdentifier(), 
offloadingStatusAfter.getUpdateIdentifier());
+    }
+
     private NodeIdentifier createNodeId(final int index) {
         return new NodeIdentifier(String.valueOf(index), "localhost", 8000 + 
index, "localhost", 9000 + index, "localhost", 10000 + index, 11000 + index, 
false);
     }
diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/StandardFlowService.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/StandardFlowService.java
index 5d21b334cbd..8b9e7d1fd57 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/StandardFlowService.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/StandardFlowService.java
@@ -612,7 +612,8 @@ public class StandardFlowService implements FlowService, 
ProtocolHandler {
         return connectionGeneration.get();
     }
 
-    private void handleReconnectionRequest(final ReconnectionRequestMessage 
request) {
+    // Visible for testing
+    void handleReconnectionRequest(final ReconnectionRequestMessage request) {
         try {
             logger.info("Processing reconnection request from cluster 
coordinator.");
 
@@ -643,8 +644,9 @@ public class StandardFlowService implements FlowService, 
ProtocolHandler {
 
             loadFromConnectionResponse(connectionResponse);
 
-            
clusterCoordinator.resetNodeStatuses(connectionResponse.getNodeConnectionStatuses().stream()
-                    
.collect(Collectors.toMap(NodeConnectionStatus::getNodeIdentifier, status -> 
status)));
+            // Node statuses received while the flow was loading are current; 
listeners are notified of them so that
+            // components created while loading the flow align with the 
cluster topology
+            clusterCoordinator.notifyNodeStatusListeners();
             // reconnected, this node needs to explicitly write the inherited 
flow to disk, and resume heartbeats
             saveFlowChanges();
             controller.onClusterConnect();
diff --git 
a/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/TestStandardFlowService.java
 
b/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/TestStandardFlowService.java
index a2ca29fecec..0fa083108bc 100644
--- 
a/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/TestStandardFlowService.java
+++ 
b/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/TestStandardFlowService.java
@@ -27,6 +27,7 @@ import org.apache.nifi.cluster.protocol.NodeIdentifier;
 import org.apache.nifi.cluster.protocol.StandardDataFlow;
 import org.apache.nifi.cluster.protocol.impl.NodeProtocolSenderListener;
 import org.apache.nifi.cluster.protocol.message.DisconnectMessage;
+import org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage;
 import org.apache.nifi.components.state.Scope;
 import org.apache.nifi.components.state.StateManager;
 import org.apache.nifi.components.state.StateManagerProvider;
@@ -54,6 +55,7 @@ import org.mockito.InOrder;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -76,7 +78,9 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.clearInvocations;
 import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -198,6 +202,65 @@ public class TestStandardFlowService {
         verify(clusteredController, never()).setClustered(anyBoolean(), any());
     }
 
+    @Test
+    public void 
testReconnectionPreservesNodeStatusUpdatedDuringFlowSynchronization() throws 
Exception {
+        final FlowController clusteredController = mock(FlowController.class);
+        final StateManagerProvider stateManagerProvider = 
controller.getStateManagerProvider();
+        final ExtensionManager extensionManager = 
controller.getExtensionManager();
+        
when(clusteredController.getStateManagerProvider()).thenReturn(stateManagerProvider);
+        
when(clusteredController.getExtensionManager()).thenReturn(extensionManager);
+
+        final StandardFlowService clusteredFlowService = 
spy(StandardFlowService.createClusteredInstance(clusteredController, 
nifiProperties,
+                mock(NodeProtocolSenderListener.class), clusterCoordinator, 
mock(RevisionManager.class), mock(NarManager.class),
+                mock(AssetSynchronizer.class), mock(AssetSynchronizer.class), 
mock(Authorizer.class)));
+        doNothing().when(clusteredFlowService).loadFromBytes(any(), eq(true), 
eq(BundleUpdateStrategy.USE_SPECIFIED_OR_COMPATIBLE_OR_GHOST));
+        doNothing().when(clusteredFlowService).saveFlowChanges();
+
+        final NodeIdentifier localNodeId = createNodeIdentifier();
+        final NodeIdentifier peerNodeId = new NodeIdentifier("node-2", 
"localhost", 8444, "localhost", 9091, "localhost", 6343, 10444, false);
+        final StandardDataFlow dataFlow = new StandardDataFlow(new byte[0], 
new byte[0], new byte[0], Collections.emptySet());
+        final ComponentRevisionSnapshot revisions = 
mock(ComponentRevisionSnapshot.class);
+
+        // Establishes the local node identifier required to build the 
connection response for a reconnection request
+        clusteredFlowService.loadFromConnectionResponse(new 
ConnectionResponse(localNodeId, dataFlow, "instance-0",
+                List.of(new NodeConnectionStatus(localNodeId, 
NodeConnectionState.CONNECTING)), revisions));
+        clearInvocations(clusteredController, clusterCoordinator, 
clusteredFlowService);
+
+        final Map<String, NodeConnectionStatus> trackedStatuses = new 
HashMap<>();
+        doAnswer(invocation -> {
+            final Map<NodeIdentifier, NodeConnectionStatus> replacements = 
invocation.getArgument(0);
+            replacements.forEach((nodeIdentifier, status) -> 
trackedStatuses.put(nodeIdentifier.getId(), status));
+            return null;
+        }).when(clusterCoordinator).resetNodeStatuses(any(Map.class));
+        doAnswer(invocation -> {
+            trackedStatuses.put(peerNodeId.getId(), new 
NodeConnectionStatus(peerNodeId, NodeConnectionState.CONNECTED));
+            return null;
+        }).when(clusteredFlowService).loadFromBytes(any(), eq(true), 
eq(BundleUpdateStrategy.USE_SPECIFIED_OR_COMPATIBLE_OR_GHOST));
+
+        final ReconnectionRequestMessage request = new 
ReconnectionRequestMessage();
+        request.setNodeId(localNodeId);
+        request.setDataFlow(dataFlow);
+        request.setInstanceId("instance-1");
+        request.setComponentRevisions(revisions);
+        request.setNodeConnectionStatuses(List.of(
+                new NodeConnectionStatus(localNodeId, 
NodeConnectionState.CONNECTING),
+                new NodeConnectionStatus(peerNodeId, 
NodeConnectionState.CONNECTING)));
+
+        clusteredFlowService.handleReconnectionRequest(request);
+
+        assertEquals(NodeConnectionState.CONNECTED, 
trackedStatuses.get(peerNodeId.getId()).getState());
+        assertEquals(NodeConnectionState.CONNECTING, 
trackedStatuses.get(localNodeId.getId()).getState());
+        verify(clusterCoordinator, times(1)).resetNodeStatuses(any(Map.class));
+        verify(clusteredController, never()).setClustered(false, null);
+
+        final InOrder inOrder = inOrder(clusterCoordinator, 
clusteredController, clusteredFlowService);
+        inOrder.verify(clusterCoordinator).resetNodeStatuses(any(Map.class));
+        inOrder.verify(clusteredFlowService).loadFromBytes(any(), eq(true), 
eq(BundleUpdateStrategy.USE_SPECIFIED_OR_COMPATIBLE_OR_GHOST));
+        inOrder.verify(clusterCoordinator).notifyNodeStatusListeners();
+        inOrder.verify(clusteredFlowService).saveFlowChanges();
+        inOrder.verify(clusteredController).onClusterConnect();
+    }
+
     @Test
     @Timeout(10)
     public void testOffloadWaitsForHeldStopFutureBeforeTerminatingProcessors() 
throws Exception {
diff --git 
a/nifi-system-tests/nifi-system-test-suite/src/test/java/org/apache/nifi/tests/system/clustering/OffloadIT.java
 
b/nifi-system-tests/nifi-system-test-suite/src/test/java/org/apache/nifi/tests/system/clustering/OffloadIT.java
index dfc07cc84d8..1313ce77c49 100644
--- 
a/nifi-system-tests/nifi-system-test-suite/src/test/java/org/apache/nifi/tests/system/clustering/OffloadIT.java
+++ 
b/nifi-system-tests/nifi-system-test-suite/src/test/java/org/apache/nifi/tests/system/clustering/OffloadIT.java
@@ -17,6 +17,8 @@
 
 package org.apache.nifi.tests.system.clustering;
 
+import org.apache.nifi.controller.queue.LoadBalanceCompression;
+import org.apache.nifi.controller.queue.LoadBalanceStrategy;
 import org.apache.nifi.tests.system.InstanceConfiguration;
 import org.apache.nifi.tests.system.NiFiInstanceFactory;
 import org.apache.nifi.tests.system.NiFiSystemIT;
@@ -162,6 +164,48 @@ public class OffloadIT extends NiFiSystemIT {
         destroyFlow();
     }
 
+    /**
+     * Verifies that a node can be offloaded immediately after reconnecting to 
the cluster, while it holds FlowFiles in a
+     * load-balanced connection that was added while the node was 
disconnected. The node creates that connection's queue while
+     * synchronizing the flow during reconnection, so offload depends on the 
queue having been aligned with the current cluster
+     * topology, and on the node's view of its peer still being connected, in 
order to transfer every FlowFile to the peer.
+     */
+    @Test
+    @Timeout(value = 5, unit = TimeUnit.MINUTES)
+    public void 
testOffloadImmediatelyAfterReconnectionWithConnectionAddedWhileDisconnected() 
throws NiFiClientException, IOException, InterruptedException {
+        final NodeDTO nodeTwoDto = getNodeDtoByApiPort(5672);
+        final String nodeId = nodeTwoDto.getNodeId();
+        disconnectNode(nodeTwoDto);
+        waitForNodeStatus(nodeTwoDto, "DISCONNECTED");
+
+        ProcessorEntity generate = 
getClientUtil().createProcessor("GenerateFlowFile");
+        final ProcessorEntity terminate = 
getClientUtil().createProcessor("TerminateFlowFile");
+        final ConnectionEntity connection = 
getClientUtil().createConnection(generate, terminate, "success");
+        getClientUtil().updateConnectionLoadBalancing(connection, 
LoadBalanceStrategy.ROUND_ROBIN, LoadBalanceCompression.DO_NOT_COMPRESS, null);
+
+        generate = getClientUtil().updateProcessorProperties(generate, 
Map.of("File Size", "1 KB"));
+        final ProcessorConfigDTO generateConfig = 
generate.getComponent().getConfig();
+        generateConfig.setSchedulingPeriod("100 millis");
+        getClientUtil().updateProcessorConfig(generate, generateConfig);
+        getClientUtil().startProcessor(generate);
+
+        getClientUtil().connectNode(nodeId);
+        waitForAllNodesConnected();
+        waitForQueueNotEmpty(connection.getId());
+
+        // Stop generating so that the queue holds a bounded number of 
FlowFiles that the peer can accept without back pressure
+        getClientUtil().stopProcessor(generate);
+
+        // Offload the node as soon as it has rejoined, while FlowFiles are 
queued in the load-balanced connection
+        disconnectNode(nodeTwoDto);
+        getClientUtil().offloadNode(nodeId);
+        waitForNodeStatus(nodeTwoDto, "OFFLOADED");
+
+        getClientUtil().connectNode(nodeId);
+        waitForAllNodesConnected();
+        destroyFlow();
+    }
+
     private void disconnectNode(final NodeDTO nodeDto) throws 
NiFiClientException, IOException, InterruptedException {
         getClientUtil().disconnectNode(nodeDto.getNodeId());
 

Reply via email to