apache/hadoop · error · InvalidPathException
Wrong FS: {path}, expected: {this.getUri()}
Error message
Wrong FS: {path}, expected: {this.getUri()} What it means
AbstractFileSystem.checkPath requires the path's scheme and host (both case-insensitive) to match the FS instance's own URI; it also rejects a path with a host when this instance has none. Operating on a path that belongs to a different cluster or filesystem through the wrong handle throws InvalidPathException("Wrong FS: ...").
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java:395
}
throw new InvalidPathException("relative paths not allowed:" +
path);
} else {
throw new InvalidPathException(
"Path without scheme with non-null authority:" + path);
}
}
String thisScheme = this.getUri().getScheme();
String thisHost = this.getUri().getHost();
String thatHost = uri.getHost();
// 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);
}
}View on GitHub (pinned to 2add963021)
Solutions
- Operate on each path with its own handle: FileSystem.get(path.toUri(), conf) or use FileContext, which resolves the FS per path
- Fix fs.defaultFS / HA nameservice config so the handle matches the paths
- For cross-filesystem copies, use FileUtil.copy(srcFS, src, dstFS, dst, deleteSource, overwrite) instead of single-handle operations
Example fix
// before
FileSystem hA = FileSystem.get(new URI("hdfs://clusterA:8020"), conf);
hA.rename(new Path("hdfs://clusterB:8020/x"), new Path("hdfs://clusterB:8020/y")); // Wrong FS
// after
FileSystem hB = FileSystem.get(new Path("hdfs://clusterB:8020/x").toUri(), conf);
hB.rename(new Path("/x"), new Path("/y")); Defensive patterns
Strategy: validation
Validate before calling
URI fsUri = afs.getUri();
URI pUri = p.toUri();
boolean schemeOk = pUri.getScheme() != null && fsUri.getScheme().equalsIgnoreCase(pUri.getScheme());
boolean hostOk = fsUri.getHost() == null ? pUri.getHost() == null : fsUri.getHost().equalsIgnoreCase(pUri.getHost());
if (!schemeOk || !hostOk) {
afs = AbstractFileSystem.get(pUri, conf); // rebind to the path's own file system
} Try / catch
catch (InvalidPathException e) { if (e.getMessage().startsWith("Wrong FS")) { afs = AbstractFileSystem.get(p.toUri(), conf); /* retry with correct handle */ } else throw e; } Prevention
- Derive the handle from the path (FileSystem.get(path.toUri(), conf)) instead of reusing a cached one
- Use FileContext, which resolves the file system per path
- For cross-cluster work use FileUtil.copy between two handles rather than single-handle ops
When it happens
Trigger: An AbstractFileSystem/FileSystem handle created for hdfs://clusterA used with a path hdfs://clusterB/x; passing file:///tmp/x to an HDFS instance; an instance whose URI has null host (file:///) given a path carrying a host.
Common situations: Cached FileSystem instances reused across clusters in a long-lived service; migration/distcp jobs carrying absolute source-cluster paths; wrong fs.defaultFS; viewfs mount tables routing to a different nameservice than the handle.
Related errors
- Uri without authority: {uri}
- Wrong FS: {path} and port={thatPort}, expected: {this.getUri
- Bad configuration of hadoop.security.key.provider.path at ${
- No scheme in default FS: ${uri}
- Could not initialize target File System for URI : {targetDir
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/de7f96607f31483d.
Report an issue: GitHub.