Skip to content

GH-50282: [C++][FlightRPC] Refactor gRPC server and transport classes - #50407

Merged
raulcd merged 35 commits into
apache:mainfrom
Alex-PLACET:refactor/internal-transport
Oct 21, 2026
Merged

raulcd merged 35 commits into
apache:mainfrom
Alex-PLACET:refactor/internal-transport

Conversation

@Alex-PLACET

@Alex-PLACET Alex-PLACET commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

The goal of the pullrequest is to refactor the FligtRPC code to prepare the async implementation.

What changes are included in this PR

  • extracts server.cc/transport_server.cc internals into transport_server_internal.{h,cc} and the gRPC-specific call context, auth/middleware helper, and endpoint/builder setup into transport/grpc/grpc_server_internal.{h,cc}, templated on the gRPC context type so ServerContext and CallbackServerContext can share one implementation.
  • Moves signal-driven shutdown into internal::ServerSignalState, fixing teardown ordering so handlers are restored even when Serve() fails early, a SIGPIPE race when waking the signal thread, and shutdown of an uninitialized server clearing another serving instance's signal target. (Github Copilot issue report)
  • Adds public Location::uri() and Location::port(), and includes arrow/util/uri.h from types.h so the Result<util::Uri> return type is instantiable by consumers of the installed header. (Github Copilot issue report in the PR)
  • Wait() on an uninitialized server now returns Status::Invalid instead of dereferencing null. No other public behavior change. ServerTransport, TransportMessageReader/Writer, WriteDataStream and friends stay internal. (Github Copilot issue report in the PR)
  • Drops the unused gRPC FlightData message registry.

Are these changes tested?

  • Full CI tests
  • Adds tests covering signal handling and server middleware headers that depend on handler state.

Are there any user-facing changes?

  • New API: Location::uri() → Result<util::Uri>, Location::port() → int (-1 when absent)
  • Compilation fix: types.h now includes arrow/util/uri.h. Before, Result<util::Uri> was uninstantiable for anyone including only the installed header.
  • Behavior: Wait() on an uninitialized server returns Status::Invalid (was a null deref). Serve() restores prior signal handlers on failure. Shutdown() on an uninitialized server no longer disables signal shutdown for another serving instance, signal thread wake no longer risks SIGPIPE-killing the process. (Github Copilot issue report in the PR)

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI
    Reviewed before submission by:
  • Human
  • AI
  • [] Not reviewed

@github-actions github-actions Bot added the awaiting review Awaiting review label Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@Alex-PLACET Alex-PLACET changed the title GH-#50282: [C++][FlightRPC] Refactor GRPC server and transport classes GH-50282: [C++][FlightRPC] Refactor GRPC server and transport classes Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #50282 has been automatically assigned in GitHub to PR creator.

@Alex-PLACET

Copy link
Copy Markdown
Contributor Author

@raulcd Can you launch the CI please

@Alex-PLACET

Copy link
Copy Markdown
Contributor Author

@raulcd Can you run the CI again please?

@Alex-PLACET

Copy link
Copy Markdown
Contributor Author

@raulcd Can you launch the CI please

@Alex-PLACET

Copy link
Copy Markdown
Contributor Author

@raulcd Can you launch the CI please (I hope it will be the last time)

@raulcd

raulcd commented Jul 22, 2026

Copy link
Copy Markdown
Member

@raulcd Can you launch the CI please (I hope it will be the last time)

Sorry @Alex-PLACET , I missed this comment. Just kicked it

@Alex-PLACET
Alex-PLACET force-pushed the refactor/internal-transport branch 2 times, most recently from 560bd8d to 20c2325 Compare July 23, 2026 11:41
Comment thread ci/docker/ubuntu-22.04-cpp.dockerfile Outdated
Comment thread cpp/src/arrow/flight/transport/grpc/customize_grpc.h Outdated
@raulcd raulcd added the CI: Extra: Package: Linux Run extra Linux Packages CI label Jul 31, 2026
@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting review Awaiting review labels Jul 31, 2026

@Alex-PLACET Alex-PLACET Sep 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code was moved from transport_server.cc

std::vector<std::shared_ptr<ServerMiddleware>> middleware_;
std::unordered_map<std::string, std::shared_ptr<ServerMiddleware>> middleware_map_;
CallHeaders incoming_headers_;
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions github-actions Bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Sep 2, 2026
Comment on lines -586 to -621
// Allow uploading messages of any length
builder.SetMaxReceiveMessageSize(-1);

const std::string scheme = uri.scheme();
int port = 0;
if (scheme == kSchemeGrpc || scheme == kSchemeGrpcTcp || scheme == kSchemeGrpcTls) {
std::stringstream address;
address << arrow::util::UriEncodeHost(uri.host()) << ':' << 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, &port);
} else if (scheme == kSchemeGrpcUnix) {
std::stringstream address;
address << "unix:" << uri.path();
builder.AddListeningPort(address.str(), ::grpc::InsecureServerCredentials());
location_ = options.location;
} else {
return Status::NotImplemented("Scheme is not supported: " + scheme);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to a dedicated function: AddServerListeningPort

Comment on lines +38 to +74
Status AddServerListeningPort(const FlightServerOptions& options,
const arrow::util::Uri& uri, ::grpc::ServerBuilder* builder,
Location* location, int* port) {
const std::string scheme = uri.scheme();
if (scheme == kSchemeGrpc || scheme == kSchemeGrpcTcp || scheme == kSchemeGrpcTls) {
std::stringstream address;
address << arrow::util::UriEncodeHost(uri.host()) << ':' << 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, port);
return Status::OK();
}
if (scheme == kSchemeGrpcUnix) {
std::stringstream address;
address << "unix:" << uri.path();
builder->AddListeningPort(address.str(), ::grpc::InsecureServerCredentials());
*location = options.location;
return Status::OK();
}
return Status::NotImplemented("Scheme is not supported: " + scheme);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from grpc_server.cc

}

namespace {
class TransportIpcMessageReader : public ipc::MessageReader {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to transport_server_internal.cc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code comes from server.cc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code moved to grpc_server_internal cpp and h

@Alex-PLACET
Alex-PLACET marked this pull request as ready for review September 17, 2026 14:40
Copilot AI lite review requested due to automatic review settings September 17, 2026 14:40
@Alex-PLACET
Alex-PLACET force-pushed the refactor/internal-transport branch from 6bafb10 to 6c2c010 Compare October 20, 2026 10:51
@github-actions github-actions Bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Oct 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raulcd raulcd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failures are unrelated. I have reviewed changes and validated the parts that have been moved around.
Thanks @Alex-PLACET for the PR LGTM.

@lidavidm can you take a look again?

@kou maybe you are interested in a quick look

@github-actions github-actions Bot added awaiting merge Awaiting merge and removed awaiting change review Awaiting change review labels Oct 20, 2026

@kou kou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Could you use "gRPC" not "GRPC" in PR title?

Is the current PR description updated? If not, could you update the PR description for the latest changes?

/// \param[in] not_started_message Logged if shutdown is called before the server
/// starts. \param[in] shutdown_warning Logged if shutdown itself fails.
template <typename WaitFn, typename ShutdownFn>
Status Serve(WaitFn&& wait, ShutdownFn&& shutdown, const char* not_started_message,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that not_started_message isn't used. Can we remove it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 removed

@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting merge Awaiting merge labels Oct 21, 2026

@lidavidm lidavidm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for working through this!

@github-actions github-actions Bot added awaiting merge Awaiting merge and removed awaiting changes Awaiting changes labels Oct 21, 2026
@raulcd raulcd changed the title GH-50282: [C++][FlightRPC] Refactor GRPC server and transport classes GH-50282: [C++][FlightRPC] Refactor gRPC server and transport classes Oct 21, 2026
Copilot AI lite review requested due to automatic review settings October 21, 2026 08:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Adapt FinishRequest status handling for grpc::CallbackServerContext before approval.

3 open issues
1 resolved since last review

Review effort: Lite

@Alex-PLACET

Copy link
Copy Markdown
Contributor Author

@kou The description is up to date now and I replaced the GRPC->gRPC in both the issue and this PR.

@raulcd raulcd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the failures are related. The two comments have been tackled. I am going to merge and if there's any follow up I am happy to tackle it.

@raulcd
raulcd merged commit 373d64c into apache:main Oct 21, 2026
115 of 124 checks passed
@raulcd raulcd removed the awaiting merge Awaiting merge label Oct 21, 2026
@github-actions github-actions Bot added the awaiting merge Awaiting merge label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants