markap14 commented on code in PR #8158:
URL: https://github.com/apache/nifi/pull/8158#discussion_r1426763926


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/StandardNiFiServiceFacade.java:
##########
@@ -6233,27 +6237,54 @@ protected Collection<AbstractMetricsRegistry> 
populateFlowMetrics() {
 
         // Get Connection Status Analytics (predictions, e.g.)
         Set<Connection> connections = 
controllerFacade.getFlowManager().findAllConnections();
-        for (Connection c : connections) {
-            // If a ResourceNotFoundException is thrown, analytics hasn't been 
enabled
+        Collection<Map<String, Long>> predictions = new 
ConcurrentLinkedDeque<>();
+
+        final boolean analyticsEnabled = 
Boolean.parseBoolean(properties.getProperty(NiFiProperties.ANALYTICS_PREDICTION_ENABLED,
 Boolean.FALSE.toString()));
+
+        if (analyticsEnabled) {
+            final int parallelProcessingThreads = 
properties.getIntegerProperty(NiFiProperties.ANALYTICS_PREDICTION_PARALLEL_PROCESSING_THREADS,
 4);
+            final long parallelProcessingTimeout = 
Math.round(FormatUtils.getPreciseTimeDuration(
+                    
properties.getProperty(NiFiProperties.ANALYTICS_PREDICTION_PARALLEL_PROCESSING_TIMEOUT,
 "1 min"), TimeUnit.MILLISECONDS));
+
+            final ForkJoinPool.ForkJoinWorkerThreadFactory factory = pool -> {
+                final ForkJoinWorkerThread worker = 
ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
+                
worker.setName("analytics-prediction-parallel-processing-thread-" + 
UUID.randomUUID());
+                return worker;
+            };
+
+            final ForkJoinPool parallelProcessingThreadPool = new 
ForkJoinPool(parallelProcessingThreads, factory, null, false);
             try {
-                final StatusAnalytics statusAnalytics = 
controllerFacade.getConnectionStatusAnalytics(c.getIdentifier());
-                
PrometheusMetricsUtil.createConnectionStatusAnalyticsMetrics(connectionAnalyticsMetricsRegistry,
-                        statusAnalytics,
-                        instanceId,
-                        "Connection",
-                        c.getName(),
-                        c.getIdentifier(),
-                        c.getProcessGroup().getIdentifier(),
-                        c.getSource().getName(),
-                        c.getSource().getIdentifier(),
-                        c.getDestination().getName(),
-                        c.getDestination().getIdentifier()
-                );
-                
PrometheusMetricsUtil.aggregateConnectionPredictionMetrics(aggregatedMetrics, 
statusAnalytics.getPredictions());
-            } catch (ResourceNotFoundException rnfe) {
-                break;
+                parallelProcessingThreadPool.submit(
+                        () -> connections.parallelStream().forEach((c) -> {
+                            final StatusAnalytics statusAnalytics = 
controllerFacade.getConnectionStatusAnalytics(c.getIdentifier());
+                            
PrometheusMetricsUtil.createConnectionStatusAnalyticsMetrics(connectionAnalyticsMetricsRegistry,
+                                    statusAnalytics,
+                                    instanceId,
+                                    "Connection",
+                                    c.getName(),
+                                    c.getIdentifier(),
+                                    c.getProcessGroup().getIdentifier(),
+                                    c.getSource().getName(),
+                                    c.getSource().getIdentifier(),
+                                    c.getDestination().getName(),
+                                    c.getDestination().getIdentifier()
+                            );
+                            predictions.add(statusAnalytics.getPredictions());
+                        }));
+            } finally {
+                parallelProcessingThreadPool.shutdown();
+                try {
+                    boolean finished = 
parallelProcessingThreadPool.awaitTermination(parallelProcessingTimeout, 
TimeUnit.MILLISECONDS);
+                    if (!finished) {

Review Comment:
   If it doesn't finish in time, the result is irrelevant so we should use 
`parallelProcessingThreadPool.shutdownNow()` in this instance also.



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