-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathpython_server.cpp
More file actions
446 lines (367 loc) · 16.3 KB
/
python_server.cpp
File metadata and controls
446 lines (367 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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.
#include "udf/python/python_server.h"
#include <arrow/type_fwd.h>
#include <butil/fd_utility.h>
#include <dirent.h>
#include <fmt/core.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <chrono>
#include <fstream>
#include <future>
#include <thread>
#include "arrow/flight/client.h"
#include "common/config.h"
#include "udf/python/python_udaf_client.h"
#include "udf/python/python_udf_client.h"
#include "udf/python/python_udtf_client.h"
#include "util/cpu_info.h"
namespace doris {
template <typename T>
Status PythonServerManager::get_client(const PythonUDFMeta& func_meta, const PythonVersion& version,
std::shared_ptr<T>* client,
const std::shared_ptr<arrow::Schema>& data_schema) {
// Ensure process pool is initialized for this version
RETURN_IF_ERROR(ensure_pool_initialized(version));
ProcessPtr process;
RETURN_IF_ERROR(get_process(version, &process));
if constexpr (std::is_same_v<T, PythonUDAFClient>) {
RETURN_IF_ERROR(T::create(func_meta, std::move(process), data_schema, client));
} else {
RETURN_IF_ERROR(T::create(func_meta, std::move(process), client));
}
return Status::OK();
}
Status PythonServerManager::ensure_pool_initialized(const PythonVersion& version) {
std::lock_guard<std::mutex> lock(_pools_mutex);
// Check if already initialized
if (_initialized_versions.count(version)) return Status::OK();
std::vector<ProcessPtr>& pool = _process_pools[version];
// 0 means use CPU core count as default, otherwise use the specified value
int max_pool_size = config::max_python_process_num > 0 ? config::max_python_process_num
: CpuInfo::num_cores();
LOG(INFO) << "Initializing Python process pool for version " << version.to_string() << " with "
<< max_pool_size
<< " processes (config::max_python_process_num=" << config::max_python_process_num
<< ", CPU cores=" << CpuInfo::num_cores() << ")";
std::vector<std::future<Status>> futures;
std::vector<ProcessPtr> temp_processes(max_pool_size);
for (int i = 0; i < max_pool_size; i++) {
futures.push_back(std::async(std::launch::async, [this, &version, i, &temp_processes]() {
ProcessPtr process;
Status s = fork(version, &process);
if (s.ok()) {
temp_processes[i] = std::move(process);
}
return s;
}));
}
int success_count = 0;
int failure_count = 0;
const auto init_start_time = std::chrono::steady_clock::now();
constexpr auto progress_log_interval = std::chrono::seconds(20);
for (int i = 0; i < max_pool_size; i++) {
// Print init log every 20s until the current slot is ready.
while (futures[i].wait_for(progress_log_interval) != std::future_status::ready) {
const auto now = std::chrono::steady_clock::now();
const auto total_elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(now - init_start_time)
.count();
LOG(INFO) << "Python process pool initialization progress for version "
<< version.to_string() << ": waiting_slot=" << (i + 1) << "/" << max_pool_size
<< ", success=" << success_count << ", failed=" << failure_count
<< ", elapsed_ms=" << total_elapsed_ms;
}
Status s = futures[i].get();
if (s.ok() && temp_processes[i]) {
pool.push_back(std::move(temp_processes[i]));
success_count++;
} else {
failure_count++;
LOG(WARNING) << "Failed to create Python process " << (i + 1) << "/" << max_pool_size
<< ": " << s.to_string();
}
}
if (pool.empty()) {
return Status::InternalError(
"Failed to initialize Python process pool: all {} process creation attempts failed",
max_pool_size);
}
const auto total_elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - init_start_time)
.count();
LOG(INFO) << "Python process pool initialized for version " << version.to_string()
<< ": created " << success_count << " processes"
<< (failure_count > 0 ? fmt::format(" ({} failed)", failure_count) : "")
<< ", elapsed_ms=" << total_elapsed_ms;
_initialized_versions.insert(version);
_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];
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(); });
// Return process with minimum load
*process = *min_iter;
return Status::OK();
}
Status PythonServerManager::fork(const PythonVersion& version, ProcessPtr* process) {
std::string python_executable_path = version.get_executable_path();
std::string fight_server_path = get_fight_server_path();
std::string base_unix_socket_path = get_base_unix_socket_path();
std::vector<std::string> args = {"-u", fight_server_path, base_unix_socket_path};
boost::process::environment env = boost::this_process::environment();
boost::process::ipstream child_output;
try {
boost::process::child c(
python_executable_path, args, boost::process::std_out > child_output,
boost::process::env = env,
boost::process::on_exit([](int exit_code, const std::error_code& ec) {
if (ec) {
LOG(WARNING) << "Python UDF server exited with error: " << ec.message();
}
}));
// Wait for socket file to be created (indicates server is ready)
std::string expected_socket_path = get_unix_socket_file_path(c.id());
bool started_successfully = false;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
const auto timeout = std::chrono::milliseconds(5000);
while (std::chrono::steady_clock::now() - start < timeout) {
struct stat buffer;
if (stat(expected_socket_path.c_str(), &buffer) == 0) {
started_successfully = true;
break;
}
if (!c.running()) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
if (!started_successfully) {
if (c.running()) {
c.terminate();
c.wait();
}
return Status::InternalError("Python server start failed: socket file not found at {}",
expected_socket_path);
}
*process = std::make_shared<PythonUDFProcess>(std::move(c), std::move(child_output));
} catch (const std::exception& e) {
return Status::InternalError("Failed to start Python UDF server: {}", e.what());
}
return Status::OK();
}
void PythonServerManager::_start_health_check_thread() {
if (_health_check_thread) return;
LOG(INFO) << "Starting Python process health check thread (interval: 30 seconds)";
_health_check_thread = std::make_unique<std::thread>([this]() {
// Health check loop
while (!_shutdown_flag.load(std::memory_order_acquire)) {
// Wait for interval or shutdown signal
{
std::unique_lock<std::mutex> lock(_health_check_mutex);
_health_check_cv.wait_for(lock, std::chrono::seconds(30), [this]() {
return _shutdown_flag.load(std::memory_order_acquire);
});
}
if (_shutdown_flag.load(std::memory_order_acquire)) break;
_check_and_recreate_processes();
_refresh_memory_stats();
}
LOG(INFO) << "Python process health check thread exiting";
});
}
void PythonServerManager::_check_and_recreate_processes() {
std::lock_guard<std::mutex> lock(_pools_mutex);
int total_checked = 0;
int total_dead = 0;
int total_recreated = 0;
for (auto& [version, pool] : _process_pools) {
for (size_t i = 0; i < pool.size(); ++i) {
auto& process = pool[i];
if (!process) continue;
total_checked++;
if (!process->is_alive()) {
total_dead++;
LOG(WARNING) << "Detected dead Python process (pid=" << process->get_child_pid()
<< ", version=" << version.to_string() << "), recreating...";
ProcessPtr new_process;
Status s = fork(version, &new_process);
if (s.ok()) {
pool[i] = std::move(new_process);
total_recreated++;
LOG(INFO) << "Successfully recreated Python process for version "
<< version.to_string();
} else {
LOG(ERROR) << "Failed to recreate Python process for version "
<< version.to_string() << ": " << s.to_string();
pool.erase(pool.begin() + i);
--i;
}
}
}
}
if (total_dead > 0) {
LOG(INFO) << "Health check completed: checked=" << total_checked << ", dead=" << total_dead
<< ", recreated=" << total_recreated;
}
}
void PythonServerManager::shutdown() {
// Signal health check thread to stop
_shutdown_flag.store(true, std::memory_order_release);
_health_check_cv.notify_one();
if (_health_check_thread && _health_check_thread->joinable()) {
_health_check_thread->join();
_health_check_thread.reset();
}
// Shutdown all processes
std::lock_guard<std::mutex> lock(_pools_mutex);
for (auto& [version, pool] : _process_pools) {
for (auto& process : pool) {
if (process) {
process->shutdown();
}
}
}
_process_pools.clear();
}
Status PythonServerManager::_read_process_memory(pid_t pid, size_t* rss_bytes) {
// Read from /proc/{pid}/statm
// Format: size resident shared text lib data dt
std::string statm_path = fmt::format("/proc/{}/statm", pid);
std::ifstream statm_file(statm_path);
if (!statm_file.is_open()) {
return Status::InternalError("Cannot open {}", statm_path);
}
size_t size_pages = 0, rss_pages = 0;
// we only care about RSS, read and ignore the total size field
statm_file >> size_pages >> rss_pages;
if (statm_file.fail()) {
return Status::InternalError("Failed to read {}", statm_path);
}
// Convert pages to bytes
long page_size = sysconf(_SC_PAGESIZE);
*rss_bytes = rss_pages * page_size;
return Status::OK();
}
void PythonServerManager::_refresh_memory_stats() {
std::lock_guard<std::mutex> lock(_pools_mutex);
int64_t total_rss = 0;
for (const auto& [version, pool] : _process_pools) {
for (const auto& process : pool) {
if (!process || !process->is_alive()) continue;
size_t rss_bytes = 0;
Status s = _read_process_memory(process->get_child_pid(), &rss_bytes);
if (s.ok()) {
total_rss += rss_bytes;
} else [[unlikely]] {
LOG(WARNING) << "Failed to read memory info for Python process (pid="
<< process->get_child_pid() << "): " << s.to_string();
}
}
}
_mem_tracker.set_consumption(total_rss);
LOG(INFO) << _mem_tracker.log_usage();
if (config::python_udf_processes_memory_limit_bytes > 0 &&
total_rss > config::python_udf_processes_memory_limit_bytes) {
LOG(WARNING) << "Python UDF process memory usage exceeds limit: rss_bytes=" << total_rss
<< ", limit_bytes=" << config::python_udf_processes_memory_limit_bytes;
}
}
Status PythonServerManager::clear_module_cache(const std::string& location) {
if (location.empty()) {
return Status::InvalidArgument("Empty location for clear_module_cache");
}
std::lock_guard<std::mutex> lock(_pools_mutex);
std::string body = fmt::format(R"({{"location": "{}"}})", location);
int success_count = 0;
int fail_count = 0;
bool has_active_process = false;
for (auto& [version, pool] : _process_pools) {
for (auto& process : pool) {
if (!process || !process->is_alive()) {
continue;
}
has_active_process = true;
try {
auto loc_result = arrow::flight::Location::Parse(process->get_uri());
if (!loc_result.ok()) [[unlikely]] {
fail_count++;
continue;
}
auto client_result = arrow::flight::FlightClient::Connect(*loc_result);
if (!client_result.ok()) [[unlikely]] {
fail_count++;
continue;
}
auto client = std::move(*client_result);
arrow::flight::Action action;
action.type = "clear_module_cache";
action.body = arrow::Buffer::FromString(body);
auto result_stream = client->DoAction(action);
if (!result_stream.ok()) {
fail_count++;
continue;
}
auto result = (*result_stream)->Next();
if (result.ok() && *result) {
success_count++;
} else {
fail_count++;
}
} catch (...) {
fail_count++;
}
}
}
if (!has_active_process) {
return Status::OK();
}
LOG(INFO) << "clear_module_cache completed for location=" << location
<< ", success=" << success_count << ", failed=" << fail_count;
if (fail_count > 0) {
return Status::InternalError(
"clear_module_cache failed for location={}, success={}, failed={}", location,
success_count, fail_count);
}
return Status::OK();
}
// Explicit template instantiation for UDF, UDAF and UDTF clients
template Status PythonServerManager::get_client<PythonUDFClient>(
const PythonUDFMeta& func_meta, const PythonVersion& version,
std::shared_ptr<PythonUDFClient>* client,
const std::shared_ptr<arrow::Schema>& data_schema);
template Status PythonServerManager::get_client<PythonUDAFClient>(
const PythonUDFMeta& func_meta, const PythonVersion& version,
std::shared_ptr<PythonUDAFClient>* client,
const std::shared_ptr<arrow::Schema>& data_schema);
template Status PythonServerManager::get_client<PythonUDTFClient>(
const PythonUDFMeta& func_meta, const PythonVersion& version,
std::shared_ptr<PythonUDTFClient>* client,
const std::shared_ptr<arrow::Schema>& data_schema);
} // namespace doris