apache/hadoop · error · InvalidPathException

Wrong FS: {path} and port={thatPort}, expected: {this.getUri

Error message

Wrong FS: {path} and port={thatPort}, expected: {this.getUri()} with port={thisPort}

What it means

After scheme and host match, AbstractFileSystem.checkPath compares ports: a path port of -1 is resolved to this FS's default port (getUriDefaultPort()), and any remaining difference throws InvalidPathException("Wrong FS ... and port=..."). The path and the handle must agree on port once defaults are applied.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java:407

    // Schemes and hosts must match.
    // Allow for null Authority for file:///
    if (!thisScheme.equalsIgnoreCase(thatScheme) ||
       (thisHost != null && 
            !thisHost.equalsIgnoreCase(thatHost)) ||
       (thisHost == null && thatHost != null)) {
      throw new InvalidPathException("Wrong FS: " + path + ", expected: "
          + this.getUri());
    }
    
    // Ports must match, unless this FS instance is using the default port, in
    // which case the port may be omitted from the given URI
    int thisPort = this.getUri().getPort();
    int thatPort = uri.getPort();
    if (thatPort == -1) { // -1 => defaultPort of Uri scheme
      thatPort = this.getUriDefaultPort();
    }
    if (thisPort != thatPort) {
      throw new InvalidPathException("Wrong FS: " + path
          + " and port=" + thatPort
          + ", expected: "
          + this.getUri()
          + " with port=" + thisPort);
    }
  }
  
  /**
   * Get the path-part of a pathname. Checks that URI matches this file system
   * and that the path-part is a valid name.
   * 
   * @param p path
   * 
   * @return path-part of the Path p
   */
  public String getUriPath(final Path p) {
    checkPath(p);
    String s = p.toUri().getPath();

View on GitHub (pinned to 2add963021)

Solutions

  1. Use one URI consistently for both the handle and the paths (create the handle from the path's own URI)
  2. Prefer HA logical URIs (hdfs://myNameservice, port omitted) so port comparison never fires
  3. Align fs.defaultFS and any hardcoded host:port in configs/paths

Example fix

// before
FileSystem fs = FileSystem.get(new URI("hdfs://nn:9000"), conf);
fs.exists(new Path("hdfs://nn:8020/x")); // Wrong FS ... port=8020 ... with port=9000

// after
FileSystem fs = FileSystem.get(new Path("hdfs://nn:8020/x").toUri(), conf);
fs.exists(new Path("/x"));
Defensive patterns

Strategy: validation

Validate before calling

int thatPort = p.toUri().getPort();
if (thatPort == -1) thatPort = afs.getUriDefaultPort();
if (afs.getUri().getPort() != thatPort) {
  afs = AbstractFileSystem.get(p.toUri(), conf); // port mismatch: use the path's own FS
}

Try / catch

catch (InvalidPathException e) { if (e.getMessage().contains("and port=")) { /* port mismatch: rebind handle to path URI and retry */ } else throw e; }

Prevention

When it happens

Trigger: Handle created from hdfs://nn:9000 but path says hdfs://nn:8020/x (or vice versa); handle bound to a non-default port while the path omits the port (resolving to the scheme default); NameNode RPC port changed on one side of the config only.

Common situations: Mixing the 8020 and 9000 port conventions between client config and hardcoded paths; stale cached handles after a cluster port change; some tools writing the port into paths while others rely on fs.defaultFS.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/5c5ba26bbb4689c3. Report an issue: GitHub.