Skip to content

GH-46315 [C#] Apache Arrow Flight Middleware #46316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

HackPoint
Copy link
Contributor

@HackPoint HackPoint commented May 5, 2025

Enhancement: Apache Arrow Flight Middleware in C#

Overview

This Pull Request enhances middleware support for Apache Arrow Flight using C#, focusing on improved metadata header management and propagation for better observability and extensibility. It also provides handling for HTTP/HTTPS communication.

Rationale for this Change

Effective middleware is critical for managing metadata headers, ensuring accurate request/response handling, and simplifying debugging in distributed systems. By improving middleware capabilities, we enhance reliability and observability, significantly benefiting developers and operational teams managing complex Flight-based applications.

What's Included in this PR?

  • Middleware enhancements supporting complete metadata header propagation.
  • Middleware lifecycle hooks for better request/response management.
  • Comprehensive integration tests validating middleware functionality.
  • Documentation updates reflecting middleware improvements.

Key Features

  • Complete Header Propagation: Ensures accurate propagation of gRPC metadata headers throughout middleware lifecycle events.
  • HTTP/HTTPS Handling: Supports middleware integration and metadata propagation for HTTP and HTTPS communications.
  • Middleware Lifecycle Management: Supports reliable middleware hooks (OnBeforeSendingHeaders, OnHeadersReceived, OnCallCompleted).
  • Enhanced Testing: Adds comprehensive integration tests to verify correct middleware behavior.

Impact

  • Improves middleware reliability and simplifies debugging.
  • Enhances transparency in gRPC and HTTP/S communication within Flight-based applications.

Are These Changes Tested?

Testing Overview

Unit Tests:

  • Added tests for middleware lifecycle event execution (e.g., OnBeforeSendingHeaders, OnHeadersReceived, OnCallCompleted).
  • Verified internal logic for capturing and storing gRPC metadata headers.

Integration Tests:

  • Tested end-to-end with a real Flight client and in-memory server setup.
  • Validated propagation of custom headers (e.g., x-server-header, Set-Cookie) between client and server.

End-to-End Tests:

  • Simulated real-world Flight requests to ensure headers are processed consistently across middleware layers.
  • Confirmed correct invocation order and middleware behavior under different server responses.

Example Test Cases:

  • Verify that OnHeadersReceived correctly captures server-sent headers.
  • Ensure custom client middleware modifies request headers as expected.
  • Validate that OnCallCompleted is triggered on both success and error cases.

Checklist

  • Implementation completed
  • Tests added and passing

HackPoint and others added 7 commits April 15, 2025 19:11
…ing headers and cookie values

- Implemented tests verifying that CookieMiddleware correctly captures and persists values from both headers and cookies.
- Added CapturingCookieMiddleware and CapturingCookieMiddlewareFactory for validating middleware lifecycle.
- Ensured middleware correctly processes cookie strings and validates persistence throughout the request lifecycle.
@HackPoint HackPoint requested a review from CurtHagenlocher as a code owner May 5, 2025 09:44
Copy link

github-actions bot commented May 5, 2025

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

HackPoint and others added 4 commits May 8, 2025 15:00
…nto feat/flight-csharp-middleware

# Conflicts:
#	csharp/Directory.Build.props
#	csharp/src/Apache.Arrow.Flight/Middleware/ClientCookieMiddleware.cs
#	csharp/src/Apache.Arrow.Flight/Middleware/ClientCookieMiddlewareFactory.cs
#	csharp/src/Apache.Arrow.Flight/Middleware/CookieManager.cs
#	csharp/src/Apache.Arrow.Flight/Middleware/Extensions/FlightMethodParser.cs
#	csharp/src/Apache.Arrow.Flight/Middleware/Extensions/StatusUtils.cs
#	csharp/src/Apache.Arrow.Flight/Middleware/Interceptors/ClientInterceptorAdapter.cs
#	csharp/test/Apache.Arrow.Flight.Tests/MiddlewareTests/ClientCookieMiddlewareTests.cs
#	csharp/test/Apache.Arrow.Flight.Tests/MiddlewareTests/Stubs/ClientCookieMiddlewareMock.cs
…r for cookie middleware

- Remove unused and redundant files
@CurtHagenlocher
Copy link
Contributor

@HackPoint can you please rebase this change now that the other change is checked-in?

@CurtHagenlocher
Copy link
Contributor

@HackPoint can you please rebase this change now that the other change is checked-in?

Alternatively, is it okay if we fork the C# implementation to a separate repo now? Rebasing this change might be complicated enough that we get no real benefit from continuing to use the same repo and this change is the last "pending" one before the proposed fork.

@HackPoint
Copy link
Contributor Author

@HackPoint can you please rebase this change now that the other change is checked-in?

Alternatively, is it okay if we fork the C# implementation to a separate repo now? Rebasing this change might be complicated enough that we get no real benefit from continuing to use the same repo and this change is the last "pending" one before the proposed fork.

Than what's your preferred thing to do? Should I continue with merge and rebase or not?

@CurtHagenlocher
Copy link
Contributor

Than what's your preferred thing to do? Should I continue with merge and rebase or not?

Merge and rebase is fine; I wasn't sure if you were working on this.

@HackPoint
Copy link
Contributor Author

Than what's your preferred thing to do? Should I continue with merge and rebase or not?

Merge and rebase is fine; I wasn't sure if you were working on this.

Sir, I was so glad that you merged the pr , I started the moment you have mentioned this rebase. I almost done and started testing it against our db. The moment I will finish, I'll push the update.

…ddleware

# Conflicts:
#	csharp/src/Apache.Arrow.Flight.Sql/Client/FlightSqlClient.cs
#	csharp/src/Apache.Arrow.Flight.Sql/DoPutResult.cs
#	csharp/src/Apache.Arrow.Flight.Sql/PreparedStatement.cs
#	csharp/src/Apache.Arrow.Flight.Sql/SqlActions.cs
#	csharp/src/Apache.Arrow.Flight.Sql/Transaction.cs
#	csharp/test/Apache.Arrow.Flight.Sql.Tests/FlightSqlClientTests.cs
@HackPoint
Copy link
Contributor Author

@CurtHagenlocher Hi sir, it's completed and all the integration has been done and tested. Please merge it it should be working fine.

@HackPoint
Copy link
Contributor Author

Than what's your preferred thing to do? Should I continue with merge and rebase or not?

Merge and rebase is fine; I wasn't sure if you were working on this.
@CurtHagenlocher Hi, can you please review the PR? Seems like it's missing functionality for our needs.
UPD:
@jeremyosterhoudt

Copy link
Contributor

@CurtHagenlocher CurtHagenlocher left a comment

Choose a reason for hiding this comment

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

Thanks! I left some feedback and some questions.


namespace Apache.Arrow.Flight.Middleware;

public class MetadataAdapter : ICallHeaders
Copy link
Contributor

Choose a reason for hiding this comment

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

This class doesn't appear to be used in this PR. Is it possible it's been superseded by CallHeaders?

}
catch (FormatException ex)
{

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove blank lines here and at line 69

{
outgoingHeaders.Insert(COOKIE_HEADER, cookieValue);
}
_logger.LogInformation("Sending Headers: " + string.Join(", ", outgoingHeaders));
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this log? It doesn't look like any of the types implementing ICallHeaders have overridden ToString. And given that the cookies might include credential information, it's probably not a good idea to log all their values.

Same comment applies to line 53.

using Apache.Arrow.Flight.Middleware.Extensions;
using Apache.Arrow.Flight.Middleware.Interfaces;
using Microsoft.Extensions.Logging;
namespace Apache.Arrow.Flight.Middleware;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: insert a blank line before the namespace declaration


public ClientCookieMiddlewareFactory(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it make more sense to create and hold on to a single ILogger instance instead of creating a new one for every call?

m?.OnHeadersReceived(new CallHeaders(headers));
}

return task.Result;
Copy link
Contributor

Choose a reason for hiding this comment

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

If task.Exception can be not-null, then should this throw the exception instead of returning null?

{
var headers = task.Result;
foreach (var m in middlewares)
m?.OnHeadersReceived(new CallHeaders(headers));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the new CallHeaders(headers) be lifted out of the loop to save allocations?

var headers = await headersTask.ConfigureAwait(false);
foreach (var m in middlewares)
{
m?.OnHeadersReceived(new CallHeaders(headers));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the new CallHeaders(headers) be lifted out of the loop to save allocations?

var response = await responseTask.ConfigureAwait(false);
foreach (var m in middlewares)
{
m?.OnCallCompleted(getStatus(), getTrailers());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the getStatus() and getTrailers() be lifted out of the loop instead of being called repeatedly? If necessary, this could be e.g. var status = middlewares.Count > 0 ? getStatus() : default to avoid the calls when no middleware is present.

}
catch
{
foreach (var m in middlewares)
Copy link
Contributor

Choose a reason for hiding this comment

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

If the very last call to OnCallCompleted throws an exception, we're going to cycle through the list a second time calling all the middleware again. Consider instead catching errors inside of the enumeration and then only bubbling the exception out at the very end -- perhaps wrapping in an AggregateException if there is more than one.

@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 14, 2025
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.

3 participants