static ZookeeperTxId checkZooKeeperBeforeFailover ( Configuration startupConf , Configuration confg , boolean noverification ) throws IOException { AvatarZooKeeperClient zk = null ; String fsname = startupConf . get ( NameNode . DFS_NAMENODE_RPC_ADDRESS_KEY ) ; int maxTries = startupConf . getInt ( "dfs.avatarnode.zk.retries" , 3 ) ; Exception lastException = null ; for ( int i = 0 ; i < maxTries ; i ++ ) { try { zk = new AvatarZooKeeperClient ( confg , null , false ) ; LOG . info ( "Failover: Checking if the primary is empty" ) ; String zkRegistry = zk . getPrimaryAvatarAddress ( fsname , new Stat ( ) , false , i > 0 ) ; if ( zkRegistry != null ) { throw new IOException ( "Can't switch the AvatarNode to primary since " + "zookeeper record is not clean. Either use shutdownAvatar to kill " + "the current primary and clean the ZooKeeper entry, " + "or clear out the ZooKeeper entry if the primary is dead" ) ; } if ( noverification ) { return null ; } LOG . info ( "Failover: Obtaining last transaction id from ZK" ) ; String address = startupConf . get ( NameNode . DFS_NAMENODE_RPC_ADDRESS_KEY ) ; long sessionId = zk . getPrimarySsId ( address , i > 0 ) ; ZookeeperTxId zkTxId = zk . getPrimaryLastTxId ( address , i > 0 ) ; if ( sessionId != zkTxId . getSessionId ( ) ) { throw new IOException ( "Session Id in the ssid node : " + sessionId + " does not match the session Id in the txid node : " + zkTxId . getSessionId ( ) ) ; } return zkTxId ; } catch ( Exception e ) { LOG . error ( "Got Exception reading primary node registration " + "from ZooKeeper. Will retry..." , e ) ; lastException = e ; } finally { shutdownZkClient ( zk ) ; } } throw new IOException ( lastException ) ; }
Verifies whether we are in a consistent state before we perform a failover