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


##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/components/connector/StandardConnectorRepository.java:
##########
@@ -586,10 +616,152 @@ private void syncFromProviderForRead(final ConnectorNode 
connector) {
     }
 
     @Override
-    public Future<Void> startConnector(final ConnectorNode connector) {
+    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(lifecycleExecutor);
+        } catch (final Throwable failure) {
+            logger.error("Failed to prepare flow evolution for {}", connector, 
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 {
+            final Future<Void> startFuture = runFlowEvolution(connector, 
persistenceContext);
+            startAttempted = true;
+            // The evolution is complete once its start is issued. A later 
START or STOP then acts on the connector directly.
+            
evolveThenStartFuturesByConnectorId.remove(connector.getIdentifier(), 
evolutionFuture);
+            startFuture.get();
+            evolutionFuture.complete(null);
+        } catch (final Throwable failure) {
+            if (failure instanceof InterruptedException) {
+                Thread.currentThread().interrupt();
+            }
+
+            if (startAttempted) {
+                logger.error("{} was evolved but did not start", connector, 
failure);
+            } else {
+                logger.error("Flow evolution failed for {}", connector, 
failure);
+            }
+
+            handleFlowEvolutionFailure(connector, evolutionFuture, failure, 
startAttempted);
+
+            if (failure instanceof final Error error) {
+                throw error;
+            }
+        } finally {
+            
evolveThenStartFuturesByConnectorId.remove(connector.getIdentifier(), 
evolutionFuture);
+        }
+    }
+
+    /**
+     * Runs the evolve-drain phase of the evolution, then applies the upgraded 
flow as a configuration update.
+     *
+     * @return the future for starting the evolved connector
+     */
+    private Future<Void> runFlowEvolution(final ConnectorNode connector, final 
ConnectorUpdateContext persistenceContext) throws Throwable {
+        awaitFlowEvolutionDrain(connector, 
connector.evolveFlow(flowEvolutionCoordinatorExecutor));
+
+        // No node applies the upgraded flow until every node that still has 
the outgoing flow has drained it.
+        waitForFlowEvolutionStatus(connector);
+
+        // Every node has drained, so as at startup the preparation is local 
and no cluster-wide wait for UPDATING is needed.
+        // A peer that has not yet observed the drain barrier may still be 
DRAINING or preparing.
+        connector.beginEvolvedFlowUpdate();
+        connector.prepareForUpdate();
+        completeUpdate(connector, persistenceContext, 
Set.of(ConnectorState.PREPARING_FOR_UPDATE, ConnectorState.UPDATING, 
ConnectorState.DRAINING));

Review Comment:
   [Bob] TLDR on this is the cure might be worse than the disease. 
   
   [Claude Opus 5.5] Steps 1 to 4 are accurate: the merged state hides a peer 
that falls back to STOPPED. I don't think it needs a fix, though, because B 
can't run the outgoing flow:
   
   StandardConnectorNode.start refuses to start while the evolution is pending, 
so B can only be started through StandardConnectorRepository.startConnector, 
which runs the evolution first. Apply is also rejected while the evolution is 
pending.
   The failed attempt isn't saved, so B's persisted flow keeps the pre-upgrade 
snapshot and desired RUNNING. A restart resumes the evolution instead of 
starting the old flow.
   B's FlowFiles stay in the stopped outgoing flow and the next attempt drains 
them. On that attempt A reports INACTIVE, which the drain barrier ignores, and 
A being RUNNING doesn't block the UPDATED wait, so the next START finishes the 
upgrade on B alone.
   So the cluster is split between a running upgraded node and a stopped node 
that can only move forward. It isn't silent either: B's FAILED wins the merged 
flow evolution status, B is INVALID with the flow evolution failure, and the 
failure is logged through the connector's ComponentLog, which raises a 
bulletin. The START response couldn't report it in any case, because 
scheduleConnector returns as soon as the evolution is started.
   
   Failing A on a peer's FAILED would leave A in UPDATE_FAILED after it has 
already applied and saved the upgraded flow. The cluster would go from one node 
stopped to none running, and recovery would still be another START. It would 
also turn the UPDATED wait into a second all-or-nothing barrier after the 
drain, and the drain is where the two flows must not overlap.



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