Merge ~mariosplivalo/charm-mongodb:lp1420852 into ~mongodb-charmers/charm-mongodb:master

Proposed by Mario Splivalo
Status: Merged
Approved by: Jay Kuri
Approved revision: 410a8381c4328453c7bfd17c9ed047a16c39489d
Merged at revision: 96c4efc1d32fb34dcab2e6f18dd24a2fc3349214
Proposed branch: ~mariosplivalo/charm-mongodb:lp1420852
Merge into: ~mongodb-charmers/charm-mongodb:master
Diff against target: 96 lines (+29/-21)
2 files modified
hooks/hooks.py (+12/-21)
unit_tests/test_hooks.py (+17/-0)
Reviewer Review Type Date Requested Status
Jay Kuri (community) Approve
Review via email: mp+330837@code.launchpad.net

Commit message

Make replicaset-relation-broken not fail
Closes-Bug: LP #lp1436029

Description of the change

Make replicaset-relation-broken not fail

When units are being removed they are querying replicaset status
to locate the primary unit. Then, they use primary unit to remove
themselves from replicaset.

However, when the last unit is being removed (during complete
service removal) the 'primary' key in replicaseet status is no
longer there (as the unit switched to secondary, and there is no
primary).
This code change anticipates missing 'primary' key. If so, it will
cleanly exit the hook so that juju can remove the unit cleanly.

To post a comment you must log in.
Revision history for this message
Jay Kuri (jk0ne) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hooks/hooks.py b/hooks/hooks.py
2index eafce5a..d54919c 100755
3--- a/hooks/hooks.py
4+++ b/hooks/hooks.py
5@@ -93,13 +93,11 @@ hooks = Hooks()
6 default_mongodb_config = "/etc/mongodb.conf"
7 default_mongodb_init_config = "/etc/init/mongodb.conf"
8 default_mongos_list = "/etc/mongos.list"
9-default_wait_for = 10
10-default_max_tries = 5
11+default_wait_for = 3
12+default_max_tries = 7
13
14 INSTALL_PACKAGES = ['mongodb-server', 'python-yaml']
15
16-INIT_LOCKFILE = '/tmp/mongodb-charm.lock'
17-
18 # number of seconds init_replset will pause while looping to check if
19 # replicaset is initialized
20 INIT_CHECK_DELAY = 1.5
21@@ -1166,12 +1164,6 @@ def replica_set_relation_joined():
22 juju_log("replica_set_relation_joined-finish")
23
24
25-def in_replica_set():
26- c = Connection('localhost')
27- r = run_admin_command(c, 'isMaster')
28- return 'setName' in r
29-
30-
31 def am_i_primary():
32 c = Connection('localhost')
33 for i in xrange(10):
34@@ -1266,11 +1258,6 @@ def replica_set_relation_departed():
35 def replica_set_relation_broken():
36 juju_log('replica_set_relation_broken-start')
37
38- if not in_replica_set():
39- juju_log('not in replica set', level=DEBUG)
40- juju_log('replica_set_relation_broken-finish')
41- return
42-
43 if am_i_primary():
44 juju_log('I was primary - removing myself via new primary.', 'DEBUG')
45 mongo_client('localhost', 'rs.stepDown()')
46@@ -1278,12 +1265,16 @@ def replica_set_relation_broken():
47
48 c = Connection('localhost')
49 r = c.admin.command('isMaster')
50- master_node = r['primary']
51- unit = "%s:%s" % (unit_get('private-address'),
52- config('port'))
53-
54- juju_log('Removing myself via %s' % (master_node), 'DEBUG')
55- leave_replset(master_node, unit)
56+
57+ try:
58+ master_node = r['primary']
59+ except KeyError:
60+ pass
61+
62+ if 'master_node' in locals(): # unit is part of replicaset, remove it!
63+ unit = "%s:%s" % (unit_get('private-address'), config('port'))
64+ juju_log('Removing myself via %s' % (master_node), 'DEBUG')
65+ leave_replset(master_node, unit)
66
67 juju_log('replica_set_relation_broken-finish')
68
69diff --git a/unit_tests/test_hooks.py b/unit_tests/test_hooks.py
70index aad1a96..c95e480 100644
71--- a/unit_tests/test_hooks.py
72+++ b/unit_tests/test_hooks.py
73@@ -344,6 +344,23 @@ class MongoHooksTest(CharmTestCase):
74 call1 = call('juju-local:27017', 'juju-remote:27017')
75 mock_leave_replset.assert_has_calls([call1])
76
77+ @patch('time.sleep')
78+ @patch.object(hooks, 'Connection')
79+ @patch.object(hooks, 'unit_get')
80+ @patch.object(hooks, 'leave_replset')
81+ @patch.object(hooks, 'am_i_primary')
82+ def test_replica_set_relation_broken(self, mock_am_i_primary,
83+ mock_leave_replset, mock_unit_get,
84+ mock_Connection, mock_sleep):
85+
86+ mock_am_i_primary.return_value = False
87+ hooks.replica_set_relation_broken()
88+ self.assertEqual(1, mock_leave_replset.call_count)
89+ mock_am_i_primary.reset_mock()
90+ mock_leave_replset.reset_mock()
91+ mock_am_i_primary.return_value = True
92+ self.assertEqual(0, mock_leave_replset.call_count)
93+
94 def test_get_current_mongo_config(self):
95 test_config = u"""
96 # Comment

Subscribers

People subscribed via source and target branches

to status/vote changes: