diff options
| author | Mario Splivalo <mario.splivalo@canonical.com> | 2015-01-22 02:54:25 +0100 |
|---|---|---|
| committer | Mario Splivalo <mario.splivalo@canonical.com> | 2015-01-22 02:54:25 +0100 |
| commit | 6b5efd041473501d1392df585f65d8ca814e1b02 (patch) | |
| tree | 597f24e5d5ac1ac3a3e470cb39f6d8389d557434 /hooks | |
| parent | f40848307af59d2ff994ee1e3a05e66027eed310 (diff) | |
Try not to destroy replicaset if primary is being removed
Diffstat (limited to 'hooks')
| -rwxr-xr-x | hooks/hooks.py | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py index ec55a65..0f37f76 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -108,6 +108,9 @@ MONGO_DOWN = 8 MONGO_ROLLBACK = 9 MONGO_REMOVED = 10 +# Ill and quick way to make relatinon-departed and relation-broken communicate +was_i_primary = False + ############################################################################### # Supporting functions ############################################################################### @@ -1190,6 +1193,7 @@ def replica_set_relation_changed(): juju_log('replica_set_relation_changed-finish') + @hooks.hook('replicaset-relation-departed') def replica_set_relation_departed(): juju_log('replica_set_relation_departed-start') @@ -1197,15 +1201,49 @@ def replica_set_relation_departed(): if not am_i_primary(): juju_log('replica_set_relation_departed-finish') return - - unit = "%s:%s" % (unit_get('private-address'), + + unit_address, unit_port = unit_get('private-address'), config('port') + remote_address = relation_get('private-address') + remote_port = relation_get('port') + + # If I am the unit being removed, step me down from being primary + if (unit_address, unit_port) == (remote_address, remote_port): + juju_log('Stepping down from being primary...') + global was_i_primary + was_i_primary = True + mongo_client('localhost', 'rs.stepDown()') + juju_log('replica_set_relation_departed-finish') + return + + unit = "%s:%s" % (unit_get('private-address'), config('port')) - unit_remote = "%s:%s" % (relation_get('hostname'), + unit_remote = "%s:%s" % (relation_get('hostname'), relation_get('port')) + leave_replset(unit, unit_remote) juju_log('Removed %s from replicaset' % unit_remote) - juju_log('replica_set_relation_departed-finish') + + +@hooks.hook('replicaset-relation-broken') +def replica_set_relation_broken(): + juju_log('replica_set_relation_broken-start') + + if am_i_primary(): + juju_log('I was primary - removing myself via new primary.', 'DEBUG') + mongo_client('localhost', 'rs.stepDown()') + time.sleep(5) # give some time to for re-election to happen + + c = Connection('localhost') + r = c.admin.command('isMaster') + master_node = r['primary'] + unit = "%s:%s" % (unit_get('private-address'), + config('port')) + + juju_log('Removing myself via %s' % (master_node), 'DEBUG') + leave_replset(master_node, unit) + + juju_log('replica_set_relation_broken-finish') @hooks.hook('data-relation-joined') |
