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


##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/components/connector/StandardConnectorNode.java:
##########
@@ -1619,6 +1775,691 @@ public boolean isExtensionMissing() {
         return extensionMissing;
     }
 
+    @Override
+    public boolean isPendingFlowEvolution() {
+        synchronized (flowEvolutionLock) {
+            if (extensionMissing) {
+                return false;
+            }
+
+            final ConnectorFlowSnapshot snapshot = authoritativeFlowSnapshot;
+            final Bundle snapshotBundle = snapshot == null ? null : 
snapshot.bundle();
+            return snapshotBundle != null && !sameBundle(snapshotBundle, 
bundleCoordinate);
+        }
+    }
+
+    @Override
+    public FlowEvolutionStatus getReportedFlowEvolutionStatus() {
+        return flowEvolutionProgress.get().reportedStatus();
+    }
+
+    private boolean isEvolveDrainComplete() {
+        return flowEvolutionProgress.get().isDrainComplete();
+    }
+
+    private boolean isApplyingEvolvedFlow() {
+        return getReportedFlowEvolutionStatus() == 
FlowEvolutionStatus.EVOLVE_DRAIN_COMPLETE;
+    }
+
+    boolean hasFlowEvolutionFailed() {
+        return flowEvolutionProgress.get().hasFailed();
+    }
+
+    private VersionedProcessGroup getAuthoritativeManagedProcessGroup() {
+        final ConnectorFlowSnapshot snapshot = authoritativeFlowSnapshot;
+        return snapshot == null ? null : snapshot.flow();
+    }
+
+    @Override
+    public ConnectorFlowSnapshot getAuthoritativeFlowSnapshot() {
+        return authoritativeFlowSnapshot;
+    }
+
+    @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.
+            restorePersistedFlow(persistedManagedGroup, 
persisted.getManagedParameterContext(), parameterSecretReferences, true);
+            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, false);
+        } 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(), 
true);
+                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() {
+        if (getAuthoritativeManagedProcessGroup() != null || extensionMissing) 
{
+            return;
+        }
+
+        // Without a snapshot, a later NAR upgrade has no pre-upgrade flow to 
evolve.
+        verifyCanCaptureAuthoritativeSnapshot();
+        final ConnectorFlowSnapshot snapshot = createAuthoritativeSnapshot();
+
+        synchronized (flowEvolutionLock) {
+            // Another thread may have installed a snapshot, such as the 
pre-upgrade flow, while the live flow was being captured
+            if (getAuthoritativeManagedProcessGroup() != null) {
+                return;
+            }
+
+            verifyCanCaptureAuthoritativeSnapshot();
+            installAuthoritativeSnapshot(snapshot);
+        }
+    }
+
+    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, false);
+        } 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();
+        }
+        return null;
+    }
+
+    private boolean sameManagedBundle(final Bundle managedFlowBundle) {
+        if (managedFlowBundle == null || bundleCoordinate == null) {
+            return true;
+        }
+        return sameBundle(managedFlowBundle, bundleCoordinate);
+    }
+
+    /**
+     * Rewrites processor and controller service bundles in a persisted group 
to the only installed alternative when
+     * the exact coordinate is missing. A ghost Connector keeps the snapshot 
exactly as persisted.
+     */
+    void resolvePersistedBundles(final VersionedProcessGroup group) {
+        if (extensionMissing || group == null || initializationContext == 
null) {
+            return;
+        }
+
+        initializationContext.resolveBundles(group, 
BundleCompatibility.RESOLVE_BUNDLE);
+    }
+
+    void adoptAuthoritativeSnapshot(final VersionedProcessGroup flow, final 
VersionedParameterContext parameterContext,
+            final Map<String, SecretReference> parameterSecretReferences, 
final Bundle managedFlowBundle) {
+        installAuthoritativeSnapshot(new ConnectorFlowSnapshot(flow, 
parameterContext, parameterSecretReferences, managedFlowBundle,
+                
activeFlowContext.getConfigurationContext().toConnectorConfiguration()));
+    }
+
+    void restorePersistedFlow(final VersionedProcessGroup flow, final 
VersionedParameterContext parameterContext,
+            final Map<String, SecretReference> parameterSecretReferences, 
final boolean resumeComponents) {
+        final ParameterSecretReferences.Resolution resolution = 
resolvePersistedParameterSecrets(parameterContext, parameterSecretReferences);
+        if (!resolution.undecryptableParameters().isEmpty()) {
+            logger.warn("{} could not decrypt the persisted values of 
sensitive parameters {}; components are left stopped", this, 
resolution.undecryptableParameters());
+        }
+
+        if (resumeComponents && unresolvedParameterSecretReferences.isEmpty() 
&& resolution.undecryptableParameters().isEmpty()) {
+            activeFlowContext.restorePersistedFlow(flow, 
resolution.parameterContext());
+        } else {
+            activeFlowContext.restorePersistedFlow(flow, 
resolution.parameterContext(), false);

Review Comment:
   [Claude Opus 5.5] Agreed, it's now one call with the resume state worked out 
up front: LEAVE_STOPPED when a secret is unresolved or a value can't be 
decrypted, otherwise the requested state. Passing a plain true would have 
bypassed autoResumeState, and the enum avoids that.



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