cyb70289 commented on a change in pull request #11524: URL: https://github.com/apache/arrow/pull/11524#discussion_r737122290
########## File path: cpp/examples/arrow/flight_grpc_example.cc ########## @@ -0,0 +1,100 @@ +// 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 <signal.h> +#include <cstdlib> +#include <iostream> + +#include <arrow/api.h> +#include <arrow/flight/api.h> +#include <gflags/gflags.h> +#include <grpc++/grpc++.h> + +#include "examples/arrow/helloworld.grpc.pb.h" +#include "examples/arrow/helloworld.pb.h" + +// Demonstrate registering a gRPC service alongside a Flight service +// +// The gRPC service can be accessed with a gRPC client, on the same +// port as the Flight service. Additionally, the CMake config for this +// example links against the gRPC reflection library, enabling tools +// like grpc_cli and grpcurl to list and call RPCs on the server +// without needing local copies of the Protobuf definitions. +// For example, with grpcurl (https://github.com/fullstorydev/grpcurl): +// +// grpcurl -d '{"name": "Rakka"}' -plaintext localhost:31337 HelloWorldService/SayHello + +DEFINE_int32(port, -1, "Server port to listen on"); + +namespace flight = ::arrow::flight; + +#define ABORT_ON_FAILURE(expr) \ + do { \ + arrow::Status status_ = (expr); \ + if (!status_.ok()) { \ + std::cerr << status_.message() << std::endl; \ + abort(); \ + } \ + } while (0); + +// Flight service +class SimpleFlightServer : public flight::FlightServerBase {}; + +// gRPC service +class HelloWorldServiceImpl : public HelloWorldService::Service { + grpc::Status SayHello(grpc::ServerContext* ctx, const HelloRequest* request, + HelloResponse* reply) override { + const std::string& name = request->name(); + if (name.empty()) { + return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Must provide a name!"); + } + reply->set_reply("Hello, " + name); + return grpc::Status::OK; + } +}; + +int main(int argc, char** argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (FLAGS_port < 0) { + // For CI + std::cout << "Must specify a port with -port" << std::endl; + return EXIT_SUCCESS; + } + + std::unique_ptr<flight::FlightServerBase> server; + server.reset(new SimpleFlightServer()); + + flight::Location bind_location; + ABORT_ON_FAILURE(flight::Location::ForGrpcTcp("0.0.0.0", FLAGS_port, &bind_location)); + flight::FlightServerOptions options(bind_location); + + HelloWorldServiceImpl grpc_service; + int extra_port = 0; + + options.builder_hook = [&](void* raw_builder) { + auto* builder = reinterpret_cast<grpc::ServerBuilder*>(raw_builder); + builder->AddListeningPort("0.0.0.0:0", grpc::InsecureServerCredentials(), + &extra_port); + builder->RegisterService(&grpc_service); + }; + ABORT_ON_FAILURE(server->Init(options)); Review comment: I'm getting a crash from grpc: https://github.com/grpc/grpc/blob/v1.35.0/src/core/lib/iomgr/resolve_address.cc#L48 `grpc_resolve_address_impl` is nullptr. Please **note** I commented out grpc++_reflection in cmake and build with bundled grpc v1.35. Build with `cmake -GNinja -DARROW_FLIGHT=ON -DCMAKE_BUILD_TYPE=Debug -DARROW_BUILD_EXAMPLES=ON ..` Test command `debug/flight_grpc_example -port 8899`. Call stack attached if useful. ``` #0 0x0000555555844c80 in grpc_blocking_resolve_address (name=0x555555f07b28 "0.0.0.0:0", default_port=0x555555bf6706 "https", addresses=0x7fffffffd8c8) at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/core/lib/iomgr/resolve_address.cc:48 #1 0x0000555555962e0a in grpc_core::<lambda()>::operator()(void) const (__closure=0x7fffffffd910) at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/core/ext/transport/chttp2/server/chttp2_server.cc:533 #2 0x00005555559633ed in grpc_core::Chttp2ServerAddPort (server=0x555555f02710, addr=0x555555f07b28 "0.0.0.0:0", args=0x555555f04a60, port_num=0x7fffffffd98c) at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/core/ext/transport/chttp2/server/chttp2_server.cc:575 #3 0x000055555581b835 in grpc_server_add_insecure_http2_port (server=0x555555f026f0, addr=0x555555f07b28 "0.0.0.0:0") at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/core/ext/transport/chttp2/server/insecure/server_chttp2.cc:35 #4 0x00005555555bd744 in grpc::(anonymous namespace)::InsecureServerCredentialsImpl::AddPortToServer (this=0x555555efe250, addr="0.0.0.0:0", server=0x555555f026f0) at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/cpp/server/insecure_server_credentials.cc:29 #5 0x00007ffff765815d in grpc::Server::AddListeningPort (this=0x555555f01ff0, addr="0.0.0.0:0", creds=0x555555efe250) at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/cpp/server/server_cc.cc:1100 #6 0x00007ffff7647ee5 in grpc::ServerBuilder::BuildAndStart (this=0x7fffffffdd10) at /home/cyb/arrow/cpp/debug/grpc_ep-prefix/src/grpc_ep/src/cpp/server/server_builder.cc:399 #7 0x00007ffff760c305 in arrow::flight::FlightServerBase::Init (this=0x555555efee70, options=...) at ../src/arrow/flight/server.cc:963 #8 0x00005555555a4386 in main (argc=1, argv=0x7fffffffe308) at ../examples/arrow/flight_grpc_example.cc:95 ``` -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org