This is an automated email from the ASF dual-hosted git repository.
pvillard31 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new f7e35ee2e35 NIFI-16474 Fetch nested versioned flows once per
occurrence when loading flow contents (#11913)
f7e35ee2e35 is described below
commit f7e35ee2e356fc79f6d34f95c7ac619ba7b3b582
Author: Raul Roberto da Silva <[email protected]>
AuthorDate: Tue Nov 17 17:21:48 2026 -0300
NIFI-16474 Fetch nested versioned flows once per occurrence when loading
flow contents (#11913)
* NIFI-16474 Fetch each nested versioned flow once when loading flow
contents
Loading a flow with its nested versioned flows called the recursive
population twice for every top-level child, and each nested flow was
fetched with its own descendants and then fetched again by the loop over
its children. A flow nested three levels deep was fetched 18 times, a
factor of three per level, each fetch costing Git provider API requests.
Each nested flow is now fetched without its descendants, which the loop
over its children fetches exactly once.
---
.../flow/StandardFlowRegistryClientNode.java | 9 +-
.../flow/StandardFlowRegistryClientNodeTest.java | 167 +++++++++++++++++++++
.../core/RegistryFlowSnapshotProvider.java | 3 +
.../core/RegistryFlowSnapshotProviderTest.java | 82 ++++++++++
4 files changed, 256 insertions(+), 5 deletions(-)
diff --git
a/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNode.java
b/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNode.java
index 03772cf5e5f..eba358f8712 100644
---
a/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNode.java
+++
b/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNode.java
@@ -264,7 +264,6 @@ public final class StandardFlowRegistryClientNode extends
AbstractComponentNode
for (final Map.Entry<String, VersionedParameterContext>
childParameterContext : childParameterContexts.entrySet()) {
flowSnapshot.getParameterContexts().putIfAbsent(childParameterContext.getKey(),
childParameterContext.getValue());
}
- populateVersionedContentsRecursively(context, child,
snapshotContainer);
}
}
@@ -419,7 +418,8 @@ public final class StandardFlowRegistryClientNode extends
AbstractComponentNode
final VersionedFlowCoordinates coordinates =
group.getVersionedFlowCoordinates();
if (coordinates != null) {
- final RegisteredFlowSnapshot snapshot = fetchFlowContents(context,
coordinates, true);
+ // Fetch only this flow: the loop over its children below fetches
its own nested versioned flows
+ final RegisteredFlowSnapshot snapshot = fetchFlowContents(context,
coordinates);
final VersionedProcessGroup contents = snapshot.getFlowContents();
group.setVersionedFlowCoordinates(coordinates);
@@ -461,8 +461,7 @@ public final class StandardFlowRegistryClientNode extends
AbstractComponentNode
return accumulatedParameterContexts;
}
- private RegisteredFlowSnapshot fetchFlowContents(final
FlowRegistryClientUserContext context, final VersionedFlowCoordinates
coordinates,
- final boolean
fetchRemoteFlows) throws FlowRegistryException {
+ private RegisteredFlowSnapshot fetchFlowContents(final
FlowRegistryClientUserContext context, final VersionedFlowCoordinates
coordinates) throws FlowRegistryException {
final String storageLocation = coordinates.getStorageLocation();
final String branch = coordinates.getBranch();
@@ -475,7 +474,7 @@ public final class StandardFlowRegistryClientNode extends
AbstractComponentNode
for (final FlowRegistryClientNode clientNode : clientNodes) {
try {
logger.debug("Attempting to fetch flow from Branch [{}] for
Bucket [{}] Flow [{}] Version [{}] using {}", branch, bucketId, flowId,
version, clientNode);
- final FlowSnapshotContainer snapshotContainer =
clientNode.getFlowContents(context, flowVersionLocation, fetchRemoteFlows);
+ final FlowSnapshotContainer snapshotContainer =
clientNode.getFlowContents(context, flowVersionLocation, false);
final RegisteredFlowSnapshot snapshot =
snapshotContainer.getFlowSnapshot();
coordinates.setRegistryId(clientNode.getIdentifier());
diff --git
a/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNodeTest.java
b/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNodeTest.java
new file mode 100644
index 00000000000..41a7d2fbd2e
--- /dev/null
+++
b/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/registry/flow/StandardFlowRegistryClientNodeTest.java
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.registry.flow;
+
+import org.apache.nifi.authorization.resource.Authorizable;
+import org.apache.nifi.bundle.BundleCoordinate;
+import org.apache.nifi.components.validation.ValidationStatus;
+import org.apache.nifi.components.validation.ValidationTrigger;
+import org.apache.nifi.controller.LoggableComponent;
+import org.apache.nifi.controller.ReloadComponent;
+import org.apache.nifi.controller.TerminationAwareLogger;
+import org.apache.nifi.controller.ValidationContextFactory;
+import org.apache.nifi.controller.flow.FlowManager;
+import org.apache.nifi.controller.service.ControllerServiceProvider;
+import org.apache.nifi.flow.VersionedFlowCoordinates;
+import org.apache.nifi.flow.VersionedParameterContext;
+import org.apache.nifi.flow.VersionedProcessGroup;
+import org.apache.nifi.flow.VersionedProcessor;
+import org.apache.nifi.nar.ExtensionManager;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+class StandardFlowRegistryClientNodeTest {
+ private static final String BUCKET_ID = "bucket";
+ private static final String BRANCH = "main";
+ private static final String VERSION = "1";
+ private static final String STORAGE_LOCATION = "storage";
+
+ // top contains a, a contains b, b contains c: every flow below top is
nested in another versioned flow
+ // shared contains a twice, so each occurrence of a, b and c is fetched
for its own copy of the contents
+ private static final Map<String, List<String>> NESTED_FLOW_IDS = Map.of(
+ "top", List.of("a"),
+ "shared", List.of("a", "a"),
+ "a", List.of("b"),
+ "b", List.of("c"));
+
+ private final Map<String, Integer> fetchCounts = new HashMap<>();
+
+ private StandardFlowRegistryClientNode clientNode;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ final FlowRegistryClient flowRegistryClient =
mock(FlowRegistryClient.class);
+ when(flowRegistryClient.isStorageLocationApplicable(any(),
anyString())).thenReturn(true);
+ when(flowRegistryClient.getFlowContents(any(),
any())).thenAnswer(invocation -> {
+ final FlowVersionLocation location = invocation.getArgument(1);
+ fetchCounts.merge(location.getFlowId(), 1, Integer::sum);
+ return createSnapshot(location.getFlowId());
+ });
+
+ final FlowManager flowManager = mock(FlowManager.class);
+ final LoggableComponent<FlowRegistryClient> component = new
LoggableComponent<>(flowRegistryClient,
+ mock(BundleCoordinate.class),
mock(TerminationAwareLogger.class));
+ final StandardFlowRegistryClientNode node = new
StandardFlowRegistryClientNode(mock(Authorizable.class), flowManager,
component, "client-id",
+ mock(ValidationContextFactory.class),
mock(ControllerServiceProvider.class), FlowRegistryClient.class.getName(),
FlowRegistryClient.class.getName(),
+ mock(ReloadComponent.class), mock(ExtensionManager.class),
mock(ValidationTrigger.class), false);
+ // Registry calls require a valid client node, and validating it for
real needs a configured extension
+ clientNode = spy(node);
+
doReturn(ValidationStatus.VALID).when(clientNode).getValidationStatus();
+
when(flowManager.getAllFlowRegistryClients()).thenReturn(Set.of(clientNode));
+ }
+
+ @Test
+ void testGetFlowContentsFetchesEachNestedFlowOnce() throws Exception {
+ final FlowSnapshotContainer container =
clientNode.getFlowContents(FlowRegistryClientContextFactory.getAnonymousContext(),
+ new FlowVersionLocation(BRANCH, BUCKET_ID, "top", VERSION),
true);
+
+ assertEquals(Map.of("top", 1, "a", 1, "b", 1, "c", 1), fetchCounts);
+
+ final VersionedProcessGroup groupA =
container.getFlowSnapshot().getFlowContents().getProcessGroups().iterator().next();
+ final VersionedProcessGroup groupB =
groupA.getProcessGroups().iterator().next();
+ final VersionedProcessGroup groupC =
groupB.getProcessGroups().iterator().next();
+ assertEquals("processor-c",
groupC.getProcessors().iterator().next().getIdentifier());
+
+ assertNotNull(container.getChildSnapshot(groupA.getIdentifier()));
+ assertNotNull(container.getChildSnapshot(groupC.getIdentifier()));
+
+ final Map<String, VersionedParameterContext> parameterContexts =
container.getFlowSnapshot().getParameterContexts();
+ assertEquals(Set.of("context-top", "context-a", "context-b",
"context-c"), parameterContexts.keySet());
+ }
+
+ @Test
+ void testGetFlowContentsFetchesNestedFlowOncePerOccurrence() throws
Exception {
+ final FlowSnapshotContainer container =
clientNode.getFlowContents(FlowRegistryClientContextFactory.getAnonymousContext(),
+ new FlowVersionLocation(BRANCH, BUCKET_ID, "shared", VERSION),
true);
+
+ assertEquals(Map.of("shared", 1, "a", 2, "b", 2, "c", 2), fetchCounts);
+
+ final Set<VersionedProcessGroup> groupsA =
container.getFlowSnapshot().getFlowContents().getProcessGroups();
+ assertEquals(2, groupsA.size());
+ for (final VersionedProcessGroup groupA : groupsA) {
+ final VersionedProcessGroup groupB =
groupA.getProcessGroups().iterator().next();
+ final VersionedProcessGroup groupC =
groupB.getProcessGroups().iterator().next();
+ assertEquals("processor-c",
groupC.getProcessors().iterator().next().getIdentifier());
+ assertNotNull(container.getChildSnapshot(groupA.getIdentifier()));
+ }
+ }
+
+ private static RegisteredFlowSnapshot createSnapshot(final String flowId) {
+ final VersionedProcessor processor = new VersionedProcessor();
+ processor.setIdentifier("processor-" + flowId);
+
+ final VersionedProcessGroup contents = new VersionedProcessGroup();
+ contents.setIdentifier("contents-" + flowId);
+ contents.setProcessors(Set.of(processor));
+
+ final List<String> nestedFlowIds =
NESTED_FLOW_IDS.getOrDefault(flowId, List.of());
+ final Set<VersionedProcessGroup> nestedGroups = new HashSet<>();
+ for (int i = 0; i < nestedFlowIds.size(); i++) {
+
nestedGroups.add(createNestedGroup("group-%s-%d".formatted(nestedFlowIds.get(i),
i), nestedFlowIds.get(i)));
+ }
+ contents.setProcessGroups(nestedGroups);
+
+ final VersionedParameterContext parameterContext = new
VersionedParameterContext();
+ parameterContext.setName("context-" + flowId);
+ final Map<String, VersionedParameterContext> parameterContexts = new
HashMap<>();
+ parameterContexts.put(parameterContext.getName(), parameterContext);
+
+ final RegisteredFlowSnapshot snapshot = new RegisteredFlowSnapshot();
+ snapshot.setFlowContents(contents);
+ snapshot.setParameterContexts(parameterContexts);
+ return snapshot;
+ }
+
+ private static VersionedProcessGroup createNestedGroup(final String
groupId, final String flowId) {
+ final VersionedFlowCoordinates coordinates = new
VersionedFlowCoordinates();
+ coordinates.setStorageLocation(STORAGE_LOCATION);
+ coordinates.setBranch(BRANCH);
+ coordinates.setBucketId(BUCKET_ID);
+ coordinates.setFlowId(flowId);
+ coordinates.setVersion(VERSION);
+
+ final VersionedProcessGroup nestedGroup = new VersionedProcessGroup();
+ nestedGroup.setIdentifier(groupId);
+ nestedGroup.setVersionedFlowCoordinates(coordinates);
+ return nestedGroup;
+ }
+}
diff --git
a/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/main/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProvider.java
b/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/main/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProvider.java
index 4706bed34d3..b6ad02f09de 100644
---
a/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/main/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProvider.java
+++
b/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/main/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProvider.java
@@ -164,6 +164,9 @@ public class RegistryFlowSnapshotProvider implements
FlowSnapshotProvider {
group.setDefaultBackPressureDataSizeThreshold(contents.getDefaultBackPressureDataSizeThreshold());
group.setLogFileSuffix(contents.getLogFileSuffix());
coordinates.setLatest(snapshot.isLatest());
+
+ // The nested provider has already populated the versioned flows
nested in this one
+ return;
}
for (final VersionedProcessGroup child : group.getProcessGroups()) {
diff --git
a/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/test/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProviderTest.java
b/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/test/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProviderTest.java
index 65e44f8b932..f562a0ca62c 100644
---
a/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/test/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProviderTest.java
+++
b/nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/test/java/org/apache/nifi/stateless/core/RegistryFlowSnapshotProviderTest.java
@@ -18,9 +18,11 @@ package org.apache.nifi.stateless.core;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import mockwebserver3.Dispatcher;
import mockwebserver3.MockResponse;
import mockwebserver3.MockWebServer;
import mockwebserver3.RecordedRequest;
+import org.apache.nifi.flow.VersionedFlowCoordinates;
import org.apache.nifi.flow.VersionedProcessGroup;
import org.apache.nifi.registry.flow.VersionedFlow;
import org.apache.nifi.registry.flow.VersionedFlowSnapshot;
@@ -31,6 +33,9 @@ import org.junit.jupiter.api.Timeout;
import java.io.IOException;
import java.net.HttpURLConnection;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -54,6 +59,18 @@ class RegistryFlowSnapshotProviderTest {
private static final String FLOW_VERSION_PATH =
"/nifi-registry-api/buckets/testing/flows/primary/versions/100";
+ private static final String CHILD_FLOW_ID = "child";
+
+ private static final String GRANDCHILD_FLOW_ID = "grandchild";
+
+ private static final String NESTED_VERSION = "1";
+
+ private static final String LEAF_GROUP_NAME = "leaf";
+
+ private static final String CHILD_FLOW_VERSION_PATH =
"/buckets/testing/flows/child/versions/1";
+
+ private static final String GRANDCHILD_FLOW_VERSION_PATH =
"/buckets/testing/flows/grandchild/versions/1";
+
private static final String CONTENT_TYPE_HEADER = "Content-Type";
private static final String APPLICATION_JSON = "application/json";
@@ -101,6 +118,71 @@ class RegistryFlowSnapshotProviderTest {
assertFlowVersionRequestRecorded();
}
+ @Test
+ void testGetFlowSnapshotFetchesEachNestedFlowOnce() throws Exception {
+ final Map<String, Integer> requestCounts = new ConcurrentHashMap<>();
+ final Map<String, String> responses = Map.of(
+ FLOW_VERSION_PATH,
getSnapshotResponseBody(getVersionedGroup(CHILD_FLOW_ID)),
+ CHILD_FLOW_VERSION_PATH,
getSnapshotResponseBody(getVersionedGroup(GRANDCHILD_FLOW_ID)),
+ GRANDCHILD_FLOW_VERSION_PATH,
getSnapshotResponseBody(getLeafGroup())
+ );
+ mockWebServer.setDispatcher(new Dispatcher() {
+ @Override
+ public MockResponse dispatch(final RecordedRequest request) {
+ final String path = request.getUrl().encodedPath();
+ requestCounts.merge(path, 1, Integer::sum);
+ final String responseBody = responses.get(path);
+ if (responseBody == null) {
+ return new
MockResponse.Builder().code(HttpURLConnection.HTTP_NOT_FOUND).build();
+ }
+ return new MockResponse.Builder()
+ .code(HttpURLConnection.HTTP_OK)
+ .setHeader(CONTENT_TYPE_HEADER, APPLICATION_JSON)
+ .body(responseBody)
+ .build();
+ }
+ });
+
+ final VersionedFlowSnapshot snapshot =
provider.getFlowSnapshot(BUCKET_ID, FLOW_ID, VERSION);
+
+ assertEquals(Map.of(FLOW_VERSION_PATH, 1, CHILD_FLOW_VERSION_PATH, 1,
GRANDCHILD_FLOW_VERSION_PATH, 1), requestCounts);
+
+ final VersionedProcessGroup child =
snapshot.getFlowContents().getProcessGroups().iterator().next();
+ final VersionedProcessGroup grandchild =
child.getProcessGroups().iterator().next();
+ final VersionedProcessGroup leaf =
grandchild.getProcessGroups().iterator().next();
+ assertEquals(LEAF_GROUP_NAME, leaf.getName());
+ }
+
+ private VersionedProcessGroup getVersionedGroup(final String flowId) {
+ final VersionedFlowCoordinates coordinates = new
VersionedFlowCoordinates();
+ coordinates.setStorageLocation(mockWebServer.url(API_PATH).toString());
+ coordinates.setBucketId(BUCKET_ID);
+ coordinates.setFlowId(flowId);
+ coordinates.setVersion(NESTED_VERSION);
+
+ final VersionedProcessGroup group = new VersionedProcessGroup();
+ group.setIdentifier(flowId);
+ group.setName(flowId);
+ group.setVersionedFlowCoordinates(coordinates);
+ return group;
+ }
+
+ private static VersionedProcessGroup getLeafGroup() {
+ final VersionedProcessGroup group = new VersionedProcessGroup();
+ group.setIdentifier(LEAF_GROUP_NAME);
+ group.setName(LEAF_GROUP_NAME);
+ return group;
+ }
+
+ private static String getSnapshotResponseBody(final VersionedProcessGroup
childGroup) throws JsonProcessingException {
+ final VersionedProcessGroup flowContents = new VersionedProcessGroup();
+ flowContents.setProcessGroups(Set.of(childGroup));
+
+ final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
+ snapshot.setFlowContents(flowContents);
+ return objectMapper.writeValueAsString(snapshot);
+ }
+
private void enqueueFlowResponse() throws JsonProcessingException {
final VersionedFlow versionedFlow = new VersionedFlow();
versionedFlow.setVersionCount(VERSION);