Commit Graph

484 Commits

Author SHA1 Message Date
Reed Allman
20089c4e83 make headers quasi-consistent (#660)
possible breakages:

* `FN_HEADER` on cold are no longer `s/-/_/` -- this is so that cold functions
can rebuild the headers as they were when they came in on the request (fdks,
specifically), there's no guarantee that a reversal `s/_/-/` is the original
header on the request.
* app and route config no longer `s/-/_/` -- it seemed really weird to rewrite
the users config vars on these. should just pass them exactly as is to env.
* headers no longer contain the environment vars (previously, base config; app
config, route config, `FN_PATH`, etc.), these are still available in the
environment.

this gets rid of a lot of the code around headers, specifically the stuff that
shoved everything into headers when constructing a call to begin with. now we
just store the headers separately and add a few things, like FN_CALL_ID to
them, and build a separate 'config' now to store on the call. I thought
'config' was more aptly named, 'env' was confusing, though now 'config' is
exactly what 'base_vars' was, which is only the things being put into the env.
we weren't storing this field in the db, this doesn't break unless there are
messages in a queue from another version, anyway, don't think we're there and
don't expect any breakage for anybody with field name changes.

this makes the configuration stuff pretty straight forward, there's just two
separate buckets of things, and cold just needs to mash them together into the
env, and otherwise hot containers just need to put 'config' in the env, and then
hot format can shove 'headers' in however they'd like. this seems better than
my last idea about making this easier but worse (RIP).

this means:

* headers no longer contain all vars, the set of base vars can only be found
in the environment.
* headers is only the headers from request + call_id, deadline, method, url
* for cold, we simply add the headers to the environment, prepending
`FN_HEADER_` to them, BUT NOT upper casing or `s/-/_/`
* fixes issue where async hot functions would end up with `Fn_header_`
prefixed headers
* removes idea of 'base' vars and 'env'. this was a strange concept. now we just have
'config' which was base vars, and headers, which was base_env+headers; i.e.
they are disjoint now.
* casing for all headers will lean to be `My-Header` style, which should help
with consistency. notable exceptions for cold only are FN_CALL_ID, FN_METHOD,
and FN_REQUEST_URL -- this is simply to avoid breakage, in either hot format
they appear as `Fn_call_id` still.
* removes FN_PARAM stuff
* updated doc with behavior

weird things left:

`Fn_call_id` e.g. isn't a correctly formatted http header, it should likely be
`Fn-Call-Id` but I wanted to live to fight another day on this one, it would
add some breakage.

examples to be posted of each format below

closes #329
2018-01-09 10:08:30 -08:00
Tolga Ceylan
6f1f5e365d fn: URL parsing updates to fix json request_url (#657)
*) Updated fn-test-utils to latest fdk-go
*) Added hot-json to runner tests
*) Removed anon function in FromRequest which had
a side effect to set req.URL.Host. This is now more
explicit and eliminates some corresponding logic in
protocol http.
*) in gin, http request RequestURI is not set, removed
code that references this. (use Call.URL instead)
2018-01-08 10:28:50 -08:00
Travis Reeder
5cdee5579d Fixes 404 responses from functions that go through NoRoute path. (#651)
* Fixes 404 responses from functions that go through NoRoute path.

* cleanup

* cleanupp

* fix link

* Rollback a bad change.
2018-01-08 10:03:33 -08:00
Denis Makogon
60d2ca234f Ship call logs to the user as text/plain instead of JSON (#615)
* Ship call logs to the user as text/plain instead of JSON

* Fixing swagger doc

* c.String instead of c.JSON

* Make Logs API backward compatible

* Loop over accepted MIME types

* Bump swagger API version

* Fix client build script

 previous version was producing the following "couldn't find a swagger spec"

* Logs API regression test

* Write response body without buffering

* Switch JSON and text/plain cases

* Handle Accepted content types properly

* More solid response content type handling

* Write HTTP 406 with corresponding error body

* Remove unused import

* Use handleErrorResponse
2018-01-02 16:01:22 -06:00
Tolga Ceylan
fe80b50e30 fn: tests: revert a hack for agent shutdown (#632)
Reverting due to fix for #623
2018-01-02 15:17:44 -06:00
Denis Makogon
faaf5846ce Use retry func while trying to ping SQL datastore (#630)
* Use retry func while trying to ping SQL datastore

 - implements retry func specifically for SQL datastore ping
 - fmt fixes
 - using sqlx.Db.PingContext instead of sqlx.Db.Ping
 - propogate context to SQL datastore

* Rely on context from ServerOpt

* Consolidate log instances

* Cleanup

* Fix server usage in API tests
2018-01-02 12:32:10 -06:00
Reed Allman
683fef8c2e allow user configured agent in full node (#627)
* allow user configured agent in full node

this should keep the old default behavior but allow users to pass in a
configured agent to configure the server themselves, without having to worry
about a russian agent being a british agent.

also closes any agent given to an api node.

closes #623

* don't close agent in runner test
2017-12-26 11:04:14 -06:00
Reed Allman
f51792ae5e Timestamps on apps / routes (#614)
* route updated_at

* add app created at, fix some route updated_at bugs

* add app updated_at

TODO need to add tests through front end
TODO for validation we don't really want to use the validate wrapper since
it's a programmer error and not a user error, hopefully tests block this.

* add tests for timestamps to exist / change on apps&routes

* route equals at done, fix tests wit dis

* fix up the equals sugar

* add swagger

* fix rebase

* precisely allocate maps in clone

* vetted

* meh

* fix api tests
2017-12-23 09:57:36 -06:00
Tolga Ceylan
feeeca3321 fn: agent shutdown improvements (#622) 2017-12-22 12:52:31 -08:00
Reed Allman
a8a3e143c7 unexport all data abstractions on Server (#618)
this patch has no behavior changes, changes are:

* server.Datastore() -> server.datastore
* server.MQ -> server.mq
* server.LogDB -> server.logstore
* server.Agent -> server.agent

these were at a minimum not uniform. further, it's probably better to force
configuration through initialization in `server.New` to ensure thread safety
of referencing if someone does want to modify these as well as forcing things
into our initialization path and reducing the surface area of the Server
abstraction.
2017-12-21 13:21:02 -06:00
Travis Reeder
fdb4188146 Adds before/after app get/list. And some bug fixes/cleanup. (#610)
* Adds before/after app get/list. And some bug fixes/cleanup.

* Fix test
2017-12-21 09:32:03 -08:00
Tolga Ceylan
7290579e7d fn: tests: adding hot container timeout and huge memory cases (#611)
* fn: adding hot container timeout and huge memory cases

*) switching TestRouteRunnerTimeout to fn-test-utils to handle
    both hot and cold.
*) in server_test added content-length handling as protocol http
    does not create content-length if it is not present.
2017-12-20 10:11:57 -08:00
Tolga Ceylan
419298e1c0 Async hot hdr fix (#604)
* fn: for async hot requests ensure/fix content-length/type

* fn: added tests for FromModel for content type/length

* fn: restrict the content-length fix to async in FromModel()
2017-12-15 14:32:25 -08:00
Reed Allman
404250a417 loggy loo for node type on startup (#602)
* loggy loo for node type on startup

* additional english verse

* update to most recent suggestion
2017-12-14 13:43:21 -08:00
Reed Allman
fab788b539 makes span use init() to run before server.New (#597) 2017-12-13 16:31:19 -08:00
Dario Domizioli
6ba8bf4488 Change runner api env var to FN_RUNNER_API_URL (#592) 2017-12-13 10:28:19 -08:00
Reed Allman
bb92547b95 Hybrid plumby (#585)
* fix configuration of agent and server to be future proof and plumb in the hybrid client agent

* fixes up the tests, turns off /r/ on api nodes

* fix up defaults for runner nodes

* shove the runner async push code down into agent land to use client

* plumb up async-age

* return full call from async dequeue endpoint, since we're storing a whole
call in the MQ we don't need to worry about caching of app/route [for now]
* fast safe shutdown of dequeue looper in runner / tidying of agent
* nice errors for path not found against /r/, /v1/ or other path not found
* removed some stale TODO in agent
* mq backends are only loud mouths in debug mode now

* update tests

* Add caching to hybrid client

* Fix HTTP error handling in hybrid client.

The type switch was on the value rather than a pointer.

* Gofmt.

* Better caching with a nice caching wrapper

* Remove datastore cache which is now unused

* Don't need to manually wrap interface methods

* Go fmt
2017-12-12 15:54:55 -08:00
Reed Allman
2ebc9c7480 hybrid mergy (#581)
* so it begins

* add clarification to /dequeue, change response to list to future proof

* Specify that runner endpoints are also under /v1

* Add a flag to choose operation mode (node type).

This is specified using the `FN_NODE_TYPE` environment variable. The
default is the existing behaviour, where the server supports all
operations (full API plus asynchronous and synchronous runners).

The additional modes are:
* API - the full API is available, but no functions are executed by the
  node. Async calls are placed into a message queue, and synchronous
  calls are not supported (invoking them results in an API error).
* Runner - only the invocation/route API is present. Asynchronous and
  synchronous invocation requests are supported, but asynchronous
  requests are placed onto the message queue, so might be handled by
  another runner.

* Add agent type and checks on Submit

* Sketch of a factored out data access abstraction for api/runner agents

* Fix tests, adding node/agent types to constructors

* Add tests for full, API, and runner server modes.

* Added atomic UpdateCall to datastore

* adds in server side endpoints

* Made ServerNodeType public because tests use it

* Made ServerNodeType public because tests use it

* fix test build

* add hybrid runner client

pretty simple go api client that covers surface area needed for hybrid,
returning structs from models that the agent can use directly. not exactly
sure where to put this, so put it in `/clients/hybrid` but maybe we should
make `/api/runner/client` or something and shove it in there. want to get
integration tests set up and use the real endpoints next and then wrap this up
in the DataAccessLayer stuff.

* gracefully handles errors from fn
* handles backoff & retry on 500s
* will add to existing spans for debuggo action

* minor fixes

* meh
2017-12-11 10:43:19 -08:00
Travis Reeder
6b8627d1c5 Fixes to recent extension changes. (#568)
* Fixes to recent extension changes.

* Fixes issue where gin will continue calling the handler even if next() isn't called.

* Updated docs.
2017-12-06 10:12:55 -08:00
Travis Reeder
0798f9fac8 Middleware upgrade (#554)
* Adds root level middleware

* Added todo

* Better way for extensions to be added.

* Bad conflict merge?
2017-12-05 08:22:03 -08:00
Tolga Ceylan
dd88ec5d4e fn: sigterm graceful shutdown handling (#557) 2017-12-01 16:56:17 -08:00
Denis Makogon
5c68a88599 Fn-prefix everything (#545)
* Fn-prefix everything

Closes: #492

* Global replacement

* missed one fn_
2017-11-29 17:50:24 -08:00
Nigel Deakin
9a75785cbf Per route api extensions (#542)
* Extend extension mechanism to support per-route API extensions

* Tidy up comment

* Remove print statement

* Minor improvement to README

* Avoid calling c.Request.Context() twice
2017-11-29 12:03:23 +00:00
Travis Reeder
a67d5a6290 Drop viper dependency (#550)
* Removed viper dependency.

* removed from glide files
2017-11-28 15:46:17 -08:00
Tolga Ceylan
89dc79f0b0 fn: remove redundant httprouter code (#532)
*) tree from https://github.com/julienschmidt/httprouter
is already in Gin and this only seems to be parsing
parameters from URI.
2017-11-22 13:58:10 -06:00
Tolga Ceylan
2551be446a fn: introducing 503 responses for out of capacity case (#518)
* fn: introducing 503 responses for out of capacity case

*) Adding 503 with Retry-After header case if request failed
during waiting for slots.
*) TODO: return 503 without Retry-After if the request can
never be met by this fn server.
*) fn: runner test docker pull fixup
*) fn: MaxMemory for routes is now a variable to allow
testing and adjusting it according to fleet memory sizes.
2017-11-21 12:42:02 -08:00
Reed Allman
2d8c528b48 S3 loggyloo (#511)
* add minio-go dep, update deps

* add minio s3 client

minio has an s3 compatible api and is an open source project and, notably, is
not amazon, so it seems best to use their client (fwiw the aws-sdk-go is a
giant hair ball of things we don't need, too). it was pretty easy and seems
to work, so rolling with it. also, minio is a totally feasible option for fn
installs in prod / for demos / for local.

* adds 's3' package for s3 compatible log storage api, for use with storing
logs from calls and retrieving them.
* removes DELETE /v1/apps/:app/calls/:call/log endpoint
* removes internal log deletion api
* changes the GetLog API to use an io.Reader, which is a backwards step atm
due to the json api for logs, I have another branch lined up to make a plain
text log API and this will be much more efficient (also want to gzip)
* hooked up minio to the test suite and fixed up the test suite
* add how to run minio docs and point fn at it docs

some notes: notably we aren't cleaning up these logs. there is a ticket
already to make a Mr. Clean who wakes up periodically and nukes old stuff, so
am punting any api design around some kind of TTL deletion of logs. there are
a lot of options really for Mr. Clean, we can notably defer to him when apps
are deleted, too, so that app deletion is fast and then Mr. Clean will just
clean them up later (seems like a good option).

have not tested against BMC object store, which has an s3 compatible API. but
in theory it 'just works' (the reason for doing this). in any event, that's
part of the service land to figure out.

closes #481
closes #473

* add log not found error to minio land
2017-11-20 17:39:45 -08:00
Travis Reeder
ab18e467fa updates functions -> fnserver (#516)
* updates functions -> fn-server and fnlb -> fn-lb

* changed to fnserver and fnlb
2017-11-17 15:53:44 -08:00
Tolga Ceylan
57b24d63c3 fn: prometheus collector concurrent map access (#510)
* fn: prometheus collector concurrent map access

*) Added mutex to guard against concurrent access to maps

* fn: prometheus collector method receivers should be ptr

* fn: prometheus collector concurrent map access

*) Moved the mutex into getHistogramVec()
2017-11-17 12:46:53 -08:00
Nigel Deakin
910612d0b1 Docker stats to Prometheus (#486)
* Docker stats to Prometheus

* Fix compilation error in docker_test

* Refactor docker driver Run function to wait for  the container to have stopped before stopping the colleciton of statistics

* Fix go fmt errors

* Updates to sending docker stats to Prometheus

* remove new test TestWritResultImpl because we changes to support multiple waiters have been removed

* Update docker.Run to use channels not contextrs to shut down stats collector
2017-11-16 11:02:33 -08:00
Alexander Bransby-Sharples
c5ec0cc41e Add CORS support to fn api (#455)
The Gin middleware is being used if one or more Origins are specified. Default setup for each Origin is as follows:

- GET,POST, PUT, HEAD methods allowed
- Credentials share disabled
- Preflight requests cached for 12 hours

Which are the defaults gin-contrib/cors comes with out of the box.

Gin-cors will return a 403 if it gets a request with an Origin header that isn't on its' list. If no Origin header is specified then it will just return the servers response.

Start fn with CORS enabled:

`API_CORS="http://localhost:4000, http://localhost:3000" make run`
2017-11-16 15:37:26 +00:00
Reed Allman
61b416a9b5 automagic sql db migrations (#461)
* adds migrations

closes #57

migrations only run if the database is not brand new. brand new
databases will contain all the right fields when CREATE TABLE is called,
this is for readability mostly more than efficiency (do not want to have
to go through all of the database migrations to ascertain what columns a table
has). upon startup of a new database, the migrations will be analyzed and the
highest version set, so that future migrations will be run. this should also
avoid running through all the migrations, which could bork db's easily enough
(if the user just exits from impatience, say).

otherwise, all migrations that a db has not yet seen will be run against it
upon startup, this should be seamless to the user whether they had a db that
had 0 migrations run on it before or N. this means users will not have to
explicitly run any migrations on their dbs nor see any errors when we upgrade
the db (so long as things go well). if migrations do not go so well, users
will have to manually repair dbs (this is the intention of the `migrate`
library and it seems sane), this should be rare, and I'm unsure myself how
best to resolve not having gone through this myself, I would assume it will
require running down migrations and then manually updating the migration
field; in any case, docs once one of us has to go through this.

migrations are written to files and checked into version control, and then use
go-bindata to generate those files into go code and compiled in to be consumed
by the migrate library (so that we don't have to put migration files on any
servers) -- this is also in vcs. this seems to work ok. I don't like having to
use the separate go-bindata tool but it wasn't really hard to install and then
go generate takes care of the args. adding migrations should be relatively
rare anyway, but tried to make it pretty painless.

1 migration to add created_at to the route is done here as an example of how
to do migrations, as well as testing these things ;) -- `created_at` will be
`0001-01-01T00:00:00.000Z` for any existing routes after a user runs this
version. could spend the extra time adding 'today's date to any outstanding
records, but that's not really accurate, the main thing is nobody will have to
nuke their db with the migrations in place & we don't have any prod clusters
really to worry about. all future routes will correctly have `created_at` set,
and plan to add other timestamps but wanted to keep this patch as small as
possible so only did routes.created_at.

there are tests that a spankin new db will work as expected as well as a db
after running all down & up migrations works. the latter tests only run on mysql
and postgres, since sqlite3 does not like ALTER TABLE DROP COLUMN; up
migrations will need to be tested manually for sqlite3 only, but in theory if
they are simple and work on postgres and mysql, there is a good likelihood of
success; the new migration from this patch works on sqlite3 fine.

for now, we need to use `github.com/rdallman/migrate` to move forward, as
getting integrated into upstream is proving difficult due to
`github.com/go-sql-driver/mysql` being broken on master (yay dependencies).
Fortunately for us, we vendor a version of the `mysql` bindings that actually
works, thus, we are capable of using the `mattes/migrate` library with success
due to that. this also will require go1.9 to use the new `database/sql.Conn`
type, CI has been updated accordingly.

some doc fixes too from testing.. and of course updated all deps.

anyway, whew. this should let us add fields to the db without busting
everybody's dbs. open to feedback on better ways, but this was overall pretty
simple despite futzing with mysql.

* add migrate pkg to deps, update deps

use rdallman/migrate until we resolve in mattes land

* add README in migrations package

* add ref to mattes lib
2017-11-14 12:54:33 -08:00
Tolga Ceylan
af8eed098d fn: graceful shutdown adjustments (#498)
Graceful shutdown should wait for Shutdown call instead of
ListenAndServe. This is because ListenAndServe returns
immediately upon Shutdown call.
2017-11-13 17:58:15 -08:00
Tolga Ceylan
a530cd9be3 Minor naming and control flow changes to satisfy golint 2017-11-02 15:36:55 -07:00
Nigel Deakin
222a80bbd6 Run gofmt 2017-10-27 16:29:35 +01:00
Nigel Deakin
063c2b2e91 Call setTracer() before Agent creation 2017-10-27 16:15:01 +01:00
Reed Allman
241cca5b92 Merge pull request #465 from fnproject/fix_inconsistent_cardinality
Add protection against inconsistent cardinaliity errors
2017-10-26 12:49:51 -07:00
Nigel Deakin
7bc64ecc0a Add protection against inconsistent cardinaliity errors 2017-10-26 19:38:46 +01:00
Reed Allman
ce252d0448 Merge pull request #424 from fnproject/call-listener
CallListener - replaces RunnerListener
2017-10-26 10:36:14 -07:00
Nigel Deakin
c6dd66f3ce Send serve_http spans to Prometheus as well. Revamp Grafana dashboard. 2017-10-25 15:22:25 +01:00
Travis Reeder
de04562b8e Pushed triggers into start() and end() 2017-10-25 14:14:31 +02:00
Travis Reeder
633b26594e minor 2017-10-25 14:13:26 +02:00
Travis Reeder
d080c23981 First draft of modifying RunnerListener to CallListener to get it closer to the action (and named better). 2017-10-25 14:13:25 +02:00
Nigel Deakin
0ddb750eaa Send serve_http spans to Prometheus as well. Revamp Grafana dashboard. 2017-10-25 12:58:08 +01:00
Nigel Deakin
e72cd55a4a Various changes in response to comments 2017-10-23 15:32:50 +01:00
Nigel Deakin
02d49ffd93 Run gofmt 2017-10-20 19:04:47 +01:00
Nigel Deakin
39feaf8b69 Send tracing spans to Prometheus 2017-10-20 16:30:19 +01:00
Nigel Deakin
1646d25c01 Merge pull request #396 from fnproject/add_prometheus_metrics
Add Prometheus statistics and an example to showcase them using Grafana
2017-10-10 09:37:28 +01:00
Reed Allman
8a59654582 go vet yourself (#397)
go vet caught some nifty bugs. so fixed those here, and also made it so that
we vet everything from now on since the robots seem to do a better job of
vetting than we have managed to.

also adds gofmt check to circle. could move this to the test.sh script (didn't
want a script calling a script, because $reasons) and it's nice and isolated
in its own little land as it is. side note, changed the script so it runs in
100ms instead of 3s, i think find is a lot faster than go list.

attempted some minor cleanup of various scripts
2017-10-06 08:42:33 -07:00
Nigel Deakin
ae31944224 Add Prometheus statistics and an example to showcase them using Grafana 2017-10-05 16:21:31 +01:00