Skip to content

Extracts the transformation logic on Future instances into a dedicated class #688

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 1 commit into
base: main
Choose a base branch
from

Conversation

yaelselig30
Copy link

@yaelselig30 yaelselig30 commented May 22, 2025

Description

This PR extracts the transformation logic on Future instances into a dedicated class to improve readability, enable easier extension, and make the logic more testable.

Additional context and related issues

Changes:

  • Introduced a new class, responsible for applying these transformations.
  • Moved transformation logic out of the handler to this class.

Benefits:

  • Easier to test transformation logic independently.
  • Better separation of concerns.
  • More maintainable structure for adding future behavior.

Release notes

(x) This is not user-visible or is docs only, and no release notes are required.

Copy link

cla-bot bot commented May 22, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

Comment on lines 44 to 45
public ProxyResponseTransformer(QueryHistoryManager queryHistoryManager,
RoutingManager routingManager)
Copy link
Member

Choose a reason for hiding this comment

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

The format is wrong. It should be:

    public ProxyResponseTransformer(
            QueryHistoryManager queryHistoryManager,
            RoutingManager routingManager)

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 47 to 51
this.routingManager = requireNonNull(routingManager, "routingManager is null");
this.queryHistoryManager = requireNonNull(queryHistoryManager, "queryHistoryManager is null");
Copy link
Member

Choose a reason for hiding this comment

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

Replace the order of these lines for consistency with constructor parameter and fields.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 39 to 43
private static final Logger log = Logger.get(ProxyRequestHandler.class);
private final QueryHistoryManager queryHistoryManager;
Copy link
Member

Choose a reason for hiding this comment

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

Add an empty line to separate static final and final.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 51 to 60
public ProxyResponse transform(Request request, ProxyResponse response, @Nullable String username, RoutingDestination routingDestination)
{
return recordBackendForQueryId(request, response, username, routingDestination);
}

private ProxyResponse recordBackendForQueryId(Request request, ProxyResponse response,
Copy link
Member

Choose a reason for hiding this comment

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

Merge these methods into one.

Copy link
Author

Choose a reason for hiding this comment

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

The main reason for introducing ProxyResponseTransformer is to make it easier to extend the functionality of the FluentFuture.
With that in mind, I believe it's better to separate the transform method from the internal helper methods it relies on.
@ebyhr What do you think?


if (response.statusCode() == OK.getStatusCode()) {
try {
HashMap<String, String> results = OBJECT_MAPPER.readValue(response.body(), HashMap.class);
Copy link
Member

Choose a reason for hiding this comment

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

Fix warning or add //noinspection unchecked.

Comment on lines 74 to 75
catch (IOException e) {
log.error("Failed to get QueryId from response [%s] , Status code [%s]", response.body(), response.statusCode());
Copy link
Member

Choose a reason for hiding this comment

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

Please include e in the logger message.

log.error(e, "Failed to get QueryId from response [%s] , Status code [%s]", response.body(), response.statusCode());

@ebyhr
Copy link
Member

ebyhr commented May 22, 2025

extract future of handle response to a new class

The commit title should start with an uppercase character: https://trino.io/development/process#pull-request-and-commit-guidelines-

@yaelselig30 yaelselig30 force-pushed the yael/refactor-proxy-request-handler branch from 9a921d9 to 21a7937 Compare May 25, 2025 08:18
Copy link

cla-bot bot commented May 25, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

@yaelselig30 yaelselig30 force-pushed the yael/refactor-proxy-request-handler branch from 21a7937 to 2fadbb7 Compare May 25, 2025 08:34
Copy link

cla-bot bot commented May 25, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

@yaelselig30 yaelselig30 requested a review from ebyhr May 25, 2025 08:55

public class ProxyResponseTransformer
{
private static final Logger log = Logger.get(ProxyRequestHandler.class);
Copy link
Author

Choose a reason for hiding this comment

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

change to ProxyResponseTransformer
private static final Logger log = Logger.get(ProxyResponseTransformer.class);


@Inject
public ProxyRequestHandler(
@ForProxy HttpClient httpClient,
RoutingManager routingManager,
QueryHistoryManager queryHistoryManager,
ProxyResponseTransformer proxyResponseTransformer,
Copy link
Member

Choose a reason for hiding this comment

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

I don't see how is ProxyResponseTransformer being provided

@ebyhr ebyhr requested a review from Copilot June 2, 2025 07:44
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extracts the transformation logic on Future instances into the new ProxyResponseTransformer class to improve readability and testability. Key changes include the extraction of query transformation logic from ProxyRequestHandler, removal of the inner ProxyResponse record in favor of a dedicated file, and overall cleanup of redundant logic.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyResponseTransformer.java New class for handling transformation and query detail recording
gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyResponseHandler.java Removed inner ProxyResponse record and tidied up unused imports
gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyResponse.java New record to represent proxy responses
gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyRequestHandler.java Refactored to utilize the new ProxyResponseTransformer and removed redundant logic


public class ProxyResponseTransformer
{
private static final Logger log = Logger.get(ProxyRequestHandler.class);
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

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

Logger in ProxyResponseTransformer should reference ProxyResponseTransformer.class instead of ProxyRequestHandler.class to ensure accurate logging context.

Suggested change
private static final Logger log = Logger.get(ProxyRequestHandler.class);
private static final Logger log = Logger.get(ProxyResponseTransformer.class);

Copilot uses AI. Check for mistakes.

QueryHistoryManager.QueryDetail queryDetail = new QueryHistoryManager.QueryDetail();
queryDetail.setBackendUrl(getRemoteTarget(request.getUri()));
queryDetail.setCaptureTime(System.currentTimeMillis());
if (Strings.isNullOrEmpty(username)) {
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

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

The condition appears inverted; username should be set only if it is not null or empty (i.e., use if (!Strings.isNullOrEmpty(username))).

Suggested change
if (Strings.isNullOrEmpty(username)) {
if (!Strings.isNullOrEmpty(username)) {

Copilot uses AI. Check for mistakes.

This change moves the transformation logic applied to Future instances
into a separate class, making it easier to extend and maintain. It also
helps clarify the responsibilities of each component involved.
@yaelselig30 yaelselig30 force-pushed the yael/refactor-proxy-request-handler branch from 2fadbb7 to 79c9b5d Compare June 3, 2025 14:18
Copy link

cla-bot bot commented Jun 3, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

@@ -83,6 +85,7 @@ public class HaGatewayProviderModule
protected void configure()
{
jaxrsBinder(binder()).bindInstance(resourceSecurityDynamicFeature);
bind(ProxyResponseTransformer.class).in(Scopes.SINGLETON);
Copy link
Member

Choose a reason for hiding this comment

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

can we use optionalBinder() so that others can override it?

Suggested change
bind(ProxyResponseTransformer.class).in(Scopes.SINGLETON);
newOptionalBinder(binder(), ProxyResponseTransformer.class).setBinding()
.to(ProxyResponseTransformer.class)
.in(Scopes.SINGLETON);

or we can make ProxyResponseTransformer into an interface and have different impl...

@ebyhr
Copy link
Member

ebyhr commented Jun 17, 2025

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label Jun 17, 2025
Copy link

cla-bot bot commented Jun 17, 2025

The cla-bot has been summoned, and re-checked this pull request!

@andythsu
Copy link
Member

Please resolve merge conflict @yaelselig30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants