From fff0148a0e057277b34e647ef31c441ed5b4bdc3 Mon Sep 17 00:00:00 2001
From: Mark Payne
Date: Wed, 30 Nov 2016 11:07:32 -0500
Subject: [PATCH] NIFI-3129: When adding controller services to a snippet,
ensure that we don't add the service multiple times, even when it's
referenced by child process groups. This closes #1284
---
.../nifi/util/ComponentIdGenerator.java | 8 +++++++-
.../apache/nifi/web/util/SnippetUtils.java | 19 +++++++++++++------
2 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/ComponentIdGenerator.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/ComponentIdGenerator.java
index 49f00b8d51..51cf0c0b6e 100644
--- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/ComponentIdGenerator.java
+++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/ComponentIdGenerator.java
@@ -19,6 +19,9 @@ package org.apache.nifi.util;
import java.security.SecureRandom;
import java.util.UUID;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* IMPORTANT: This component is not part of public API!
* ====================================================
@@ -48,6 +51,7 @@ import java.util.UUID;
*
*/
public class ComponentIdGenerator {
+ private static final Logger logger = LoggerFactory.getLogger(ComponentIdGenerator.class);
public static final Object lock = new Object();
@@ -97,6 +101,8 @@ public class ComponentIdGenerator {
long clockSequenceHi = clockSequence;
clockSequenceHi <<= 48;
lsb = clockSequenceHi | lsb;
- return new UUID(time, lsb);
+ final UUID uuid = new UUID(time, lsb);
+ logger.debug("Generating UUID {} for msb={}, lsb={}, ensureUnique={}", uuid, msb, lsb, ensureUnique);
+ return uuid;
}
}
\ No newline at end of file
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java
index 4f8afde189..c5baa8c9dc 100644
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/SnippetUtils.java
@@ -110,6 +110,7 @@ public final class SnippetUtils {
}
final Set controllerServices = new HashSet<>();
+ final Set allServicesReferenced = new HashSet<>();
// add any processors
final Set processors = new LinkedHashSet<>();
@@ -122,7 +123,10 @@ public final class SnippetUtils {
processors.add(dtoFactory.createProcessorDto(processor));
if (includeControllerServices) {
- controllerServices.addAll(getControllerServices(processor.getProperties()));
+ // Include all referenced services that are not already included in this snippet.
+ getControllerServices(processor.getProperties()).stream()
+ .filter(svc -> allServicesReferenced.add(svc))
+ .forEach(svc -> controllerServices.add(svc));
}
}
}
@@ -199,7 +203,7 @@ public final class SnippetUtils {
final ProcessGroupDTO childGroupDto = dtoFactory.createProcessGroupDto(childGroup, recurse);
processGroups.add(childGroupDto);
- addControllerServices(childGroup, childGroupDto);
+ addControllerServices(childGroup, childGroupDto, allServicesReferenced);
}
}
@@ -241,7 +245,7 @@ public final class SnippetUtils {
return snippetDto;
}
- private void addControllerServices(final ProcessGroup group, final ProcessGroupDTO dto) {
+ private void addControllerServices(final ProcessGroup group, final ProcessGroupDTO dto, final Set allServicesReferenced) {
final FlowSnippetDTO contents = dto.getContents();
if (contents == null) {
return;
@@ -250,8 +254,10 @@ public final class SnippetUtils {
final Set controllerServices = new HashSet<>();
for (final ProcessorNode procNode : group.getProcessors()) {
- final Set servicesForProcessor = getControllerServices(procNode.getProperties());
- controllerServices.addAll(servicesForProcessor);
+ // Include all referenced services that are not already included in this snippet.
+ getControllerServices(procNode.getProperties()).stream()
+ .filter(svc -> allServicesReferenced.add(svc))
+ .forEach(svc -> controllerServices.add(svc));
}
contents.setControllerServices(controllerServices);
@@ -266,7 +272,7 @@ public final class SnippetUtils {
continue;
}
- addControllerServices(childGroup, childDto);
+ addControllerServices(childGroup, childDto, allServicesReferenced);
}
}
@@ -826,6 +832,7 @@ public final class SnippetUtils {
uuid = new UUID(msb, seedId.getLeastSignificantBits());
}
}
+ logger.debug("Generating UUID {} from currentId={}, seed={}, isCopy={}", uuid, currentId, seed, isCopy);
return uuid.toString();
}