code: protected void detectJournalManager() throws IOException {
      int failures = 0;
      do {
        try {
          Stat stat = new Stat();
          String primaryAddr = zk.getPrimaryAvatarAddress(logicalName, 
                                                       stat, true, true);
          if (primaryAddr == null || primaryAddr.trim().isEmpty()) {
            primaryURI = null;
            remoteJournalManager = null;
            LOG.warn("Failover detected, wait for it to finish...");
            failures = 0;
            sleep(FAILOVER_RETRY_SLEEP);
            continue;
          }
          
          primaryURI = addrToURI(primaryAddr);
          
          LOG.info("Read primary URI from zk: " + primaryURI);
          if (primaryURI.equals(avatarZeroURI)) {
            remoteJournalManager = remoteJournalManagerZero;
          } else if (primaryURI.equals(avatarOneURI)) {
            remoteJournalManager = remoteJournalManagerOne;
          } else {
            LOG.warn("Invalid primaryURI: " + primaryURI);
            primaryURI = null;
            remoteJournalManager = null;
            failures = 0;
            sleep(FAILOVER_RETRY_SLEEP);
          }
        
        } catch (KeeperException kex) {
          if (KeeperException.Code.CONNECTIONLOSS == kex.code()
              && failures < AvatarZooKeeperClient.ZK_CONNECTION_RETRIES) {
            failures++;
            // This means there was a failure connecting to zookeeper
            // we should retry since some nodes might be down.
            sleep(FAILOVER_RETRY_SLEEP);
            continue;
          }
          throwIOException(kex.getMessage(), kex);
        } catch (InterruptedException e) {
          throwIOException(e.getMessage(), e);
        } catch (URISyntaxException e) {
          throwIOException(e.getMessage(), e);
        }
      } while (remoteJournalManager == null);
    }
docs: Detect the primary node and the current Journal Manager;
  @throws IOException