regexapache-nifi

NiFi HandleHttpRequest not responding for /api/* endpoints with "Request timed out" or "AsyncContext completed" errors


I'm setting up a simple web server using Apache NiFi with HandleHttpRequest and HandleHttpResponse. The goal is to handle multiple GET endpoints like:

  1. /accdetails
  2. /api/accdetails

Here's what I did:

Configured HandleHttpRequest processor: Allowed Paths: ^(\/api\/.*|\/accdetails)$ HTTP Method: GET Expiration Timeout: 5 mins (increased from the default 1 min)

Observed Behavior: When I hit http://localhost:/accdetails, it works as expected. When I hit http://localhost:/api/accdetails, I get the following error in the logs:

StandardHttpContextMap[id=3829e85d-0196-1000-d070-e882bfb48ab3] Request from 127.0.0.1 timed out; responding with SERVICE_UNAVAILABLE

What I Tried Next: Changed the Allowed Paths to explicitly define /api/accdetails. After this change, I started getting a new error:

StandardHttpContextMap[id=3829e85d-0196-1000-d070-e882bfb48ab3] Failed to respond with SERVICE_UNAVAILABLE message: java.lang.IllegalStateException: AsyncContext completed and/or Request lifecycle recycled

It seems that the flow isn't responding in time or properly returning the response for /api/* paths.

My Questions:

  1. How should I define the Allowed Paths in HandleHttpRequest to correctly handle all routes under /api/*?
  2. What might be causing the timeout or the AsyncContext error?
  3. Are there any best practices for routing /api/* style endpoints in NiFi?

Any help or pointers would be much appreciated!


Solution

  • Based on my understanding, the HandleHttpRequest.java servlet configuration currently uses the path "/" as the base. If we change this to "/api/", then all API endpoints will be handled under the /api/ path, meaning requests like /api/yourendpoint will be routed correctly by default.

            final ServletContextHandler handler = new ServletContextHandler();
            handler.addServlet(standardServlet, "/");
            server.setHandler(handler);
    
            this.server = server;
            server.start();
    

    https://github.com/apache/nifi/blob/a7e336f416a0f31af354451b70e2cce6696ff71d/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/HandleHttpRequest.java#L502