summaryrefslogtreecommitdiff
diff options
authorMartin Hilton <martin.hilton@canonical.com>2019-01-28 07:53:42 +0000
committerCanonial IS Mergebot <canonical-is-mergebot@canonical.com>2019-01-28 07:53:42 +0000
commit3c1439fe6ce619d6dca082f8aadcdf7d36480a99 (patch)
tree98b8784086aa987664e8115b9aa4e2d2587feedc
parentf478e62856504dc484dfdc005fb368bb29fbb727 (diff)
parentec832f4a77659552fc2174f0025bf6ab11a24252 (diff)
Reduce the amount of unnecessary service restarts.
Reviewed-on: https://code.launchpad.net/~martin-hilton/mongodb-charm/+git/mongodb-charm/+merge/362290 Reviewed-by: Stuart Bishop <stuart.bishop@canonical.com>
-rwxr-xr-xhooks/hooks.py77
1 files changed, 44 insertions, 33 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py
index c23198b..25e1d69 100755
--- a/hooks/hooks.py
+++ b/hooks/hooks.py
@@ -46,6 +46,7 @@ from charmhelpers.fetch import (
from charmhelpers.core.host import (
service,
lsb_release,
+ file_hash,
)
from charmhelpers.core.hookenv import (
@@ -117,7 +118,7 @@ MONGO_DOWN = 8
MONGO_ROLLBACK = 9
MONGO_REMOVED = 10
-# Ill and quick way to make relatinon-departed and relation-broken communicate
+# Ill and quick way to make relation-departed and relation-broken communicate
was_i_primary = False
###############################################################################
@@ -976,35 +977,13 @@ def config_changed():
status_set('maintenance', 'Configuring unit')
config_data = config()
juju_log("config_data: {}".format(config_data), level=DEBUG)
+ mongodb_config_hash = file_hash(default_mongodb_config)
mongodb_config = open(default_mongodb_config).read()
- # Trigger volume initialization logic for permanent storage
- volid = volume_get_volume_id()
- if not volid:
- # Invalid configuration (whether ephemeral, or permanent)
- stop_hook()
- mounts = volume_get_all_mounted()
- if mounts:
- juju_log("current mounted volumes: {}".format(mounts))
- juju_log(
- "Disabled and stopped mongodb service, "
- "because of broken volume configuration - check "
- "'volume-ephemeral-storage' and 'volume-map'")
- sys.exit(1)
- if volume_is_permanent(volid):
- # config_changed_volume_apply will stop the service if it finds
- # it necessary, ie: new volume setup
- if config_changed_volume_apply():
- start_hook()
- else:
- stop_hook()
- mounts = volume_get_all_mounted()
- if mounts:
- juju_log("current mounted volumes: {}".format(mounts))
- juju_log(
- "Disabled and stopped mongodb service "
- "(config_changed_volume_apply failure)")
- sys.exit(1)
+ if config_data.changed('volume-ephemeral-storage') or \
+ config_data.changed('volume-map') or \
+ config_data.changed('volume-dev-regexp'):
+ config_changed_volume()
# current ports
current_mongodb_port = re.search(r'^#*port\s+=\s+(\w+)',
@@ -1047,9 +1026,10 @@ def config_changed():
# write mongodb logrotate configuration file
write_logrotate_config(config_data)
- # restart mongodb
- status_set('maintenance', 'Restarting mongod')
- restart_mongod()
+ # restart mongodb if the configuration file has changed.
+ if file_hash(default_mongodb_config) != mongodb_config_hash:
+ status_set('maintenance', 'Restarting mongod')
+ restart_mongod()
# attach to replSet ( if needed )
if config_data['replicaset_master'] != "auto":
@@ -1421,13 +1401,13 @@ def data_relation_changed():
juju_log("mountpoint from storage subordinate not ready, let's wait")
return(True)
- config_changed()
+ config_changed_volume()
@hooks.hook('data-relation-departed')
def data_relation_departed():
juju_log("data_relation_departed")
- return(config_changed())
+ return(config_changed_volume())
@hooks.hook('configsvr-relation-joined')
@@ -1705,6 +1685,37 @@ def volume_get_volume_id():
return volid
+# Re-initialize the mounted data volume, if necessary.
+def config_changed_volume():
+ # Trigger volume initialization logic for permanent storage
+ volid = volume_get_volume_id()
+ if not volid:
+ # Invalid configuration (whether ephemeral, or permanent)
+ stop_hook()
+ mounts = volume_get_all_mounted()
+ if mounts:
+ juju_log("current mounted volumes: {}".format(mounts))
+ juju_log(
+ "Disabled and stopped mongodb service, "
+ "because of broken volume configuration - check "
+ "'volume-ephemeral-storage' and 'volume-map'")
+ sys.exit(1)
+ if volume_is_permanent(volid):
+ # config_changed_volume_apply will stop the service if it finds
+ # it necessary, ie: new volume setup
+ if config_changed_volume_apply():
+ start_hook()
+ else:
+ stop_hook()
+ mounts = volume_get_all_mounted()
+ if mounts:
+ juju_log("current mounted volumes: {}".format(mounts))
+ juju_log(
+ "Disabled and stopped mongodb service "
+ "(config_changed_volume_apply failure)")
+ sys.exit(1)
+
+
# Initialize and/or mount permanent storage, it straightly calls
# shell helper
def volume_init_and_mount(volid):