Pass context to functionCache functions and debug messages in pool cache (#2244)
* Active requests count tracking with debug messages * Pass required contexts to cache functions * Fix duplicate imports Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
@@ -19,11 +19,14 @@ limitations under the License.
|
||||
package poolcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
|
||||
ferror "github.com/fission/fission/pkg/error"
|
||||
otelUtils "github.com/fission/fission/pkg/utils/otel"
|
||||
)
|
||||
|
||||
type requestType int
|
||||
@@ -49,10 +52,12 @@ type (
|
||||
Cache struct {
|
||||
cache map[interface{}]map[interface{}]*value
|
||||
requestChannel chan *request
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
request struct {
|
||||
requestType
|
||||
ctx context.Context
|
||||
function interface{}
|
||||
address interface{}
|
||||
value interface{}
|
||||
@@ -69,10 +74,11 @@ type (
|
||||
)
|
||||
|
||||
// NewPoolCache create a Cache object
|
||||
func NewPoolCache() *Cache {
|
||||
func NewPoolCache(logger *zap.Logger) *Cache {
|
||||
c := &Cache{
|
||||
cache: make(map[interface{}]map[interface{}]*value),
|
||||
requestChannel: make(chan *request),
|
||||
logger: logger,
|
||||
}
|
||||
go c.service()
|
||||
return c
|
||||
@@ -94,6 +100,9 @@ func (c *Cache) service() {
|
||||
if values[addr].activeRequests < req.requestsPerPod && values[addr].currentCPUUsage.Cmp(values[addr].cpuLimit) < 1 {
|
||||
// mark active
|
||||
values[addr].activeRequests++
|
||||
if c.logger.Core().Enabled(zap.DebugLevel) {
|
||||
otelUtils.LoggerWithTraceID(req.ctx, c.logger).Debug("Increase active requests with getValue", zap.String("function", req.function.(string)), zap.String("address", addr.(string)), zap.Int("activeRequests", values[addr].activeRequests))
|
||||
}
|
||||
resp.value = values[addr].val
|
||||
found = true
|
||||
break
|
||||
@@ -114,12 +123,22 @@ func (c *Cache) service() {
|
||||
}
|
||||
c.cache[req.function][req.address].val = req.value
|
||||
c.cache[req.function][req.address].activeRequests++
|
||||
if c.logger.Core().Enabled(zap.DebugLevel) {
|
||||
otelUtils.LoggerWithTraceID(req.ctx, c.logger).Debug("Increase active requests with setValue", zap.String("function", req.function.(string)), zap.String("address", req.address.(string)), zap.Int("activeRequests", c.cache[req.function][req.address].activeRequests))
|
||||
}
|
||||
c.cache[req.function][req.address].cpuLimit = req.cpuUsage
|
||||
case listAvailableValue:
|
||||
vals := make([]interface{}, 0)
|
||||
for _, values := range c.cache {
|
||||
for _, value := range values {
|
||||
for key1, values := range c.cache {
|
||||
for key2, value := range values {
|
||||
debugLevel := c.logger.Core().Enabled(zap.DebugLevel)
|
||||
if debugLevel {
|
||||
otelUtils.LoggerWithTraceID(req.ctx, c.logger).Debug("Reading active requests", zap.String("function", key1.(string)), zap.String("address", key2.(string)), zap.Int("activeRequests", value.activeRequests))
|
||||
}
|
||||
if value.activeRequests == 0 {
|
||||
if debugLevel {
|
||||
otelUtils.LoggerWithTraceID(req.ctx, c.logger).Debug("Function service with no acitve requests", zap.String("function", key1.(string)), zap.String("address", key2.(string)), zap.Int("activeRequests", value.activeRequests))
|
||||
}
|
||||
vals = append(vals, value.val)
|
||||
}
|
||||
}
|
||||
@@ -137,6 +156,9 @@ func (c *Cache) service() {
|
||||
if _, ok := c.cache[req.function]; ok {
|
||||
if _, ok = c.cache[req.function][req.address]; ok {
|
||||
c.cache[req.function][req.address].activeRequests--
|
||||
if c.logger.Core().Enabled(zap.DebugLevel) {
|
||||
otelUtils.LoggerWithTraceID(req.ctx, c.logger).Debug("Decrease active requests", zap.String("function", req.function.(string)), zap.String("address", req.address.(string)), zap.Int("activeRequests", c.cache[req.function][req.address].activeRequests))
|
||||
}
|
||||
}
|
||||
}
|
||||
case deleteValue:
|
||||
@@ -151,9 +173,10 @@ func (c *Cache) service() {
|
||||
}
|
||||
|
||||
// GetValue returns a value interface with status inActive else return error
|
||||
func (c *Cache) GetValue(function interface{}, requestsPerPod int) (interface{}, int, error) {
|
||||
func (c *Cache) GetValue(ctx context.Context, function interface{}, requestsPerPod int) (interface{}, int, error) {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
ctx: ctx,
|
||||
requestType: getValue,
|
||||
function: function,
|
||||
requestsPerPod: requestsPerPod,
|
||||
@@ -175,9 +198,10 @@ func (c *Cache) ListAvailableValue() []interface{} {
|
||||
}
|
||||
|
||||
// SetValue marks the value at key [function][address] as active(begin used)
|
||||
func (c *Cache) SetValue(function, address, value interface{}, cpuLimit resource.Quantity) {
|
||||
func (c *Cache) SetValue(ctx context.Context, function, address, value interface{}, cpuLimit resource.Quantity) {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
ctx: ctx,
|
||||
requestType: setValue,
|
||||
function: function,
|
||||
address: address,
|
||||
@@ -210,9 +234,10 @@ func (c *Cache) MarkAvailable(function, address interface{}) {
|
||||
}
|
||||
|
||||
// DeleteValue deletes the value at key composed of [function][address]
|
||||
func (c *Cache) DeleteValue(function, address interface{}) error {
|
||||
func (c *Cache) DeleteValue(ctx context.Context, function, address interface{}) error {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
ctx: ctx,
|
||||
requestType: deleteValue,
|
||||
function: function,
|
||||
address: address,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package poolcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
|
||||
"github.com/fission/fission/pkg/utils/loggerfactory"
|
||||
)
|
||||
|
||||
func checkErr(err error) {
|
||||
@@ -14,15 +17,17 @@ func checkErr(err error) {
|
||||
}
|
||||
|
||||
func TestPoolCache(t *testing.T) {
|
||||
c := NewPoolCache()
|
||||
ctx := context.Background()
|
||||
logger := loggerfactory.GetLogger()
|
||||
c := NewPoolCache(logger)
|
||||
|
||||
c.SetValue("func", "ip", "value", resource.MustParse("45m"))
|
||||
c.SetValue(ctx, "func", "ip", "value", resource.MustParse("45m"))
|
||||
|
||||
c.SetValue("func2", "ip2", "value2", resource.MustParse("50m"))
|
||||
c.SetValue(ctx, "func2", "ip2", "value2", resource.MustParse("50m"))
|
||||
|
||||
c.SetValue("func2", "ip22", "value22", resource.MustParse("33m"))
|
||||
c.SetValue(ctx, "func2", "ip22", "value22", resource.MustParse("33m"))
|
||||
|
||||
checkErr(c.DeleteValue("func2", "ip2"))
|
||||
checkErr(c.DeleteValue(ctx, "func2", "ip2"))
|
||||
|
||||
cc := c.ListAvailableValue()
|
||||
if len(cc) != 0 {
|
||||
@@ -31,28 +36,28 @@ func TestPoolCache(t *testing.T) {
|
||||
|
||||
c.MarkAvailable("func", "ip")
|
||||
|
||||
_, active, err := c.GetValue("func", 5)
|
||||
_, active, err := c.GetValue(ctx, "func", 5)
|
||||
if active != 1 {
|
||||
log.Panicln("Expected 1 active, found", active)
|
||||
}
|
||||
checkErr(err)
|
||||
|
||||
checkErr(c.DeleteValue("func", "ip"))
|
||||
checkErr(c.DeleteValue(ctx, "func", "ip"))
|
||||
|
||||
_, _, err = c.GetValue("func", 5)
|
||||
_, _, err = c.GetValue(ctx, "func", 5)
|
||||
if err == nil {
|
||||
log.Panicf("found deleted element")
|
||||
}
|
||||
|
||||
c.SetValue("cpulimit", "100", "value", resource.MustParse("3m"))
|
||||
c.SetValue(ctx, "cpulimit", "100", "value", resource.MustParse("3m"))
|
||||
c.SetCPUUtilization("cpulimit", "100", resource.MustParse("4m"))
|
||||
|
||||
_, _, err = c.GetValue("cpulimit", 5)
|
||||
_, _, err = c.GetValue(ctx, "cpulimit", 5)
|
||||
|
||||
if err == nil {
|
||||
log.Panicf("received pod address with higher CPU usage than limit")
|
||||
}
|
||||
c.SetCPUUtilization("cpulimit", "100", resource.MustParse("2m"))
|
||||
_, _, err = c.GetValue("cpulimit", 5)
|
||||
_, _, err = c.GetValue(ctx, "cpulimit", 5)
|
||||
checkErr(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user