mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
calls, apps, and routes listing were previously returning the entire data set, which just won't scale. this adds pagination with cursoring forward to each of these endpoints (see the [docs](docs/definitions.md)). the patch is really mostly tests, shouldn't be that bad to pick through. some blarble about implementation is in order: calls are sorted by ids but allow searching within certain `created_at` ranges (finally). this is because sorting by `created_at` isn't feasible when combined with paging, as `created_at` is not guaranteed to be unique -- id's are (eliding theoreticals). i.e. on a page boundary, if there are 200 calls with the same `created_at`, providing a `cursor` of that `created_at` will skip over the remaining N calls with that `created_at`. also using id will be better on the index anyway (well, less of them). yay having sortable ids! I can't discern any issues doing this, as even if 200 calls have the same created_at, they will have different ids, and the sort should allow paginating them just fine. ids are also url safe, so the id works as the cursor value just fine. apps and routes are sorted by alphabetical order. as they aren't guaranteed to be url safe, we are base64'ing them in the front end to a url safe format and then returning them, and then base64 decoding them when we get them. this does mean that they can be relatively large if the path/app is long, but if we don't want to add ids then they were going to be pretty big anyway. a bonus that this kind of obscures them. if somebody has better idea on formatting, by all means. notably, we are not using the sql paging facilities, and we are baking our own based on cursors, which ends up being much more efficient for querying longer lists of resources. this also should be easy to implement in other non-sql dbs and the cursoring formats we can change on the fly since we are just exposing them as opaque strings. the front end deals with the base64 / formatting, etc and the back end is taking raw values (strfmt.DateTime or the id for calls). the cursor that is being passed to/by the user is simply the last resource on the previous page, so in theory we don't even need to return it, but it does make it a little easier to use, also, cursor being blank on the last page depends on page full-ness, so sometimes users will get a cursor when there are no results on next page (1/N chance, and it's not really end of world -- actually searching for the next thing would make things more complex). there are ample tests for this behavior. I've turned off all query parameters allowing `LIKE` queries on certain listing endpoints, as we should not expose sql behavior through our API in the event that we end up not using a sql db down the road. I think we should only allow prefix matching, which sql can support as well as other types of databases relatively cheaply, but this is not hooked up here as it didn't 'just work' when I was fiddling with it (can add later, they're unnecessary and weren't wired in before in front end). * remove route listing across apps (unused) * fix panic when doing `/app//`. this is prob possible for other types of endpoints, out of scope here. added a guard in front of all endpoints for this * adds `from_time` and `to_time` query parameters to calls, so you can e.g. list the last hour of tasks. these are not required and default to oldest/newest. * hooked back up the datastore tests to the sql db, only run with sqlite atm, but these are useful, added a lot to them too. * added a bunch of tests to the front end, so pretty sure this all works now. * added to swagger, we'll need to re-gen. also wrote some words about pagination workings, I'm not sure how best to link to these, feedback welcome. * not sure how we want to manage indexes, but we may need to add some (looking at created_at, mostly) * `?route` changed to `?path` in routes listing, to keep consistency with everything else * don't 404 when searching for calls where the route doesn't exist, just return an empty list (it's a query param ffs) closes #141
140 lines
4.7 KiB
Go
140 lines
4.7 KiB
Go
package models
|
|
|
|
import (
|
|
"github.com/go-openapi/strfmt"
|
|
)
|
|
|
|
const (
|
|
// TypeNone ...
|
|
TypeNone = ""
|
|
// TypeSync ...
|
|
TypeSync = "sync"
|
|
// TypeAsync ...
|
|
TypeAsync = "async"
|
|
)
|
|
|
|
const (
|
|
// FormatDefault ...
|
|
FormatDefault = "default"
|
|
// FormatHTTP ...
|
|
FormatHTTP = "http"
|
|
)
|
|
|
|
var possibleStatuses = [...]string{"delayed", "queued", "running", "success", "error", "cancelled"}
|
|
|
|
type CallLog struct {
|
|
CallID string `json:"call_id"`
|
|
Log string `json:"log"`
|
|
AppName string `json:"app_name"`
|
|
}
|
|
|
|
// Call is a representation of a specific invocation of a route.
|
|
type Call struct {
|
|
// Unique identifier representing a specific call.
|
|
ID string `json:"id"`
|
|
|
|
// NOTE: this is stale, retries are not implemented atm, but this is nice, so leaving
|
|
// States and valid transitions.
|
|
//
|
|
// +---------+
|
|
// +---------> delayed <----------------+
|
|
// +----+----+ |
|
|
// | |
|
|
// | |
|
|
// +----v----+ |
|
|
// +---------> queued <----------------+
|
|
// +----+----+ *
|
|
// | *
|
|
// | retry * creates new call
|
|
// +----v----+ *
|
|
// | running | *
|
|
// +--+-+-+--+ |
|
|
// +---------|-|-|-----+-------------+
|
|
// +---|---------+ | +-----|---------+ |
|
|
// | | | | | |
|
|
// +-----v---^-+ +--v-------^+ +--v---^-+
|
|
// | success | | cancelled | | error |
|
|
// +-----------+ +-----------+ +--------+
|
|
//
|
|
// * delayed - has a delay.
|
|
// * queued - Ready to be consumed when it's turn comes.
|
|
// * running - Currently consumed by a runner which will attempt to process it.
|
|
// * success - (or complete? success/error is common javascript terminology)
|
|
// * error - Something went wrong. In this case more information can be obtained
|
|
// by inspecting the "reason" field.
|
|
// - timeout
|
|
// - killed - forcibly killed by worker due to resource restrictions or access
|
|
// violations.
|
|
// - bad_exit - exited with non-zero status due to program termination/crash.
|
|
// * cancelled - cancelled via API. More information in the reason field.
|
|
// - client_request - Request was cancelled by a client.
|
|
Status string `json:"status"`
|
|
|
|
// App this call belongs to.
|
|
AppName string `json:"app_name"`
|
|
|
|
// Path of the route that is responsible for this call
|
|
Path string `json:"path"`
|
|
|
|
// Name of Docker image to use.
|
|
Image string `json:"image"`
|
|
|
|
// Number of seconds to wait before queueing the call for consumption for the
|
|
// first time. Must be a positive integer. Calls with a delay start in state
|
|
// "delayed" and transition to "running" after delay seconds.
|
|
Delay int32 `json:"delay,omitempty"`
|
|
|
|
// Type indicates whether a task is to be run synchronously or asynchronously.
|
|
Type string `json:"type,omitempty"`
|
|
|
|
// Format is the format to pass input into the function.
|
|
Format string `json:"format,omitempty"`
|
|
|
|
// Payload for the call. This is only used by async calls, to store their input.
|
|
// TODO should we copy it into here too for debugging sync?
|
|
Payload string `json:"payload,omitempty"`
|
|
|
|
// Full request url that spawned this invocation.
|
|
URL string `json:"url,omitempty"`
|
|
|
|
// Method of the http request used to make this call.
|
|
Method string `json:"method,omitempty"`
|
|
|
|
// Priority of the call. Higher has more priority. 3 levels from 0-2. Calls
|
|
// at same priority are processed in FIFO order.
|
|
Priority *int32 `json:"priority"`
|
|
|
|
// Maximum runtime in seconds.
|
|
Timeout int32 `json:"timeout,omitempty"`
|
|
|
|
// Hot function idle timeout in seconds before termination.
|
|
IdleTimeout int32 `json:"idle_timeout,omitempty"`
|
|
|
|
// Memory is the amount of RAM this call is allocated.
|
|
Memory uint64 `json:"memory,omitempty"`
|
|
|
|
// BaseEnv are the env vars for hot containers, not request specific.
|
|
BaseEnv map[string]string `json:"base_env,omitempty"`
|
|
|
|
// Env vars for the call. Comes from the ones set on the Route.
|
|
EnvVars map[string]string `json:"env_vars,omitempty"`
|
|
|
|
// Time when call completed, whether it was successul or failed. Always in UTC.
|
|
CompletedAt strfmt.DateTime `json:"completed_at,omitempty"`
|
|
|
|
// Time when call was submitted. Always in UTC.
|
|
CreatedAt strfmt.DateTime `json:"created_at,omitempty"`
|
|
|
|
// Time when call started execution. Always in UTC.
|
|
StartedAt strfmt.DateTime `json:"started_at,omitempty"`
|
|
}
|
|
|
|
type CallFilter struct {
|
|
Path string // match
|
|
AppName string // match
|
|
FromTime strfmt.DateTime
|
|
ToTime strfmt.DateTime
|
|
Cursor string
|
|
PerPage int
|
|
}
|