mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
clean up hotf(x) concurrency, rm max c
this patch gets rid of max concurrency for functions altogether, as discussed, since it will be challenging to support across functions nodes. as a result of doing so, the previous version of functions would fall over when offered 1000 functions, so there was some work needed in order to push this through. further work is necessary as docker basically falls over when trying to start enough containers at the same time, and with this patch essentially every function can scale infinitely. it seems like we could add some kind of adaptive restrictions based on task run length and configured wait time so that fast running functions will line up to run in a hot container instead of them all creating new hot containers. this patch takes a first cut at whacking out some of the insanity that was the previous concurrency model, which was problematic in that it limited concurrency significantly across all functions since every task went through the same unbuffered channel, which could create blocking issues for all functions if the channel is not picked off fast enough (it's not apparent that this was impossible in the previous implementation). in any event, each request has a goroutine already, there's no reason not to use it. not too hard to wrap a map in a lock, not sure what the benefits were (added insanity?) in effect this is marginally easier to understand and less insane (marginally). after getting rid of max c this adds a blocking mechanism for the first invocation of any function so that all other hot functions will wait on the first one to finish to avoid a herd issue (was making docker die...) -- this could be slightly improved, but works in a pinch. reduced some memory usage by having redundant maps of htfnsvr's and task.Requests (by a factor of 2!). cleaned up some of the protocol stuff, need to clean this up further. anyway, it's a first cut. have another patch that rewrites all of it but was getting into rabbit hole territory, would be happy to oblige if anybody else has problems understanding this rat's nest of channels. there is a good bit of work left to make this prod ready (regardless of removing max c). a warning that this will break the db schemas, didn't put the effort in to add migration stuff since this isn't deployed anywhere in prod... TODO need to clean out the htfnmgr bucket with LRU TODO need to clean up runner interface TODO need to unify the task running paths across protocols TODO need to move the ram checking stuff into worker for noted reasons TODO need better elasticity of hot f(x) containers
This commit is contained in:
@@ -152,7 +152,7 @@ func (s *Server) loadroutes(ctx context.Context, filter models.RouteFilter) ([]*
|
||||
}
|
||||
|
||||
// TODO: Should remove *gin.Context from these functions, should use only context.Context
|
||||
func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, found *models.Route, app *models.App, route, reqID string, payload io.Reader, enqueue models.Enqueue, ) (ok bool) {
|
||||
func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, found *models.Route, app *models.App, route, reqID string, payload io.Reader, enqueue models.Enqueue) (ok bool) {
|
||||
ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"app": appName, "route": found.Path, "image": found.Image})
|
||||
|
||||
params, match := matchRoute(found.Path, route)
|
||||
@@ -193,18 +193,17 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun
|
||||
}
|
||||
|
||||
cfg := &task.Config{
|
||||
AppName: appName,
|
||||
Path: found.Path,
|
||||
Env: envVars,
|
||||
Format: found.Format,
|
||||
ID: reqID,
|
||||
Image: found.Image,
|
||||
MaxConcurrency: found.MaxConcurrency,
|
||||
Memory: found.Memory,
|
||||
Stdin: payload,
|
||||
Stdout: &stdout,
|
||||
Timeout: time.Duration(found.Timeout) * time.Second,
|
||||
IdleTimeout: time.Duration(found.IdleTimeout) * time.Second,
|
||||
AppName: appName,
|
||||
Path: found.Path,
|
||||
Env: envVars,
|
||||
Format: found.Format,
|
||||
ID: reqID,
|
||||
Image: found.Image,
|
||||
Memory: found.Memory,
|
||||
Stdin: payload,
|
||||
Stdout: &stdout,
|
||||
Timeout: time.Duration(found.Timeout) * time.Second,
|
||||
IdleTimeout: time.Duration(found.IdleTimeout) * time.Second,
|
||||
}
|
||||
|
||||
// ensure valid values
|
||||
@@ -244,8 +243,7 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun
|
||||
c.JSON(http.StatusAccepted, map[string]string{"call_id": newTask.ID})
|
||||
|
||||
default:
|
||||
result, err := runner.RunTrackedTask(newTask, s.tasks, ctx, cfg, s.Datastore)
|
||||
|
||||
result, err := s.Runner.RunTrackedTask(newTask, ctx, cfg, s.Datastore)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, runnerResponse{
|
||||
RequestID: cfg.ID,
|
||||
|
||||
@@ -121,8 +121,6 @@ func TestRouteRunnerExecution(t *testing.T) {
|
||||
rnr, cancelrnr := testRunner(t)
|
||||
defer cancelrnr()
|
||||
|
||||
go runner.StartWorkers(ctx, rnr, tasks)
|
||||
|
||||
srv := testServer(datastore.NewMockInit(
|
||||
[]*models.App{
|
||||
{Name: "myapp", Config: models.Config{}},
|
||||
@@ -179,7 +177,6 @@ func TestRouteRunnerTimeout(t *testing.T) {
|
||||
|
||||
rnr, cancelrnr := testRunner(t)
|
||||
defer cancelrnr()
|
||||
go runner.StartWorkers(ctx, rnr, tasks)
|
||||
|
||||
srv := testServer(datastore.NewMockInit(
|
||||
[]*models.App{
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"gitlab-odx.oracle.com/odx/functions/api/mqs"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/common"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/runner/task"
|
||||
"gitlab-odx.oracle.com/odx/functions/api/server/internal/routecache"
|
||||
)
|
||||
|
||||
@@ -49,7 +48,6 @@ type Server struct {
|
||||
|
||||
mu sync.Mutex // protects hotroutes
|
||||
hotroutes *routecache.Cache
|
||||
tasks chan task.Request
|
||||
singleflight singleflight // singleflight assists Datastore
|
||||
}
|
||||
|
||||
@@ -83,14 +81,12 @@ func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, apiUR
|
||||
return nil
|
||||
}
|
||||
|
||||
tasks := make(chan task.Request)
|
||||
s := &Server{
|
||||
Runner: rnr,
|
||||
Router: gin.New(),
|
||||
Datastore: ds,
|
||||
MQ: mq,
|
||||
hotroutes: routecache.New(cacheSize),
|
||||
tasks: tasks,
|
||||
Enqueue: DefaultEnqueue,
|
||||
apiURL: apiURL,
|
||||
}
|
||||
@@ -200,7 +196,6 @@ func extractFields(c *gin.Context) logrus.Fields {
|
||||
func (s *Server) Start(ctx context.Context) {
|
||||
ctx = contextWithSignal(ctx, os.Interrupt)
|
||||
s.startGears(ctx)
|
||||
close(s.tasks)
|
||||
}
|
||||
|
||||
func (s *Server) startGears(ctx context.Context) {
|
||||
@@ -245,14 +240,11 @@ func (s *Server) startGears(ctx context.Context) {
|
||||
})
|
||||
|
||||
svr.AddFunc(func(ctx context.Context) {
|
||||
runner.RunAsyncRunner(ctx, s.apiURL, s.tasks, s.Runner, s.Datastore)
|
||||
})
|
||||
|
||||
svr.AddFunc(func(ctx context.Context) {
|
||||
runner.StartWorkers(ctx, s.Runner, s.tasks)
|
||||
runner.RunAsyncRunner(ctx, s.apiURL, s.Runner, s.Datastore)
|
||||
})
|
||||
|
||||
svr.Serve(ctx)
|
||||
s.Runner.Wait() // wait for tasks to finish (safe shutdown)
|
||||
}
|
||||
|
||||
func (s *Server) bindHandlers(ctx context.Context) {
|
||||
@@ -321,11 +313,11 @@ type tasksResponse struct {
|
||||
}
|
||||
|
||||
type fnCallResponse struct {
|
||||
Message string `json:"message"`
|
||||
Message string `json:"message"`
|
||||
Call *models.FnCall `json:"call"`
|
||||
}
|
||||
|
||||
type fnCallsResponse struct {
|
||||
Message string `json:"message"`
|
||||
Calls models.FnCalls `json:"calls"`
|
||||
Message string `json:"message"`
|
||||
Calls models.FnCalls `json:"calls"`
|
||||
}
|
||||
|
||||
@@ -104,8 +104,6 @@ func TestFullStack(t *testing.T) {
|
||||
rnr, rnrcancel := testRunner(t)
|
||||
defer rnrcancel()
|
||||
|
||||
go runner.StartWorkers(ctx, rnr, tasks)
|
||||
|
||||
srv := testServer(ds, &mqs.Mock{}, rnr, tasks)
|
||||
srv.hotroutes = routecache.New(2)
|
||||
|
||||
|
||||
@@ -20,8 +20,6 @@ func TestSpecialHandlerSet(t *testing.T) {
|
||||
// rnr, cancelrnr := testRunner(t)
|
||||
// defer cancelrnr()
|
||||
|
||||
// go runner.StartWorkers(ctx, rnr, tasks)
|
||||
|
||||
// s := &Server{
|
||||
// Runner: rnr,
|
||||
// Router: gin.New(),
|
||||
|
||||
Reference in New Issue
Block a user