code: public synchronized int getPrimaryMasterIndex(int timeoutMs)
        throws TimeoutException, InterruptedException {
      final FileSystem fs = getFileSystemClient();
      final MasterInquireClient inquireClient = getMasterInquireClient();
      CommonUtils.waitFor("a primary master to be serving", () -> {
        try {
          // Make sure the leader is serving.
          fs.getStatus(new AlluxioURI("/"));
          return true;
        } catch (UnavailableException e) {
          return false;
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }, WaitForOptions.defaults().setTimeoutMs(timeoutMs));
      int primaryRpcPort;
      try {
        primaryRpcPort = inquireClient.getPrimaryRpcAddress().getPort();
      } catch (UnavailableException e) {
        throw new RuntimeException(e);
      }
      // Returns the master whose RPC port matches the primary RPC port.
      for (int i = 0; i < mMasterAddresses.size(); i++) {
        if (mMasterAddresses.get(i).getRpcPort() == primaryRpcPort) {
          return i;
        }
      }
      throw new RuntimeException(
          String.format("No master found with RPC port %d. Master addresses: %s", primaryRpcPort,
              mMasterAddresses));
    }
docs: Gets the index of the primary master.
  
  @param timeoutMs maximum amount of time to wait, in milliseconds
  @return the index of the primary master