lidavidm commented on a change in pull request #11524: URL: https://github.com/apache/arrow/pull/11524#discussion_r738706628
########## 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: The issue was that by default, the example would link both to libarrow_flight.so and the static gRPC libraries, violating ODR. The solution is to link to static libarrow_flight.a if we're using static gRPC. I tried building gRPC dynamically, but that poses issues because then the gRPC Protobuf plugin can't find libgrpc. -- 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