protected void writeLeaderInformation ( UUID leaderSessionID ) { // this method does not have to be synchronized because the curator framework client // is thread-safe try { if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( "Write leader information: Leader={}, session ID={}." , leaderContender . getAddress ( ) , leaderSessionID ) ; } ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; ObjectOutputStream oos = new ObjectOutputStream ( baos ) ; oos . writeUTF ( leaderContender . getAddress ( ) ) ; oos . writeObject ( leaderSessionID ) ; oos . close ( ) ; boolean dataWritten = false ; while ( ! dataWritten && leaderLatch . hasLeadership ( ) ) { Stat stat = client . checkExists ( ) . forPath ( leaderPath ) ; if ( stat != null ) { long owner = stat . getEphemeralOwner ( ) ; long sessionID = client . getZookeeperClient ( ) . getZooKeeper ( ) . getSessionId ( ) ; if ( owner == sessionID ) { try { client . setData ( ) . forPath ( leaderPath , baos . toByteArray ( ) ) ; dataWritten = true ; } catch ( KeeperException . NoNodeException noNode ) { // node was deleted in the meantime } } else { try { client . delete ( ) . forPath ( leaderPath ) ; } catch ( KeeperException . NoNodeException noNode ) { // node was deleted in the meantime --> try again } } } else { try { client . create ( ) . creatingParentsIfNeeded ( ) . withMode ( CreateMode . EPHEMERAL ) . forPath ( leaderPath , baos . toByteArray ( ) ) ; dataWritten = true ; } catch ( KeeperException . NodeExistsException nodeExists ) { // node has been created in the meantime --> try again } } } if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( "Successfully wrote leader information: Leader={}, session ID={}." , leaderContender . getAddress ( ) , leaderSessionID ) ; } } catch ( Exception e ) { leaderContender . handleError ( new Exception ( "Could not write leader address and leader session ID to " + "ZooKeeper." , e ) ) ; } }
Writes the current leader s address as well the given leader session ID to ZooKeeper .