icinga2 icon indicating copy to clipboard operation
icinga2 copied to clipboard

Stream responses where appropriate

Open yhabteab opened this issue 1 year ago • 11 comments

This PR changes some of our HTTP handlers to stream responses instead of buffering them in memory, just like how @jschmidt-icinga did for the ObjectQueryHandler in #10516. However, this change breaks also some the fine-grained HTTP error code reporting for the /v1/actions handler. Previously, the actual HTTP status code was chosen from all the individual action results, but since we now stream the response, we can't wait until the end to determine the final status code. Thus, I've changed the handlers to use 202 Accepted as the general status code for these endpoints ^1, indicating that the request has been accepted for processing, without guaranteeing that the processing will be completed successfully.

fixes #10544 fixes #10407 fixes #9535

yhabteab avatar Sep 11 '25 11:09 yhabteab

The change to return http::status::accepted instead seems reasonable, but shouldn't that be documented somewhere?

Not that I know of. Of course, this is kinda a breaking change, so it should definitely be mentioned in the changelog, but I don't think that we have a specific place for documenting changes to the API return codes.

Edit: The "Responses" section needs to be adapted.

Ok!

yhabteab avatar Sep 11 '25 13:09 yhabteab

Edit: The "Responses" section needs to be adapted.

On second thought, I think it is basically fine as it is.

The next section "HTTP Statuses" mentions that everything from 200 to 299 is a success. Maybe a short mention that most handlers return either 200 or 202 in case of success, depending on whether the result is known at the time the Header is sent.

But the example code snippets definitely need to be fixed, currently they all check for Ok/200 specifically on "v1/objects/services", which would fail now. Even though that's easy to notice what's wrong, that'd not be a good documentation experience for the user.

jschmidt-icinga avatar Sep 11 '25 13:09 jschmidt-icinga

Same for the GET side of /v1/config/packages

That handler holds a locked mutex while generating the body, so can't use streaming. Plus, the response is minimal, so it doesn't need any workaround.

yhabteab avatar Sep 12 '25 08:09 yhabteab

The change would be minimal, I think freezing the Array and adding the yc to SendJsonBody() should be enough now. Same for the GET side of /v1/config/packages and /v1/config/stages.

Done! I've frozen the results array for the /v1/config/packages handler as well, to allow streaming in case the package has a bunch of orphaned stages that could exceed our flush threshold when accumulated.

yhabteab avatar Sep 12 '25 09:09 yhabteab

Does one of you understand why this test failed? It says AssertServerDisconnected was unsuccessful. I'm generally not especially happy with that code, but it shouldn't cause any problems, 11 seconds is generous as it is. Also this PR touches none of that code and I haven't seen a check fail like that until now.

The exception could be from the HttpServerConnection object outliving the stream/socket object due to the keep_alive in the coroutine. But what is preventing CheckLiveness form initiating the disconnect here?

Edit: It might be because of a clock anomaly where the runner might have adjusted time back a second which would affect HttpServerConnection, but not the test case. Fortunately something like this might be fixed by the changes in #10550 by using std::chrono::steady_clock instead.

jschmidt-icinga avatar Sep 12 '25 11:09 jschmidt-icinga

Edit: It might be because of a clock anomaly where the runner might have adjusted time back a second which would affect HttpServerConnection, but not the test case. Fortunately something like this might be fixed by the changes in #10550 by using std::chrono::steady_clock instead.

I think, I have seen the error with #10550 before I changed it to use std::chrono::steady_clock as well and I just retried it without thinking that much about it, so you might be right about this.

yhabteab avatar Sep 12 '25 12:09 yhabteab

I think, I have seen the error with #10550 before I changed it to use std::chrono::steady_clock as well and I just retried it without thinking that much about it, so you might be right about this.

Was this also on Windows?

Edit: It might be because of a clock anomaly where the runner might have adjusted time back a second which would affect HttpServerConnection, but not the test case. Fortunately something like this might be fixed by the changes in #10550 by using std::chrono::steady_clock instead.

Sounds plausible, but also like it would take quite some digging to confirm. Anyway, this PR only touches HTTP handlers, which aren't even accessed by that test case (it only creates a idle connection), so really nothing to do here in this PR. Either #10550 just fixes it and we're happy, or if it happens again, we might want to take a closer look.

julianbrost avatar Sep 15 '25 08:09 julianbrost

Rebased and addressed all the requested changes:

  • 68deaaf refactors the existing ValueGenerator class and makes it templated, so that it can be used with any std container.
  • 3f54253 removes the ActionsHandler::AuthenticatedApiUser and passes the api user as param to the actions instead.

yhabteab avatar Sep 15 '25 09:09 yhabteab

I think, I have seen the error with #10550 before I changed it to use std::chrono::steady_clock as well and I just retried it without thinking that much about it, so you might be right about this.

Was this also on Windows?

Edit: It might be because of a clock anomaly where the runner might have adjusted time back a second which would affect HttpServerConnection, but not the test case. Fortunately something like this might be fixed by the changes in #10550 by using std::chrono::steady_clock instead.

Sounds plausible, but also like it would take quite some digging to confirm. Anyway, this PR only touches HTTP handlers, which aren't even accessed by that test case (it only creates a idle connection), so really nothing to do here in this PR. Either #10550 just fixes it and we're happy, or if it happens again, we might want to take a closer look.

Apparently, Windows has some serious issues. Now, Win64 is failing due to the newly changed timer/invoke intervals. It didn't fail before in the PR that changed the intervals, but now it does. I don't understand this ** OS at all.

yhabteab avatar Sep 15 '25 12:09 yhabteab

Rebased + addressed the comment from https://github.com/Icinga/icinga2/pull/10554#discussion_r2351848998.

yhabteab avatar Sep 16 '25 12:09 yhabteab

FYI: This PR got a merge conflict now.

julianbrost avatar Oct 16 '25 14:10 julianbrost