be/src/udf/python/python_client.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 <arrow/status.h> |
21 | | |
22 | | #include "arrow/flight/client.h" |
23 | | #include "common/status.h" |
24 | | #include "format/arrow/arrow_utils.h" |
25 | | #include "storage/olap_define.h" |
26 | | #include "udf/python/python_udf_meta.h" |
27 | | #include "udf/python/python_udf_runtime.h" |
28 | | |
29 | | namespace doris { |
30 | | |
31 | | class PythonUDFPoolLease; |
32 | | |
33 | | /** |
34 | | * Base class for Python UDF/UDAF/UDTF clients |
35 | | * |
36 | | * Provides common functionality for communicating with Python server via Arrow Flight: |
37 | | * - Connection management |
38 | | * - Stream initialization |
39 | | * - Error handling |
40 | | * - Process lifecycle management |
41 | | */ |
42 | | class PythonClient { |
43 | | public: |
44 | | using FlightDescriptor = arrow::flight::FlightDescriptor; |
45 | | using FlightClient = arrow::flight::FlightClient; |
46 | | using FlightStreamWriter = arrow::flight::FlightStreamWriter; |
47 | | using FlightStreamReader = arrow::flight::FlightStreamReader; |
48 | | |
49 | 1.47k | PythonClient() = default; |
50 | 1.47k | virtual ~PythonClient() = default; |
51 | | |
52 | | /** |
53 | | * Initialize connection to Python server |
54 | | * @param func_meta Function metadata (contains client_type for operation name) |
55 | | * @param process Python process handle |
56 | | * @return Status |
57 | | */ |
58 | | Status init(const PythonUDFMeta& func_meta, ProcessPtr process); |
59 | | |
60 | | /** |
61 | | * Close connection and cleanup resources |
62 | | * @return Status |
63 | | */ |
64 | | Status close(); |
65 | | |
66 | | /** |
67 | | * Handle Arrow Flight error |
68 | | * @param status Arrow status |
69 | | * @return Doris Status with formatted error message |
70 | | */ |
71 | | Status handle_error(arrow::Status status); |
72 | | |
73 | | /** |
74 | | * Get process information for debugging |
75 | | * @return Process string representation |
76 | | */ |
77 | 890 | std::string print_process() const { return _process ? _process->to_string() : "null"; } |
78 | | |
79 | | /** |
80 | | * Get the underlying Python process |
81 | | * @return Process pointer |
82 | | */ |
83 | 117 | ProcessPtr get_process() const { return _process; } |
84 | | |
85 | | protected: |
86 | | /** |
87 | | * Begin Flight stream with schema (called only once per stream) |
88 | | * @param schema Input schema |
89 | | * @return Status |
90 | | */ |
91 | | Status begin_stream(const std::shared_ptr<arrow::Schema>& schema); |
92 | | |
93 | | /** |
94 | | * Write RecordBatch to server |
95 | | * @param input Input RecordBatch |
96 | | * @return Status |
97 | | */ |
98 | | Status write_batch(const arrow::RecordBatch& input); |
99 | | |
100 | | /** |
101 | | * Read RecordBatch from server |
102 | | * @param output Output RecordBatch |
103 | | * @return Status |
104 | | */ |
105 | | Status read_batch(std::shared_ptr<arrow::RecordBatch>* output); |
106 | | |
107 | | // Released after the stream and process, so idle reclamation cannot interrupt cleanup. |
108 | | std::shared_ptr<PythonUDFPoolLease> _pool_lease; |
109 | | |
110 | | // Common state |
111 | | bool _inited = false; |
112 | | bool _begin = false; // Track if Begin() has been called |
113 | | std::string _operation_name; // Operation name for error messages |
114 | | std::unique_ptr<FlightClient> _arrow_client; |
115 | | std::unique_ptr<FlightStreamWriter> _writer; |
116 | | std::unique_ptr<FlightStreamReader> _reader; |
117 | | ProcessPtr _process; |
118 | | |
119 | | private: |
120 | | friend class PythonServerManager; |
121 | | |
122 | | DISALLOW_COPY_AND_ASSIGN(PythonClient); |
123 | | }; |
124 | | |
125 | | } // namespace doris |