* Optimize router response time by adding update lock In 0.9.2, the router sends multiple GetServiceForFunction requests to executor to get the service URL. However, the response time of executor will increase under high-concurrency situation due to too many requests are waiting for processing. To solve the problem, an update lock was added to the router. All of goroutines belongs to the same function need to grab the update lock before sending the request. Only the first goroutine which gets the update lock is allowed to send request. In this way, we reduce the burden of executor and lower the failure rate.
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
/*
|
|
Copyright 2016 The Fission Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package router
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"github.com/fission/fission"
|
|
"github.com/fission/fission/crd"
|
|
)
|
|
|
|
func TestRouter(t *testing.T) {
|
|
// metadata for a fake function
|
|
fn := &metav1.ObjectMeta{Name: "foo", Namespace: metav1.NamespaceDefault}
|
|
|
|
// and a reference to it
|
|
fr := fission.FunctionReference{
|
|
Type: fission.FunctionReferenceTypeFunctionName,
|
|
Name: fn.Name,
|
|
}
|
|
|
|
// start a fake service
|
|
testResponseString := "hi"
|
|
testServiceUrl := createBackendService(testResponseString)
|
|
|
|
// set up the cache with this fake service
|
|
fmap := makeFunctionServiceMap(0)
|
|
fmap.assign(fn, testServiceUrl)
|
|
|
|
frmap := makeFunctionRecorderMap(time.Minute)
|
|
|
|
trmap := makeTriggerRecorderMap(time.Minute)
|
|
|
|
// HTTP trigger set with a trigger for this function
|
|
triggers, _, _ := makeHTTPTriggerSet(fmap, frmap, trmap, nil, nil, nil, nil,
|
|
&tsRoundTripperParams{
|
|
timeout: 50 * time.Millisecond,
|
|
timeoutExponent: 2,
|
|
keepAlive: 30 * time.Second,
|
|
maxRetries: 10,
|
|
}, false, MakeUpdateLocks(30*time.Second))
|
|
triggerUrl := "/foo"
|
|
triggers.triggers = append(triggers.triggers,
|
|
crd.HTTPTrigger{
|
|
Metadata: metav1.ObjectMeta{
|
|
Name: "xxx",
|
|
Namespace: metav1.NamespaceDefault,
|
|
ResourceVersion: "1234",
|
|
},
|
|
Spec: fission.HTTPTriggerSpec{
|
|
RelativeURL: triggerUrl,
|
|
FunctionReference: fr,
|
|
Method: "GET",
|
|
},
|
|
})
|
|
|
|
// set up the resolver's cache for this function
|
|
frr := makeFunctionReferenceResolver(nil)
|
|
nfr := namespacedTriggerReference{
|
|
namespace: metav1.NamespaceDefault,
|
|
triggerName: "xxx",
|
|
triggerResourceVersion: "1234",
|
|
}
|
|
|
|
fnMetaMap := make(map[string]*metav1.ObjectMeta, 1)
|
|
fnMetaMap[fn.Name] = fn
|
|
|
|
rr := resolveResult{
|
|
resolveResultType: resolveResultSingleFunction,
|
|
functionMetadataMap: fnMetaMap,
|
|
}
|
|
frr.refCache.Set(nfr, rr)
|
|
|
|
// run the router
|
|
port := 4242
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go serve(ctx, port, triggers, frr)
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// hit the router
|
|
testUrl := fmt.Sprintf("http://localhost:%v%v", port, triggerUrl)
|
|
testRequest(testUrl, testResponseString)
|
|
}
|