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


##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/components/connector/StandardConnectorNode.java:
##########
@@ -1619,6 +1773,240 @@ public boolean isExtensionMissing() {
         return extensionMissing;
     }
 
+    @Override
+    public boolean isPendingFlowEvolution() {
+        return flowEvolution.isPending();
+    }
+
+    @Override
+    public FlowEvolutionStatus getReportedFlowEvolutionStatus() {
+        return flowEvolution.getReportedStatus();
+    }
+
+    boolean hasFlowEvolutionFailed() {
+        return flowEvolution.hasFailed();
+    }
+
+    @Override
+    public ConnectorFlowSnapshot getAuthoritativeFlowSnapshot() {
+        return flowEvolution.getSnapshot();
+    }
+
+    @Override
+    public boolean restorePendingFlowEvolution(final VersionedConnector 
persisted, final List<VersionedConfigurationStep> activeConfiguration,
+            final List<VersionedConfigurationStep> workingConfiguration) 
throws FlowUpdateException {
+        final Bundle managedFlowBundle = resolveManagedFlowBundle(persisted);
+        final VersionedProcessGroup persistedManagedGroup = 
persisted.getManagedProcessGroup();
+        final Map<String, SecretReference> parameterSecretReferences = 
ParameterSecretReferences.convertManagedParameterSecretReferences(persisted);
+        // Components in the persisted group may name NAR versions that are no 
longer installed. A missing coordinate
+        // is rewritten only when exactly one compatible bundle is installed. 
A ghost has no initialization context,
+        // and its coordinates have to stay as persisted.
+        if (!extensionMissing) {
+            resolvePersistedBundles(persistedManagedGroup);
+        }
+
+        if (extensionMissing && persistedManagedGroup != null) {
+            // Adopt before inheritConfiguration. A flow save with no snapshot 
omits the managed group, and inherit
+            // rebuilds the live group before the persisted one is installed 
below.
+            adoptAuthoritativeSnapshot(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
managedFlowBundle);
+            return false;
+        }
+
+        if (!extensionMissing && !sameManagedBundle(managedFlowBundle) && 
persistedManagedGroup != null) {
+            // Skip inheritConfiguration. Its applyUpdate would replace the 
pre-upgrade group with the new bundle's
+            // flow. The restored group keeps the previous bundle, so the next 
start evolves it. A failed restore still loads
+            // the configuration and adopts the persisted snapshot, so the 
next flow save keeps the pre-upgrade flow.
+            try {
+                restorePersistedFlow(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
ResumeComponentState.RESUME_AS_PERSISTED);
+            } catch (final Exception e) {
+                logger.error("{} failed to restore the persisted pre-upgrade 
managed flow; the Connector cannot start until NiFi is restarted and the flow 
is restored", this, e);
+                markManagedFlowRestoreFailed();
+            }
+
+            loadConfigurationsWithoutRebuildingFlow(activeConfiguration, 
workingConfiguration);
+            adoptAuthoritativeSnapshot(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
managedFlowBundle);
+            return true;
+        }
+
+        return false;
+    }
+
+    @Override
+    public void restorePersistedManagedFlow(final VersionedConnector 
persisted) {
+        if (!extensionMissing) {
+            throw new IllegalStateException("Cannot restore the persisted 
managed flow for " + this + " because its extension is not missing");
+        }
+
+        final VersionedProcessGroup persistedManagedGroup = 
persisted.getManagedProcessGroup();
+        if (persistedManagedGroup == null) {
+            return;
+        }
+
+        final Bundle managedFlowBundle = resolveManagedFlowBundle(persisted);
+        final Map<String, SecretReference> parameterSecretReferences = 
ParameterSecretReferences.convertManagedParameterSecretReferences(persisted);
+
+        // Install the persisted flow while ghosted so queues exist before the 
FlowFile repository recovers.
+        // Components stay stopped: a ghost cannot start, and resuming would 
run them under a missing extension.
+        try {
+            restorePersistedFlow(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
ResumeComponentState.LEAVE_STOPPED);
+        } catch (final Exception e) {
+            logger.error("{} failed to restore the persisted managed flow 
while ghosted; "
+                    + "queued FlowFiles may not rejoin until NiFi restarts 
after the Connector becomes loadable again", this, e);
+            markManagedFlowRestoreFailed();
+        }
+        adoptAuthoritativeSnapshot(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
managedFlowBundle);
+    }
+
+    @Override
+    public void restoreTroubleshootingManagedFlow(final VersionedConnector 
persisted) throws FlowUpdateException {
+        final Bundle managedFlowBundle = resolveManagedFlowBundle(persisted);
+        final VersionedProcessGroup persistedManagedGroup = 
persisted.getManagedProcessGroup();
+        final Map<String, SecretReference> parameterSecretReferences = 
ParameterSecretReferences.convertManagedParameterSecretReferences(persisted);
+
+        if (extensionMissing && persistedManagedGroup != null) {
+            adoptAuthoritativeSnapshot(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
managedFlowBundle);
+            restoreTroubleshootingFlow(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences);
+            return;
+        }
+
+        if (sameManagedBundle(managedFlowBundle) && persistedManagedGroup != 
null) {
+            try {
+                restorePersistedFlow(persistedManagedGroup, null, Map.of(), 
ResumeComponentState.RESUME_AS_PERSISTED);
+                adoptAuthoritativeSnapshot(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, 
managedFlowBundle);
+            } catch (final Exception e) {
+                logger.error("{} failed to restore the managed flow while 
restoring Troubleshooting", this, e);
+                throw new FlowUpdateException("Failed to restore Managed 
Process Group from persisted flow: " + e.getMessage(), e);
+            }
+        }
+    }
+
+    @Override
+    public void ensureAuthoritativeFlowSnapshot() {
+        flowEvolution.ensureSnapshot();
+    }
+
+    private void restoreTroubleshootingFlow(final VersionedProcessGroup 
persistedManagedGroup, final VersionedParameterContext parameterContext,
+            final Map<String, SecretReference> parameterSecretReferences) 
throws FlowUpdateException {
+        logger.info("{} was persisted in Troubleshooting mode; restoring 
Managed Process Group from persisted snapshot", this);
+        try {
+            restorePersistedFlow(persistedManagedGroup, parameterContext, 
parameterSecretReferences, ResumeComponentState.LEAVE_STOPPED);
+        } catch (final Exception e) {
+            logger.error("{} failed to restore the managed flow while 
restoring Troubleshooting", this, e);
+            throw new FlowUpdateException("Failed to restore Managed Process 
Group from persisted flow: " + e.getMessage(), e);
+        }
+    }
+
+    /**
+     * A group saved before managedFlowBundle existed is attributed to the 
connector bundle stored in the flow definition.
+     */
+    private static Bundle resolveManagedFlowBundle(final VersionedConnector 
versionedConnector) {
+        if (versionedConnector.getManagedFlowBundle() != null) {
+            return versionedConnector.getManagedFlowBundle();
+        }
+        if (versionedConnector.getManagedProcessGroup() != null) {
+            return versionedConnector.getBundle();

Review Comment:
   [Claude Opus 5.5] Agreed, and it's worse than confusing: while the upgrade 
is pending, leaving Troubleshooting requires empty queues, and components with 
no parameters can't drain them. I didn't restore the old order, because running 
the new version's applyUpdate before the restore means a failed inherit leaves 
the Troubleshooting group unrestored, and the next save drops it along with its 
queued FlowFiles. Instead, the pre-upgrade group is still restored first, and 
when the persisted parameter context is missing, the loaded version's 
getActiveFlow fills the managed Parameter Context, the same flow 
endTroubleshooting installs. The captured context is adopted with the previous 
bundle, so the upgrade stays pending and the next save writes the parameters. 
If the fill fails, it logs a warning and the restore behaves as before.



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