From e9d7761e78b5b0e71460c879f7cfde9156ba49df Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Sat, 29 Sep 2018 01:45:23 -0400 Subject: [PATCH] Add a dedicated primary key to keep master selection consistent --- router-daemon/pvcrd/Daemon.py | 19 +++++++++++++++---- router-daemon/pvcrd/RouterInstance.py | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/router-daemon/pvcrd/Daemon.py b/router-daemon/pvcrd/Daemon.py index ceaf12a5..04dca1ce 100644 --- a/router-daemon/pvcrd/Daemon.py +++ b/router-daemon/pvcrd/Daemon.py @@ -201,6 +201,14 @@ else: transaction.create('/routers/{}/ipmipassword'.format(myhostname), config['ipmi_password'].encode('ascii')) transaction.commit() +# Check that the primary key exists, and create it with us as master if not +current_primary = zkhandler.readdata(zk_conn, '/routers') +if current_primary: + print('Current primary router is {}"{}"{}.'.format(ansiiprint.blue(), current_primary, ansiiprint.end())) +else: + print('No primary router key found; creating with us as primary.') + zkhandler.writedata(zk_conn, { '/routers': myhostname }) + zkhandler.writedata(zk_conn, { '/routers/{}/daemonstate'.format(myhostname): 'init' }) t_router = dict() @@ -224,6 +232,13 @@ this_router = t_router[myhostname] update_zookeeper = this_router.update_zookeeper update_zookeeper() +@zk_conn.DataWatch('/routers') +def updateprimary(data, meta): + if data == myhostname: + this_router.set_primary() + else: + this_router.set_secondary() + @zk_conn.ChildrenWatch('/networks') def updatenetworks(new_network_list): global network_list @@ -239,10 +254,6 @@ def updatenetworks(new_network_list): network_list = new_network_list print(ansiiprint.blue() + 'Network list: ' + ansiiprint.end() + '{}'.format(' '.join(network_list))) -# Ensure we force startup of interfaces if we're primary -if this_router.getnetworkstate() == 'primary': - this_router.become_primary() - # Create timer to update this router in Zookeeper def createKeepaliveTimer(): interval = int(config['keepalive_interval']) diff --git a/router-daemon/pvcrd/RouterInstance.py b/router-daemon/pvcrd/RouterInstance.py index 037d2703..d4ef5cda 100644 --- a/router-daemon/pvcrd/RouterInstance.py +++ b/router-daemon/pvcrd/RouterInstance.py @@ -107,8 +107,13 @@ class RouterInstance(): self.s_network[network].removeAddress() def set_secondary(self): - zkhandler.writedata(self.zk_conn, { '/routers/{}/networkstate'.format(self.name): 'secondary' }) - + try: + zkhandler.writedata(self.zk_conn, { + '/routers/{}/networkstate'.format(self.name): 'secondary' + }) + except: + time.sleep(1) + def become_primary(self): ansiiprint.echo('Setting router {} to primary state.'.format(self.name), '', 'i') ansiiprint.echo('Network list: {}'.format(', '.join(self.network_list)), '', 'c') @@ -120,7 +125,13 @@ class RouterInstance(): zkhandler.writedata(self.zk_conn, { '/routers/{}/networkstate'.format(self.t_router[router].getname()): 'secondary' }) def set_primary(self): - zkhandler.writedata(self.zk_conn, { '/routers/{}/networkstate'.format(self.name): 'primary' }) + try: + zkhandler.writedata(self.zk_conn, { + '/routers/primary': self.name, + '/routers/{}/networkstate'.format(self.name): 'primary', + }) + except: + time.sleep(1) def update_zookeeper(self):