Skip to content

RetryHttpClient to enable Retry for UC - #1145

Merged
tdas merged 42 commits into
unitycatalog:mainfrom
timothyw553:1111-retryHttpClient
Nov 12, 2025
Merged

tdas merged 42 commits into
unitycatalog:mainfrom
timothyw553:1111-retryHttpClient

Conversation

@timothyw553

@timothyw553 timothyw553 commented Oct 29, 2025 •

Copy link
Copy Markdown
Collaborator

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

Instead of wrapping each type of Api such as TempCredsApi or TablesApi with a "Retryable" version, we instead create our own RetryableHttpClient and retry at the http level. This way, new Apis just need to pass the necessary configuration to the ApiClientFactory, which creates a RetryingApiClient, which then creates a RetryingHttpClient. The user may pass in a custom interceptor -- a function that defines under which error codes, status codes, and exception they would want the ApiClient to retry.

Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>

for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
HttpResponse<T> response = delegate.send(request, handler);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is actually some kind of intercepter that defined by our own.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Will it be more simpler for us to use the apache-httpclient or OkhttpClient's default Interceptor API do the retry ?

The okhttpclient seems have the best API to do that, but need to introduce the extra okhttpclient. The apache-httpclient also have the API to do that, but not sure if it's feasible to use the HttpRequestRetryHandler to do this.

Signed-off-by: TimothyW553 <[email protected]>
* Interface for determining whether an HTTP request should be retried.
*
* <p>This interface allows users to implement custom retry logic for both
* exceptions and HTTP responses. Implementations should be stateless and thread-safe.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@tdas tdas Nov 8, 2025 •

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.

I understand why RetryingApiClient is needed. but why do we need this interface for 1 implementation? why not directly have the RetryHandler class be what you have defined as DefaultRetryHandler?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The original intent was to allow future API clients to use their own custom retry handler, but in most cases they would probably use the DefaultRetryHandler so I agree.

We can have just the RetryHandler class as the DefaultRetryHandler - if in the future anyone wants to use a custom one, it would be easy to refactor.

@tdas tdas Nov 9, 2025 •

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.

exactly. this is premature optimization. unless there is a concrete need to create an interface, its premature to create one. Especially when the functionality is so narrow that we can easily add interface when we have that concrete need for multiple implementations. until then.. unnecessary interfaces are just code bloat.

Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
@timothyw553
timothyw553 marked this pull request as ready for review October 30, 2025 23:37
@timothyw553
timothyw553 requested a review from openinx October 30, 2025 23:37
@timothyw553 timothyw553 changed the title RetryHttpClient prototype RetryHttpClient to enable Retry for UC Oct 30, 2025
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
client.setHost(url.getHost())
.setScheme(url.getScheme())
.setPort(url.getPort())
.setBasePath(url.getPath());

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.

To preserve same behaviour as current getApiClient, basePath should have something like api/unity-catalog/2.1 as suffix on url.getPath.

@timothyw553 timothyw553 Oct 31, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thank you! Yes, I've removed it to comply with the behaviour of current getApiClient

Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
@timothyw553
timothyw553 requested a review from tdas November 10, 2025 19:50
Comment on lines +14 to +17
public static final int DEFAULT_REQUEST_MAX_ATTEMPTS = 3;
public static final long DEFAULT_REQUEST_INITIAL_DELAY_MS = 500L;
public static final double DEFAULT_REQUEST_MULTIPLIER = 2.0;
public static final double DEFAULT_REQUEST_JITTER_FACTOR = 0.5;

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.

Not obvious to a reader what each of these configurations means for the request. i understand max attempts, but what is "initial" delay? what is multiplier? multiplies what?
what is jitter factor? jittering over what baseline? whats the unit?

@timothyw553 timothyw553 Nov 11, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, good point. Added explanations for these parameters, their units, and how they are used in calculating the delay.

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.

Sorry.. one more comment.. let move these descriptions either class docs, or the method docs of this class. So that these show up the java docs in IDEs and stuff.

.setRequestMaxAttempts(2)
.setRequestInitialDelayMs(50L)
.setRequestMultiplier(1.0)
.setRequestJitterFactor(0.0); // Disable jitter

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.

there is not test testing jitter?

@timothyw553 timothyw553 Nov 11, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right, just added testing with jitter. The test involves going through the normal retrying while asserting that, with a set jitter factor, the time elapsed is within the expected range.

It should be stable as it uses the ManualClock, so the only random part is the jitter, and we assert the elapsed time stays within the expected [baseDelay * (1 ± jitter)] window. Because the jitter factor is bounded and the clock is deterministic, the test shouldn’t flake.

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.

is multiplier tested?
is initial delay tested?

@timothyw553 timothyw553 Nov 11, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

With jitter disabled the tests expect the manual clock to advance by the exact delays computed from the initial delay and multiplier (e.g. 100 ms + 200 ms, or 50 ms + 100 ms). If either value (multiplier or initial delay) were ignored or misapplied, those assertions would fail

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.

for initial delay i see that different tests have different delay .. so across teh tests its getting tested that setting the initial delay to different things work correctly.

for multiplier, i see the previous test is setting it to 1. but the previous test is succeeding in only one attempt to multiplier does not kick in. so effectively you are testing multiple with only 1 value = 2? like.. if your internal code is hard coded to 2 and the conf is ignored, all your tests will pass right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

ah yes that's true, thanks for pointing that out. updated.

long maxDelay = (long) Math.ceil(baseDelay * (1 + jitterFactor));
assertThat(elapsedMs)
.as("retry delay should stay within jitter-adjusted bounds")
.isBetween(minDelay, maxDelay);

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.

this does not ensure that the jitter is actually being applied. for example, if the jitter was not applied and the delay is actually 100ms, then this assert will still pass right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That's a good point. Another approach that @openinx and I thought of is to let the retry handler take an injected sequence of jitter values, so tests can assert the exact delay that gets applied and fail if jitter isn’t honored.

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.

cant you just test with and without jitter? without jitter the clock will be exactly 100ms delayed. with jitter it will be != 100ms.

isnt that good enough?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hi @tdas , I think with the jiter tests we use !=100ms can be easily flaky, since it's possible to be 100ms if the Math.random() get the number.

Maybe we can remove all the jitter retry policy in this PR, and just use the fixed sleep time to make this PR merged. because currently, seems like the PR involves complicated context enough. And adding the end-to-end jiter sleep test make the retry policy and the retry behavirour even more coupled.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For this PR, let's just focus on the key things (not the jitter retry algorithm):

  1. The general retry framework, we choose to use the HttpRetryHandler to handle the general http request retry.
  2. What are the recoverable codes and exceptional error that we need to cover in this case.

For the jitter retry algorithm, let's try to address in the next PR ? then we can try to create a clean and decoupling design (I mean decouple the retry algorithm itself from the retry handler ) to fully address all the test cases that you concerned.

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.

why will anything be flaky? the whole thing is with manual clock.. isnt it?

@timothyw553 timothyw553 Nov 12, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Math.random() could return exactly 0.5, which makes the jitter term zero and the delay stay at 100 ms. That’s vanishingly rare, but it means a "delay != 100 ms" assertion can flake. I guess if we agree that it's basically impossible and can ignore it, it should be totally fine and valid.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Either we use the random sequence or test-customized sequence (some kind of mocking random sequence) to test the jitter. If we use the random then it's possible to be flaky and also hard to have the accurate sleep time assertion. If we use the test-customized sequence to test the jitter, then we finally need to decouple the jitter algorithm out of the http retry framework.

That's why I suggest to make it into a separate PR to make this forward:

  1. The current PR just merge the http retry change. ( with fixed sleep time directly).
  2. The next PR replace the fixed sleep time with jitter time, also add jitter full coverage.

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.

I see the how flakiness can happen. basically with there is 1 in 100 chance (with 100 ms and jitterfactor = 0.5) that we get exactly 100ms. so 1 in 100 chance that the test for 100ms will fail.

Thanks for pointing it out.

While I agree the absolutely ideal thing to do is what @openinx suggested ... i am also concerned whether it is worth the testing complexity. Lets just leave it as is in this PR. i am happy to approve. in a follow up PR, pull out the jitter stuff into a separate jitter generator class that can be independently tested, and also reused to generate jitter in other places (like leadTimeMillis).

How does that sound?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that sounds like a good idea. I have some follow up ideas/conerns that I'll leave in the next iteration.

Signed-off-by: Timothy Wang <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>

@tdas tdas 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.

LGTM. Lets follow up on the jitter question.

@tdas
tdas merged commit 71837e9 into unitycatalog:main Nov 12, 2025
@tdas

tdas commented Nov 12, 2025

Copy link
Copy Markdown
Contributor

Created backport PR for branch-0.3:

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin backport-1145-to-branch-0.3
git worktree add --checkout .worktree/backport-1145-to-branch-0.3 backport-1145-to-branch-0.3
cd .worktree/backport-1145-to-branch-0.3
git reset --hard HEAD^
git cherry-pick -x 71837e9d2556cc4d740b249c5dc4eb7dd574d32e
git push --force-with-lease

@tdas

tdas commented Nov 12, 2025

Copy link
Copy Markdown
Contributor

@timothyw553 tried to autocreate the backport PR .. but it had conflicts. so i closed that PR. can you make a 0.3 backport PR?

@timothyw553

Copy link
Copy Markdown
Collaborator Author

@tdas yes will do!

timothyw553 added a commit to timothyw553/unitycatalog that referenced this pull request Nov 12, 2025
**PR Checklist**

- [ ] A description of the changes is added to the description of this
PR.
- [ ] If there is a related issue, make sure it is linked to this PR.
- [ ] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Instead of wrapping each type of Api such as TempCredsApi or TablesApi
with a "Retryable" version, we instead create our own
RetryableHttpClient and retry at the http level. This way, new Apis just
need to pass the necessary configuration to the ApiClientFactory, which
creates a RetryingApiClient, which then creates a RetryingHttpClient.
The user may pass in a custom interceptor -- a function that defines
under which error codes, status codes, and exception they would want the
ApiClient to retry.

<!-- Please state what you've changed and how it might affect the users.
-->

---------

Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>
(cherry picked from commit 71837e9)
openinx added a commit to openinx/unitycatalog that referenced this pull request Nov 14, 2025
**PR Checklist**

- [ ] A description of the changes is added to the description of this
PR.
- [ ] If there is a related issue, make sure it is linked to this PR.
- [ ] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Instead of wrapping each type of Api such as TempCredsApi or TablesApi
with a "Retryable" version, we instead create our own
RetryableHttpClient and retry at the http level. This way, new Apis just
need to pass the necessary configuration to the ApiClientFactory, which
creates a RetryingApiClient, which then creates a RetryingHttpClient.
The user may pass in a custom interceptor -- a function that defines
under which error codes, status codes, and exception they would want the
ApiClient to retry.

Original author is: TimothyW553 <[email protected]>
Original pull request is: unitycatalog#1145

---------

Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>
Signed-off-by: Zheng Hu <[email protected]>
tdas pushed a commit that referenced this pull request Nov 14, 2025
**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Backport [PR
change](#1145) of
[issue](#1111) to
branch-0.3
The whole backport progress is here:
#1163


Original author is: TimothyW553 <[email protected]>
Original pull request is:
#1145

Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: Timothy Wang <[email protected]>
Signed-off-by: Zheng Hu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants