be/src/udf/python/python_udf_runtime.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <boost/process.hpp> |
21 | | #include <chrono> |
22 | | #include <memory> |
23 | | |
24 | | #include "udf/python/python_env.h" |
25 | | |
26 | | namespace doris { |
27 | | |
28 | | struct UserFunctionCacheEntry; |
29 | | |
30 | | static const char* UNIX_SOCKET_PREFIX = "grpc+unix://"; |
31 | | // Use /tmp directory for Unix socket to avoid path length limit (max 107 chars) |
32 | | static const char* BASE_UNIX_SOCKET_PATH_TEMPLATE = "/tmp/doris_python_udf"; |
33 | | static const char* UNIX_SOCKET_PATH_TEMPLATE = "{}_{}.sock"; |
34 | | static const char* FLIGHT_SERVER_PATH_TEMPLATE = "{}/plugins/python_udf/{}"; |
35 | | static const char* FLIGHT_SERVER_FILENAME = "python_server.py"; |
36 | | |
37 | 3.73k | inline std::string get_base_unix_socket_path() { |
38 | 3.73k | return fmt::format("{}{}", UNIX_SOCKET_PREFIX, BASE_UNIX_SOCKET_PATH_TEMPLATE); |
39 | 3.73k | } |
40 | | |
41 | 1.87k | inline std::string get_unix_socket_path(pid_t child_pid) { |
42 | 1.87k | return fmt::format(UNIX_SOCKET_PATH_TEMPLATE, get_base_unix_socket_path(), child_pid); |
43 | 1.87k | } |
44 | | |
45 | 3.72k | inline std::string get_unix_socket_file_path(pid_t child_pid) { |
46 | 3.72k | return fmt::format(UNIX_SOCKET_PATH_TEMPLATE, BASE_UNIX_SOCKET_PATH_TEMPLATE, child_pid); |
47 | 3.72k | } |
48 | | |
49 | 1.86k | inline std::string get_fight_server_path() { |
50 | 1.86k | return fmt::format(FLIGHT_SERVER_PATH_TEMPLATE, std::getenv("DORIS_HOME"), |
51 | 1.86k | FLIGHT_SERVER_FILENAME); |
52 | 1.86k | } |
53 | | |
54 | | class PythonUDFProcess; |
55 | | |
56 | | using ProcessPtr = std::shared_ptr<PythonUDFProcess>; |
57 | | |
58 | | class PythonUDFProcess { |
59 | | public: |
60 | | PythonUDFProcess(boost::process::child child, boost::process::ipstream output_stream) |
61 | 1.87k | : _is_shutdown(false), |
62 | 1.87k | _child_pid(child.id()), |
63 | 1.87k | _uri(get_unix_socket_path(_child_pid)), |
64 | 1.87k | _unix_socket_file_path(get_unix_socket_file_path(_child_pid)), |
65 | 1.87k | _child(std::move(child)), |
66 | 1.87k | _output_stream(std::move(output_stream)) {} |
67 | | |
68 | 1.86k | ~PythonUDFProcess() { shutdown(); } |
69 | | |
70 | 5.81k | std::string get_uri() const { return _uri; } |
71 | | |
72 | | const std::string& get_socket_file_path() const { return _unix_socket_file_path; } |
73 | | |
74 | | bool is_shutdown() const { return _is_shutdown; } |
75 | | |
76 | 26.9k | bool is_alive() const { return !_is_shutdown && _child.running(); } |
77 | | |
78 | | void remove_unix_socket(); |
79 | | |
80 | | void shutdown(); |
81 | | |
82 | | enum class ChildExitWaitResult { EXITED, ALREADY_REAPED, TIMEOUT, ERROR }; |
83 | | |
84 | | static ChildExitWaitResult wait_child_exit(pid_t pid, std::chrono::milliseconds timeout, |
85 | | int* exit_status); |
86 | | |
87 | | // Hand off a killed child that could not be reaped synchronously. The background reaper keeps |
88 | | // waitpid ownership so a later child exit will not become a zombie under BE. |
89 | | static void enqueue_child_for_reap(pid_t pid); |
90 | | |
91 | | std::string to_string() const; |
92 | | |
93 | 723 | pid_t get_child_pid() const { return _child_pid; } |
94 | | |
95 | 1.81k | void set_package_cache_entry(std::shared_ptr<UserFunctionCacheEntry> entry) { |
96 | 1.81k | _package_cache_entry = std::move(entry); |
97 | 1.81k | } |
98 | | |
99 | 1.15k | bool operator==(const PythonUDFProcess& other) const { return _child_pid == other._child_pid; } |
100 | | |
101 | 1.15k | bool operator!=(const PythonUDFProcess& other) const { return !(*this == other); } |
102 | | |
103 | | #ifdef BE_TEST |
104 | | void set_uri_for_test(std::string uri) { _uri = std::move(uri); } |
105 | | |
106 | | static bool wait_background_reaped_for_test(pid_t pid, std::chrono::milliseconds timeout); |
107 | | |
108 | | static void force_child_exit_timeouts_for_test(int count); |
109 | | #endif |
110 | | |
111 | | private: |
112 | | constexpr static size_t MAX_ACCUMULATED_LOG_SIZE = 65536; |
113 | | |
114 | | // Keep package files available for lazy imports until this process is destroyed. |
115 | | std::shared_ptr<UserFunctionCacheEntry> _package_cache_entry; |
116 | | bool _is_shutdown {false}; |
117 | | pid_t _child_pid; |
118 | | std::string _uri; |
119 | | std::string _unix_socket_file_path; |
120 | | mutable boost::process::child _child; |
121 | | boost::process::ipstream _output_stream; |
122 | | std::string _accumulated_log; |
123 | | }; |
124 | | |
125 | | } // namespace doris |