apache/hadoop · error · IOException

"Invalid Content-Length header: " + contentLength

Error message

"Invalid Content-Length header: " + contentLength

What it means

IOException('Invalid Content-Length header') from EditLogFileInputStream.URLLog: the HTTP response for a remote edit log carried a Content-Length whose parsed value was <= 0. The fetcher requires a known positive length (it backs length() used for tail validation), so a non-positive value is treated as a protocol violation and the fetch aborts before reading the body.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/EditLogFileInputStream.java:505

                connection = (HttpURLConnection)
                    connectionFactory.openConnection(url, isSpnegoEnabled);
              } catch (AuthenticationException e) {
                throw new IOException(e);
              }
              
              if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new HttpGetFailedException(
                    "Fetch of " + url +
                    " failed with status code " + connection.getResponseCode() +
                    "\nResponse message:\n" + connection.getResponseMessage(),
                    connection);
              }
        
              String contentLength = connection.getHeaderField(CONTENT_LENGTH);
              if (contentLength != null) {
                advertisedSize = Long.parseLong(contentLength);
                if (advertisedSize <= 0) {
                  throw new IOException("Invalid " + CONTENT_LENGTH + " header: " +
                      contentLength);
                }
              } else {
                throw new IOException(CONTENT_LENGTH + " header is not provided " +
                                      "by the server when trying to fetch " + url);
              }
        
              return connection.getInputStream();
            }
          });
    }

    @Override
    public long length() {
      return advertisedSize;
    }

    @Override

View on GitHub (pinned to 2add963021)

Solutions

  1. Bypass proxies for NN hosts: add the NN host to http.nonProxyHosts (or no_proxy) in the tailer's JVM env, or fix LB pass-through.
  2. Verify the raw response by hand: curl -sv '<getedit URL>' --negotiate and inspect headers; a real 200 with a sane Content-Length points back at the intermediary.
  3. If the source NN itself emits 0-length, check its logs for the servlet exception and upgrade/patch if it is a known servlet bug.

Example fix

# before: JVM routes NN fetch via corporate proxy -> Content-Length: 0
export JAVA_TOOL_OPTIONS=-Dhttp.nonProxyHosts='nn1.example.com|nn2.example.com|localhost'
# after: fetch goes direct, servlet's real Content-Length is honored
Defensive patterns

Strategy: try-catch

Validate before calling

curl -sv --negotiate '<getedit URL>' -o /dev/null 2>&1 | grep -i content-length
# a sane positive Content-Length from a direct fetch confirms an intermediary is mangling it

Try / catch

catch (IOException e) {
  if (e.getMessage() != null && e.getMessage().contains("Invalid Content-Length")) {
    // do not retry blindly: fix proxy/LB in the path first, then retry once
  } else { throw e; }
}

Prevention

When it happens

Trigger: A proxy/LB in front of the NameNode rewriting the journal servlet response into a zero-length answer; a broken or half-failed GetImageServlet responding 200 with Content-Length: 0; malformed hand-built URLs hitting a different endpoint.

Common situations: JVM http proxy settings (http.proxyHost) routing NN traffic through a corporate proxy; LB health-check intercept; NN under extreme load closing early; version-skewed servlet between tailer and source.

Related errors


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