Repository navigation
grass.temporal: Roll back a failed transaction - #8217
Muhaddis-igis wants to merge 2 commits into
Conversation
c31659a to
ca908b2
Compare
|
The failed Ubuntu job is not related to this change. Its only failure is The same test fails the same way on |
When a statement in execute_transaction() failed, the transaction stayed open. On SQLite, the next execute_transaction() or the commit() in close() then saved the statements that ran before the error, also at process exit through __del__(). On PostgreSQL, the connection stayed in the aborted transaction and the next execute_transaction() failed. Roll back in the finally block of execute_transaction(). close() no longer commits, so it rolls back a transaction that is still open. With psycopg2, that commit() was what saved the writes made with execute(), so execute() now commits after the statement on PostgreSQL. SQLite connections are in autocommit mode and need no commit there. Add tests that a failed transaction saves nothing, also when the caller keeps the connection or an error other than a database error stops it.
If closing the cursor or the connection raised, close() stopped before setting connected to False, so the object still counted as connected and a later close() tried again on the broken connection. Set connected to False first, and close the connection even if closing the cursor raises. The error is still raised to the caller. Add a test with an SQLite connection that was closed underneath.
ca908b2 to
5364aaf
Compare
|
@echoix this is the follow-up you suggested in your review of #8202. It now also covers your point about I rebased onto the current Could you review it? |
Description
Follow-up to #8202 for
DBConnectioningrass.temporal.core: a failedexecute_transaction()now saves nothing.execute_transaction()or thecommit()inclose()saved the statements that ran before the error. When the process exited instead,__del__()calledclose(), which committed them. On PostgreSQL, the connection stayed in the aborted transaction and the nextexecute_transaction()failed withInFailedSqlTransaction.execute_transaction()now callsrollback()infinally. After a successfulcommit()there is nothing to roll back.close()no longer commits, so a transaction that is still open is rolled back.close()setsconnectedtoFalsebefore it closes the cursor and the connection, and closes the connection even if closing the cursor raises. Before, an error there leftconnectedatTrue, so a laterclose(), for example from__del__(), tried again on the broken connection, as noted in the review of grass.temporal: Close the SQLite connection in DBConnection.close() #8202 (grass.temporal: Close the SQLite connection in DBConnection.close() #8202 (review)). The error is still raised.commit()inclose()was what saved the writes made withexecute()(insert(),update(),update_all()anddelete()inbase.py), because psycopg2 runs every statement in a transaction it does not commit.execute()now commits after the statement on PostgreSQL. SQLite connections are in autocommit mode (isolation_level = None), soexecute()is unchanged there.One behaviour change on PostgreSQL: writes made with
execute()before a transaction are now saved even if the transaction fails, as on SQLite. For example,t.unregisterupdates each map's list of space time datasets withexecute()before its transaction, so after a failedt.unregisterthe maps no longer list the STRDS, while the STRDS still lists them. Before this PR, PostgreSQL rolled these updates back with the failed transaction. I have not tested what this mismatch breaks later.Five new tests in
grass_temporal_core_db_connection_test.pycheck that a failed transaction saves nothing, also when the caller keeps the connection or an error other than a database error stops it, thatclose()rolls back an open transaction, and thatclose()leaves the object disconnected when closing fails.Motivation and context
A failed transaction being saved in part was open point 1 in #8123 (comment). #8202 left it as it was.
How has this been tested?
Ubuntu 24.04 (WSL2), CMake build of
mainat 6ce29f8 and this branch, Python 3.13.18 with SQLite 3.53.1 in itssqlite3module, PostgreSQL 16.15 with psycopg2 2.9.13.grass_temporal_core_db_connection_test.py: 8 passed with this PR, 5 failed onmain(output below).nc_spm_full_v2beta1on SQLite: 24 monthly maps for 2020 and 2021 computed fromelevation, registered in several STRDS. On each STRDS, I tried to unregister June to November 2021 and made the attempt fail:t.unregisterwith its 4thDELETEfailing (a trigger stands in for a real error), the same calls from a Python session that catches the error, and that session again where the commit fails withdatabase is lockedbecause another process is reading. Onmain, 3 or all 6 maps were gone afterwards although each attempt reported an error,t.infostill counted 24, and the 2021 mean fromt.rast.aggregatechanged from 14.783 to 11.747 or 10.229. With this PR, all 24 maps were still there.mainand with this PR.close()after the connection broke underneath it (close_raises_check.py, output below): onmain,connectedstaysTruein all three cases. With this PR it isFalsein all three. On PostgreSQL,close()no longer raises, because it no longer callscommit(). On SQLite, closing the cursor still raisesProgrammingError.pytest.ymlon SQLite: 2291 + 69 + 94 passed.python/grass/temporal/tests: 171 passed, 0unclosed databasewarnings.nc_spm_full_v2beta1on SQLite:temporal/45 of 46 files (test_t_register_raster.pyfails intearDownClassonmaintoo),python/grass/temporal/13 of 13.init()locally so that each new mapset gets its own database throught.connect driver=pg. I also had to work around a bug onmainthat is not part of this PR:t.createfails withTypeError: can only concatenate str (not "bytes") to str, because psycopg2'scursor.mogrify()returns bytes. These runs were onmainat f3ea797 with grass.temporal: Close the SQLite connection in DBConnection.close() #8202, with the same transaction change (only the comments differ) and without theclose()change. The pytest steps had the same 7 failures as without the change, gunittest ontemporal/the same 3 failing files of 47, andpython/grass/temporal/13 of 13 files passed. Without the change, the algebra tests inpython/grass/temporal/testsuite/hung on a lock, because a connection stayedidle in transactionafter anUPDATEmade withexecute().pre-commit runon the changed files passes.Test output
pytest -v python/grass/temporal/tests/grass_temporal_core_db_connection_test.pywithcore.pyofmain(6ce29f8) and of this PR:close()after the connection broke underneath it.mainis from the same check on 7e611ee, whereclose()was the same as on 6ce29f8:Failed unregister on
nc_spm_full_v2beta1.unchangedandintendedare references (nothing removed, six maps removed by a workingt.unregister),tool,sessionandlockedthe failed attempts. "t.info" isnumber_of_mapsfromt.info -g, "listed" the maps int.rast.list, "series avg" the mean oft.rast.series method=average, "2020" and "2021" the means of thet.rast.aggregate granularity="1 years" method=averagemaps, and "maps listing it" how many of the six maps still have the STRDS in their own list of datasets:pytest steps of
pytest.ymlon SQLite with this PR:I used an AI assistant to set up PostgreSQL, write the tests and run the test suites; I reviewed the code and the results myself.