Merge ~martin-hilton/charm-mongodb:004-reduce-restarts into ~mongodb-charmers/charm-mongodb:master

Proposed by Martin Hilton
Status: Merged
Approved by: Stuart Bishop
Approved revision: ec832f4a77659552fc2174f0025bf6ab11a24252
Merged at revision: 3c1439fe6ce619d6dca082f8aadcdf7d36480a99
Proposed branch: ~martin-hilton/charm-mongodb:004-reduce-restarts
Merge into: ~mongodb-charmers/charm-mongodb:master
Diff against target: 130 lines (+44/-33)
1 file modified
hooks/hooks.py (+44/-33)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+362290@code.launchpad.net

Commit message

Reduce the amount of unnecessary service restarts.

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Stuart Bishop (stub) wrote :

Looks good. I think there is an unnecessary import, per inline comment, which can be removed before landing.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 3c1439fe6ce619d6dca082f8aadcdf7d36480a99

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 c23198b..25e1d69 100755
3--- a/hooks/hooks.py
4+++ b/hooks/hooks.py
5@@ -46,6 +46,7 @@ from charmhelpers.fetch import (
6 from charmhelpers.core.host import (
7 service,
8 lsb_release,
9+ file_hash,
10 )
11
12 from charmhelpers.core.hookenv import (
13@@ -117,7 +118,7 @@ MONGO_DOWN = 8
14 MONGO_ROLLBACK = 9
15 MONGO_REMOVED = 10
16
17-# Ill and quick way to make relatinon-departed and relation-broken communicate
18+# Ill and quick way to make relation-departed and relation-broken communicate
19 was_i_primary = False
20
21 ###############################################################################
22@@ -976,35 +977,13 @@ def config_changed():
23 status_set('maintenance', 'Configuring unit')
24 config_data = config()
25 juju_log("config_data: {}".format(config_data), level=DEBUG)
26+ mongodb_config_hash = file_hash(default_mongodb_config)
27 mongodb_config = open(default_mongodb_config).read()
28
29- # Trigger volume initialization logic for permanent storage
30- volid = volume_get_volume_id()
31- if not volid:
32- # Invalid configuration (whether ephemeral, or permanent)
33- stop_hook()
34- mounts = volume_get_all_mounted()
35- if mounts:
36- juju_log("current mounted volumes: {}".format(mounts))
37- juju_log(
38- "Disabled and stopped mongodb service, "
39- "because of broken volume configuration - check "
40- "'volume-ephemeral-storage' and 'volume-map'")
41- sys.exit(1)
42- if volume_is_permanent(volid):
43- # config_changed_volume_apply will stop the service if it finds
44- # it necessary, ie: new volume setup
45- if config_changed_volume_apply():
46- start_hook()
47- else:
48- stop_hook()
49- mounts = volume_get_all_mounted()
50- if mounts:
51- juju_log("current mounted volumes: {}".format(mounts))
52- juju_log(
53- "Disabled and stopped mongodb service "
54- "(config_changed_volume_apply failure)")
55- sys.exit(1)
56+ if config_data.changed('volume-ephemeral-storage') or \
57+ config_data.changed('volume-map') or \
58+ config_data.changed('volume-dev-regexp'):
59+ config_changed_volume()
60
61 # current ports
62 current_mongodb_port = re.search(r'^#*port\s+=\s+(\w+)',
63@@ -1047,9 +1026,10 @@ def config_changed():
64 # write mongodb logrotate configuration file
65 write_logrotate_config(config_data)
66
67- # restart mongodb
68- status_set('maintenance', 'Restarting mongod')
69- restart_mongod()
70+ # restart mongodb if the configuration file has changed.
71+ if file_hash(default_mongodb_config) != mongodb_config_hash:
72+ status_set('maintenance', 'Restarting mongod')
73+ restart_mongod()
74
75 # attach to replSet ( if needed )
76 if config_data['replicaset_master'] != "auto":
77@@ -1421,13 +1401,13 @@ def data_relation_changed():
78 juju_log("mountpoint from storage subordinate not ready, let's wait")
79 return(True)
80
81- config_changed()
82+ config_changed_volume()
83
84
85 @hooks.hook('data-relation-departed')
86 def data_relation_departed():
87 juju_log("data_relation_departed")
88- return(config_changed())
89+ return(config_changed_volume())
90
91
92 @hooks.hook('configsvr-relation-joined')
93@@ -1705,6 +1685,37 @@ def volume_get_volume_id():
94 return volid
95
96
97+# Re-initialize the mounted data volume, if necessary.
98+def config_changed_volume():
99+ # Trigger volume initialization logic for permanent storage
100+ volid = volume_get_volume_id()
101+ if not volid:
102+ # Invalid configuration (whether ephemeral, or permanent)
103+ stop_hook()
104+ mounts = volume_get_all_mounted()
105+ if mounts:
106+ juju_log("current mounted volumes: {}".format(mounts))
107+ juju_log(
108+ "Disabled and stopped mongodb service, "
109+ "because of broken volume configuration - check "
110+ "'volume-ephemeral-storage' and 'volume-map'")
111+ sys.exit(1)
112+ if volume_is_permanent(volid):
113+ # config_changed_volume_apply will stop the service if it finds
114+ # it necessary, ie: new volume setup
115+ if config_changed_volume_apply():
116+ start_hook()
117+ else:
118+ stop_hook()
119+ mounts = volume_get_all_mounted()
120+ if mounts:
121+ juju_log("current mounted volumes: {}".format(mounts))
122+ juju_log(
123+ "Disabled and stopped mongodb service "
124+ "(config_changed_volume_apply failure)")
125+ sys.exit(1)
126+
127+
128 # Initialize and/or mount permanent storage, it straightly calls
129 # shell helper
130 def volume_init_and_mount(volid):

Subscribers

People subscribed via source and target branches

to status/vote changes: