Merge ~blake-rouse/maas:fix-bad-postgres-connection into maas:master

Proposed by Blake Rouse
Status: Merged
Approved by: Blake Rouse
Approved revision: b843207535c85c8ca2fdfd9bc61976da4186108b
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~blake-rouse/maas:fix-bad-postgres-connection
Merge into: maas:master
Diff against target: 57 lines (+27/-0)
2 files modified
src/maasserver/utils/orm.py (+11/-0)
src/maasserver/utils/tests/test_orm.py (+16/-0)
Reviewer Review Type Date Requested Status
Mike Pontillo (community) Approve
Review via email: mp+332741@code.launchpad.net

Commit message

Ensure a good postgresql connection with the connected utility.

All the twisted helpers including the deferToDatabase use the connected() utility. This utility was not verifying that the postgresql connection that it could use was actually a good connection. This now ensures that it is a usable connection before yielding.

Description of the change

How to test:

sudo systemctl start postgresql
sudo systemctl start maas-regiond
sudo systemctl restart postgresql
sudo systemctl start maas-rackd

No stacktrace will occur with "connection already closed". Before this patch "sudo systemctl restart postgresql" would cause the regiond processes to hold onto a connection to a dead postgresql process.

To post a comment you must log in.
Revision history for this message
Mike Pontillo (mpontillo) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/maasserver/utils/orm.py b/src/maasserver/utils/orm.py
index 2ec0590..40f8a3f 100644
--- a/src/maasserver/utils/orm.py
+++ b/src/maasserver/utils/orm.py
@@ -616,14 +616,25 @@ def connected():
616616
617 If there is not yet a connection to the database, this will connect on617 If there is not yet a connection to the database, this will connect on
618 entry and disconnect on exit. Preexisting connections will be left alone.618 entry and disconnect on exit. Preexisting connections will be left alone.
619
620 If the preexisting connection is not usable it is closed and a new
621 connection is made.
619 """622 """
620 if connection.connection is None:623 if connection.connection is None:
624 connection.close_if_unusable_or_obsolete()
621 connection.ensure_connection()625 connection.ensure_connection()
622 try:626 try:
623 yield627 yield
624 finally:628 finally:
625 connection.close()629 connection.close()
630 elif connection.is_usable():
631 yield
626 else:632 else:
633 # Connection is not usable, so we disconnect and reconnect. Since
634 # the connection was previously connected we do not disconnect this
635 # new connection.
636 connection.close_if_unusable_or_obsolete()
637 connection.ensure_connection()
627 yield638 yield
628639
629640
diff --git a/src/maasserver/utils/tests/test_orm.py b/src/maasserver/utils/tests/test_orm.py
index 064f1b0..94ee817 100644
--- a/src/maasserver/utils/tests/test_orm.py
+++ b/src/maasserver/utils/tests/test_orm.py
@@ -1004,6 +1004,22 @@ class TestConnected(MAASTransactionServerTestCase):
1004 self.assertThat(connection.connection, Is(preexisting_connection))1004 self.assertThat(connection.connection, Is(preexisting_connection))
1005 self.assertThat(connection.connection, Is(preexisting_connection))1005 self.assertThat(connection.connection, Is(preexisting_connection))
10061006
1007 def test__disconnects_and_reconnects_if_not_usable(self):
1008 connection.ensure_connection()
1009 preexisting_connection = connection.connection
1010
1011 connection.errors_occurred = True
1012 self.patch(connection, "is_usable").return_value = False
1013
1014 self.assertThat(connection.connection, Not(Is(None)))
1015 with orm.connected():
1016 self.assertThat(
1017 connection.connection, Not(Is(preexisting_connection)))
1018 self.assertThat(connection.connection, Not(Is(None)))
1019
1020 self.assertThat(connection.connection, Not(Is(preexisting_connection)))
1021 self.assertThat(connection.connection, Not(Is(None)))
1022
10071023
1008class TestWithConnection(MAASTransactionServerTestCase):1024class TestWithConnection(MAASTransactionServerTestCase):
1009 """Tests for the `orm.with_connection` decorator."""1025 """Tests for the `orm.with_connection` decorator."""

Subscribers

People subscribed via source and target branches