Repository: nifi
Updated Branches:
  refs/heads/0.x 35a5667f3 -> 8dbb5d8aa


NIFI-2160 fixed service startup ordering

fixed imports


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/8dbb5d8a
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/8dbb5d8a
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/8dbb5d8a

Branch: refs/heads/0.x
Commit: 8dbb5d8aab1509bd27bc3ec31f28f9cfb19e6197
Parents: 35a5667
Author: Oleg Zhurakousky <o...@suitcase.io>
Authored: Tue Jul 5 17:47:51 2016 -0400
Committer: Oleg Zhurakousky <o...@suitcase.io>
Committed: Thu Jul 7 22:39:35 2016 -0400

----------------------------------------------------------------------
 .../service/StandardControllerServiceNode.java  |  4 +-
 .../StandardControllerServiceProvider.java      | 27 ++++----
 .../TestStandardControllerServiceProvider.java  | 66 +++++++++++++++-----
 3 files changed, 67 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/8dbb5d8a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceNode.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceNode.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceNode.java
index 2deb4ed..7a416af 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceNode.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceNode.java
@@ -115,7 +115,7 @@ public class StandardControllerServiceNode extends 
AbstractConfiguredComponent i
 
     @Override
     public List<ControllerServiceNode> getRequiredControllerServices() {
-        List<ControllerServiceNode> requiredServices = new ArrayList<>();
+        Set<ControllerServiceNode> requiredServices = new HashSet<>();
         for (Entry<PropertyDescriptor, String> pEntry : 
this.getProperties().entrySet()) {
             PropertyDescriptor descriptor = pEntry.getKey();
             if (descriptor.getControllerServiceDefinition() != null && 
descriptor.isRequired()) {
@@ -124,7 +124,7 @@ public class StandardControllerServiceNode extends 
AbstractConfiguredComponent i
                 requiredServices.addAll(rNode.getRequiredControllerServices());
             }
         }
-        return requiredServices;
+        return new ArrayList<>(requiredServices);
     }
 
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/8dbb5d8a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceProvider.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceProvider.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceProvider.java
index 4b18751..58add5d 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceProvider.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/service/StandardControllerServiceProvider.java
@@ -25,7 +25,6 @@ import java.lang.reflect.Proxy;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -313,17 +312,11 @@ public class StandardControllerServiceProvider implements 
ControllerServiceProvi
         }
 
         if (shouldStart) {
-            List<ControllerServiceNode> services = new 
ArrayList<>(serviceNodes);
-            Collections.sort(services, new Comparator<ControllerServiceNode>() 
{
-                @Override
-                public int compare(ControllerServiceNode s1, 
ControllerServiceNode s2) {
-                    return s2.getRequiredControllerServices().contains(s1) ? 
-1 : 1;
-                }
-            });
-
-            for (ControllerServiceNode controllerServiceNode : services) {
+            for (ControllerServiceNode controllerServiceNode : serviceNodes) {
                 try {
-                    this.enableControllerService(controllerServiceNode);
+                    if (!controllerServiceNode.isActive()) {
+                        
this.enableControllerServiceDependenciesFirst(controllerServiceNode);
+                    }
                 } catch (Exception e) {
                     logger.error("Failed to enable " + controllerServiceNode + 
" due to " + e);
                     if (this.bulletinRepo != null) {
@@ -335,6 +328,18 @@ public class StandardControllerServiceProvider implements 
ControllerServiceProvi
         }
     }
 
+    private void 
enableControllerServiceDependenciesFirst(ControllerServiceNode serviceNode) {
+        for (ControllerServiceNode depNode : 
serviceNode.getRequiredControllerServices()) {
+            if (!depNode.isActive()) {
+                this.enableControllerServiceDependenciesFirst(depNode);
+            }
+        }
+        if (logger.isDebugEnabled()) {
+            logger.debug("Enabling " + serviceNode);
+        }
+        this.enableControllerService(serviceNode);
+    }
+
     static List<List<ControllerServiceNode>> determineEnablingOrder(final 
Map<String, ControllerServiceNode> serviceNodeMap) {
         final List<List<ControllerServiceNode>> orderedNodeLists = new 
ArrayList<>();
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/8dbb5d8a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/service/TestStandardControllerServiceProvider.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/service/TestStandardControllerServiceProvider.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/service/TestStandardControllerServiceProvider.java
index 8041aef..857252a 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/service/TestStandardControllerServiceProvider.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/service/TestStandardControllerServiceProvider.java
@@ -371,26 +371,58 @@ public class TestStandardControllerServiceProvider {
         StandardProcessScheduler scheduler = createScheduler();
         StandardControllerServiceProvider provider = new 
StandardControllerServiceProvider(scheduler, null, stateManagerProvider, null);
 
-        ControllerServiceNode serviceNode1 = 
provider.createControllerService(ServiceA.class.getName(), "1", false);
-        ControllerServiceNode serviceNode2 = 
provider.createControllerService(ServiceA.class.getName(), "2", false);
-        ControllerServiceNode serviceNode3 = 
provider.createControllerService(ServiceA.class.getName(), "3", false);
-        ControllerServiceNode serviceNode4 = 
provider.createControllerService(ServiceB.class.getName(), "4", false);
-        ControllerServiceNode serviceNode5 = 
provider.createControllerService(ServiceA.class.getName(), "5", false);
+        ControllerServiceNode A = 
provider.createControllerService(ServiceA.class.getName(), "A", false);
+        ControllerServiceNode B = 
provider.createControllerService(ServiceA.class.getName(), "B", false);
+        ControllerServiceNode C = 
provider.createControllerService(ServiceA.class.getName(), "C", false);
+        ControllerServiceNode D = 
provider.createControllerService(ServiceB.class.getName(), "D", false);
+        ControllerServiceNode E = 
provider.createControllerService(ServiceA.class.getName(), "E", false);
+
+        A.setProperty(ServiceA.OTHER_SERVICE.getName(), "B");
+        B.setProperty(ServiceA.OTHER_SERVICE.getName(), "D");
+        C.setProperty(ServiceA.OTHER_SERVICE.getName(), "B");
+        C.setProperty(ServiceA.OTHER_SERVICE_2.getName(), "D");
+        E.setProperty(ServiceA.OTHER_SERVICE.getName(), "A");
+
+        provider.enableControllerServices(Arrays.asList(new 
ControllerServiceNode[] { A, B, C, D, E }));
+
+        assertTrue(A.isActive());
+        assertTrue(B.isActive());
+        assertTrue(C.isActive());
+        assertTrue(D.isActive());
+        assertTrue(E.isActive());
+    }
 
-        serviceNode1.setProperty(ServiceA.OTHER_SERVICE.getName(), "2");
-        serviceNode2.setProperty(ServiceA.OTHER_SERVICE.getName(), "4");
-        serviceNode3.setProperty(ServiceA.OTHER_SERVICE.getName(), "2");
-        serviceNode3.setProperty(ServiceA.OTHER_SERVICE_2.getName(), "4");
-        serviceNode5.setProperty(ServiceA.OTHER_SERVICE.getName(), "1");
+    /**
+     * This test is similar to the above, but different combination of service
+     * dependencies
+     *
+     */
+    @Test
+    public void validateEnableServices2() {
+        StandardProcessScheduler scheduler = createScheduler();
+        StandardControllerServiceProvider provider = new 
StandardControllerServiceProvider(scheduler, null,
+                stateManagerProvider, null);
 
-        provider.enableControllerServices(
-                Arrays.asList(new ControllerServiceNode[] { serviceNode1, 
serviceNode2, serviceNode3, serviceNode4, serviceNode5}));
+        ControllerServiceNode A = 
provider.createControllerService(ServiceC.class.getName(), "A", false);
+        ControllerServiceNode B = 
provider.createControllerService(ServiceA.class.getName(), "B", false);
+        ControllerServiceNode C = 
provider.createControllerService(ServiceB.class.getName(), "C", false);
+        ControllerServiceNode D = 
provider.createControllerService(ServiceA.class.getName(), "D", false);
+        ControllerServiceNode F = 
provider.createControllerService(ServiceA.class.getName(), "F", false);
 
-        assertTrue(serviceNode1.isActive());
-        assertTrue(serviceNode2.isActive());
-        assertTrue(serviceNode3.isActive());
-        assertTrue(serviceNode4.isActive());
-        assertTrue(serviceNode5.isActive());
+        A.setProperty(ServiceC.REQ_SERVICE_1.getName(), "B");
+        A.setProperty(ServiceC.REQ_SERVICE_2.getName(), "D");
+        B.setProperty(ServiceA.OTHER_SERVICE.getName(), "C");
+
+        F.setProperty(ServiceA.OTHER_SERVICE.getName(), "D");
+        D.setProperty(ServiceA.OTHER_SERVICE.getName(), "C");
+
+        provider.enableControllerServices(Arrays.asList(new 
ControllerServiceNode[] { C, F, A, B, D }));
+
+        assertTrue(A.isActive());
+        assertTrue(B.isActive());
+        assertTrue(C.isActive());
+        assertTrue(D.isActive());
+        assertTrue(F.isActive());
     }
 
     @Test

Reply via email to