Repository navigation
RetryHttpClient to enable Retry for UC - #1145
Conversation
1b95f22 to
4cbfa42
Compare
Signed-off-by: TimothyW553 <[email protected]>
4cbfa42 to
9cc912b
Compare
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); |
There was a problem hiding this comment.
This is actually some kind of intercepter that defined by our own.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
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]>
Signed-off-by: TimothyW553 <[email protected]>
| client.setHost(url.getHost()) | ||
| .setScheme(url.getScheme()) | ||
| .setPort(url.getPort()) | ||
| .setBasePath(url.getPath()); |
There was a problem hiding this comment.
To preserve same behaviour as current getApiClient, basePath should have something like api/unity-catalog/2.1 as suffix on url.getPath.
There was a problem hiding this comment.
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]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
| 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes, good point. Added explanations for these parameters, their units, and how they are used in calculating the delay.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
there is not test testing jitter?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
is multiplier tested?
is initial delay tested?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
ah yes that's true, thanks for pointing that out. updated.
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
| long maxDelay = (long) Math.ceil(baseDelay * (1 + jitterFactor)); | ||
| assertThat(elapsedMs) | ||
| .as("retry delay should stay within jitter-adjusted bounds") | ||
| .isBetween(minDelay, maxDelay); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
For this PR, let's just focus on the key things (not the jitter retry algorithm):
- The general retry framework, we choose to use the
HttpRetryHandlerto handle the general http request retry. - 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.
There was a problem hiding this comment.
why will anything be flaky? the whole thing is with manual clock.. isnt it?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- The current PR just merge the http retry change. ( with fixed sleep time directly).
- The next PR replace the
fixed sleep timewithjitter time, also add jitter full coverage.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
LGTM. Lets follow up on the jitter question.
|
Created backport PR for
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 |
|
@timothyw553 tried to autocreate the backport PR .. but it had conflicts. so i closed that PR. can you make a 0.3 backport PR? |
|
@tdas yes will do! |
**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)
**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]>
**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]>
PR Checklist
docsis updatedDescription 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.