Skip to content

Return step logs strictly in line order - #6999

Merged
qwerty287 merged 7 commits into
woodpecker-ci:mainfrom
stevapple:fix/order-logs-by-line
Aug 14, 2026
Merged

qwerty287 merged 7 commits into
woodpecker-ci:mainfrom
stevapple:fix/order-logs-by-line

Conversation

@stevapple

@stevapple stevapple commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

LogFind returned a step's log entries sorted by the auto-increment primary key. That works as long as auto-increment values arrive in insertion order, which holds on a single-node engine.

Distributed engines allocate them from per-node caches. On TiDB, consecutive LogAppend calls land on different nodes and receive id ranges that interleave, so a batch written later can carry lower ids than one written earlier. Sorting by id then reorders the log at batch granularity, and since the UI renders rows as the API returns them, the line numbers jump backwards mid-log: 572, then 107, then 1. We confirmed that over a half of historical runner steps have such interleaved logs.

log-order

line is what the agent assigns in LineWriter as it produces the output. It already carries the order, and it is populated for existing rows, so this is a query change with no backfill.

Tests

TestLogFindOrdersByLine assigns ids explicitly so the first batch takes 4000/4001 and the second takes 30/31, reproducing the interleave. It fails against the old behaviour, returning [third fourth first second].

The existing log store tests asserted row counts alone, which is why CI stayed green through both of these.

Future directions

A composite index on (step_id, line) would let this sort be served straight from the index. It was left out of this PR deliberately. Declaring it UNIQUE(s) would match how steps and workflows declare (pipeline_id, pid), but that constraint does not hold on real data today, so it would need a dedup migration first. This is requested per review and will be covered by a follow-up PR.

stevapple and others added 2 commits August 14, 2026 04:54
LogFind sorted a step's log entries by their primary key, which assumes
auto-increment values are handed out in insertion order. That holds on a
single-node engine only.

Distributed engines allocate auto-increment values from per-node caches,
so a batch inserted later can receive lower ids than an earlier one. On
TiDB this reorders the rendered log at batch granularity, making the
displayed line numbers jump backwards.

The line number is what the agent assigns as it produces the output, so
sort by that.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
An agent that reconnects mid-step can resend a batch the server already
stored, leaving a line number present more than once. Those rows then
print twice in the log view.

Report the first entry of every line number, which is what a
UNIQUE(step_id, line) index would guarantee. Ordering by line already
puts any repeats next to each other, so this is a linear pass over the
rows that were fetched anyway.

Doing it in SQL would need `id IN (SELECT MIN(id) ... GROUP BY line)`,
since grouping by line alone cannot return whole rows on Postgres or on
MySQL under ONLY_FULL_GROUP_BY, and xorm accepts that subquery only as a
raw string.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@stevapple
stevapple force-pushed the fix/order-logs-by-line branch from e95475c to ac353a8 Compare August 13, 2026 20:55
@qwerty287

qwerty287 commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Thanks.

We found (step_id, line) is not unique in practice. Checking our instance turned up 182 duplicate pairs out of ~56k steps. Sampling them, each is a pair of byte-identical rows, which points at an agent re-sending a batch after a reconnect rather than at two genuinely different lines colliding, so it's generally lossless to dedup, but worth further investigation.

I never saw this. But I'm not sure if your solution is the right way to go here – I would prefer making (step_id, line) unique (and handle errors on insert due to duplicates properly) and then we can skip the deduplication. Would that be a possible fix instead of your current solution?

@stevapple

Copy link
Copy Markdown
Contributor Author

I would prefer making (step_id, line) unique (and handle errors on insert due to duplicates properly) and then we can skip the deduplication.

I would prefer this solution too. The worry is from migration side — if we already ensure conflicting lines is unlikely to happen, then we should deduplicate once during migration, and make the (step_id, line) truly unique.

@qwerty287

Copy link
Copy Markdown
Contributor

I would just delete duplicated lines in the migration. I never saw this issue before, seems to only happen in very special setups (and as you said the DB data is the same for the log lines).

Following review: rather than filtering repeated line numbers on every
read, let the schema rule them out.

log_entries gains UNIQUE(step_id, line), so the read path goes back to a
plain ordered query. A migration clears the way by keeping the lowest id
of every line number and dropping the rest; it is restricted to line
numbers that actually repeat, so the work is proportional to the
duplicates rather than to the size of the table.

That migration must run, hence it is not marked Long: the index is
created from the model on every start, and skipping the cleanup would
leave a database the server cannot start against.

Storing an entry twice now fails instead of passing unnoticed. The agent
retries the log RPC with the batch it already sent, so this can happen
when a batch was stored but its response was lost. Reporting it keeps
that visible.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@stevapple

Copy link
Copy Markdown
Contributor Author

Both migration and unique index added. Here's our findings on how the duplication may happen:

The agent delivers logs at least once. In agent/rpc/client_grpc.go, sendLogs builds the request once, outside the retry:

func (c *client) sendLogs(ctx context.Context, entries []*proto.LogEntry) error {
	req := &proto.LogRequest{LogEntries: entries}

	_, err := retryRPC(ctx, c, "log", func() (*proto.Empty, error) {
		r, err := c.client.Log(ctx, req)
		return r, classifyRPCErr(ctx, err)
	})
	return err
}

retryRPC runs that closure through backoff.Retry, so every attempt sends the same req. The server's Log handler appends whatever arrives, with no idempotency key. So if the server commits a batch and the response does not make it back — connection dropped, or a timeout that fires after the write landed — the agent retries and the same entries are stored a second time.

@stevapple stevapple changed the title Return step logs in line order, deduplicated Add unique index on log lines and return in line order Aug 14, 2026
@qwerty287

Copy link
Copy Markdown
Contributor

Thanks, could you split that out into 2 PRs? I.e. here only returning in line order and another PR the unique migration

stevapple and others added 2 commits August 14, 2026 20:47
This reverts commit 1cce561.

The unique index and its migration move to a separate pull request, as
suggested in review, leaving this one to the ordering fix alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Filtering repeated line numbers on every read belongs with the unique
index that makes them impossible, so it moves to that pull request
together with the migration.

This leaves the ordering fix on its own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@stevapple stevapple changed the title Add unique index on log lines and return in line order Return step logs strictly in line order Aug 14, 2026
@stevapple

Copy link
Copy Markdown
Contributor Author

Thanks, could you split that out into 2 PRs? I.e. here only returning in line order and another PR the unique migration

Done with #7004

@qwerty287
qwerty287 merged commit 150fbf0 into woodpecker-ci:main Aug 14, 2026
@woodpecker-bot woodpecker-bot mentioned this pull request Aug 14, 2026
1 task
@6543 6543 added refactor delete or replace old code server labels Aug 14, 2026
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
woodpecker-cli 3.18.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## [3.18.0](https://github.com/woodpecker-ci/woodpecker/releases/tag/v3.18.0) - 2026-08-24

⚠️ This release contains a migration that alters log storage. Depending on the number of stored log lines, it can take a while to complete. Back up before upgrading and allow for extra downtime.

### ❤️ Thanks to all contributors! ❤️

Special thanks to @arpitjain099, @abdullaabdullazade, @George928 and @RealFakeAccount for reporting security vulnerabilities!

@6543, @AhmadNajiKam, @DEATIN, @OCram85, @Palleas, @YouCD, @anbraten, @confusedsushi, @gerbsen, @hasufell, @jrouly, @kastl-ars, @lafriks, @qwerty287, @retif, @romain-rossi, @spam-n-eggs, @stevapple, @xoxys, @zmully

### 🔒 Security

- k8s: allow to disable runtime class name backend option [[#7046](woodpecker-ci/woodpecker#7046)]
- Don't inject additional env vars into plugins [[#7040](woodpecker-ci/woodpecker#7040)]
- Don't inject matrix env vars into plugins [[#7036](woodpecker-ci/woodpecker#7036)]
- Update `golang.org/x/mod` [[#7012](woodpecker-ci/woodpecker#7012)]
- Change group identifier to full_path for GitLab [[#6950](woodpecker-ci/woodpecker#6950)]

### 📈 Enhancement

- Allow to add custom message to manual pipelines [[#6973](woodpecker-ci/woodpecker#6973)]
- Add hidden `decode-base64` agent command for Windows local backend [[#7023](woodpecker-ci/woodpecker#7023)]
- Enforce unique name for services and steps if dag is used [[#7032](woodpecker-ci/woodpecker#7032)]
- Add custom list opts with all [[#6992](woodpecker-ci/woodpecker#6992)]
- Add pipeline log download endpoint [[#6876](woodpecker-ci/woodpecker#6876)]
- Reject invalid started and done workflow states [[#7029](woodpecker-ci/woodpecker#7029)]
- Sort workflow files by name [[#7021](woodpecker-ci/woodpecker#7021)]
- Reject incompatible gRPC proto version agents [[#7028](woodpecker-ci/woodpecker#7028)]
- Enforce one log entry per line with a unique index [[#7004](woodpecker-ci/woodpecker#7004)]
- Order the user feed by creation time instead of id [[#7005](woodpecker-ci/woodpecker#7005)]
- Return step logs strictly in line order [[#6999](woodpecker-ci/woodpecker#6999)]
- Find the preceding pipeline by number instead of id [[#7003](woodpecker-ci/woodpecker#7003)]
- perf(schema): cache compiled YAML validation schema [[#7002](woodpecker-ci/woodpecker#7002)]
- Pick a repo's latest pipeline by number instead of id [[#7001](woodpecker-ci/woodpecker#7001)]
- feat: allow configuring allowed orgs per forge [[#6874](woodpecker-ci/woodpecker#6874)]
- Make the Kubernetes cluster domain configurable [[#6957](woodpecker-ci/woodpecker#6957)]
- Fix vite deprecation [[#6974](woodpecker-ci/woodpecker#6974)]
- feat(web): extract ListEditor form component [[#6962](woodpecker-ci/woodpecker#6962)]
- Rename `WOODPECKER_GRPC_VERIFY` to `WOODPECKER_GRPC_SKIP_VERIFY` [[#6947](woodpecker-ci/woodpecker#6947)]
- Update frontend schema.json for documented backend options [[#6933](woodpecker-ci/woodpecker#6933)]

### 🐛 Bug Fixes

- Prevent repository buttons from overlapping search bar [[#7047](woodpecker-ci/woodpecker#7047)]
- fix(server): prefix manual trigger Ref with refs/heads/ [[#7051](woodpecker-ci/woodpecker#7051)]
- fix runtime to ignore cancel as runtime error [[#7031](woodpecker-ci/woodpecker#7031)]
- Fix orphaned pipelines after repo delete [[#6971](woodpecker-ci/woodpecker#6971)]
- Migrate modernize encryption service [[#6830](woodpecker-ci/woodpecker#6830)]
- Fix running multiple cmdlets on windows [[#6968](woodpecker-ci/woodpecker#6968)]
- server: drop stale queued tasks for terminal workflows [[#7015](woodpecker-ci/woodpecker#7015)]
- fix(gitlab): avoid N+1 member lookups in Repos() [[#6991](woodpecker-ci/woodpecker#6991)]
- agent: retry connect on DeadlineExceeded, not just Unavailable [[#6975](woodpecker-ci/woodpecker#6975)]

### 📚 Documentation

- Update pnpm to v11.23.0 [[#7061](woodpecker-ci/woodpecker#7061)]
- Update dependency marked to v18.0.10 [[#7052](woodpecker-ci/woodpecker#7052)]
- k8s backend: fix step example using incorrect "detached" property [[#7016](woodpecker-ci/woodpecker#7016)]
- Update docs npm deps non-major [[#7010](woodpecker-ci/woodpecker#7010)]
- highlight the nodeSelector doesn't work by default [[#7007](woodpecker-ci/woodpecker#7007)]
- Update pnpm to v11.21.0 [[#6982](woodpecker-ci/woodpecker#6982)]
- Update docs npm deps non-major [[#6979](woodpecker-ci/woodpecker#6979)]
- Update documentation link for NestGuard plugin [[#6954](woodpecker-ci/woodpecker#6954)]
- Make intro example actually usable [[#6953](woodpecker-ci/woodpecker#6953)]
- Update docs npm deps non-major [[#6925](woodpecker-ci/woodpecker#6925)]
- Fix old link to Gitea docs [[#6932](woodpecker-ci/woodpecker#6932)]

### 📦️ Dependency

- Update web npm deps non-major [[#7060](woodpecker-ci/woodpecker#7060)]
- Lock file maintenance [[#7062](woodpecker-ci/woodpecker#7062)]
- Update web npm deps non-major [[#7054](woodpecker-ci/woodpecker#7054)]
- Update woodpeckerci/plugin-git Docker tag to v2.10.0 [[#7059](woodpecker-ci/woodpecker#7059)]
- Update docker.io/golang Docker tag to v1.27 [[#7055](woodpecker-ci/woodpecker#7055)]
- Update pre-commit hook golangci/golangci-lint to v2.13.1 [[#7058](woodpecker-ci/woodpecker#7058)]
- Update docker.io/techknowlogick/xgo Docker tag to go-1.27.x [[#7056](woodpecker-ci/woodpecker#7056)]
- Update module gitlab.com/gitlab-org/api/client-go/v2 to v2.58.2 [[#7053](woodpecker-ci/woodpecker#7053)]
- Update golang-packages to v0.36.4 [[#7050](woodpecker-ci/woodpecker#7050)]
- Update golang-packages [[#7045](woodpecker-ci/woodpecker#7045)]
- Update module github.com/getkin/kin-openapi to v0.147.0 [[#7039](woodpecker-ci/woodpecker#7039)]
- Update module github.com/urfave/cli/v3 to v3.11.0 [[#7017](woodpecker-ci/woodpecker#7017)]
- Lock file maintenance [[#7018](woodpecker-ci/woodpecker#7018)]
- Update web npm deps non-major [[#7009](woodpecker-ci/woodpecker#7009)]
- Update module gitlab.com/gitlab-org/api/client-go/v2 to v2.58.0 [[#7014](woodpecker-ci/woodpecker#7014)]
- Update postgres Docker tag to v18.6 [[#7011](woodpecker-ci/woodpecker#7011)]
- Update dependency golang to v1.26.6 [[#7008](woodpecker-ci/woodpecker#7008)]
- Update module golang.org/x/net to v0.58.0 [[#6996](woodpecker-ci/woodpecker#6996)]
- Update module golang.org/x/crypto to v0.55.0 [[#6994](woodpecker-ci/woodpecker#6994)]
- Update module google.golang.org/protobuf to v1.36.12 [[#6989](woodpecker-ci/woodpecker#6989)]
- Update pnpm to v11.21.0 [[#6983](woodpecker-ci/woodpecker#6983)]
- Update dependency eslint to v10.8.1 [[#6977](woodpecker-ci/woodpecker#6977)]
- Update module github.com/google/go-github/v89 to v90 [[#6980](woodpecker-ci/woodpecker#6980)]
- Update google.golang.org/protobuf digest to 644d026 [[#6976](woodpecker-ci/woodpecker#6976)]
- Update woodpeckerci/plugin-git Docker tag to v2.9.3 [[#6978](woodpecker-ci/woodpecker#6978)]
- Update web npm deps non-major [[#6939](woodpecker-ci/woodpecker#6939)]
- Update module github.com/docker/cli to v29.7.2+incompatible [[#6966](woodpecker-ci/woodpecker#6966)]
- Update web npm deps non-major [[#6928](woodpecker-ci/woodpecker#6928)]

### Misc

- Remove gitpod [[#7013](woodpecker-ci/woodpecker#7013)]
- Drop star-history [[#6997](woodpecker-ci/woodpecker#6997)]
- Do not run openapi generation twice on fail [[#6987](https://github.com/woodpecker-ci/woodpecker/pull/6987)]</pre>
  <p>View the full release notes at <a href="https://github.com/woodpecker-ci/woodpecker/releases/tag/v3.18.0">https://github.com/woodpecker-ci/woodpecker/releases/tag/v3.18.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17633
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor delete or replace old code server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants