bobpaulin commented on code in PR #11872:
URL: https://github.com/apache/nifi/pull/11872#discussion_r4556389327


##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/components/connector/StandardConnectorRepository.java:
##########
@@ -586,8 +611,147 @@ private void syncFromProviderForRead(final ConnectorNode 
connector) {
     }
 
     @Override
-    public Future<Void> startConnector(final ConnectorNode connector) {
-        return connector.start(lifecycleExecutor);
+    public Future<Void> startConnector(final ConnectorNode connector, final 
ConnectorUpdateContext persistenceContext) {
+        Objects.requireNonNull(persistenceContext, "Flow persistence context 
is required");
+
+        final String connectorId = connector.getIdentifier();
+        final CompletableFuture<Void> runningEvolution = 
evolveThenStartFuturesByConnectorId.get(connectorId);
+        if (runningEvolution != null) {
+            return runningEvolution;
+        }
+        if (!connector.isPendingFlowEvolution()) {
+            return connector.start(lifecycleExecutor);
+        }
+
+        final CompletableFuture<Void> evolutionFuture = new 
CompletableFuture<>();
+        final CompletableFuture<Void> existingEvolution = 
evolveThenStartFuturesByConnectorId.putIfAbsent(connectorId, evolutionFuture);
+        if (existingEvolution != null) {
+            return existingEvolution;
+        }
+
+        try {
+            connector.prepareFlowEvolution();
+        } catch (final Throwable failure) {
+            evolutionFuture.completeExceptionally(failure);
+            evolveThenStartFuturesByConnectorId.remove(connectorId, 
evolutionFuture);
+            if (failure instanceof final Error error) {
+                throw error;
+            }
+            return evolutionFuture;
+        }
+
+        try {
+            flowEvolutionCoordinatorExecutor.submit(() -> 
evolveThenStart(connector, evolutionFuture, persistenceContext));
+        } catch (final Throwable failure) {
+            try {
+                connector.abortUpdate(failure);
+            } catch (final Throwable cleanupFailure) {
+                failure.addSuppressed(cleanupFailure);
+            }
+            evolutionFuture.completeExceptionally(failure);
+            evolveThenStartFuturesByConnectorId.remove(connectorId, 
evolutionFuture);
+            if (failure instanceof final Error error) {
+                throw error;
+            }
+        }
+
+        return evolutionFuture;
+    }
+
+    private void evolveThenStart(final ConnectorNode connector, final 
CompletableFuture<Void> evolutionFuture, final ConnectorUpdateContext 
persistenceContext) {
+        boolean startAttempted = false;
+        try {
+            runFlowEvolution(connector, persistenceContext);
+            startAttempted = true;
+            startEvolvedConnector(connector, persistenceContext);
+            evolutionFuture.complete(null);
+        } catch (final Throwable failure) {
+            if (failure instanceof InterruptedException) {
+                Thread.currentThread().interrupt();
+            }
+
+            logger.error("Flow evolution failed for {}", connector, failure);
+            handleFlowEvolutionFailure(connector, evolutionFuture, failure, 
startAttempted, persistenceContext);
+
+            if (failure instanceof final Error error) {
+                throw error;
+            }
+        } finally {
+            
evolveThenStartFuturesByConnectorId.remove(connector.getIdentifier(), 
evolutionFuture);
+        }
+    }
+
+    private void runFlowEvolution(final ConnectorNode connector, final 
ConnectorUpdateContext persistenceContext) throws Throwable {
+        // Read before the drain. The drain advances the status, and a stop 
that arrived first already reports FAILED.
+        final boolean hookComplete = connector.getFlowEvolutionStatus() == 
FlowEvolutionStatus.PHASE_ONE_COMPLETE;
+        if (!hookComplete) {
+            waitForFlowEvolutionStatus(connector, 
FlowEvolutionStatus.PHASE_ONE_PENDING);
+        }
+
+        awaitFlowEvolutionDrain(connector, 
connector.evolveFlow(flowEvolutionCoordinatorExecutor));
+        if (!hookComplete) {
+            waitForFlowEvolutionStatus(connector, 
FlowEvolutionStatus.PHASE_ONE_COMPLETE);
+        }
+
+        connector.applyUpdate();
+        persistenceContext.saveFlow();
+        waitForFlowEvolutionStatus(connector, 
FlowEvolutionStatus.PHASE_TWO_COMPLETE);
+    }
+
+    private void awaitFlowEvolutionDrain(final ConnectorNode connector, final 
Future<Void> future) throws Throwable {
+        try {
+            
future.get(TimeUnit.NANOSECONDS.convert(flowEvolutionDrainTimeout), 
TimeUnit.NANOSECONDS);
+        } catch (final TimeoutException e) {
+            final FlowUpdateException timedOut = new FlowUpdateException("Flow 
evolution drain timed out for " + connector, e);
+            connector.abortUpdate(timedOut);

Review Comment:
   [Claude Opus 5.5] Fixed. `handleFlowEvolutionFailure` is now the only place 
that aborts. The test is now `testFlowEvolutionDrainTimeoutFailsEvolution` and 
checks the result instead of call counts: the Connector is STOPPED, its status 
is FAILED, the upgrade is still pending, the upgraded flow was never applied, 
and the validation error mentions the timeout.



-- 
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]

Reply via email to