From c605d1d2c6f48887316c8881f947375b66164b45 Mon Sep 17 00:00:00 2001 From: Rahul Bhati Date: Mon, 28 Sep 2020 14:09:55 +0530 Subject: [PATCH] Added code to prevent deletion of active fn pod (#1724) Co-authored-by: Vishal --- pkg/executor/fscache/functionServiceCache.go | 2 +- pkg/newcache/poolcache.go | 14 ++++++++------ pkg/newcache/poolcache_test.go | 10 +++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/executor/fscache/functionServiceCache.go b/pkg/executor/fscache/functionServiceCache.go index 7a8882ba..8bf84b5e 100644 --- a/pkg/executor/fscache/functionServiceCache.go +++ b/pkg/executor/fscache/functionServiceCache.go @@ -136,7 +136,7 @@ func (fsc *FunctionServiceCache) service() { } fsc.logger.Info("function service cache", zap.Int("item_count", len(funcCopy)), zap.Strings("cache", info)) case LISTOLDPOOL: - fscs := fsc.connFunctionCache.ListValue() + fscs := fsc.connFunctionCache.ListAvailableValue() funcObjects := make([]*FuncSvc, 0) for _, funcSvc := range fscs { fsvc := funcSvc.(*FuncSvc) diff --git a/pkg/newcache/poolcache.go b/pkg/newcache/poolcache.go index 709cfef1..7518d36b 100644 --- a/pkg/newcache/poolcache.go +++ b/pkg/newcache/poolcache.go @@ -28,7 +28,7 @@ type requestType int const ( getValue requestType = iota - listValue + listAvailableValue getTotalAvailable setValue markAvailable @@ -99,11 +99,13 @@ func (c *Cache) service() { resp.error = ferror.MakeError(ferror.ErrorNotFound, fmt.Sprintf("funtion '%v' No inactive function found", req.function)) } req.responseChannel <- resp - case listValue: + case listAvailableValue: vals := make([]interface{}, 0) for _, values := range c.cache { for _, value := range values { - vals = append(vals, value.val) + if !value.isActive { + vals = append(vals, value.val) + } } } resp.allValues = vals @@ -159,11 +161,11 @@ func (c *Cache) GetValue(function interface{}) (interface{}, error) { return resp.value, resp.error } -// ListValue returns a list of the function services stored in the Cache -func (c *Cache) ListValue() []interface{} { +// ListAvailableValue returns a list of the available function services stored in the Cache +func (c *Cache) ListAvailableValue() []interface{} { respChannel := make(chan *response) c.requestChannel <- &request{ - requestType: listValue, + requestType: listAvailableValue, responseChannel: respChannel, } resp := <-respChannel diff --git a/pkg/newcache/poolcache_test.go b/pkg/newcache/poolcache_test.go index 3bdbb97c..f8b8b9d0 100644 --- a/pkg/newcache/poolcache_test.go +++ b/pkg/newcache/poolcache_test.go @@ -21,9 +21,9 @@ func TestPoolCache(t *testing.T) { c.SetValue("func2", "ip22", "value22") - cc := c.ListValue() - if len(cc) != 3 { - log.Panicf("expected 2 items") + cc := c.ListAvailableValue() + if len(cc) != 0 { + log.Panicf("expected 0 available items") } active := c.GetTotalAvailable("func2") if active != 2 { @@ -33,7 +33,11 @@ func TestPoolCache(t *testing.T) { c.DeleteValue("func2", "ip2") c.MarkAvailable("func", "ip") + cc = c.ListAvailableValue() + if len(cc) != 1 { + log.Panic("expected 1 available items, received", len(cc)) + } _, err := c.GetValue("func") checkErr(err)