Skip to content

Optimize Optional usage and improve code consistency in gateway-ha module #712

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

Closed
wants to merge 0 commits into from

Conversation

Akanksha-kedia
Copy link
Contributor

Description

This commit introduces several optimizations to improve code quality, readability, and consistency in the gateway-ha module. The changes focus on standardizing Optional usage patterns, improving method naming consistency, and applying functional programming patterns where appropriate.

These optimizations maintain the existing functionality while making the code more maintainable and aligned with modern Java practices. The changes are non-invasive and preserve the overall structure of the codebase.

Files Changed

gateway-ha/src/main/java/io/trino/gateway/ha/router/HaResourceGroupsManager.java

  • Updated updateSelector method to use Optional.ofNullable() with ifPresentOrElse() for consistent null handling
  • Optimized upcast method to use stream API for consistency with other similar methods
  • Enhanced getExactMatchSourceSelector to use Optional for null safety

gateway-ha/src/main/java/io/trino/gateway/ha/router/QueryCountBasedRouter.java

  • Simplified the method by eliminating unnecessary intermediate variable

gateway-ha/src/main/java/io/trino/gateway/ha/handler/RoutingTargetHandler.java, LbFilter.java

  • Eliminated unnecessary intermediate variables for better memory efficiency

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required, with the following suggested text:

* Fix some things.

@Akanksha-kedia
Copy link
Contributor Author

@ebyhr please help to review. I want to contribute this project starting with refactoring

Optional.ofNullable(dao.findFirstById(resourceGroup.getResourceGroupId()))
.ifPresentOrElse(
_ -> dao.update(resourceGroup),
() -> dao.create(resourceGroup));
Copy link
Member

Choose a reason for hiding this comment

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

I prefer the original code. The new code has redundant indentations.

@@ -263,8 +253,9 @@ public List<ExactSelectorsDetail> readExactMatchSourceSelector()
public ExactSelectorsDetail getExactMatchSourceSelector(
ExactSelectorsDetail exactSelectorDetail)
{
ExactMatchSourceSelectors exactSelector = exactMatchSourceSelectorsDao.findFirst(exactSelectorDetail);
return upcastExactSelectors(exactSelector);
return Optional.ofNullable(exactMatchSourceSelectorsDao.findFirst(exactSelectorDetail))
Copy link
Member

Choose a reason for hiding this comment

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

We should change the return type of findFirst() method instead.

Copy link
Member

Choose a reason for hiding this comment

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

#716 removed this code block. Could you rebase on main to resolve conflicts?

@Chaho12
Copy link
Member

Chaho12 commented Jul 1, 2025

This close & push 0 commit is not by mistake i assume?

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