mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-02-03 20:51:07 -05:00
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing-integration / test-mariadb (v10.6) (push) Waiting to run
testing-integration / test-mariadb (v11.8) (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
The `repository` table has quite a few "count of related objects" fields on it, including the number of issues, closed issues, pull requests, and closed pull requests. These fields specifically will cause deadlocks during concurrent PR merges as documented in #9785. These fields are not used in database queries. In order to eliminate the deadlock possibility on them, I've moved them to be calculated on-demand with caching, with the cache being invalidated in the same places that the recalc used to be triggered. I've supplemented the already in-place automated testing with manual testing performing simple close & reopen of issues & PRs, and the counts which are used in the tabs at the top of the repo page are updated correctly as expected. Near future work: - Similar change can probably be performed to fix #9846 - Last known deadlock identified from #9785; I'm hoping to incorporate the synthetic deadlock test in a near future PR to prevent regressions ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - Tests were already in-place covering these fields; they've been adjusted from using the fields to the new accessor methods. - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/9922): <!--number 9922 --><!--line 0 --><!--description cmVkdWNlIGRlYWRsb2NrcyBtZXJnaW5nIFBScyBieSB1c2luZyBjYWNoaW5nIGZvciByZXBvIGlzc3VlIGNvdW50IHN0YXRz-->reduce deadlocks merging PRs by using caching for repo issue count stats<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9922 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
185 lines
3.8 KiB
Go
185 lines
3.8 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"forgejo.org/modules/setting"
|
|
|
|
mc "code.forgejo.org/go-chi/cache"
|
|
|
|
_ "code.forgejo.org/go-chi/cache/memcache" // memcache plugin for cache
|
|
)
|
|
|
|
var conn mc.Cache
|
|
|
|
func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
|
|
return mc.NewCacher(mc.Options{
|
|
Adapter: cacheConfig.Adapter,
|
|
AdapterConfig: cacheConfig.Conn,
|
|
Interval: cacheConfig.Interval,
|
|
})
|
|
}
|
|
|
|
// Init start cache service
|
|
func Init() error {
|
|
var err error
|
|
|
|
if conn == nil {
|
|
if conn, err = newCache(setting.CacheService.Cache); err != nil {
|
|
return err
|
|
}
|
|
if err = conn.Ping(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
const (
|
|
testCacheKey = "DefaultCache.TestKey"
|
|
SlowCacheThreshold = 100 * time.Microsecond
|
|
)
|
|
|
|
func Test() (time.Duration, error) {
|
|
if conn == nil {
|
|
return 0, errors.New("default cache not initialized")
|
|
}
|
|
|
|
testData := fmt.Sprintf("%x", make([]byte, 500))
|
|
|
|
start := time.Now()
|
|
|
|
if err := conn.Delete(testCacheKey); err != nil {
|
|
return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err)
|
|
}
|
|
if err := conn.Put(testCacheKey, testData, 10); err != nil {
|
|
return 0, fmt.Errorf("expect cache to store data but got: %w", err)
|
|
}
|
|
testVal := conn.Get(testCacheKey)
|
|
if testVal == nil {
|
|
return 0, errors.New("expect cache hit but got none")
|
|
}
|
|
if testVal != testData {
|
|
return 0, errors.New("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
|
|
}
|
|
|
|
// GetString returns the key value from cache with callback when no key exists in cache
|
|
func GetString(key string, getFunc func() (string, error)) (string, error) {
|
|
if conn == nil || setting.CacheService.TTL <= 0 {
|
|
return getFunc()
|
|
}
|
|
|
|
cached := conn.Get(key)
|
|
|
|
if cached == nil {
|
|
value, err := getFunc()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
}
|
|
|
|
if value, ok := cached.(string); ok {
|
|
return value, nil
|
|
}
|
|
|
|
if stringer, ok := cached.(fmt.Stringer); ok {
|
|
return stringer.String(), nil
|
|
}
|
|
|
|
return fmt.Sprintf("%s", cached), nil
|
|
}
|
|
|
|
// GetInt returns key value from cache with callback when no key exists in cache
|
|
func GetInt(key string, getFunc func() (int, error)) (int, error) {
|
|
if conn == nil || setting.CacheService.TTL <= 0 {
|
|
return getFunc()
|
|
}
|
|
|
|
cached := conn.Get(key)
|
|
|
|
if cached == nil {
|
|
value, err := getFunc()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
}
|
|
|
|
switch v := cached.(type) {
|
|
case int:
|
|
return v, nil
|
|
case string:
|
|
value, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return value, nil
|
|
default:
|
|
value, err := getFunc()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
}
|
|
}
|
|
|
|
// GetInt64 returns key value from cache with callback when no key exists in cache
|
|
func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
|
|
if conn == nil || setting.CacheService.TTL <= 0 {
|
|
return getFunc()
|
|
}
|
|
|
|
cached := conn.Get(key)
|
|
|
|
if cached == nil {
|
|
value, err := getFunc()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
}
|
|
|
|
switch v := conn.Get(key).(type) {
|
|
case int64:
|
|
return v, nil
|
|
case string:
|
|
value, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return value, nil
|
|
default:
|
|
value, err := getFunc()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
}
|
|
}
|
|
|
|
// Remove key from cache
|
|
func Remove(key string) {
|
|
if conn == nil {
|
|
return
|
|
}
|
|
_ = conn.Delete(key)
|
|
}
|