Fix task log persistence on Postgres and harden the log flush path#212
Open
damilolaedwards wants to merge 1 commit into
Open
Fix task log persistence on Postgres and harden the log flush path#212damilolaedwards wants to merge 1 commit into
damilolaedwards wants to merge 1 commit into
Conversation
The task log upsert used ON CONFLICT (run_id, task_id, log_time), but the only unique key on task_logs is the primary key (run_id, task_id, log_idx). Postgres rejects an ON CONFLICT target that does not match a unique constraint, so every task log insert failed on Postgres and no task logs were stored. Point the conflict target at the primary key, which both engines share. The flush path also failed badly whenever a flush errored, which on Postgres was every time. On error it logged through the writer's own logger, whose database hook re-entered the flush and deadlocked on the buffer mutex; the buffer was only cleared on success, so it grew without bound; and the flush used a shared channel that could be closed twice and panic. Rewrite the flush coordination around the buffer mutex and a scheduled flag. The buffer is always cleared after a flush attempt, flush errors are reported through the parent logger so they cannot re-enter the hook, and the cancellation channel is removed, so a delayed flush simply finds an empty buffer when an earlier flush already ran. Add tests for persistence and upsert, for progress and a bounded buffer under a failing database, and for the normal persistence path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two related problems in the task log persistence path, both specific to the write to the database. SQLite, the default engine, was not affected by the first one.
Task logs are never stored on Postgres
InsertTaskLogupserts withON CONFLICT (run_id, task_id, log_time), but the only unique key ontask_logsis the primary key(run_id, task_id, log_idx); there is no unique index onlog_time. Postgres rejects anON CONFLICTtarget that does not match a unique or exclusion constraint, so every task log insert fails on Postgres at execution time and no task logs are ever stored. The SQLite branch usesINSERT OR REPLACEkeyed by the primary key and works.The flush path fails badly on any flush error
Whenever a flush fails (on Postgres, every time), three things went wrong:
Fireand tries to take the buffer mutex that the flush already holds, so the logging goroutine deadlocks.Fix
(run_id, task_id, log_idx), which both engines share.Tests
go build ./...,go vet, and the logger and db packages under-racepass.