cyb70289 commented on a change in pull request #12465:
URL: https://github.com/apache/arrow/pull/12465#discussion_r823293471
##########
File path: cpp/src/arrow/flight/client.h
##########
@@ -336,8 +333,9 @@ class ARROW_FLIGHT_EXPORT FlightClient {
private:
FlightClient();
Status CheckOpen() const;
- class FlightClientImpl;
- std::unique_ptr<FlightClientImpl> impl_;
+ std::unique_ptr<internal::ClientTransport> impl_;
Review comment:
Is `transport_` better name than `impl_` here?
##########
File path: cpp/src/arrow/flight/server.cc
##########
@@ -927,65 +201,18 @@ FlightServerBase::FlightServerBase() { impl_.reset(new
Impl); }
FlightServerBase::~FlightServerBase() {}
Status FlightServerBase::Init(const FlightServerOptions& options) {
- impl_->service_.reset(new FlightServiceImpl(
- options.auth_handler, options.memory_manager, options.middleware, this));
-
- grpc::ServerBuilder builder;
- // Allow uploading messages of any length
- builder.SetMaxReceiveMessageSize(-1);
-
- const Location& location = options.location;
- const std::string scheme = location.scheme();
- if (scheme == kSchemeGrpc || scheme == kSchemeGrpcTcp || scheme ==
kSchemeGrpcTls) {
- std::stringstream address;
- address << arrow::internal::UriEncodeHost(location.uri_->host()) << ':'
- << location.uri_->port_text();
-
- std::shared_ptr<grpc::ServerCredentials> creds;
- if (scheme == kSchemeGrpcTls) {
- grpc::SslServerCredentialsOptions ssl_options;
- for (const auto& pair : options.tls_certificates) {
- ssl_options.pem_key_cert_pairs.push_back({pair.pem_key,
pair.pem_cert});
- }
- if (options.verify_client) {
- ssl_options.client_certificate_request =
- GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY;
- }
- if (!options.root_certificates.empty()) {
- ssl_options.pem_root_certs = options.root_certificates;
- }
- creds = grpc::SslServerCredentials(ssl_options);
- } else {
- creds = grpc::InsecureServerCredentials();
- }
-
- builder.AddListeningPort(address.str(), creds, &impl_->port_);
- } else if (scheme == kSchemeGrpcUnix) {
- std::stringstream address;
- address << "unix:" << location.uri_->path();
- builder.AddListeningPort(address.str(), grpc::InsecureServerCredentials());
- } else {
- return Status::NotImplemented("Scheme is not supported: " + scheme);
- }
-
- builder.RegisterService(impl_->service_.get());
-
- // Disable SO_REUSEPORT - it makes debugging/testing a pain as
- // leftover processes can handle requests on accident
- builder.AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0);
-
- if (options.builder_hook) {
- options.builder_hook(&builder);
- }
+ flight::transport::grpc::InitializeFlightGrpcServer();
Review comment:
Will we add more transport initializations here later?
Is it possible to move it inside `transport.cc` and call something like
`fight::transport::InitializeServers()` here?
Or maybe we don't need to do anything explicitly here. Let
`transport::MakeServer` and `MakeClient` to runonce all the registrations.
##########
File path: cpp/src/arrow/flight/transport.h
##########
@@ -0,0 +1,225 @@
+// 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.
+
+/// \file
+/// Internal (but not private) interface for implementing
+/// alternate network transports in Flight.
+///
+/// \warning EXPERIMENTAL. Subject to change.
+///
+/// To implement a transport, implement ServerTransport and
+/// ClientTransport, and register the desired URI schemes with
+/// TransportRegistry. Flight takes care of most of the per-RPC
+/// details; transports only handle connections and providing a I/O
+/// stream implementation (TransportDataStream).
+///
+/// On the server side:
+///
+/// 1. Applications subclass FlightServerBase and override RPC handlers.
+/// 2. FlightServerBase::Init will look up and create a ServerTransport
+/// based on the scheme of the Location given to it.
+/// 3. The ServerTransport will start the actual server. (For instance,
+/// for gRPC, it creates a gRPC server and registers a gRPC service.)
+/// That server will handle connections.
+/// 4. The transport should forward incoming calls to the server to the RPC
+/// handlers defined on ServerTransport, which implements the actual
+/// RPC handler using the interfaces here. Any I/O the RPC handler needs
+/// to do is managed by transport-specific implementations of
+/// TransportDataStream.
+/// 5. ServerTransport calls FlightServerBase for the actual application
+/// logic.
+///
+/// On the client side:
+///
+/// 1. Applications create a FlightClient with a Location.
+/// 2. FlightClient will look up and create a ClientTransport based on
+/// the scheme of the Location given to it.
+/// 3. When calling a method on FlightClient, FlightClient will delegate to
+/// the ClientTransport. There is some indirection, e.g. for DoGet,
+/// FlightClient only requests that the ClientTransport start the
+/// call and provide it with an I/O stream. The "Flight implementation"
+/// itself still lives in FlightClient.
+
+#pragma once
+
+#include <functional>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/flight/type_fwd.h"
+#include "arrow/flight/visibility.h"
+#include "arrow/type_fwd.h"
+
+namespace arrow {
+namespace ipc {
+class Message;
+}
+namespace flight {
+namespace internal {
+
+/// Internal, not user-visible type used for memory-efficient reads
+struct FlightData {
+ /// Used only for puts, may be null
+ std::unique_ptr<FlightDescriptor> descriptor;
+
+ /// Non-length-prefixed Message header as described in format/Message.fbs
+ std::shared_ptr<Buffer> metadata;
+
+ /// Application-defined metadata
+ std::shared_ptr<Buffer> app_metadata;
+
+ /// Message body
+ std::shared_ptr<Buffer> body;
+
+ /// Open IPC message from the metadata and body
+ ::arrow::Result<std::unique_ptr<ipc::Message>> OpenMessage();
+};
+
+/// \brief A transport-specific interface for reading/writing Arrow data.
+///
+/// New transports will implement this to read/write IPC payloads to
+/// the underlying stream.
+class ARROW_FLIGHT_EXPORT TransportDataStream {
+ public:
+ virtual ~TransportDataStream() = default;
+ /// \brief Attempt to read the next FlightData message.
+ ///
+ /// \return success true if data was populated, false if there was
+ /// an error. For clients, the error can be retrieved from
+ /// Finish(Status).
+ virtual bool ReadData(FlightData* data);
+ /// \brief Attempt to write a FlightPayload.
+ ///
+ /// \param[in] payload The data to write.
+ /// \return true if the message was accepted by the transport, false
+ /// if not (e.g. due to client/server disconnect), Status if there
+ /// was an error (e.g. with the payload itself).
+ virtual arrow::Result<bool> WriteData(const FlightPayload& payload);
+ /// \brief Indicate that there are no more writes on this stream.
+ ///
+ /// This is only a hint for the underlying transport and may not
+ /// actually do anything.
+ virtual Status WritesDone();
+};
+
+/// \brief A transport-specific interface for reading/writing Arrow
+/// data for a client.
+class ARROW_FLIGHT_EXPORT ClientDataStream : public TransportDataStream {
+ public:
+ /// \brief Attempt to read a non-data message.
+ ///
+ /// Only implemented for DoPut; mutually exclusive with
+ /// ReadData(FlightData*).
+ virtual bool ReadPutMetadata(std::shared_ptr<Buffer>* out);
+ /// \brief Attempt to cancel the call.
+ ///
+ /// This is only a hint and may not take effect immediately. The
+ /// client should still finish the call with Finish(Status) as usual.
+ virtual void TryCancel() {}
+ /// \brief Finish the call, reporting the server-sent status and/or
+ /// any client-side errors as appropriate.
+ ///
+ /// Implies WritesDone() and DoFinish().
+ ///
+ /// \param[in] st A client-side status to combine with the
+ /// server-side error. That is, if an error occurs on the
+ /// client-side, call Finish(Status) to finish the server-side
+ /// call, get the server-side status, and merge the statuses
+ /// together so context is not lost.
+ Status Finish(Status st);
+
+ protected:
+ /// \brief End the call, returning the final server status.
+ ///
+ /// For implementors: should imply WritesDone() (even if it does not
+ /// directly call it).
+ ///
+ /// Implies WritesDone().
+ virtual Status DoFinish() = 0;
+};
+
+/// An implementation of a Flight client for a particular transport.
+///
+/// Transports should override the methods they are capable of
+/// supporting. The default method implementations return an error.
+class ARROW_FLIGHT_EXPORT ClientTransport {
+ public:
+ virtual ~ClientTransport() = default;
+
+ /// Initialize the client.
+ virtual Status Init(const FlightClientOptions& options, const Location&
location,
+ const arrow::internal::Uri& uri) = 0;
+ /// Close the client. Once this returns, the client is no longer usable.
+ virtual Status Close() = 0;
+
+ virtual Status Authenticate(const FlightCallOptions& options,
+ std::unique_ptr<ClientAuthHandler> auth_handler);
+ virtual arrow::Result<std::pair<std::string, std::string>>
AuthenticateBasicToken(
+ const FlightCallOptions& options, const std::string& username,
+ const std::string& password);
+ virtual Status DoAction(const FlightCallOptions& options, const Action&
action,
+ std::unique_ptr<ResultStream>* results);
+ virtual Status ListActions(const FlightCallOptions& options,
+ std::vector<ActionType>* actions);
+ virtual Status GetFlightInfo(const FlightCallOptions& options,
+ const FlightDescriptor& descriptor,
+ std::unique_ptr<FlightInfo>* info);
+ virtual Status GetSchema(const FlightCallOptions& options,
+ const FlightDescriptor& descriptor,
+ std::unique_ptr<SchemaResult>* schema_result);
+ virtual Status ListFlights(const FlightCallOptions& options, const Criteria&
criteria,
+ std::unique_ptr<FlightListing>* listing);
+ virtual Status DoGet(const FlightCallOptions& options, const Ticket& ticket,
+ std::unique_ptr<ClientDataStream>* stream);
+ virtual Status DoPut(const FlightCallOptions& options,
+ std::unique_ptr<ClientDataStream>* stream);
+ virtual Status DoExchange(const FlightCallOptions& options,
+ std::unique_ptr<ClientDataStream>* stream);
+};
+
+/// A registry of transport implementations.
+class ARROW_FLIGHT_EXPORT TransportRegistry {
+ public:
+ using ClientFactory =
std::function<arrow::Result<std::unique_ptr<ClientTransport>>()>;
+ using ServerFactory =
std::function<arrow::Result<std::unique_ptr<ServerTransport>>(
+ FlightServerBase*, std::shared_ptr<MemoryManager> memory_manager)>;
+
+ TransportRegistry();
+ ~TransportRegistry();
+
+ arrow::Result<std::unique_ptr<ClientTransport>> MakeClient(const
std::string& scheme);
+ arrow::Result<std::unique_ptr<ServerTransport>> MakeServer(
+ const std::string& scheme, FlightServerBase* base,
+ std::shared_ptr<MemoryManager> memory_manager);
Review comment:
`const` member function?
--
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]