mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
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
This commit is contained in:
@@ -37,6 +37,7 @@ const (
|
||||
EnvMQURL = "FN_MQ_URL"
|
||||
EnvDBURL = "FN_DB_URL"
|
||||
EnvLOGDBURL = "FN_LOGSTORE_URL"
|
||||
EnvNodeType = "FN_NODE_TYPE"
|
||||
EnvPort = "FN_PORT" // be careful, Gin expects this variable to be "port"
|
||||
EnvAPICORS = "FN_API_CORS"
|
||||
EnvZipkinURL = "FN_ZIPKIN_URL"
|
||||
@@ -46,31 +47,52 @@ const (
|
||||
DefaultPort = 8080
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Router *gin.Engine
|
||||
Agent agent.Agent
|
||||
Datastore models.Datastore
|
||||
MQ models.MessageQueue
|
||||
LogDB models.LogStore
|
||||
type ServerNodeType int32
|
||||
|
||||
const (
|
||||
ServerTypeFull ServerNodeType = iota
|
||||
ServerTypeAPI
|
||||
ServerTypeRunner
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Router *gin.Engine
|
||||
Agent agent.Agent
|
||||
Datastore models.Datastore
|
||||
MQ models.MessageQueue
|
||||
LogDB models.LogStore
|
||||
nodeType ServerNodeType
|
||||
appListeners []fnext.AppListener
|
||||
rootMiddlewares []fnext.Middleware
|
||||
apiMiddlewares []fnext.Middleware
|
||||
}
|
||||
|
||||
func nodeTypeFromString(value string) ServerNodeType {
|
||||
switch value {
|
||||
case "api":
|
||||
return ServerTypeAPI
|
||||
case "runner":
|
||||
return ServerTypeRunner
|
||||
default:
|
||||
return ServerTypeFull
|
||||
}
|
||||
}
|
||||
|
||||
// NewFromEnv creates a new Functions server based on env vars.
|
||||
func NewFromEnv(ctx context.Context, opts ...ServerOption) *Server {
|
||||
|
||||
return NewFromURLs(ctx,
|
||||
getEnv(EnvDBURL, fmt.Sprintf("sqlite3://%s/data/fn.db", currDir)),
|
||||
getEnv(EnvMQURL, fmt.Sprintf("bolt://%s/data/fn.mq", currDir)),
|
||||
getEnv(EnvLOGDBURL, ""),
|
||||
nodeTypeFromString(getEnv(EnvNodeType, "")),
|
||||
opts...,
|
||||
)
|
||||
}
|
||||
|
||||
// Create a new server based on the string URLs for each service.
|
||||
// Sits in the middle of NewFromEnv and New
|
||||
func NewFromURLs(ctx context.Context, dbURL, mqURL, logstoreURL string, opts ...ServerOption) *Server {
|
||||
func NewFromURLs(ctx context.Context, dbURL, mqURL, logstoreURL string, nodeType ServerNodeType, opts ...ServerOption) *Server {
|
||||
ds, err := datastore.New(dbURL)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatalln("Error initializing datastore.")
|
||||
@@ -89,19 +111,30 @@ func NewFromURLs(ctx context.Context, dbURL, mqURL, logstoreURL string, opts ...
|
||||
}
|
||||
}
|
||||
|
||||
return New(ctx, ds, mq, logDB, opts...)
|
||||
return New(ctx, ds, mq, logDB, nodeType, opts...)
|
||||
}
|
||||
|
||||
// New creates a new Functions server with the passed in datastore, message queue and API URL
|
||||
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, ls models.LogStore, opts ...ServerOption) *Server {
|
||||
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, ls models.LogStore, nodeType ServerNodeType, opts ...ServerOption) *Server {
|
||||
setTracer()
|
||||
|
||||
var tp agent.AgentNodeType
|
||||
switch nodeType {
|
||||
case ServerTypeAPI:
|
||||
tp = agent.AgentTypeAPI
|
||||
case ServerTypeRunner:
|
||||
tp = agent.AgentTypeRunner
|
||||
default:
|
||||
tp = agent.AgentTypeFull
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
Agent: agent.New(cache.Wrap(ds), ls, mq), // only add datastore caching to agent
|
||||
Agent: agent.New(cache.Wrap(ds), ls, mq, tp), // only add datastore caching to agent
|
||||
Router: gin.New(),
|
||||
Datastore: ds,
|
||||
MQ: mq,
|
||||
LogDB: ls,
|
||||
nodeType: nodeType,
|
||||
}
|
||||
|
||||
// NOTE: testServer() in tests doesn't use these
|
||||
@@ -265,7 +298,7 @@ func (s *Server) bindHandlers(ctx context.Context) {
|
||||
engine.GET("/stats", s.handleStats)
|
||||
engine.GET("/metrics", s.handlePrometheusMetrics)
|
||||
|
||||
{
|
||||
if s.nodeType != ServerTypeRunner {
|
||||
v1 := engine.Group("/v1")
|
||||
v1.Use(s.apiMiddlewareWrapper())
|
||||
v1.GET("/apps", s.handleAppList)
|
||||
@@ -291,6 +324,15 @@ func (s *Server) bindHandlers(ctx context.Context) {
|
||||
apps.GET("/calls/:call", s.handleCallGet)
|
||||
apps.GET("/calls/:call/log", s.handleCallLogGet)
|
||||
}
|
||||
|
||||
{
|
||||
runner := v1.Group("/runner")
|
||||
runner.PUT("/async", s.handleRunnerEnqueue)
|
||||
runner.GET("/async", s.handleRunnerDequeue)
|
||||
|
||||
runner.POST("/start", s.handleRunnerStart)
|
||||
runner.POST("/finish", s.handleRunnerFinish)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user