Skip to content

pkg/gorp: add SaveLogBatch and LogBatcher, use it in report test2json - #47

Open
DzmitryHumianiuk wants to merge 2 commits into
feat/test2json-parametersfrom
feat/log-batcher
Open

DzmitryHumianiuk wants to merge 2 commits into
feat/test2json-parametersfrom
feat/log-batcher

Conversation

@DzmitryHumianiuk

Copy link
Copy Markdown
Member

Summary

Part of #27 (slice L3). Stacked on C6 → C5 → C4 → C3 → C2 → #29. The library part needs only #29 (multipart
SaveLog); the reporter part touches the same code as C4 and C5.

Two gaps in the log API of pkg/gorp:

  1. ReportPortal answers a multipart log batch with 201 even when it rejects logs of the batch. A rejected log
    is an entry of responses[] with message/stackTrace instead of id. SaveLogMultipart returns only the
    first ID, so rejected logs were never noticed.
  2. Batching lived inside the test2json reporter and knew only "10 entries". There was no flush by time, so the
    output of a long-running test stayed in the reporter until 10 lines had been printed or the run ended.

Changes

pkg/gorp:

  • ReportingClient.SaveLogBatch(ctx, logs, files) ([]LogBatchEntry, error): same request as SaveLogMultipart,
    returns the answer for every log. LogBatchEntry{ID, Message, StackTrace} with Failed() and Reason().
    Reason() returns the one-line message whichever field it is in: the server fills message with the stack trace
    and stackTrace with the message (Investigation log 2).
  • SaveLogMultipart is now a thin wrapper and behaves as before (first ID, no error for a rejected log).
  • LogBatcher (NewLogBatcher(ctx, client, LogBatcherOptions), Add(log, files...), Flush(), Close()):
    • a batch is sent when it has MaxEntries logs (default 10), when the next log would take it over MaxBytes
      (16 MiB) or MaxAttachments (49), every FlushInterval (5 s; negative disables), and on Flush/Close;
    • requests run in an errgroup.Group limited to MaxInFlight (4); Add blocks while that many are under way;
    • the timer goroutine selects on the ticker and a stop channel, without default; Close stops and waits for
      it before it waits for the requests, and the buffer lock is not held while a request is started;
    • Close returns the first failed request, or *LogBatchError for rejected logs; it is idempotent; Add after
      Close returns ErrLogBatcherClosed;
    • a failed batch is not sent again (a replayed batch could store its logs twice).
  • DefaultLogBatchAttachments = 49: verified, see below.

report test2json:

  • The reporter uses LogBatcher with MaxEntries: 10, MaxInFlight: 4 set explicitly (the previous values) and
    the library defaults for the rest.
  • The reporter used to merge continuation output into the last buffered log. The batcher owns a log once it has
    it, so the reporter now holds back the one log that may still be continued (pendingLog: error /
    error-continue output of Go 1.27+, or any line of a stream without OutputType). It is handed to the batcher
    when the next log or any non-output event arrives, and after at most one second of silent input
    (pendingLogMaxAge, a ticker in the receive loop).

Behaviour change

  • Logs of a running test arrive while it runs (after about 5 s, plus up to 1 s for a line that may be
    continued) instead of with the tenth line or at the end.
  • A log the server rejects fails the report: the launch is finished as INTERRUPTED and the command exits
    non-zero, exactly like a failed log request did already. Before, such a log was lost silently. The reporter
    builds well-formed logs, so this is expected only when something is wrong (for example an expired launch).
  • Merging of continuation lines: a continuation that arrives after another test's output is now a separate
    ERROR log; before it was appended to whatever log was last in the buffer, which could be the other test's.
  • The error message of a failed log upload now starts with reporting logs: sending a batch of N logs: …; the
    per-batch unable to report logs log line is gone (the error is reported once, at the end).
  • Library: additions only.

Verification

  • go build ./... && go vet ./... && go vet -tags e2e ./e2e/ && go test -race -count=1 ./...: ok; the batcher tests
    also with -race -count=5. Lint (run ./..., run --build-tags e2e ./e2e/...): 0 issues.
  • Unit, pkg/gorp/log_batcher_test.go: every entry returned; flush by count (25 logs → 10, 10, 5), by size (incl.
    an oversized log on its own), by attachments (never more than the limit, a file travels with its log), by timer
    (no Flush/Close), Flush; at most MaxInFlight concurrent requests; Close with rejected logs and with a
    failed request; Add after Close; 8 goroutines adding 400 logs while the timer flushes every millisecond →
    every log arrives exactly once.
  • Unit, internal/commands: held-back log is released while the input is silent; only continuable output is held
    back; rejected logs fail the report and interrupt the launch; all earlier reporter tests pass unchanged except
    the three white-box tests that read reporter.logs (they now read what the fake server received).
  • Live, ReportPortal 6.0.0 (demo):
    • The 49-attachment claim (scratchpad/wave/probe_l3.py, log scratchpad/wave/live/l3-probe.log), one
      JSON part plus N file parts in one request: N = 10, 48, 49 → 201, N ids; N = 50, 51 →
      500 {"errorCode":5000,"message":"Unclassified error [Could not access multipart servlet request]"}
      .
      60 logs without attachments in one request → 201, 60 entries: the limit is on parts, not on logs.
    • go test -tags e2e -run TestLogBatcher → PASS TestLogBatcher_SplitsAttachmentsAndReportsRejectedLogs (3.50s):
      60 logs with attachments through LogBatcher are all stored with their files (two requests); 50 files in one
      SaveLogBatch → HTTPError 500; a log naming a file that is not sent → entry Failed() with reason
      …There is no request part or file with name not-sent.txt inside a 201; the batcher's Close returns
      *LogBatchError.
    • PASS TestReportTest2JSON_LogsOfRunningTestAreSent (7.06s): with stdin still open, the one output line of a
      running test is in ReportPortal while the launch is IN_PROGRESS, and is not sent again at the end.
  • Whole stack: go test -tags e2e -count=1 -v ./e2e/... on this branch → all 18 tests pass (71 s), log
    scratchpad/wave/live/top-e2e-full.log.

Investigation log

  1. Batch always 201: service-api develop,
    src/main/java/com/epam/reportportal/base/reporting/async/controller/LogAsyncController.java:147-179: each
    log is validated and created inside a try; the catch adds new BatchElementCreatedRS(stackTrace, message)
    and the method returns CREATED. Live: a batch of a good log, a log without launchUuid and a log naming a
    missing file → 201, responses: [{id}, {message, stackTrace}, {message, stackTrace}].
  2. Swapped fields: the same catch passes ExceptionUtils.getStackTrace(e) first and
    ExceptionUtils.getMessage(e) second. Live, the entry has
    "message": "com.epam…ReportPortalException: … \n\tat com.epam…" (the trace) and
    "stackTrace": "ReportPortalException: Binary data cannot be saved. There is no request part or file with name absent.txt" (the message). Reason() therefore takes the shorter field's first line.
  3. Part limit: scratchpad/consilium2/server.md derived "49 attachments" from Spring Boot's
    server.tomcat.max-part-count=50 default and the absence of an override in service-api
    (grep -n "max-part-count\|multipart" src/main/resources/application.properties shows only the
    spring.servlet.multipart.* size settings). That was configuration metadata, not an observation; the probe in
    Verification confirms it on the 6.0.0 demo. It is a deployment setting, so the cap is an option
    (MaxAttachments), and the e2e test only logs when a server accepts 50.
  4. Entry count: the first version of the batcher also failed when the response had fewer entries than the batch.
    The test fakes of this repo answer one entry per request, and nothing in the server produces a short
    responses[], so only entries without ID count as failures.
  5. Ownership and the merge: reporter.log appended continuation output to r.logs[len(r.logs)-1], a log the
    batcher may already be sending from its timer goroutine. Holding the log in the reporter until it cannot be
    continued anymore keeps the batcher free of a "change the last log" API and of the data race.
  6. The held-back log and silence: on a stream without OutputType every line can be continued, so the last line
    before a hang would never leave the reporter. Hence the one-second release in the receive loop; the e2e test
    uses such a stream.
  7. Close order: mark closed → stop the timer goroutine and wait for it → flush → errgroup.Wait. The timer is
    the only internal caller of Go, so no request can be started after Wait began. Add concurrently with
    Close is documented as not allowed.

Open questions

  • Is failing the report for a rejected log wanted, or should it be a warning? It follows the existing rule for
    failed log requests.
  • MaxBytes counts the JSON of the logs and the size of FileMultipart attachments. The size of a
    ReaderMultipart is unknown without reading it and is not counted.
  • DefaultLogFlushInterval of 5 s is a choice, not a measured value.

Not verified

  • Servers older than 6.0.0 (per-entry responses are the same at 5.15.6 according to server.md; not run).
  • The status of a request that exceeds the 128 MB size limit (not sent).
  • No retry of failed batches (belongs to L4).
  • One existing e2e test left its merged launch behind in this run: deleting it failed on the server with
    500 … TransientObjectException, also by hand. Not related to this change.

🤖 Generated with Claude Code

ReportPortal answers a multipart log batch with 201 even when it rejects
some of its logs: such a log is an entry of responses[] without id.
SaveLogMultipart returns only the first id, so the rest went unnoticed.

SaveLogBatch returns the entry of every log (LogBatchEntry: Failed,
Reason). LogBatcher collects logs and sends a batch when it has 10
entries, when the next log would take it over 16 MiB or 49 attachments,
and every 5 seconds; up to 4 requests are in flight, managed by an
errgroup. Close sends the rest and returns failed requests and rejected
logs (LogBatchError). One request can carry 49 attachments: the server
allows 50 multipart parts, one of which is the JSON part.

The test2json reporter uses the batcher with its previous limits (10
entries, 4 in flight). New for it: logs of a long-running test are sent
while it runs, and a rejected log fails the report like a failed request.
A log that later output may be merged into is held back by the reporter,
for at most a second of silence, since the batcher owns what it is given.

Part of #27.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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