Repository navigation
[SDK-714] Add network retry policy config and classifier - #1111
franco-zalamena-iterable wants to merge 3 commits into
Conversation
Adds the public IterableNetworkRetryPolicy and IterableConfig.Builder.setNetworkRetryPolicy(), with retry disabled by default (5 attempts, fixed 2s interval, 60s total deadline), plus a package-private classifier: 5xx and transport failures are retriable, 429 and other 4xx are terminal, JWT 401 goes to the auth path. Nothing reads the policy yet, so request behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Reword CHANGELOG and Javadoc: when the policy is not enabled the existing request retry behaviour is unchanged; code 0 is any client-side failure to get a response - setBackoffPolicy(null) falls back to the fixed 2000 ms default - Make the deadline check overflow-safe - Drop the public BackoffPolicy.Type enum - Add boundary, custom-config and null-handling tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0ea3785 to
eb051bd
Compare
| * behaviour is unchanged. | ||
| */ | ||
| @NonNull | ||
| final IterableNetworkRetryPolicy networkRetryPolicy; |
There was a problem hiding this comment.
SDK-714 calls for integrators to inspect the effective retry policy on a built IterableConfig. The package-private field is visible to the current tests, but app code cannot read it.
Suggest exposing a public getNetworkRetryPolicy() and checking that it is accessible from outside the SDK package, or updating the acceptance criteria, since a getter can always be added later without a breaking change.
| // including connection errors and timeouts. | ||
| @NonNull | ||
| static Outcome classify(int responseCode, @Nullable JSONObject json) { | ||
| if (responseCode >= 200 && responseCode < 300) { |
There was a problem hiding this comment.
classify treats every 2xx as SUCCESS, but IterableRequestTask reports several 2xx outcomes as failures:
- 201–299 →
failure(…, "Received non-200 response")(IterableRequestTask.java:267) - 200 with an unparseable or non-object body → failure (
:255,:259) - 200 with an empty body → failure (
:262) - 200 where reading the body threw an
IOException, e.g. a read timeout → failure (:206-208,:264)
So once SDK-715 wires this in, those failures come back as SUCCESS. Retry decisions are unaffected today, because shouldRetry only acts on RETRIABLE. But the read-timeout case is exactly the kind of failure this policy says it covers, and SDK-716's typed failures would have no category for these failures.
The test at IterableNetworkRetryClassifierTest.kt:36 (299 to Outcome.SUCCESS) locks this in. Code -1 (from getResponseCode()) also falls through to TERMINAL with no comment or test.
Suggest classifying the response the task already builds, which carries the success flag:
@NonNull
static Outcome classify(@NonNull IterableApiResponse response) {
if (response.success) {
return Outcome.SUCCESS;
}
// ...then the existing code/JSON rules, using response.responseCode and response.responseJson
}Then replace the 299 case with a failed non-200 2xx, add a failed 200, and add -1.
| if (deadline == 0) { | ||
| return true; | ||
| } | ||
| long elapsed = Math.max(0L, nowMillis - firstAttemptStartMillis); |
There was a problem hiding this comment.
The overflow guard covers deadline - elapsed and the delay comparison, but not this subtraction. Two cases slip through:
start = -1,now = Long.MAX_VALUE:now - startwraps toLong.MIN_VALUE,Math.maxclamps it to 0, and the retry is allowed.- A clock that moves backwards (
now < start) also clamps to 0, so the deadline budget restarts.
maxAttempts still caps the total, so this can't loop forever. But the timestamp contract isn't written down anywhere, and SDK-715 has to choose a clock. Suggest stopping retries rather than reopening the budget, and documenting a monotonic clock:
| long elapsed = Math.max(0L, nowMillis - firstAttemptStartMillis); | |
| // Timestamps must come from a monotonic clock, e.g. SystemClock.elapsedRealtime(). | |
| if (nowMillis < firstAttemptStartMillis) { | |
| return false; | |
| } | |
| long elapsed = nowMillis - firstAttemptStartMillis; | |
| if (elapsed < 0) { // the subtraction overflowed | |
| return false; | |
| } |
Plus a test for each case, next to hugeDelayDoesNotOverflowTheDeadlineCheck.
| if (responseCode >= 200 && responseCode < 300) { | ||
| return Outcome.SUCCESS; | ||
| } | ||
| if (responseCode == 0 || responseCode >= 500) { |
There was a problem hiding this comment.
>= 500 has no upper bound, so 600+ is RETRIABLE. SDK-455 FR-2 limits retries to 5xx, and the boundary test stops at 599 (IterableNetworkRetryClassifierTest.kt:42). No real server sends 6xx, so this is minor, but the bound is free:
| if (responseCode == 0 || responseCode >= 500) { | |
| if (responseCode == 0 || (responseCode >= 500 && responseCode < 600)) { |
Plus 600 to Outcome.TERMINAL in statusCodeBoundariesAreClassifiedExactly.
| boolean shouldRetry(@NonNull Outcome outcome, int failedAttempt, long firstAttemptStartMillis, long nowMillis) { | ||
| if (!policy.isRetryEnabled() || outcome != Outcome.RETRIABLE || failedAttempt >= policy.getMaxAttempts()) { |
There was a problem hiding this comment.
failedAttempt doesn't say where it starts counting. The tests treat it as 1-based (retriesStopAfterMaxAttempts, singleAttemptNeverRetries). The existing request counter is 0-based: IterableRequestDispatcher.java:79 starts the first task at retryCount 0, and retries pass it on. If SDK-715 passes retryCount straight in, the default policy makes 6 attempts instead of 5. With maxAttempts = 1, failedAttempt = 0 passes 0 >= 1 and allows a retry.
Suggest documenting it ("1-based number of the attempt that just failed") and rejecting values below 1, plus a test for 0.
| public Builder setNetworkRetryPolicy(@NonNull IterableNetworkRetryPolicy networkRetryPolicy) { | ||
| this.networkRetryPolicy = networkRetryPolicy != null ? networkRetryPolicy : IterableNetworkRetryPolicy.DEFAULT; |
There was a problem hiding this comment.
The parameter is @NonNull, but the body silently replaces null with DEFAULT, and the Javadoc doesn't mention it. Same pattern at IterableNetworkRetryPolicy.java:117-118 (setBackoffPolicy). The tests need an unchecked cast to exercise it (IterableNetworkRetryPolicyTest.kt).
There's a precedent in this file: setDataRegion (:465-477) keeps @NonNull, documents the null fallback in @param ("{@code null} is treated as …") and logs an error. Suggest doing the same in both setters. The alternative is @Nullable with documented reset-to-default semantics, but then it would differ from setDataRegion.
| public Builder setBackoffPolicy(@NonNull BackoffPolicy backoffPolicy) { | ||
| this.backoffPolicy = backoffPolicy != null ? backoffPolicy : DEFAULT_BACKOFF_POLICY; |
There was a problem hiding this comment.
Same as the setNetworkRetryPolicy comment: @NonNull with a silent fallback to DEFAULT_BACKOFF_POLICY. Suggest documenting it in @param, like setDataRegion.
| // Expects the code as IterableRequestTask reports it: 0 for any client-side failure to get a response, | ||
| // including connection errors and timeouts. |
There was a problem hiding this comment.
Code 0 isn't only "a client-side failure to get a response". IterableRequestTask maps four exception types to failure(0, …) (:269-281): JSONException, IOException, ArrayIndexOutOfBoundsException and the catch-all Exception. IterableRequestDispatcher.rejectedRequestResponse() (:140-142) also returns 0 when the executor rejects a request.
Only IOException, which includes connect and read timeouts, is a network failure. The others are deterministic. For example, a JSONException from building the GET query (json.getString(key), :127) fails the same way on every retry. SDK-714 asks for "a 5xx, timeout, or connection failure" to be retriable, and SDK-455 FR-2 says "everything else terminal".
The PR body already flags this for SDK-715. But the comment here, the CHANGELOG and the IterableNetworkRetryPolicy Javadoc ("any client-side failure including connection errors and timeouts") commit to the wider rule in public text.
Suggest having the task record whether a code-0 failure came from an IOException (or the dispatcher's rejection), classify only that case as RETRIABLE, and narrow the wording to "connection errors and timeouts". If this moves to SDK-715, is should be made an explicit acceptance criterion there.
📝 Summary
Adds a public
IterableNetworkRetryPolicyconfig and a package-private classifier for retriable request failures. Nothing reads the policy yet.🎟️ Jira Ticket: SDK-714
Stack, in order: #1111 (SDK-714), #1112, #1113, #1114, #1115. This is #1111.
📖 Description
This PR is the first slice of unified network retry and the base of a stack. SDK-708 (thread pool) goes on top of it, then SDK-715 applies the policy to requests, then SDK-716 adds the typed public API.
It adds
IterableNetworkRetryPolicyandIterableConfig.Builder.setNetworkRetryPolicy. Retry is off by default, and once enabled the defaults are 5 max attempts, a fixed 2 s backoff and a 60 s total deadline.The classifier treats 0 and 5xx as retriable. 429 and the other 4xx are terminal, which matches iOS (SDK-455). A 401 with a JWT error code goes to the existing auth path.
Nothing reads the policy yet, so request behaviour is unchanged. The only change to existing code is
matchesJWTErrorCodesgoing from private to package-private, so the classifier can reuse it.The public docs for the new retry setting will be updated with SDK-715 (#1115, where the policy is applied to requests), not in this PR, since the setting has no effect until then.
A few things to keep in mind for SDK-715.
maxAttemptscounts total attempts including the first one, while today's loop retries 5xx up to 6 times. Code 0 also covers client failures that are not network related. The offline queue treats 429 as transient today, so it will diverge from this classifier.🧪 How to test?
./gradlew :iterableapi:testDebugUnitTest --rerungives 818 tests and 0 failures. Checkstyle, lint and the androidTest compile are green. The classifier tests use injected timestamps, so there are no sleeps.🧾 Changelog
Added an entry under Unreleased in
CHANGELOG.md.🤖 Generated with Claude Code