Merge pull request #67 from fission/function-versioning-cache-bug
Update router cache on new function version
This commit is contained in:
+10
-2
@@ -119,11 +119,19 @@ func (api *API) getFunctionEnv(m *fission.Metadata) (*fission.Environment, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *API) getServiceForFunction(m *fission.Metadata) (string, error) {
|
func (api *API) getServiceForFunction(m *fission.Metadata) (string, error) {
|
||||||
// Check function -> svc map
|
// Make sure we have the full metadata. This ensures that
|
||||||
|
// poolmgr does not implicitly interpret empty-UID as latest
|
||||||
|
// version.
|
||||||
|
if len(m.Uid) == 0 {
|
||||||
|
return "", fission.MakeError(fission.ErrorInvalidArgument,
|
||||||
|
fmt.Sprintf("invalid metadata for function %v", m.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check function -> svc cache
|
||||||
log.Printf("[%v] Checking for cached function service", m.Name)
|
log.Printf("[%v] Checking for cached function service", m.Name)
|
||||||
fsvc, err := api.fsCache.GetByFunction(m)
|
fsvc, err := api.fsCache.GetByFunction(m)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Cached, return svc name
|
// Cached, return svc address
|
||||||
return fsvc.address, nil
|
return fsvc.address, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-21
@@ -18,8 +18,6 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@@ -57,11 +55,22 @@ func (ts *HTTPTriggerSet) subscribeRouter(mr *mutableRouter) {
|
|||||||
func (ts *HTTPTriggerSet) getRouter() *mux.Router {
|
func (ts *HTTPTriggerSet) getRouter() *mux.Router {
|
||||||
muxRouter := mux.NewRouter()
|
muxRouter := mux.NewRouter()
|
||||||
|
|
||||||
|
// make a name -> latest version map
|
||||||
|
latestVersions := make(map[string]string)
|
||||||
|
for _, f := range ts.functions {
|
||||||
|
latestVersions[f.Metadata.Name] = f.Metadata.Uid
|
||||||
|
}
|
||||||
|
|
||||||
// HTTP triggers setup by the user
|
// HTTP triggers setup by the user
|
||||||
for _, trigger := range ts.triggers {
|
for _, trigger := range ts.triggers {
|
||||||
|
m := trigger.Function
|
||||||
|
if len(m.Uid) == 0 {
|
||||||
|
// explicitly use the latest function version
|
||||||
|
m.Uid = latestVersions[m.Name]
|
||||||
|
}
|
||||||
fh := &functionHandler{
|
fh := &functionHandler{
|
||||||
fmap: ts.functionServiceMap,
|
fmap: ts.functionServiceMap,
|
||||||
Function: trigger.Function,
|
Function: m,
|
||||||
poolmgr: ts.poolmgr,
|
poolmgr: ts.poolmgr,
|
||||||
}
|
}
|
||||||
muxRouter.HandleFunc(trigger.UrlPattern, fh.handler)
|
muxRouter.HandleFunc(trigger.UrlPattern, fh.handler)
|
||||||
@@ -87,25 +96,10 @@ func (ts *HTTPTriggerSet) watchTriggers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the number of connection failures we'll accept before quitting
|
// the number of connection failures we'll accept before quitting
|
||||||
var err error
|
|
||||||
maxFailures := 5
|
maxFailures := 5
|
||||||
maxFailuresEnv := os.Getenv("FISSION_ROUTER_MAX_FAILURES")
|
|
||||||
if len(maxFailuresEnv) != 0 {
|
|
||||||
maxFailures, err = strconv.Atoi(maxFailuresEnv)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("FISSION_ROUTER_MAX_FAILURES must be an integer, found %v", maxFailuresEnv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// amount of time to sleep between polling calls
|
// amount of time to sleep between polling calls
|
||||||
pollSleepSec := 3
|
pollSleepDuration := 3 * time.Second
|
||||||
pollSleepEnv := os.Getenv("FISSION_ROUTER_POLL_SLEEP_SECONDS")
|
|
||||||
if len(pollSleepEnv) != 0 {
|
|
||||||
pollSleepSec, err = strconv.Atoi(pollSleepEnv)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("FISSION_ROUTER_POLL_SLEEP_SECONDS must be an integer, found %v", pollSleepEnv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Watch controller for updates to triggers and update the router accordingly.
|
// Watch controller for updates to triggers and update the router accordingly.
|
||||||
// TODO change this to use a watch API; or maybe even watch etcd directly.
|
// TODO change this to use a watch API; or maybe even watch etcd directly.
|
||||||
@@ -117,7 +111,7 @@ func (ts *HTTPTriggerSet) watchTriggers() {
|
|||||||
if failureCount >= maxFailures {
|
if failureCount >= maxFailures {
|
||||||
log.Fatalf("Failed to connect to controller after %v retries: %v", failureCount, err)
|
log.Fatalf("Failed to connect to controller after %v retries: %v", failureCount, err)
|
||||||
}
|
}
|
||||||
time.Sleep(time.Duration(pollSleepSec) * time.Second)
|
time.Sleep(pollSleepDuration)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ts.triggers = triggers
|
ts.triggers = triggers
|
||||||
@@ -129,6 +123,6 @@ func (ts *HTTPTriggerSet) watchTriggers() {
|
|||||||
ts.functions = functions
|
ts.functions = functions
|
||||||
|
|
||||||
ts.mutableRouter.updateRouter(ts.getRouter())
|
ts.mutableRouter.updateRouter(ts.getRouter())
|
||||||
time.Sleep(time.Duration(pollSleepSec) * time.Second)
|
time.Sleep(pollSleepDuration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user