Skip to content

Add WithRetry: retries with backoff for requests that are safe to repeat - #50

Open
DzmitryHumianiuk wants to merge 1 commit into
feat/client-optionsfrom
feat/http-retries
Open

DzmitryHumianiuk wants to merge 1 commit into
feat/client-optionsfrom
feat/http-retries

Conversation

@DzmitryHumianiuk

Copy link
Copy Markdown
Member

Summary

Part of #27 (slice L4). Stacked on L1 (feat/client-options), which provides Option. Does not depend on #36; it merges with #36 with the resolution described in the L1 PR (trial merge built and passed all tests).

WithRetry(RetryPolicy) makes a client repeat requests that failed for a reason that may pass. It is off by default, and the CLI does not use it yet.

Changes

  • pkg/gorp/retry.go (new):
    • RetryPolicy{MaxRetries, MinWait, MaxWait} and WithRetry. Defaults: MinWait 500 ms, MaxWait 10 s. MaxRetries <= 0 means no retries.
    • What is retried: network errors, 5xx except 501, and 429. Other 4xx never.
    • Pause: MinWait * 2^(attempt-1) capped at MaxWait, the actual pause is random between a half and the whole of that. A Retry-After header (seconds or HTTP date) is used instead when present, capped at MaxWait.
    • Retries stop when the context of the call is done. After the last attempt the caller gets the error of the last response, the same type as without retries.
  • Hand-written methods (ReportingClient, launch and filter helpers of Client): Resty's retry API with the policy's own condition, delay strategy and a hook that caps Retry-After. Each attempt is a separate http.Client.Do.
  • Generated client: a copy of the http.Client with retryTransport (http.RoundTripper). The shared client is not wrapped, otherwise Resty calls would retry twice over.
  • pkg/gorp/reporting_client.go: launch and item starts opt in to retries per request, see below.
  • README, AGENTS.md (a design-decision note), the reportportal skill.

Which requests are retried

Request Retried Why
GET, PUT (finish launch/item), DELETE yes idempotent by HTTP semantics
StartLaunch, StartTest, StartChildTest yes the body carries a UUID; the client generates one (version 4) when the request has none, on a copy of the request
Start*Raw only if the JSON body has a non-empty "uuid" the client doesn't rewrite raw bodies
Start with rerun no the server replaces the UUID of a rerun with the one of the launch it reruns
SaveLog, SaveLogs, SaveLogMultipart no the server doesn't deduplicate logs
MergeLaunches, every POST/PATCH of the generated client no not known to be safe

Behaviour change

Only with WithRetry:

  • A start without a UUID is sent with a generated one. The server answers with that UUID as id. Without WithRetry the request goes out as before.
  • 429 is retried although it is a 4xx. The plan says "never other 4xx"; 429 with Retry-After is the case retries exist for, and the server analysis recommends it. ReportPortal itself never sends 429 or Retry-After, a proxy may.
  • There is no opt-in to retry logs. I dropped a RetryLogs field after reading Resty: before a retry it rewinds multipart readers and fails with ErrReaderNotSeekable for one that can't seek (multipart.go:73-79), replacing the real error (request.go:1563), and it closes closable field readers after every attempt (client.go:2491-2497). The log batcher of L3 is the place to decide about re-sending logs.
  • A replayed DELETE through the generated client can answer 404 when the first attempt did delete and only its response was lost.
  • For the generated client, http.Client.Timeout (30 s by default, pkg/gorp/client.go) covers all attempts and pauses together, because the retries happen inside one Do. For the Resty-based methods every attempt has its own timeout.
  • On the Resty path, for 429 and 503 the cap of Retry-After is rounded up to a whole second (the header only has seconds).

Verification

On feat/http-retries (19b9ae1):

  • go build ./... && go vet ./...: ok
  • go test -race ./...: ok; go test -race -count=3 ./pkg/gorp/: ok
  • go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0 run ./...: 0 issues
  • Live: none against ReportPortal, as agreed. All behaviour is tested against a fake server (httptest) that answers with a scripted list of statuses or closes the connection without a response.

Tests (pkg/gorp/retry_test.go), each for the Resty-based methods and the generated client where it applies:

  • 503, 502, then 200: succeeds with 3 requests;
  • a table of statuses: 400/401/403/404/406/409/501 are sent once, 429/500/502/503/504 are retried;
  • exhaustion: MaxRetries: 2 gives exactly 3 requests on both clients, and errors.As still yields *HTTPError / *openapi.GenericOpenAPIError with the last body;
  • PUT finish: the body of every attempt is identical;
  • starts: every attempt carries the same generated UUID, the caller's request is not modified, a caller's UUID is kept, and without WithRetry no UUID is added;
  • rerun, raw starts without a UUID, logs (JSON, batch, with attachment) and merge are sent once;
  • a closed connection is retried; a server that is down gives the network error after all attempts;
  • cancelling the context during a one-minute pause returns context.Canceled at once with one request sent;
  • Retry-After: 1 is waited for (>= 1 s); Retry-After: 3600 is capped by MaxWait (for 503, which Resty handles itself, and for 500);
  • the transport replays a body through GetBody, and sends once what it cannot replay (POST, PATCH, a PUT with a one-shot body);
  • the pause stays within its bounds, is random, and doesn't overflow for far attempts.

Investigation log

  1. go.mod:11: resty.dev/v3 v3.0.0-rc.4. Its API was read in the module cache, not from memory.
  2. request.go:1470-1620 (Execute): retries only when RetryCount > 0 and the request is idempotent (:1490, :1534); idempotent means GET/HEAD/OPTIONS/PUT/DELETE/TRACE or SetRetryAllowNonIdempotent(true) on the request (:1895-1908). This fits: POSTs are sent once unless a request opts in.
  3. client.go:2507-2514: on a transport error the conditions get a non-nil Response with a nil RawResponse, so one condition can handle both errors and statuses.
  4. retry.go:104-125 (isDoNotRetryError): the built-in condition retries only errors that report Temporary(), which a refused or reset connection does not. So the default conditions are switched off and the policy's own are used.
  5. retry.go:153-173 (NextWaitDuration): for 429 and 503 Resty returns Retry-After as is, before it asks a custom delay strategy, and without a cap (:215-245). A retry hook runs before that (request.go:1568-1576), so the hook rewrites an excessive header to the policy's limit. For other statuses the policy's delay strategy reads the header.
  6. Server, service-api develop at 017642e9, migrations at 956ef1ad:
    • StartRQ.java:66-68 has uuid; v2 keeps a client UUID for launches (reporting/async/producer/LaunchStartProducer.java:58-63) and items (ItemStartProducer.java:54-58) and generates one only when it is blank.
    • a rerun overrides it: LaunchStartProducer.java:58-60.
    • launch.uuid and test_item.uuid are VARCHAR(36) NOT NULL UNIQUE (1_initialize_schema.up.sql:383,419). 16_remove_unused_indexes.up.sql drops extra indexes and the unique constraint of log.uuid only, so launches and items stay unique and logs don't.
    • a message that fails in the consumer with anything but "launch/suite/item not found" goes to the parking-lot queue (reporting/async/exception/ReportingErrorHandler.java:89-98). So a repeated start with the same UUID is accepted with 201 and then dropped by the unique constraint, and a repeated log is stored twice.
    • the server has no code that sends 429 or Retry-After (server analysis in the plan; not re-checked line by line).
  7. The generated model has Uuid string without omitempty (pkg/openapi/model_com_epam_reportportal_base_reporting_start_launch_rq.go:30), so an unset UUID already goes out as "" today; the client UUID replaces that.
  8. A generated UUID is 36 characters, the size of the column.

Not verified

  • That the server drops a repeated start with the same UUID. This is read from the source (item 6), not observed. A live probe was prepared but denied by the permission system, and was never run.
  • What a replayed PUT finish does on the server when the first one was applied (from the source it should fail in the consumer and be parked, harmlessly).
  • The sync v1 API and 5.x servers. ReportingClient uses v2 only.
  • Whether a replayed rerun start would be safe; it is simply not retried.

🤖 Generated with Claude Code

WithRetry(RetryPolicy) repeats requests that failed with a network
error, 5xx (except 501) or 429, with exponential backoff and jitter. A
Retry-After header is honored up to MaxWait, other 4xx are never
retried, retries stop with the context.

The hand-written methods use Resty's retry API with the policy's own
conditions and delays; the generated client gets a copy of the HTTP
client with a retrying transport. GET, PUT and DELETE are retried.
Launch and item starts are retried because they carry a client UUID,
which the client now generates when the request has none. Rerun starts,
raw starts without a uuid and logs are sent once.

Part of #27

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@DzmitryHumianiuk

Copy link
Copy Markdown
Member Author

Local ReportPortal 6.0.0 check (same build as demo), covering the two "Not verified" items:

  • Same client UUID started twice on v2: launch and item both answer 201 with the same id, and one row exists. On v1 the second launch start is a 500 (duplicate key … launch_uuid_key); ReportingClient doesn't use v1.
  • Identical finish replayed on v2: no change, for an item (status and description) and for a launch (the finish description is appended once, attributes aren't duplicated).
  • End to end: a reverse proxy applied each start/finish on the server and then answered 502 to the client. With WithRetry(RetryPolicy{MaxRetries: 3}), all six calls (launch, suite, child step, two item finishes, launch finish) succeeded on the second attempt, and the server held exactly 1 launch and 2 items, all PASSED.
  • Aside: on v2 a finish with a different status after the first one overwrites the status; retries replay the same body, so it doesn't apply here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants