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


##########
nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/main/java/org/apache/nifi/py4j/StandardPythonBridge.java:
##########
@@ -0,0 +1,315 @@
+/*
+ * 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.py4j;
+
+import org.apache.nifi.python.BoundObjectCounts;
+import org.apache.nifi.python.ControllerServiceTypeLookup;
+import org.apache.nifi.python.PythonBridge;
+import org.apache.nifi.python.PythonBridgeInitializationContext;
+import org.apache.nifi.python.PythonController;
+import org.apache.nifi.python.PythonProcessConfig;
+import org.apache.nifi.python.PythonProcessorDetails;
+import org.apache.nifi.python.processor.PythonProcessorAdapter;
+import org.apache.nifi.python.processor.PythonProcessorBridge;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+public class StandardPythonBridge implements PythonBridge {
+    private static final Logger logger = 
LoggerFactory.getLogger(StandardPythonBridge.class);
+
+    private volatile boolean running = false;
+
+    private PythonProcessConfig processConfig;
+    private ControllerServiceTypeLookup serviceTypeLookup;
+    private PythonProcess controllerProcess;
+    private final Map<ExtensionId, Integer> processorCountByType = new 
ConcurrentHashMap<>();
+    private final Map<ExtensionId, List<PythonProcess>> 
processesByProcessorType = new ConcurrentHashMap<>();
+
+
+    @Override
+    public void initialize(final PythonBridgeInitializationContext context) {
+        this.processConfig = context.getPythonProcessConfig();
+        this.serviceTypeLookup = context.getControllerServiceTypeLookup();
+    }
+
+    @Override
+    public synchronized void start() throws IOException {
+        if (running) {
+            logger.debug("{} already started, will not start again", this);
+            return;
+        }
+
+        logger.debug("{} launching Python Process", this);
+
+        try {
+            final File envHome = new 
File(processConfig.getPythonWorkingDirectory(), "controller");
+            controllerProcess = new PythonProcess(processConfig, 
serviceTypeLookup, envHome);
+            controllerProcess.start();
+            running = true;
+        } catch (final Exception e) {
+            shutdown();
+            throw e;
+        }
+    }
+
+
+    @Override
+    public void discoverExtensions() {
+        ensureStarted();
+        final List<String> extensionsDirs = 
processConfig.getPythonExtensionsDirectories().stream()
+            .map(File::getAbsolutePath)
+            .collect(Collectors.toList());
+        final String workDirPath = 
processConfig.getPythonWorkingDirectory().getAbsolutePath();
+        controllerProcess.getController().discoverExtensions(extensionsDirs, 
workDirPath);
+    }
+
+    @Override
+    public PythonProcessorBridge createProcessor(final String identifier, 
final String type, final String version, final boolean preferIsolatedProcess) {
+        ensureStarted();
+
+        logger.debug("Creating Python Processor of type {}", type);
+
+        final PythonProcess pythonProcess = getProcessForNextComponent(type, 
version, preferIsolatedProcess);
+        final String workDirPath = 
processConfig.getPythonWorkingDirectory().getAbsolutePath();
+
+        final PythonController controller = pythonProcess.getController();
+        final PythonProcessorAdapter processorAdapter = 
controller.createProcessor(type, version, workDirPath);
+        final PythonProcessorBridge processorBridge = new 
StandardPythonProcessorBridge.Builder()
+            .controller(controller)
+            .processorAdapter(processorAdapter)
+            .processorType(type)
+            .processorVersion(version)
+            .workingDirectory(processConfig.getPythonWorkingDirectory())
+            .moduleFile(new File(controller.getModuleFile(type, version)))
+            .build();
+
+        pythonProcess.addProcessor(identifier, preferIsolatedProcess);
+        final ExtensionId extensionId = new ExtensionId(type, version);
+        processorCountByType.merge(extensionId, 1, Integer::sum);
+        return processorBridge;
+    }
+
+    @Override
+    public synchronized void onProcessorRemoved(final String identifier, final 
String type, final String version) {
+        final ExtensionId extensionId = new ExtensionId(type, version);
+        final List<PythonProcess> processes = 
processesByProcessorType.get(extensionId);
+        if (processes == null) {
+            return;
+        }
+
+        // Find the Python Process that has the Processor, if any, and remove 
it.
+        // If there are no additional Processors in the Python Process, remove 
it from our list and shut down the process.
+        final Iterator<PythonProcess> processItr = processes.iterator(); // 
Use iter so we can call remove()
+        while (processItr.hasNext()) {
+            final PythonProcess process = processItr.next();
+            final boolean removed = process.removeProcessor(identifier);

Review Comment:
   I think this is working correctly, actually.
   In order to determine some details about Processors, NiFi needs an instance 
of the Processor. Things like what properties are available, default values, 
etc. It uses this information, among other things, for building the 
documentation, etc.
   So that's why you see the temp-component-PrettyPrintJson there. That's the 
temporary component. If you then add a PrettyPrintJson processor, it will add 
it to that same Process. Because it requires virtually no resources, we combine 
the temp component along with the actual Processor in order to avoid creating 
more processes than we have to. But we can't eliminate that Python process 
while the temp processor still exists.
   So basically once you add a Processor of type X, there will remain a Python 
Process for it until NiFi is restarted, even if you remove the Processor.
   Perhaps this could be improved in the future, but it's a pretty low priority 
at the moment, given that we don't expect an exorbitant number of different 
Processor types and definitely don't expect an exorbitant number types of 
Processors added to the canvas and removed.



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to