Skip to content

Commit 442d9ab

Browse files
committed
NIFI-16477: Implement Flow Evolution Drain Hooks and Flow persistence
* Implement hooks to allow the Connector to drain the flow when its loaded version changes * When the Connector version differs from the version that built the authoritative flow load the persisted flow from the managedProcessGroup.
1 parent 8fd6a98 commit 442d9ab

94 files changed

Lines changed: 6912 additions & 228 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ public class NiFiProperties extends ApplicationProperties {
148148
public static final String CONNECTOR_REPOSITORY_IMPLEMENTATION = "nifi.connector.repository.implementation";
149149
public static final String CONNECTOR_SYNC_TIMEOUT = "nifi.connector.sync.timeout";
150150
public static final String DEFAULT_CONNECTOR_SYNC_TIMEOUT = "5 mins";
151+
public static final String CONNECTOR_FLOW_EVOLUTION_TIMEOUT = "nifi.connector.flow.evolution.timeout";
152+
public static final String DEFAULT_CONNECTOR_FLOW_EVOLUTION_TIMEOUT = "30 mins";
151153

152154
// Connector Configuration Provider properties
153155
public static final String CONNECTOR_CONFIGURATION_PROVIDER_IMPLEMENTATION = "nifi.connector.configuration.provider.implementation";

‎nifi-docs/src/main/asciidoc/administration-guide.adoc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3920,6 +3920,16 @@ The NiFi Registry NAR provider retrieves NARs from a NiFi Registry instance. In
39203920
|`nifi.nar.library.provider.nifi-registry.url`| The URL of the NiFi Registry instance, such as `http://localhost:18080`. If the URL begins with `https`, then the NiFi keystore and truststore will be used to make the TLS connection.
39213921
|====
39223922

3923+
[[connector_properties]]
3924+
=== Connector Properties
3925+
3926+
These properties configure how NiFi manages Connectors. For what flow evolution is and how it runs, see <<developer-guide.adoc#connector_flow_evolution,Connector Flow Evolution>> in the Developer's Guide.
3927+
3928+
|====
3929+
|*Property*|*Description*
3930+
|`nifi.connector.flow.evolution.timeout`|How long a Connector may spend draining its outgoing flow in `prepareForFlowEvolution`. Waiting for other cluster nodes to finish their drain is not limited by this timeout. Accepts any NiFi time duration value such as `30 mins`. The default value is `30 mins`. A timeout leaves the Connector stopped and still pending.
3931+
|====
3932+
39233933
[[secrets_manager_properties]]
39243934
=== Secrets Manager Properties
39253935

‎nifi-docs/src/main/asciidoc/developer-guide.adoc‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,6 +2700,60 @@ The following command is used to generate a standard binary distribution of Apac
27002700

27012701
`mvn clean install -Pcontrib-check`
27022702

2703+
[[connector_flow_evolution]]
2704+
== Connector Flow Evolution
2705+
2706+
A Connector's managed flow is the set of processors, controller services, and connections that the Connector builds from its bundle. Whenever NiFi saves the flow, it records the bundle version that built the managed flow. That recorded version is kept separately from the bundle version the Connector is currently loaded from.
2707+
2708+
If the loaded bundle version differs from the recorded one, the managed flow must be evolved to match the new version before the Connector runs. This is called flow evolution. It is pending while the two versions differ, and it runs on the next START of the Connector.
2709+
2710+
=== When the flow is saved
2711+
2712+
There is no explicit save action. NiFi writes the flow to its flow file, `flow.json.gz` by default, as each change completes. Changes made close together are written as one update, after the delay set by `nifi.flowservice.writedelay.interval`, which defaults to `500 ms`.
2713+
2714+
Any user action that modifies the flow saves it. Changing a Connector's version records the new Connector bundle, but the managed flow keeps the bundle it was built from until flow evolution saves.
2715+
2716+
NiFi also saves the flow without a user action in these cases:
2717+
2718+
* During flow evolution, once, after the upgraded flow is applied. The Connector is recorded as running, because only a START runs an evolution. A failed evolution is not saved.
2719+
* When a node reconnects to a cluster and writes the flow it inherited from the cluster.
2720+
2721+
=== How evolution runs
2722+
2723+
On the next START, the framework calls `prepareForFlowEvolution(FlowEvolutionContext)` on the Connector. The context describes the outgoing flow and the upgraded bundle. The Connector returns a `CompletableFuture` that completes when the outgoing flow is ready. The framework waits for that future, stops the processors in the outgoing flow, and then applies the upgraded flow as described in <<connector_applying_upgraded_flow>>.
2724+
2725+
When NiFi restarts with an evolution pending, it restores the outgoing flow with its components stopped, whatever state they were saved in. The Connector's recorded state decides whether the evolution starts, and the hook starts what it needs to drain. A Connector recorded in Troubleshooting mode keeps the saved component states.
2726+
2727+
The framework does not call `prepareForFlowEvolution` when a Connector leaves the Troubleshooting state. In that case the managed flow must already be empty.
2728+
2729+
=== Draining in prepareForFlowEvolution
2730+
2731+
The default implementation of `prepareForFlowEvolution` does nothing and returns a completed future. The framework does not drain FlowFiles for the Connector, so a Connector that removes connections must empty them in its own hook.
2732+
2733+
* If any connection that the upgraded flow removes still holds FlowFiles, the Connector must drain. The hook must finish with every such connection empty, or the upgrade fails.
2734+
* The Connector does not need to drain when every connection that the upgraded flow removes is already empty. A connection that the upgraded flow keeps, with the same identifier, is updated in place and keeps its queue, so it never requires a drain.
2735+
* The framework stops the outgoing processors only after the hook's future completes. The hook can start them so that queues empty, and the processors are stopped afterward.
2736+
* The framework can cancel the future, for example when the Connector is stopped or the evolution timeout is reached. Stop any work promptly when the future is cancelled. The framework does not wait for code that ignores cancellation.
2737+
2738+
[[connector_applying_upgraded_flow]]
2739+
=== Applying the upgraded flow
2740+
2741+
Once every node that still has the outgoing flow has drained it, the framework checks again that no connection the upgraded flow removes holds FlowFiles. If one does, the evolution fails and the outgoing flow is kept. Otherwise the framework runs the same `prepareForUpdate` and `applyUpdate` process as a configuration update, saves the resulting flow, and starts the Connector.
2742+
2743+
Each node prepares on its own. Every node has already finished the hook, so the framework does not wait for the other nodes to reach `UPDATING`.
2744+
2745+
`prepareForUpdate` and `applyUpdate` must be safe to run again. If either fails, the Connector is left `UPDATE_FAILED`, and the next START calls both again without running the hook. `applyUpdate` therefore has to work on a flow that it has already partly updated.
2746+
2747+
=== What operators see
2748+
2749+
In a cluster, each node drains its own Connector. No node applies the upgraded flow until every node that still has the outgoing flow has drained it, so the Connector reports `DRAINING` while the nodes wait for one another. A node that has already applied the upgraded flow has nothing to drain and does not hold back the others. A node that disconnects after applying the upgraded flow does not block the others either: the connected nodes start. `nifi.connector.flow.evolution.timeout` limits only the local drain in `prepareForFlowEvolution`. See <<administration-guide.adoc#connector_properties,Connector Properties>> in the Administration Guide.
2750+
2751+
If evolution fails before the upgraded flow is applied, including while a node waits for the others to drain, the Connector remains stopped and still pending, and the next START runs the hook again. Stopping the Connector while it drains cancels the attempt. The failure is not saved, so after a restart the Connector comes back in the state that was last saved.
2752+
2753+
A failure while applying the upgraded flow leaves the Connector `UPDATE_FAILED`. The failure is not saved, so the Connector must be corrected and started again. On a node where `prepareForUpdate` or `applyUpdate` failed, the Connector is still pending and the next START retries both without the hook. On a node where the upgraded flow was applied, the Connector is no longer pending and the next START runs the new flow.
2754+
2755+
If the upgraded Connector fails to start, the upgraded flow has already been saved, and the Connector is handled like any other Connector that fails to start.
2756+
27032757
== Supporting migration from a Versioned Process Group
27042758

27052759
Connectors can implement a one-time migration flow that uses a source Versioned Process Group as a reference for populating a freshly created Connector.

‎nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/ConnectorDTO.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ConnectorDTO extends ComponentDTO {
3131
private String type;
3232
private BundleDTO bundle;
3333
private String state; // RUNNING, STOPPED
34+
private String flowEvolutionStatus;
3435
private String managedProcessGroupId;
3536
private ConnectorConfigurationDTO activeConfiguration;
3637
private ConnectorConfigurationDTO workingConfiguration;
@@ -40,6 +41,7 @@ public class ConnectorDTO extends ComponentDTO {
4041
private Boolean multipleVersionsAvailable;
4142
private Boolean extensionMissing;
4243
private Boolean supportsBacklogReporting;
44+
private BundleDTO expectedBundle;
4345

4446
private String configurationUrl;
4547
private String detailsUrl;
@@ -81,6 +83,17 @@ public void setState(final String state) {
8183
this.state = state;
8284
}
8385

86+
@Schema(description = "The local or merged progress of a pending Connector flow evolution.",
87+
allowableValues = {"INACTIVE", "EVOLVE_DRAIN_PENDING", "EVOLVE_DRAIN_COMPLETE", "FAILED"},
88+
accessMode = Schema.AccessMode.READ_ONLY)
89+
public String getFlowEvolutionStatus() {
90+
return flowEvolutionStatus;
91+
}
92+
93+
public void setFlowEvolutionStatus(final String flowEvolutionStatus) {
94+
this.flowEvolutionStatus = flowEvolutionStatus;
95+
}
96+
8497
@Schema(description = "The identifier of the root Process Group managed by this Connector.")
8598
public String getManagedProcessGroupId() {
8699
return managedProcessGroupId;
@@ -174,6 +187,16 @@ public void setSupportsBacklogReporting(final Boolean supportsBacklogReporting)
174187
this.supportsBacklogReporting = supportsBacklogReporting;
175188
}
176189

190+
@Schema(description = "The Connector bundle expected by the cluster coordinator when verifying a START. "
191+
+ "Not persisted; used only for cluster run-status verification.")
192+
public BundleDTO getExpectedBundle() {
193+
return expectedBundle;
194+
}
195+
196+
public void setExpectedBundle(final BundleDTO expectedBundle) {
197+
this.expectedBundle = expectedBundle;
198+
}
199+
177200
@Schema(description = "The available actions that can be performed on this Connector.")
178201
public List<ConnectorActionDTO> getAvailableActions() {
179202
return availableActions;

‎nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/entity/ConnectorRunStatusEntity.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import io.swagger.v3.oas.annotations.media.Schema;
2020
import jakarta.xml.bind.annotation.XmlType;
21+
import org.apache.nifi.web.api.dto.BundleDTO;
2122

2223
@XmlType(name = "connectorRunStatus")
2324
public class ConnectorRunStatusEntity extends ComponentRunStatusEntity {
2425
private static final String[] SUPPORTED_STATE = {"RUNNING", "STOPPED", "DISABLED"};
2526

27+
private BundleDTO expectedBundle;
28+
2629
@Override
2730
protected String[] getSupportedState() {
2831
return SUPPORTED_STATE;
@@ -33,6 +36,16 @@ protected String[] getSupportedState() {
3336
public String getState() {
3437
return super.getState();
3538
}
39+
40+
@Schema(description = "The Connector bundle the cluster coordinator has loaded. When set on a START, each node "
41+
+ "rejects the request in the validation phase if its loaded bundle differs.")
42+
public BundleDTO getExpectedBundle() {
43+
return expectedBundle;
44+
}
45+
46+
public void setExpectedBundle(final BundleDTO expectedBundle) {
47+
this.expectedBundle = expectedBundle;
48+
}
3649
}
3750

3851

0 commit comments

Comments
 (0)