Repository navigation
fix(customize): make a rejected CSV import change nothing - #9452
Open
henry-rystic wants to merge 1 commit into
Open
henry-rystic wants to merge 1 commit into
henry-rystic wants to merge 1 commit into
Conversation
Each import deleted the old data, then saved the file one row at a time without a transaction. A bad row stopped the import with an error, but the old data was already deleted and the rows before the bad row were saved. Run each import in one transaction, so a rejected file changes nothing. The issues API also saves its board in that transaction. The transaction now holds its locks until the import ends. So read the board's ids first and delete by those keys, instead of with a subquery that locks the rows of other boards, and run imports one at a time so that two imports cannot race on a shared issue or deadlock on shared accounts. Return the account errors that the QA imports ignored, because a failed statement stops a PostgreSQL transaction. Closes apache#9448 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Henry Starman <334243260+henry-rystic@users.noreply.github.com>
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.
Summary
A CSV import in the customize plugin can fail on one row. When this happens, the import has already deleted the old data, and it has saved the rows before the bad row. The API returns 400, so the user thinks that nothing changed.
Cause:
incremental=true. Then it callsimportCSV.importCSVsaves one row at a time, without a transaction. It stops at the first bad row.Fix:
Import*method now runs in one transaction. The old code is moved to a private method with the same name in lower case. A new helper,inTx, commits when the import returns no error. Otherwise it rolls back.ImportIssueToBoard). So a rejected file does not create or rename the board.ImportIssue,ImportIssueCommit,ImportIssueRepoCommit,ImportIssueChangelog,ImportIssueWorklog,ImportSprint,ImportQaApis,ImportQaTestCasesandImportQaTestCaseExecutions. An incremental import also runs in one transaction.The transaction keeps its locks until the import ends. Two more changes follow from this:
board_issues. On MySQL, this locks most rows ofissuesandboard_issues. In a long transaction, pipelines of other boards would wait for the import. The deletes now read the ids of the board first. Then they delete by these ids, 1000 ids in each statement. The rows ofboard_issuesandboard_sprintsare deleted by their full key (board id and issue or sprint id).lockDatabaseinbackend/server/services/locking.goholds an exclusive table lock while the server runs. The API reads the whole upload (ParseMultipartForm) before the import starts, so a slow upload does not hold the lock.inTxis written by hand.dbhelper.TxHelperneeds acontext.BasicRes, but the customizeServiceholds only adal.Dal.Does this close any open issues?
Closes #9448
Screenshots
N/A
Other Information
Tests added in
backend/plugins/customize/e2e/:import_rejected_file_test.go:TestImportRejectedFileChangesNothinghas one case for each of the nine imports. Each case imports a valid file fromraw_tables/rejected_*_valid.csvand checks the tables withVerifyTableWithOptionsagainstsnapshot_tables/rejected_*.csv. Then it importsraw_tables/rejected_*_bad.csv. Line 3 of each bad file has too few fields. The import must return an error, and the same snapshots must still match. Where the bad file names a new person, the test also checks that no account was saved.import_concurrency_test.go: two tests for the design of the fix, not for the original bug. They pass on the code before this change too.TestImportDoesNotLockOtherBoards(MySQL only): while an import of one board waits for its file, another connection changes an issue of another board and adds board links of other boards. It must not wait. With the old subquery deletes in a transaction, all four statements wait.TestConcurrentImportsWithSharedAuthors: two imports with the same 40 authors run at the same time. Both must succeed. Without the import lock, this test fails with a deadlock on MySQL and on PostgreSQL.TestImportIssueToBoardRejectedFileKeepsBoard: a rejected file must not rename a board or create a new one.TestImportQaApisRejectsLongCreatorName: a creator name that is too long for theaccountstable rejects the file, and nothing changes.Without the fix, these tests fail on MySQL and on PostgreSQL. In all nine cases of
TestImportRejectedFileChangesNothing, the old rows are deleted and the changed row is saved. The rejected file renames the board or creates a new one. The file with the long creator name is saved without a creator. With the fix, all tests pass on MySQL 8.4 and PostgreSQL 17.go vetpasses.Manual test. I built the backend image from this branch and ran it with MySQL 8.4. I ran the same steps on
apache/devlake:main_261124_0230_52d63a1to compare. Each request isPOST /plugins/customize/csvfiles/issues.csvwithboardId=csv-board,boardName=Boardandincremental=false.csv:1,csv:2,csv:3).csv:1, a row with too few fields (csv:9,https://example.com/issues/9,9),csv:2andcsv:3.wrong number of fields.wrong number of fields.csv:1is inissuesandboard_issues.csv:1,csv:2andcsv:3are still there.csv:1with a changed title,csv:2, and the short row last.csv:3is deleted.Known limit (MySQL only). The deletes can still lock more than the deleted rows until the import ends. A delete by
issue_idlocks the gaps before and after the rows of each of the board's issues. So a new row whose key sorts between or right after the board's issue ids waits, for example a label of an issue of another board. When the board's rows are a large part of a table, MySQL can choose a full scan, and then the delete locks the whole table. Deleting one row per statement narrows this, but it doubles the time of a full import (20,000 issues: 16.5 s against 32.7 s on MySQL), and a gap lock still remains. OnlyREAD COMMITTEDremoves the gap locks, anddal.Dal.Begin()cannot set the isolation level. PostgreSQL does not take gap locks.Behavior change. A QA import with a creator name that is too long for the
accountstable is now rejected. Before, the row was saved without a creator on MySQL.This PR was prepared with AI assistance (Claude Code). The commit carries a
Co-Authored-By:trailer.🤖 Generated with Claude Code