From 65c9b3bcef62860c485c0cb1ee8b022d8dbea77c Mon Sep 17 00:00:00 2001 From: Frances Thai Date: Wed, 5 Mar 2025 11:46:51 -0800 Subject: [PATCH 1/3] Fix linter errors Signed-off-by: Frances Thai --- go/vt/vtadmin/cache/cache.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/go/vt/vtadmin/cache/cache.go b/go/vt/vtadmin/cache/cache.go index bc53efb80db..9b21cc8cccb 100644 --- a/go/vt/vtadmin/cache/cache.go +++ b/go/vt/vtadmin/cache/cache.go @@ -23,7 +23,7 @@ import ( "sync" "time" - "github.com/patrickmn/go-cache" + cache "github.com/patrickmn/go-cache" "vitess.io/vitess/go/vt/log" ) @@ -161,8 +161,13 @@ func (c *Cache[Key, Value]) add(key string, val Value, d time.Duration) error { c.lastFill[key] = time.Now().UTC() c.m.Unlock() - // Then cache the actual value. - return c.cache.Add(key, val, d) + _, expr, exists := c.cache.GetWithExpiration(key) + if exists && expr.After(time.Now()) { + return c.cache.Replace(key, val, d) + } else { + // Then cache the actual value. + return c.cache.Add(key, val, d) + } } // Get returns the Value stored for the key, if present in the cache. If the key From 2fe4ad345ee5565659b5878451e63fb5732d439e Mon Sep 17 00:00:00 2001 From: Frances Thai Date: Wed, 5 Mar 2025 13:10:07 -0800 Subject: [PATCH 2/3] Add log line Signed-off-by: Frances Thai --- examples/common/scripts/vtctld-up.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/common/scripts/vtctld-up.sh b/examples/common/scripts/vtctld-up.sh index 4a4b2587c4f..b0e302679e0 100755 --- a/examples/common/scripts/vtctld-up.sh +++ b/examples/common/scripts/vtctld-up.sh @@ -36,6 +36,8 @@ vtctld \ --pprof-http \ > $VTDATAROOT/tmp/vtctld.out 2>&1 & +echo "Curling \"http://${hostname}:${vtctld_web_port}/debug/status\" to check if vtctld is up" + for _ in {0..300}; do curl -I "http://${hostname}:${vtctld_web_port}/debug/status" &>/dev/null && break sleep 0.1 From 25a304fdd02e692c1892801fe7a557c1cf3de214 Mon Sep 17 00:00:00 2001 From: Frances Thai Date: Wed, 5 Mar 2025 14:08:40 -0800 Subject: [PATCH 3/3] Add tests Signed-off-by: Frances Thai --- go/vt/vtadmin/cache/cache.go | 2 +- go/vt/vtadmin/cache/cache_test.go | 45 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/go/vt/vtadmin/cache/cache.go b/go/vt/vtadmin/cache/cache.go index 9b21cc8cccb..7278990f9f1 100644 --- a/go/vt/vtadmin/cache/cache.go +++ b/go/vt/vtadmin/cache/cache.go @@ -162,7 +162,7 @@ func (c *Cache[Key, Value]) add(key string, val Value, d time.Duration) error { c.m.Unlock() _, expr, exists := c.cache.GetWithExpiration(key) - if exists && expr.After(time.Now()) { + if exists && (expr.IsZero() || expr.After(time.Now())) { return c.cache.Replace(key, val, d) } else { // Then cache the actual value. diff --git a/go/vt/vtadmin/cache/cache_test.go b/go/vt/vtadmin/cache/cache_test.go index a86022a8f9d..985d51f4fc8 100644 --- a/go/vt/vtadmin/cache/cache_test.go +++ b/go/vt/vtadmin/cache/cache_test.go @@ -226,3 +226,48 @@ func TestEnqueueBackfillTimeout(t *testing.T) { assert.Equal(t, q.shouldFail, !ok, "enqueue should %s wait timeout", q.msg) } } + +func TestUpsertCacheKey(t *testing.T) { + var callcount int + c := cache.New(func(ctx context.Context, req intkey) (any, error) { + time.Sleep(time.Millisecond * 50) // make fills take time so that the second enqueue exceeds WaitTimeout + callcount++ + return nil, nil + }, cache.Config{ + BackfillEnqueueWaitTime: time.Millisecond * 10, + }) + + var inserts = []struct { + shouldFail bool + msg string + key intkey + val any + expectedVal any + duration time.Duration + }{ + { + shouldFail: false, + key: intkey(1), + val: "Old Value", + msg: "first add should succeed", + expectedVal: "Old Value", + }, + { + shouldFail: false, + key: intkey(1), + val: "New Value", + msg: "second update should succeed", + expectedVal: "New Value", + }, + } + for _, tt := range inserts { + err := c.Add(tt.key, tt.val, tt.duration) + if !tt.shouldFail { + assert.Nil(t, err) + } + + val, exists := c.Get(tt.key) + assert.True(t, exists) + assert.Equal(t, val, tt.expectedVal) + } +}