Skip to content

HDDS-16379. Recon returns 500 NPE instead of proxying error when metrics endpoint responds with a non-enum status code - #11197

Open
henry3260 wants to merge 1 commit into
apache:masterfrom
henry3260:HDDS-16379
Open

HDDS-16379. Recon returns 500 NPE instead of proxying error when metrics endpoint responds with a non-enum status code#11197
henry3260 wants to merge 1 commit into
apache:masterfrom
henry3260:HDDS-16379

Conversation

@henry3260

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Recon's metrics proxy and metrics service providers checked the upstream HTTP status with
Response.Status.fromStatusCode(code).getFamily(). Response.Status is a JAX-RS enum that
only lists common status codes, and fromStatusCode() returns null for any code not in the
enum (e.g. 422, which the Prometheus HTTP API returns when a query expression cannot be
executed). Calling .getFamily() on that null throws a NullPointerException, so Recon
responds 500 with a stack trace instead of setting 502 and proxying the Prometheus error body
back as intended.

This PR:

  • Replaces the check with Response.Status.Family.familyOf(code), which classifies any status
    code by its first digit and never returns null, in MetricsProxyEndpoint,
    PrometheusServiceProviderImpl, and JmxServiceProviderImpl.
  • Guards against HttpURLConnection#getErrorStream() returning null (per its javadoc) in the
    error path of MetricsProxyEndpoint, which previously caused a second NPE in
    Channels.newChannel(null) and in the finally close.

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-16379

How was this patch tested?

Added two unit tests to the existing TestEndpoints suite, following the mock pattern of
testGetMetricsResponse:

  • testGetMetricsResponseWithNonEnumStatusCode: upstream returns 422 with an error body;
    verifies Recon sets 502 and proxies the body back (the path that previously threw NPE).
  • testGetMetricsResponseWithNullErrorStream: upstream returns an error status with no error
    body; verifies no exception and a 502 response.

…ics endpoint responds with a non-enum status code
Copilot AI lite review requested due to automatic review settings September 3, 2026 07:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrowly scoped to robust HTTP status/error-stream handling and is covered by targeted regression unit tests for the reported failure modes.

Pull request overview

This PR fixes an error-handling bug in Recon’s metrics proxy/service providers where certain upstream HTTP status codes (eg, 422 from the Prometheus API) could trigger a NullPointerException, causing Recon to return a 500 instead of a 502 while proxying the upstream error body.

Changes:

  • Replace Response.Status.fromStatusCode(code).getFamily() with Response.Status.Family.familyOf(code) to correctly classify non-enum HTTP status codes without returning null.
  • Add a null-guard for HttpURLConnection#getErrorStream() in MetricsProxyEndpoint to avoid NPEs when the upstream returns an error with no body.
  • Add unit tests in TestEndpoints covering both non-enum status codes with an error body and error responses with a null error stream.
File summaries
File Description
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/api/MetricsProxyEndpoint.java Makes status-family checks null-safe for non-enum codes and avoids NPEs when error bodies are missing.
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/spi/impl/PrometheusServiceProviderImpl.java Uses status-family classification that works for any valid HTTP status code.
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/spi/impl/JmxServiceProviderImpl.java Uses status-family classification that works for any valid HTTP status code.
hadoop-ozone/recon/src/test/java/org/apache/hadoop/ozone/recon/api/TestEndpoints.java Adds regression tests for the prior NPE path and for null error bodies.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@chihsuan chihsuan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch! +1 LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants