sjyango commented on code in PR #57329: URL: https://github.com/apache/doris/pull/57329#discussion_r2473302171
########## be/src/udf/python/python_udf_client.cpp: ########## @@ -0,0 +1,125 @@ +// 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_udf_client.h" + +#include <utility> + +#include "arrow/flight/client.h" +#include "arrow/flight/server.h" +#include "common/status.h" +#include "udf/python/python_udf_meta.h" +#include "udf/python/python_udf_runtime.h" +#include "util/arrow/utils.h" + +namespace doris { + +Status PythonUDFClient::create(const PythonUDFMeta& func_meta, ProcessPtr process, + PythonUDFClientPtr* client) { + PythonUDFClientPtr python_udf_client = std::make_shared<PythonUDFClient>(); + RETURN_IF_ERROR(python_udf_client->init(func_meta, std::move(process))); + *client = std::move(python_udf_client); + return Status::OK(); +} + +Status PythonUDFClient::init(const PythonUDFMeta& func_meta, ProcessPtr process) { + if (_inited) { + return Status::InternalError("PythonUDFClient has already been initialized"); + } + 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)); + std::string command; + RETURN_IF_ERROR(func_meta.serialize_to_json(&command)); + 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 PythonUDFClient::evaluate(const arrow::RecordBatch& input, + std::shared_ptr<arrow::RecordBatch>* output) { + if (!_process->is_alive()) { + return Status::RuntimeError("Python UDF process is not alive"); + } + + // Step 1: Begin exchange with schema (only once) + if (UNLIKELY(!_begin)) { + auto begin_res = _writer->Begin(input.schema()); + if (!begin_res.ok()) { + return handle_error(begin_res); + } + _begin = true; + } + + // Step 2: Write the record batch to server + auto write_res = _writer->WriteRecordBatch(input); + if (!write_res.ok()) { + return handle_error(write_res); + } + + // Step 3: Read response from server + 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) { + _process->shutdown(); Review Comment: <img width="830" height="320" alt="image" src="https://github.com/user-attachments/assets/3b05c7ac-17e2-4b6e-8fef-b14320865860" /> <img width="352" height="76" alt="image" src="https://github.com/user-attachments/assets/6f182981-1988-4a70-808b-9bdb80943268" /> If a process has successfully shut down, logging will be printed and return directly, rather than returning the inactive process to the process pool -- 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]
