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


##########
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 added a python processor without cofigure it and remove it from the 
canvas. But the started python process is not stopped. I debugged the code, and 
it seems, removeProcessor is called with a wrong value?
   
   <img width="943" alt="image" 
src="https://user-images.githubusercontent.com/227213/224340612-418d7b7d-d365-45fd-a169-706daed5ee01.png";>
   
   As you can see on the picture identifier is different what is inside 
processorPrefersIsolation map. Of course this is not a serious bug, because it 
will be stop during nifi shutdown



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