static long getSpaceUsed(Set<Path> paths) {
    ArrayDeque<Path> pathStack = new ArrayDeque<>();

    for (Path path : paths) {
      pathStack.push(path);
    }

    // Track the directories we've visited to ensure we're not double counting. It would be
    // preferable to resolve overlap once at startup, but the problem is that the filesystem may
    // change over time and, in fact, at startup I found that the rocks DB store directory was not
    // created by the time the disk space monitor was started.
    Set<Path> visited = new HashSet<>();
    long totalBytes = 0;
    while (!pathStack.isEmpty()) {
      try {
        // We need to resolve to the real path to ensure that we don't inadvertently double count
        // due to different paths to the same directory (e.g. /A and /A/../A).
        Path current = pathStack.pop().toRealPath();

        if (visited.contains(current)) {
          continue;
        }
        visited.add(current);

        BasicFileAttributes currentAttrs = Files.readAttributes(current,
                                                                BasicFileAttributes.class);
        if (currentAttrs.isDirectory()) {
          try (DirectoryStream<Path> directoryListing = Files.newDirectoryStream(current)) {
            for (Path child : directoryListing) {
              pathStack.push(child);
            }
          }
        } else if (currentAttrs.isRegularFile()) {
          totalBytes += currentAttrs.size();
        }
      } catch (IOException e) {
        // If we can't stat the file, just ignore it. This can happen, for example, if we scan
        // a directory, but by the time we get to stat'ing the file it has been deleted (e.g.
        // due to compaction, rotation, etc.).
      }
    }

    return totalBytes;
  }
Returns the total size in bytes used by the specified paths. This function guarantees that it
will not double count overlapping portions of the path set. For example, with a trivial
overlap of /A and /A, it will count /A only once. It also handles other types of overlaps
similarly, such as counting /A/B only once given the paths /A and /A/B.
<p>
This function is exposed as package private to simplify testing various cases without involving
an executor. Alternatively this could have been pulled out to a utility class, but it would
unnecessarily pollute the global namespace.
samza-core/src/main/java/org/apache/samza/container/disk/PollingScanDiskSpaceMonitor.java