serving icon indicating copy to clipboard operation
serving copied to clipboard

Feature Request: Allow processing of async requests in Knative

Open nimakaviani opened this issue 7 years ago • 60 comments

/area API

Describe the feature

As a Knative user, I can request the http workload to be processed asynchronously.

Requirements

  • Same endpoint should allow for both blocking and non-blocking async requests
  • The incoming async requests trigger the running of the async job in Knative
  • An async request does not block. Instead, it returns immediately
  • The response will have an identifier that can be used to track progress for the async job
  • A follow-up request to track progress will need to supply the identifier
    • If the async job is still in progress
      • the response will return an in progress status
        (e.g, a 302 Found status code with an empty body)
    • If the async job is finished
      • the response body will have the result from the app processing the async job
      • the status code will be identical to what is returned from the app after the job is done
  • Any instance of the running Knative app should be able to accept / respond to an async query
  • A failed async job will return a HTTP 5xx code

Usecases

  • Long-running jobs
    • Notifications (e.g. mobile push notification, SMS notification, mass emails, etc)
    • Database migrations
    • Batch processing (e.g. data transformation)
  • Stream processing
  • Highly parallel jobs (document, image, … processing)
  • Fan-out workloads
  • Serveless Operator

nimakaviani avatar Jun 25 '19 05:06 nimakaviani

/cc @duglin

nimakaviani avatar Jun 25 '19 05:06 nimakaviani

Question is, why would you want to use knative for this? Most of what you described is what your app has to do. I presume, you don't really expect Knative to provide a generic state machine/workflow engine? Isn't everything you need is minScale=1, to permit async processing, without scaling to zero?

vagababov avatar Jun 25 '19 05:06 vagababov

I always thought async workloads are more of a territory of Knative Eventing in conjunction with Knative Serving. After all, the protocol for Knative Serving currently is request/response based and everything revolves around that pretty much.

Any thoughts on why Eventing is not a suitable solution for async workloads like the ones you mentioned?

markusthoemmes avatar Jun 25 '19 07:06 markusthoemmes

As Markus says, this is where eventing comes in, perhaps even eventing delivering events to something other than serving.

cc @evankanderson

mattmoor avatar Jun 25 '19 13:06 mattmoor

@vagababov I think setting minScale=1 is an anti-pattern to support async. Particularly for the case of infrequent async jobs, I dont think the expectation of having resources sitting idle is ideal.

@markusthoemmes good question. I think this is the conjunction of Knative Eventing and Knative Serving that is in fact problematic. Knative's presumption of short-lived requests with persistent connection to the client goes against what is expected from an async job, regardless of whether the request is initiated by an outside client or an internal Cloud Event. This is where I think @mattmoor 's point regarding having "something other than serving" deal with async requests comes into play. But does an app developer really want to have a separate app to send batch emails or perform data transformations? or even worse, to support separate code bases or deployment models?

so to @vagababov's question, I don't really expect Knative to provide a generic state machine or workflow engine, but for it to close the loop on common requirements in a developer's workflow without the developers having to go up and down in the stack to have different pieces of their application deployed.

nimakaviani avatar Jun 25 '19 23:06 nimakaviani

This is where I think @mattmoor 's point regarding having "something other than serving" deal with async requests comes into play

To be clear, I mean having something other than serving dealing with the async aspect, but that may result in the Eventing system having a synchonous call to Serving. For sufficiently long-running (a la batch) cases, Serving may be ill-suited for this (or the scaling model may not be right), and so it may make sense to compose eventing with other forms of addressable compute that aren't currently covered by knative.

mattmoor avatar Jun 26 '19 00:06 mattmoor

Another vote for using Eventing to handle this (turning a sync request into an async request).

Also, there is room for serverless products for handling long running job, but currently, I think this it outside the charter of knative/serving.

mikehelmick avatar Jul 02 '19 03:07 mikehelmick

I'm not following the relationship to eventing. The connection between a client and the KnService is not an event. It shouldn't be morphed into a CloudEvent, or go thru brokers, triggers, fanned-out, etc. I also wouldn't expect the client to change how it interacts with a KnService based on whether it's doing it via sync or async - meaning I would not expect the URL of the KnService to change, which is what I think using eventing for async would require. People may be interacting in both modes at the same time.

I don't see the internal flow or structure needed by async being that different from sync - with the exception of how responses are handled, so it's hard for me to see why this is out of scope for serving (yes I know there's a bit more to it, but I think the response handling is the biggie). Async is a well established model in the serverless space so it would be odd for us to exclude support for it.

duglin avatar Jul 02 '19 12:07 duglin

Thinking more about the tie-in with eventing... if what's meant is that something in the normal flow detects the "run this in async mode" flag, and as a result redirects the request to some eventing component because it knows how to store responses in some kind of persistence, then that's just an impl detail. But converting it to a CloudEvent seems odd.

In the end though, a KnService is called and I think the only connection that we need to ensure stays open the entire time is the one between the user container and the queue proxy - and I'm not sure eventing helps with that since it assumes persistent connections from channels, no? Although, if we combined this support with how eventing manages responses (meaning, instead of the channel waiting for a response, the response is sent via a call-back managed by the queue proxy) then I think those two worlds might be more aligned than I originally considered. But, that all impl details and the user should be unaware of it.

duglin avatar Jul 02 '19 12:07 duglin

I think this it outside the charter of knative/serving.

@mikehelmick can you pls point me to that charter?

mbehrendt avatar Jul 02 '19 14:07 mbehrendt

re 'handling async via eventing' , adding to what @duglin said above: if we somehow magically handled it via eventing, you still have the issue that somehow under the cover the knservice gets called synchronously. I.e. you're bound to the max execution time associated with synchronous calls, and to the resource consumption implied by potentially keeping 1000's of http connections open.

mbehrendt avatar Jul 02 '19 14:07 mbehrendt

other forms of addressable compute that aren't currently covered by knative.

@mattmoor can you pls elaborate on which other forms of addressable compute you're referring to? Are there special semantics behind you emphasizing the currently? E.g. is there sth cooking behind the scenes?

mbehrendt avatar Jul 02 '19 15:07 mbehrendt

my understanding of @mattmoor 's suggestion was that it needs to be handled through other Kubernetes controllers (e.g., deployments, etc.). If that's right then, back to my original point, it won't be a great user experience if the developers have to go up and down in the stack to have different pieces of their application deployed.

Another vote for using Eventing to handle this (turning a sync request into an async request).

For the above, or like @markusthoemmes suggested, bringing serving and eventing somehow together, requests will have to come back to the Kn app at some point for processing. With Kn requiring "persistent time-limited http connections", the problem remains, like @mbehrendt and @duglin mentioned. Unless we modify eventing to support long-running workloads.

nimakaviani avatar Jul 02 '19 16:07 nimakaviani

Also I updated the original requirements with the following item:

  • Same endpoint should allow for both blocking and non-blocking async requests

nimakaviani avatar Jul 02 '19 16:07 nimakaviani

it might be good to separate out UX from impl details/requirements.

From a UX perspective:

  • the endpoint for a KnService should be the same regardless of whether it is invoked synchronously or asynchronously
  • while it may be possible for a KnService to declare itself as an async service (and therefore all requests are treated as such and a 202 is returned immediately) that is not the case we're most interested in. We're looking for the one there the person invoking the KnService chooses whether the processing is done async or not via some flag (e.g. perhaps a query param or http header)
  • user can then get the response metadata via some mechanism

From an impl perspective:

  • I actually think trying to have eventing and serving leverage shared components makes a lot of sense. My initial push-back was because I didn't see the existing eventing infrastructure being a good fit and I was interpreting the "use eventing" statements as "expose something new to the end user" - and that breaks the first bullet point above
  • in order for this sharing to happen though eventing would need to do basically what this issue is proposing.... modify the serving side of things to support async invocations, then it could leverage that.
  • how we then store the responses and the metadata associated with it could be done via eventing or anything else -that's just an impl detail that (for the most part) isn't seen by the end user

duglin avatar Jul 02 '19 16:07 duglin

Some questions I have about the proposal:

  • At a high-level, how does the tracking and management of async tasks not turn into a workflow manager like Apache Airflow ( https://github.com/apache/airflow )? Why should this be built into Knative Serving rather than orchestrated on-top?
  • Knative Serving today relies on HTTP information to provide the "serverless magic" of autoscaling both up and down. We rely on the closing or timeout of the HTTP connection to determine that a container has finished. From the comments above it sounds like the desire is for the HTTP connection to be closed, how do you propose that we determine that an async container has finished? When finished how is the response/output of the async job is captured by the control plane?
  • How do you imagine the runtime contract for async tasks to differ from synchronous tasks? Is an HTTP endpoint still required?
  • How do you propose our settings of container concurrency work with async requests?

dgerd avatar Jul 02 '19 18:07 dgerd

My thoughts in the same order as the questions above:

  • I cannot quite establish the link between the workflow manager and async requests. It's not the flow that matters but instead, the processing of a single request and storing the results for later retrieval without the persistent external connection. how does the flow help? what am I missing?
  • Busyness for a pod and whether or not a request is internally finished is determined and tracked by the queue proxy based on open connections to the user container. It can (and should) be kept independent of whether there exists a corresponding external HTTP connection from the client. Queue proxy can track async requests too, and do the corresponding bookkeeping.
  • The runtime contract won't change, nor will the http endpoint.
  • container concurrency is also determined by the queue proxy. Given that queue proxy can track async requests too, there wont be any change to container concurrency settings.

nimakaviani avatar Jul 02 '19 19:07 nimakaviani

This contradicts the model we've been operating with.

It can (and should) be kept independent of whether there exists a corresponding external HTTP connection from the client. Queue proxy can track async requests too, and do the corresponding bookkeeping.

and

container concurrency is also determined by the queue proxy. Given that queue proxy can track async requests too, there wont be any change to container concurrency settings.

I dont' see how Q-P can achieve that.

  1. How would it determine which request is async and which one is sync, since according to your model, async is fire-and-forget kind? This needs request annotation of sorts.
  2. Currently we reason about the load, presuming each request cost about the same (cost being 1 request in flight). Async requests may/will vary in load generated and even if we somehow teach QP to discern one from another, we can't equalize them. Which means all our Autoscaling logic will be incorrect.

The runtime contract won't change, nor will the http endpoint.

Determining async=sync at QP level would require some flag/header/X to be set -- this is RTC change.

I cannot quite establish the link between the workflow manager and async requests. It's not the flow that matters but instead, the processing of a single request and storing the results for later retrieval without the persistent external connection.

You have to run a stateful load for that. You might be interested what LightBend folk are doing. As such just supporting arbitrary stateful loads is probably not what we're aiming for right now...

vagababov avatar Jul 02 '19 20:07 vagababov

Determining async=sync at QP level would require some flag/header/X to be set -- this is RTC change.

correct. like @duglin suggested earlier

(e.g. perhaps a query param or http header)

and sure there is a change in RTC too. But it is an additive change and backward compatible.

Currently we reason about the load, presuming each request cost about the same (cost being 1 request in flight). Async requests may/will vary in load generated and even if we somehow teach QP to discern one from another, we can't equalize them. Which means all our Autoscaling logic will be incorrect.

I am not sure if I understand the above. KPA works based on the number of requests and last time I checked, it supports scaling on cpu too. Even now, revision.spec.timeoutSeconds has a default value of 5m which is 3000x larger than an average 100ms request time. I didn't see any specifics on how Autoscaling presumes each request would cost about the same and what the same implies where it ranges from milliseconds to 5 minutes. Even if we set aside the termination grace period, it is still 90sec before terminating an instance.

You have to run a stateful load for that. You might be interested what LightBend folk are doing.

I am not sure if stateful load helps. there's nothing in the load that is stateful. It is more of a stateful response if anything.

This contradicts the model we've been operating with.

The only place where I see it impact the model, is the assumption of having an external connection from the client. I am not sure if loosening the assumption would go down as contradiction. Particularly if QP continues to do proper bookkeeping of connections to the user container.

nimakaviani avatar Jul 02 '19 21:07 nimakaviani

KPA never scaled based on CPU. Only on concurrency.

vagababov avatar Jul 02 '19 22:07 vagababov