bobpaulin commented on code in PR #11872: URL: https://github.com/apache/nifi/pull/11872#discussion_r4553960578
########## nifi-system-tests/nifi-system-test-suite/src/test/java/org/apache/nifi/tests/system/connectors/ClusteredConnectorChangeVersionDrainIT.java: ########## @@ -0,0 +1,369 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.tests.system.connectors; + +import org.apache.nifi.components.connector.ConnectorState; +import org.apache.nifi.components.connector.FlowEvolutionStatus; +import org.apache.nifi.tests.system.FlowDefinitionFiles; +import org.apache.nifi.tests.system.NiFiInstanceFactory; +import org.apache.nifi.toolkit.client.ConnectorClient; +import org.apache.nifi.toolkit.client.NiFiClientException; +import org.apache.nifi.web.api.entity.ConnectionEntity; +import org.apache.nifi.web.api.entity.ConnectorEntity; +import org.apache.nifi.web.api.entity.ProcessGroupFlowEntity; +import org.apache.nifi.web.api.entity.ProcessGroupStatusEntity; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Two-node version-change drain scenarios. + */ +public class ClusteredConnectorChangeVersionDrainIT extends AbstractChangeVersionDrainIT { + + private static final String LOADED_BUNDLE_MISMATCH_REASON = "Cluster nodes load different Connector versions: "; + + @Override + public NiFiInstanceFactory getInstanceFactory() { + return createTwoNodeInstanceFactory(); + } + + @Test + @Timeout(300) + public void testBarrierWaitsForBothNodes() throws Exception { + final String connectorId = queueStoppedUpgrade("DRAIN", true, null); + triggerUpgrade(connectorId); + requestStart(connectorId); + waitForNodeConnectorState(1, connectorId, ConnectorState.DRAINING); + waitForNodeConnectorState(2, connectorId, ConnectorState.DRAINING); + + createGate(1); + waitForNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.PHASE_ONE_COMPLETE); + assertNodeRemainsBeforePhaseTwo(1, connectorId); + assertNodeConnectorState(2, connectorId, ConnectorState.DRAINING); + assertNodeConnectionPresent(2, connectorId); + assertNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.PHASE_ONE_PENDING); + + createGate(2); + waitForNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.PHASE_ONE_COMPLETE); + waitForNodeConnectorState(1, connectorId, ConnectorState.RUNNING); + waitForNodeConnectorState(2, connectorId, ConnectorState.RUNNING); + assertNodeConnectionRemoved(1, connectorId); + assertNodeConnectionRemoved(2, connectorId); + assertNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.INACTIVE); + assertNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.INACTIVE); + } + + @Test + @Timeout(300) + public void testNodeLocalFailureKeepsBothStoppedBeforePhaseTwo() throws Exception { + final String connectorId = createOutgoingConnector("NO_OP", true, null); + deleteGates(); + getClientUtil().startConnector(connectorId); + waitForConnectorMinQueueCount(connectorId, 1); + // Gate only node 1 so its generator stops while TerminateFlowFile, already running with the connector, empties + // that node. Node 2 keeps generating. The status API aggregates every node, so read each node without replication. + createGate(1); + waitForNodeLocalQueueEmpty(1, connectorId); + assertTrue(nodeLocalQueuedCount(2, connectorId) > 0); + getClientUtil().stopConnector(connectorId); + assertEquals(0, nodeLocalQueuedCount(1, connectorId)); + assertTrue(nodeLocalQueuedCount(2, connectorId) > 0); + + triggerUpgrade(connectorId); + + requestStart(connectorId); + waitForNodeConnectorState(2, connectorId, ConnectorState.STOPPED); + waitForNodeConnectorState(1, connectorId, ConnectorState.STOPPED); + assertNodeStoppedPendingWithOldFlow(1, connectorId); + assertNodeStoppedPendingWithOldFlow(2, connectorId); + assertNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.FAILED); + assertNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.FAILED); + } + + @Test + @Timeout(300) + public void testDisconnectedDrainingNodeBlocksPhaseTwoAndKeepsPendingFlowOnReconnect() throws Exception { + final String connectorId = queueStoppedUpgrade("DRAIN", true, null); + triggerUpgrade(connectorId); + requestStart(connectorId); + waitForNodeConnectorState(1, connectorId, ConnectorState.DRAINING); + waitForNodeConnectorState(2, connectorId, ConnectorState.DRAINING); + + disconnectNode(2); + createGates(); + waitForNodeConnectorState(1, connectorId, ConnectorState.STOPPED); + assertNodeStoppedPendingWithOldFlow(1, connectorId); + assertNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.FAILED); + + reconnectNode(2); + waitForAllNodesConnected(); + waitForNodeConnectorState(1, connectorId, ConnectorState.STOPPED); + waitForNodeConnectorState(2, connectorId, ConnectorState.STOPPED); + assertNodeStoppedPendingWithOldFlow(1, connectorId); + assertNodeStoppedPendingWithOldFlow(2, connectorId); + assertNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.FAILED); + assertNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.FAILED); + } + + /** + * A node that drops out partway through a version change keeps the outgoing version. The change is applied to node 1 + * alone to reproduce that state deterministically. + */ + @Test + @Timeout(300) + public void testNodeDisconnectDuringChangeVersionPreventsStart() throws Exception { + final String connectorId = queueStoppedUpgrade("DRAIN", true, null); + + changeNodeVersion(1, connectorId, DRAIN_V2); + assertEquals(DRAIN_V2, nodeBundleVersion(1, connectorId)); + assertEquals(DRAIN_V1, nodeBundleVersion(2, connectorId)); + + final String startReason = actionReason(connectorId, "START"); + assertNotNull(startReason); + assertTrue(startReason.startsWith(LOADED_BUNDLE_MISMATCH_REASON), startReason); + createGates(); + final NiFiClientException failure = assertThrows(NiFiClientException.class, () -> requestStart(connectorId)); + assertTrue(failure.getMessage().contains(LOADED_BUNDLE_MISMATCH_REASON), failure.getMessage()); + assertNodeStoppedPendingWithOldFlow(1, connectorId); + assertNodeConnectorState(2, connectorId, ConnectorState.STOPPED); + assertNodeConnectionPresent(2, connectorId); + assertNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.INACTIVE); + assertNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.INACTIVE); + + changeNodeVersion(2, connectorId, DRAIN_V2); + assertNodeStoppedPendingWithOldFlow(2, connectorId); + + requestStart(connectorId); + getClientUtil().waitForConnectorState(connectorId, ConnectorState.RUNNING); + assertNodeRunningWithoutRemovedConnection(1, connectorId); + assertNodeRunningWithoutRemovedConnection(2, connectorId); + } + + @Test + @Timeout(240) + public void testApplyFailureRetriesApplyOnEveryNodeWithoutRerunningHook() throws Exception { + final String connectorId = queueStoppedUpgrade("NO_OP", false, "original"); + changeVersion(connectorId, DRAIN_V2); + getClientUtil().configureConnector(connectorId, "Upgrade", properties("NO_OP", false, "edited")); + writeNodeFile(1, APPLY_FAIL_FILE, ""); + + requestStart(connectorId); + getClientUtil().waitForConnectorState(connectorId, ConnectorState.STOPPED); + assertManagedFlowBundle(FlowDefinitionFiles.readConnector(nodeInstance(1), connectorId), outgoingBundle()); + + deleteNodeFiles(APPLY_FAIL_FILE); + requestStart(connectorId); + getClientUtil().waitForConnectorState(connectorId, ConnectorState.RUNNING); + assertNodeFlowEvolutionStatus(1, connectorId, FlowEvolutionStatus.INACTIVE); + assertNodeFlowEvolutionStatus(2, connectorId, FlowEvolutionStatus.INACTIVE); + assertEquals("edited", findProcessor(connectorId, "GenerateFlowFile").getComponent().getConfig().getComments()); + assertNotPending(connectorId); + } + + private void changeNodeVersion(final int nodeIndex, final String connectorId, final String version) throws NiFiClientException, IOException { + switchClientToNode(nodeIndex); + try { + changeVersion(getNifiClient().getConnectorClient(DO_NOT_REPLICATE), connectorId, version, false); + } finally { + setupClient(); + } + } + + private String nodeBundleVersion(final int nodeIndex, final String connectorId) throws NiFiClientException, IOException { + switchClientToNode(nodeIndex); + try { + return getNifiClient().getConnectorClient(DO_NOT_REPLICATE).getConnector(connectorId).getComponent().getBundle().getVersion(); + } finally { + setupClient(); + } + } + + private void createGate(final int nodeIndex) throws IOException { + Files.writeString(new File(nodeDirectory(nodeIndex), GATE_FILE).toPath(), ""); + } + + private int nodeLocalQueuedCount(final int nodeIndex, final String connectorId) throws NiFiClientException, IOException { + switchClientToNode(nodeIndex); + try { + final ProcessGroupStatusEntity status = getNifiClient().getConnectorClient(DO_NOT_REPLICATE).getStatus(connectorId, true); + return status.getProcessGroupStatus().getAggregateSnapshot().getFlowFilesQueued(); + } finally { + setupClient(); + } + } + + private void waitForNodeLocalQueueEmpty(final int nodeIndex, final String connectorId) throws InterruptedException { + switchClientToNode(nodeIndex); + try { + waitFor(() -> { + try { + final ProcessGroupStatusEntity status = getNifiClient().getConnectorClient(DO_NOT_REPLICATE).getStatus(connectorId, true); + return status.getProcessGroupStatus().getAggregateSnapshot().getFlowFilesQueued() == 0; + } catch (final Exception e) { + return false; + } + }); + } finally { + setupClient(); + } + } + + private void waitForNodeConnectorState(final int nodeIndex, final String connectorId, final ConnectorState expectedState) throws InterruptedException { Review Comment: [Claude Opus 5.5] Done. Per-node calls now go through a single onNode(nodeIndex, call) helper, plus waitForNode for polling, so the switch and reset happen in one place. The class no longer redefines the bundle-mismatch reason; it lives only in ConnectorEntityMerger, and ConnectorEntityMergerTest checks it there. -- 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]
