public static String findUnallocatedFileName(HdfsApi hdfs, String fullPathAndFilename, String extension)
      throws HdfsApiException {
    int triesCount = 0;
    String newFilePath;
    boolean isUnallocatedFilenameFound;

    try {
      do {
        newFilePath = String.format(fullPathAndFilename + "%s" + extension, (triesCount == 0) ? "" : "_" + triesCount);
        LOG.debug("Trying to find free filename " + newFilePath);

        isUnallocatedFilenameFound = !hdfs.exists(newFilePath);
        if (isUnallocatedFilenameFound) {
          LOG.debug("File created successfully!");
        }

        triesCount += 1;
        if (triesCount > 1000) {
          throw new HdfsApiException("HDFS100 Can't find unallocated file name " + fullPathAndFilename + "...");
        }
      } while (!isUnallocatedFilenameFound);
    } catch (IOException e) {
      throw new HdfsApiException("HDFS030 Error in creation " + fullPathAndFilename + "...", e);
    } catch (InterruptedException e) {
      throw new HdfsApiException("HDFS031 Error in creation " + fullPathAndFilename + "...", e);
    }

    return newFilePath;
  }
Increment index appended to filename until find first unallocated file
@param fullPathAndFilename path to file and prefix for filename
@param extension file extension
@return if fullPathAndFilename="/tmp/file",extension=".txt" then filename will be like "/tmp/file_42.txt"
contrib/views/utils/src/main/java/org/apache/ambari/view/utils/hdfs/HdfsUtil.java