HappenLee commented on code in PR #62620:
URL: https://github.com/apache/doris/pull/62620#discussion_r3176222908


##########
be/src/udf/python/python_server.cpp:
##########
@@ -110,29 +155,73 @@ Status PythonServerManager::ensure_pool_initialized(const 
PythonVersion& version
               << ": created " << success_count << " processes"
               << (failure_count > 0 ? fmt::format(" ({} failed)", 
failure_count) : "");
 
-    _initialized_versions.insert(version);
+    versioned_pool->initialized = true;
     _start_health_check_thread();
 
     return Status::OK();
 }
 
 Status PythonServerManager::get_process(const PythonVersion& version, 
ProcessPtr* process) {
-    std::lock_guard<std::mutex> lock(_pools_mutex);
-    std::vector<ProcessPtr>& pool = _process_pools[version];
+    auto versioned_pool = _get_process_pool(version);
+    if (!versioned_pool) {
+        return Status::InternalError("Python process pool is empty for version 
{}",
+                                     version.to_string());
+    }
+    std::lock_guard<std::mutex> lock(versioned_pool->mutex);
+    std::vector<ProcessPtr>& pool = versioned_pool->processes;
 
     if (UNLIKELY(pool.empty())) {
         return Status::InternalError("Python process pool is empty for version 
{}",
                                      version.to_string());
     }
 
-    // Find process with minimum load (use_count - 1 gives active client count)
-    auto min_iter = std::min_element(
-            pool.begin(), pool.end(),
-            [](const ProcessPtr& a, const ProcessPtr& b) { return 
a.use_count() < b.use_count(); });
+    // Prefer an already-alive process and only use load balancing inside that 
alive subset.
+    // keep dead entries stay in the pool for the background health checker
+    // unless there is no alive process left for the current request.
+    auto min_alive_iter = std::min_element(pool.begin(), pool.end(),
+                                           [](const ProcessPtr& a, const 
ProcessPtr& b) {
+                                               const bool a_alive = a && 
a->is_alive();
+                                               const bool b_alive = b && 
b->is_alive();
+                                               if (a_alive != b_alive) {
+                                                   return a_alive > b_alive;
+                                               }
+                                               if (!a_alive) {
+                                                   return false;
+                                               }
+                                               return a.use_count() < 
b.use_count();
+                                           });
+
+    if (min_alive_iter != pool.end() && *min_alive_iter && 
(*min_alive_iter)->is_alive()) {
+        *process = *min_alive_iter;
+        return Status::OK();
+    }
 
-    // Return process with minimum load
-    *process = *min_iter;
-    return Status::OK();
+    // Only reach here when the pool has no alive process at all. In that 
fallback path we
+    // rebuild one process so the caller can still make progress instead of 
waiting
+    // for the next health-check round.
+    for (size_t i = 0; i < pool.size(); ++i) {
+        auto& candidate = pool[i];
+        ProcessPtr replacement;
+        Status status = fork(version, &replacement);
+        if (!status.ok()) {

Review Comment:
   为什么不是第一个失败就直接返回报错?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to