Merge lp:~allenap/maas/purge-commit-within-atomic-block into lp:~maas-committers/maas/trunk

Proposed by Gavin Panella
Status: Merged
Approved by: Gavin Panella
Approved revision: no longer in the source branch.
Merged at revision: 4358
Proposed branch: lp:~allenap/maas/purge-commit-within-atomic-block
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 187 lines (+1/-117)
3 files modified
src/maasserver/utils/orm.py (+0/-41)
src/maasserver/utils/tests/test_orm.py (+1/-60)
src/maasserver/websockets/handlers/device.py (+0/-16)
To merge this branch: bzr merge lp:~allenap/maas/purge-commit-within-atomic-block
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+273624@code.launchpad.net

Commit message

Purge the last remnants of commit_within_atomic_block().

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/maasserver/utils/orm.py'
--- src/maasserver/utils/orm.py 2015-09-24 16:22:12 +0000
+++ src/maasserver/utils/orm.py 2015-10-06 23:57:19 +0000
@@ -13,7 +13,6 @@
1313
14__metaclass__ = type14__metaclass__ = type
15__all__ = [15__all__ = [
16 'commit_within_atomic_block',
17 'disable_all_database_connections',16 'disable_all_database_connections',
18 'enable_all_database_connections',17 'enable_all_database_connections',
19 'ExclusivelyConnected',18 'ExclusivelyConnected',
@@ -59,7 +58,6 @@
59from django.db.transaction import TransactionManagementError58from django.db.transaction import TransactionManagementError
60from django.db.utils import OperationalError59from django.db.utils import OperationalError
61from maasserver.utils.async import DeferredHooks60from maasserver.utils.async import DeferredHooks
62from provisioningserver.utils import warn_deprecated
63from provisioningserver.utils.backoff import (61from provisioningserver.utils.backoff import (
64 exponential_growth,62 exponential_growth,
65 full_jitter,63 full_jitter,
@@ -514,45 +512,6 @@
514 "Savepoints cannot be created outside of a transaction.")512 "Savepoints cannot be created outside of a transaction.")
515513
516514
517def commit_within_atomic_block(using="default"):
518 """Exits an atomic block then immediately re-enters a new one.
519
520 This relies on the fact that an atomic block commits when exiting the
521 outer-most context.
522
523 :deprecated: Absolutely don't use this.
524 """
525 warn_deprecated("use post-commit hooks instead")
526 with outside_atomic_block(using):
527 pass # We just want to exit and enter.
528
529
530@contextmanager
531def outside_atomic_block(using="default"):
532 """A context manager that guarantees to not contain an atomic block.
533
534 On entry into this context, this will exit all nested and unnested atomic
535 blocks until it reaches clear air.
536
537 On exit from this context, the same level of nesting will be
538 reestablished.
539 """
540 connection = transaction.get_connection(using)
541 atomic_context = transaction.atomic(using)
542 assert connection.in_atomic_block
543
544 depth = 0
545 while connection.in_atomic_block:
546 atomic_context.__exit__(None, None, None)
547 depth = depth + 1
548 try:
549 yield
550 finally:
551 while depth > 0:
552 atomic_context.__enter__()
553 depth = depth - 1
554
555
556def in_transaction(connection=connection):515def in_transaction(connection=connection):
557 """Is `connection` in the midst of a transaction?516 """Is `connection` in the midst of a transaction?
558517
559518
=== modified file 'src/maasserver/utils/tests/test_orm.py'
--- src/maasserver/utils/tests/test_orm.py 2015-09-24 16:22:12 +0000
+++ src/maasserver/utils/tests/test_orm.py 2015-10-06 23:57:19 +0000
@@ -31,13 +31,9 @@
31from django.db.transaction import TransactionManagementError31from django.db.transaction import TransactionManagementError
32from django.db.utils import OperationalError32from django.db.utils import OperationalError
33from maasserver.fields import MAC33from maasserver.fields import MAC
34from maasserver.testing.testcase import (34from maasserver.testing.testcase import SerializationFailureTestCase
35 MAASTransactionServerTestCase,
36 SerializationFailureTestCase,
37)
38from maasserver.utils import orm35from maasserver.utils import orm
39from maasserver.utils.orm import (36from maasserver.utils.orm import (
40 commit_within_atomic_block,
41 disable_all_database_connections,37 disable_all_database_connections,
42 DisabledDatabaseConnection,38 DisabledDatabaseConnection,
43 enable_all_database_connections,39 enable_all_database_connections,
@@ -52,7 +48,6 @@
52 macs_contain,48 macs_contain,
53 macs_do_not_contain,49 macs_do_not_contain,
54 make_serialization_failure,50 make_serialization_failure,
55 outside_atomic_block,
56 post_commit,51 post_commit,
57 post_commit_do,52 post_commit_do,
58 post_commit_hooks,53 post_commit_hooks,
@@ -781,60 +776,6 @@
781 self.expectThat(connection.savepoint_ids, HasLength(0))776 self.expectThat(connection.savepoint_ids, HasLength(0))
782777
783778
784class TestOutsideAtomicBlock(MAASTransactionServerTestCase):
785 """Tests for `outside_atomic_block`."""
786
787 def test__leaves_and_restores_atomic_block(self):
788 self.assertFalse(connection.in_atomic_block)
789 with transaction.atomic():
790 self.assertTrue(connection.in_atomic_block)
791 with outside_atomic_block():
792 self.assertFalse(connection.in_atomic_block)
793 self.assertTrue(connection.in_atomic_block)
794 self.assertFalse(connection.in_atomic_block)
795
796 def test__leaves_and_restores_multiple_levels_of_atomic_blocks(self):
797 self.assertFalse(connection.in_atomic_block)
798 with transaction.atomic():
799 with transaction.atomic():
800 with transaction.atomic():
801 with transaction.atomic():
802 with outside_atomic_block():
803 # It leaves the multiple levels of atomic blocks,
804 # but puts the same number of levels back in place
805 # on exit.
806 self.assertFalse(connection.in_atomic_block)
807 self.assertTrue(connection.in_atomic_block)
808 self.assertTrue(connection.in_atomic_block)
809 self.assertTrue(connection.in_atomic_block)
810 self.assertTrue(connection.in_atomic_block)
811 self.assertFalse(connection.in_atomic_block)
812
813 def test__restores_atomic_block_even_on_error(self):
814 with transaction.atomic():
815 exception_type = factory.make_exception_type()
816 try:
817 with outside_atomic_block():
818 raise exception_type()
819 except exception_type:
820 self.assertTrue(connection.in_atomic_block)
821
822
823class TestCommitWithinAtomicBlock(MAASTransactionServerTestCase):
824 """Tests for `commit_within_atomic_block`."""
825
826 def test__relies_on_outside_atomic_block(self):
827 outside_atomic_block = self.patch(orm, "outside_atomic_block")
828 with transaction.atomic():
829 commit_within_atomic_block()
830 self.expectThat(outside_atomic_block, MockCalledOnceWith("default"))
831 context_manager = outside_atomic_block.return_value
832 self.expectThat(
833 context_manager.__enter__, MockCalledOnceWith())
834 self.expectThat(
835 context_manager.__exit__, MockCalledOnceWith(None, None, None))
836
837
838class TestInTransaction(DjangoTransactionTestCase):779class TestInTransaction(DjangoTransactionTestCase):
839 """Tests for `in_transaction`."""780 """Tests for `in_transaction`."""
840781
841782
=== modified file 'src/maasserver/websockets/handlers/device.py'
--- src/maasserver/websockets/handlers/device.py 2015-09-24 16:22:12 +0000
+++ src/maasserver/websockets/handlers/device.py 2015-10-06 23:57:19 +0000
@@ -31,7 +31,6 @@
31from maasserver.models.staticipaddress import StaticIPAddress31from maasserver.models.staticipaddress import StaticIPAddress
32from maasserver.models.subnet import Subnet32from maasserver.models.subnet import Subnet
33from maasserver.node_action import compile_node_actions33from maasserver.node_action import compile_node_actions
34from maasserver.utils.orm import commit_within_atomic_block
35from maasserver.websockets.base import (34from maasserver.websockets.base import (
36 HandlerDoesNotExistError,35 HandlerDoesNotExistError,
37 HandlerError,36 HandlerError,
@@ -77,21 +76,6 @@
77 return None76 return None
7877
7978
80def delete_device_and_static_ip_addresses(
81 device, external_static_ips, assigned_sticky_ips):
82 """Delete the created external and sticky ips and the created device.
83
84 This function calls `commit_within_atomic_block` to force the transaction
85 to be saved.
86 """
87 for static_ip, _ in external_static_ips:
88 static_ip.deallocate()
89 for static_ip, _ in assigned_sticky_ips:
90 static_ip.deallocate()
91 device.delete()
92 commit_within_atomic_block()
93
94
95def log_static_allocations(device, external_static_ips, assigned_sticky_ips):79def log_static_allocations(device, external_static_ips, assigned_sticky_ips):
96 """Log the allocation of the static ip address."""80 """Log the allocation of the static ip address."""
97 all_ips = [81 all_ips = [