Repository navigation
Expand file tree
/
Copy pathpython_client.cpp
More file actions
147 lines (124 loc) · 4.66 KB
/
Copy pathpython_client.cpp
File metadata and controls
147 lines (124 loc) · 4.66 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
// 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_client.h"
#include "arrow/flight/client.h"
#include "arrow/flight/server.h"
#include "common/compiler_util.h"
#include "common/config.h"
#include "common/status.h"
#include "format/arrow/arrow_utils.h"
#include "udf/python/python_udf_meta.h"
#include "udf/python/python_udf_runtime.h"
namespace doris {
Status PythonClient::init(const PythonUDFMeta& func_meta, ProcessPtr process) {
if (_inited) {
return Status::InternalError("PythonClient has already been initialized");
}
// Set operation name based on client type
switch (func_meta.client_type) {
case PythonClientType::UDF:
_operation_name = "Python UDF";
break;
case PythonClientType::UDAF:
_operation_name = "Python UDAF";
break;
case PythonClientType::UDTF:
_operation_name = "Python UDTF";
break;
default:
return Status::InternalError("Invalid Python client type");
}
// Parse and connect to Python server location
arrow::flight::Location location;
RETURN_DORIS_STATUS_IF_RESULT_ERROR(location,
arrow::flight::Location::Parse(process->get_uri()));
RETURN_DORIS_STATUS_IF_RESULT_ERROR(_arrow_client, FlightClient::Connect(location));
// Serialize function metadata to JSON command
std::string command;
RETURN_IF_ERROR(func_meta.serialize_to_json(&command));
// Create Flight descriptor and establish bidirectional streaming
FlightDescriptor descriptor = FlightDescriptor::Command(command);
arrow::flight::FlightClient::DoExchangeResult exchange_res;
RETURN_DORIS_STATUS_IF_RESULT_ERROR(exchange_res, _arrow_client->DoExchange(descriptor));
_reader = std::move(exchange_res.reader);
_writer = std::move(exchange_res.writer);
_process = std::move(process);
_inited = true;
return Status::OK();
}
Status PythonClient::close() {
if (_inited && _writer) {
auto writer_res = _writer->Close();
if (!writer_res.ok()) {
LOG(WARNING) << "Error closing Python client writer: " << writer_res.message();
}
}
// Error handling may already have reset the writer; still release the pool lease.
_inited = false;
_begin = false;
_arrow_client.reset();
_writer.reset();
_reader.reset();
_process.reset();
_pool_lease.reset();
return Status::OK();
}
Status PythonClient::handle_error(arrow::Status status) {
DCHECK(!status.ok());
// Clean up resources
_writer.reset();
_reader.reset();
// Extract and clean error message
std::string msg = status.message();
LOG(ERROR) << _operation_name << " error: " << msg;
// Remove Python traceback noise for cleaner error messages
size_t pos = msg.find("The above exception was the direct cause");
if (pos != std::string::npos) {
msg = msg.substr(0, pos);
}
return Status::RuntimeError(trim(msg));
}
Status PythonClient::begin_stream(const std::shared_ptr<arrow::Schema>& schema) {
if (UNLIKELY(!_begin)) {
auto begin_res = _writer->Begin(schema);
if (!begin_res.ok()) {
return handle_error(begin_res);
}
_begin = true;
}
return Status::OK();
}
Status PythonClient::write_batch(const arrow::RecordBatch& input) {
auto write_res = _writer->WriteRecordBatch(input);
if (!write_res.ok()) {
return handle_error(write_res);
}
return Status::OK();
}
Status PythonClient::read_batch(std::shared_ptr<arrow::RecordBatch>* output) {
auto read_res = _reader->Next();
if (!read_res.ok()) {
return handle_error(read_res.status());
}
arrow::flight::FlightStreamChunk chunk = std::move(*read_res);
if (!chunk.data) {
return Status::InternalError("Received null RecordBatch from {} server", _operation_name);
}
*output = std::move(chunk.data);
return Status::OK();
}
} // namespace doris