Skip to content

fix(customize): make a rejected CSV import change nothing - #9452

Open
henry-rystic wants to merge 1 commit into
apache:mainfrom
henry-rystic:fix-9448-customize-import
Open

henry-rystic wants to merge 1 commit into
apache:mainfrom
henry-rystic:fix-9448-customize-import

Conversation

@henry-rystic

Copy link
Copy Markdown

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:

  • Each import deletes the old data first, unless incremental=true. Then it calls importCSV.
  • importCSV saves one row at a time, without a transaction. It stops at the first bad row.

Fix:

  • Each exported 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.
  • The issues API now saves the board in the same transaction (ImportIssueToBoard). So a rejected file does not create or rename the board.
  • This covers every import that deletes first: ImportIssue, ImportIssueCommit, ImportIssueRepoCommit, ImportIssueChangelog, ImportIssueWorklog, ImportSprint, ImportQaApis, ImportQaTestCases and ImportQaTestCaseExecutions. An incremental import also runs in one transaction.
  • The QA imports ignored an error when they saved an account. Now they return it. In a transaction, an ignored error is not safe: PostgreSQL stops the whole transaction after a failed statement.

The transaction keeps its locks until the import ends. Two more changes follow from this:

  • Narrower deletes. The old deletes used a subquery on board_issues. On MySQL, this locks most rows of issues and board_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 of board_issues and board_sprints are deleted by their full key (board id and issue or sprint id).
  • One import at a time. Two imports can share an issue or an account. In parallel, one import could delete an issue while the other adds it to its own board, or the two could deadlock on the same accounts. A lock in the service now runs imports one at a time. A lock in the process is enough, because DevLake runs one server instance: lockDatabase in backend/server/services/locking.go holds 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.

inTx is written by hand. dbhelper.TxHelper needs a context.BasicRes, but the customize Service holds only a dal.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: TestImportRejectedFileChangesNothing has one case for each of the nine imports. Each case imports a valid file from raw_tables/rejected_*_valid.csv and checks the tables with VerifyTableWithOptions against snapshot_tables/rejected_*.csv. Then it imports raw_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 the accounts table 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 vet passes.

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_52d63a1 to compare. Each request is POST /plugins/customize/csvfiles/issues.csv with boardId=csv-board, boardName=Board and incremental=false.

Step Before the fix After the fix
1. Send three valid issues (csv:1, csv:2, csv:3). 200. The board has the three issues. 200. The board has the three issues.
2. Send csv:1, a row with too few fields (csv:9,https://example.com/issues/9,9), csv:2 and csv:3. 400, wrong number of fields. 400, wrong number of fields.
3. Check the board. Only csv:1 is in issues and board_issues. csv:1, csv:2 and csv:3 are still there.
Send the file from step 1 again. Then send csv:1 with a changed title, csv:2, and the short row last. 400. The changed title is saved, and csv:3 is deleted. 400. Nothing changes.

Known limit (MySQL only). The deletes can still lock more than the deleted rows until the import ends. A delete by issue_id locks 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. Only READ COMMITTED removes the gap locks, and dal.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 accounts table 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

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>
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.

[Bug][Customize] A failed CSV import deletes existing data and saves part of the file

1 participant

⌂ Proxy