From 77da92f42a70909a8b68ac9730eed08f1fa90aa6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 17 Jun 2024 21:22:39 +0200 Subject: [PATCH] Add cache test for admins (#31265) Add a test to probe the cache similar to the email test func. ![image](https://github.com/go-gitea/gitea/assets/24977596/700e2733-586d-4091-900f-f5f71e6e94bf) ![image](https://github.com/go-gitea/gitea/assets/24977596/2a953802-18fc-4e81-a37d-24ebe1297365) ![image](https://github.com/go-gitea/gitea/assets/24977596/e00d62ad-bb60-41cc-9138-09993daee156) --------- Co-authored-by: delvh Co-authored-by: silverwind (cherry picked from commit 363c1235987793dffa5cc851aaae585eb81f091e) Conflicts: options/locale/locale_en-US.ini templates/admin/self_check.tmpl trivial context conflict --- modules/cache/cache.go | 31 +++++++++++++++++++++++++++++++ modules/cache/cache_test.go | 12 ++++++++++++ options/locale/locale_en-US.ini | 6 ++++++ routers/web/admin/admin.go | 9 +++++++++ routers/web/admin/config.go | 17 +++++++++++++++++ routers/web/web.go | 1 + templates/admin/config.tmpl | 12 ++++++++++-- templates/admin/self_check.tmpl | 7 +++++++ 8 files changed, 93 insertions(+), 2 deletions(-) diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 09afc8b7f..178af97bb 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -40,6 +40,37 @@ func Init() error { return err } +const ( + testCacheKey = "DefaultCache.TestKey" + SlowCacheThreshold = 100 * time.Microsecond +) + +func Test() (time.Duration, error) { + if defaultCache == nil { + return 0, fmt.Errorf("default cache not initialized") + } + + testData := fmt.Sprintf("%x", make([]byte, 500)) + + start := time.Now() + + if err := defaultCache.Delete(testCacheKey); err != nil { + return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err) + } + if err := defaultCache.Put(testCacheKey, testData, 10); err != nil { + return 0, fmt.Errorf("expect cache to store data but got: %w", err) + } + testVal, hit := defaultCache.Get(testCacheKey) + if !hit { + return 0, fmt.Errorf("expect cache hit but got none") + } + if testVal != testData { + return 0, fmt.Errorf("expect cache to return same value as stored but got other") + } + + return time.Since(start), nil +} + // GetCache returns the currently configured cache func GetCache() mc.Cache { return conn diff --git a/modules/cache/cache_test.go b/modules/cache/cache_test.go index 3f6504092..d3e108f11 100644 --- a/modules/cache/cache_test.go +++ b/modules/cache/cache_test.go @@ -34,6 +34,18 @@ func TestNewContext(t *testing.T) { assert.Nil(t, con) } +func TestTest(t *testing.T) { + defaultCache = nil + _, err := Test() + assert.Error(t, err) + + createTestCache() + elapsed, err := Test() + assert.NoError(t, err) + // mem cache should take from 300ns up to 1ms on modern hardware ... + assert.Less(t, elapsed, SlowCacheThreshold) +} + func TestGetCache(t *testing.T) { createTestCache() diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a6a3446ca..5b34670e4 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -93,6 +93,7 @@ remove_all = Remove all remove_label_str = Remove item "%s" edit = Edit view = View +test = Test enabled = Enabled disabled = Disabled @@ -3317,6 +3318,11 @@ config.cache_interval = Cache interval config.cache_conn = Cache connection config.cache_item_ttl = Cache item TTL +config.cache_test = Test Cache +config.cache_test_failed = Failed to probe the cache: %v. +config.cache_test_slow = Cache test successful, but response is slow: %s. +config.cache_test_succeeded = Cache test successful, got a response in %s. + config.session_config = Session configuration config.session_provider = Session provider config.provider_config = Provider config diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 6c778c686..067203b28 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -14,6 +14,7 @@ import ( activities_model "code.gitea.io/gitea/models/activities" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -211,6 +212,14 @@ func SelfCheck(ctx *context.Context) { ctx.Data["DatabaseCheckHasProblems"] = hasProblem } + + elapsed, err := cache.Test() + if err != nil { + ctx.Data["CacheError"] = err + } else if elapsed > cache.SlowCacheThreshold { + ctx.Data["CacheSlow"] = fmt.Sprint(elapsed) + } + ctx.HTML(http.StatusOK, tplSelfCheck) } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index 22f260ede..09f332b44 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -12,6 +12,7 @@ import ( system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -42,6 +43,22 @@ func SendTestMail(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/admin/config") } +// TestCache test the cache settings +func TestCache(ctx *context.Context) { + elapsed, err := cache.Test() + if err != nil { + ctx.Flash.Error(ctx.Tr("admin.config.cache_test_failed", err)) + } else { + if elapsed > cache.SlowCacheThreshold { + ctx.Flash.Warning(ctx.Tr("admin.config.cache_test_slow", elapsed)) + } else { + ctx.Flash.Info(ctx.Tr("admin.config.cache_test_succeeded", elapsed)) + } + } + + ctx.Redirect(setting.AppSubURL + "/admin/config") +} + func shadowPasswordKV(cfgItem, splitter string) string { fields := strings.Split(cfgItem, splitter) for i := 0; i < len(fields); i++ { diff --git a/routers/web/web.go b/routers/web/web.go index 34bfb8b60..3781ffc05 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -665,6 +665,7 @@ func registerRoutes(m *web.Route) { m.Get("", admin.Config) m.Post("", admin.ChangeConfig) m.Post("/test_mail", admin.SendTestMail) + m.Post("/test_cache", admin.TestCache) m.Get("/settings", admin.ConfigSettings) }) diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index d4e5b326f..8f2b1c12e 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -233,8 +233,8 @@
{{ctx.Locale.Tr "admin.config.mailer_user"}}
{{if .Mailer.User}}{{.Mailer.User}}{{else}}(empty){{end}}
-
{{ctx.Locale.Tr "admin.config.send_test_mail"}}
-
+
{{ctx.Locale.Tr "admin.config.send_test_mail"}}
+
{{.CsrfTokenHtml}}
@@ -264,6 +264,14 @@
{{ctx.Locale.Tr "admin.config.cache_item_ttl"}}
{{.CacheItemTTL}}
{{end}} +
+
{{ctx.Locale.Tr "admin.config.cache_test"}}
+
+ + {{.CsrfTokenHtml}} + +
+
diff --git a/templates/admin/self_check.tmpl b/templates/admin/self_check.tmpl index 5c154ac0d..afcd4cd64 100644 --- a/templates/admin/self_check.tmpl +++ b/templates/admin/self_check.tmpl @@ -28,6 +28,13 @@ {{else}}
{{ctx.Locale.Tr "admin.self_check.no_problem_found"}}
{{end}} + + {{if .CacheError}} +
{{ctx.Locale.Tr "admin.config.cache_test_failed" .CacheError}}
+ {{end}} + {{if .CacheSlow}} +
{{ctx.Locale.Tr "admin.config.cache_test_slow" .CacheSlow}}
+ {{end}}