Skip to content

Commit

Permalink
cache: cherry-pick 9048 (#9123)
Browse files Browse the repository at this point in the history
* cherry-pick

Signed-off-by: 童剑 <[email protected]>

* ci: transitioning action version from node 16 to node 20 (#8071) (#8628)

close #8070

Signed-off-by: husharp <[email protected]>

Co-authored-by: husharp <[email protected]>

---------

Signed-off-by: 童剑 <[email protected]>
Co-authored-by: Ti Chi Robot <[email protected]>
Co-authored-by: husharp <[email protected]>
  • Loading branch information
3 people authored Mar 7, 2025
1 parent 86ddba1 commit 2940bb5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 47 deletions.
16 changes: 3 additions & 13 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Checkout code
uses: actions/checkout@v3
- name: Restore cache
uses: actions/cache@v3
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
path: |
~/go/pkg/mod
~/.cache/go-build
**/.dashboard_download_cache
key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-golang
go-version: 1.19
- name: Make Check
run: |
make build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
add_labels:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v4
- uses: actions/github-script@v7
name: Add labels
with:
script: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pd-docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
strategy:
fail-fast: true
steps:
- uses: actions/setup-go@v3
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.19
- name: Checkout code
uses: actions/checkout@v3
- name: Make
run: make docker-image
26 changes: 9 additions & 17 deletions .github/workflows/pd-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,11 @@ jobs:
outputs:
job-total: 10
steps:
- uses: actions/setup-go@v3
with:
go-version: "1.19"
- name: Checkout code
uses: actions/checkout@v3
- name: Restore cache
uses: actions/cache@v3
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
path: |
~/go/pkg/mod
~/.cache/go-build
**/.tools
**/.dashboard_download_cache
key: ${{ runner.os }}-go-${{ matrix.worker_id }}-${{ hashFiles('**/go.sum') }}
go-version: "1.19"
- name: Make Test
env:
WORKER_ID: ${{ matrix.worker_id }}
Expand All @@ -49,20 +40,21 @@ jobs:
mv covprofile covprofile_$WORKER_ID
sed -i "/failpoint_binding/d" covprofile_$WORKER_ID
- name: Upload coverage result ${{ matrix.worker_id }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: cover-reports
name: cover-reports-${{ matrix.worker_id }}
path: covprofile_${{ matrix.worker_id }}
report-coverage:
needs: chunks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Download chunk report
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: cover-reports
pattern: cover-reports-*
merge-multiple: true
- name: Merge
env:
TOTAL_JOBS: ${{needs.chunks.outputs.job-total}}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tso-consistency-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
tso-consistency-test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.19"
- name: Checkout code
uses: actions/checkout@v3
- name: Make TSO Consistency Test
run: make test-tso-consistency
6 changes: 3 additions & 3 deletions .github/workflows/tso-function-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
tso-function-test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.19"
- name: Checkout code
uses: actions/checkout@v3
- name: Make TSO Function Test
run: make test-tso-function
23 changes: 16 additions & 7 deletions pkg/cache/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cache

import (
"context"
"sync/atomic"
"time"

"github.com/pingcap/log"
Expand All @@ -37,18 +38,19 @@ type ttlCache struct {
items map[interface{}]ttlCacheItem
ttl time.Duration
gcInterval time.Duration
// isGCRunning is used to avoid running GC multiple times.
isGCRunning atomic.Bool
}

// NewTTL returns a new TTL cache.
func newTTL(ctx context.Context, gcInterval time.Duration, duration time.Duration) *ttlCache {
c := &ttlCache{
ctx: ctx,
items: make(map[interface{}]ttlCacheItem),
ttl: duration,
gcInterval: gcInterval,
ctx: ctx,
items: make(map[any]ttlCacheItem),
ttl: duration,
gcInterval: gcInterval,
isGCRunning: atomic.Bool{},
}

go c.doGC()
return c
}

Expand All @@ -61,7 +63,9 @@ func (c *ttlCache) put(key interface{}, value interface{}) {
func (c *ttlCache) putWithTTL(key interface{}, value interface{}, ttl time.Duration) {
c.Lock()
defer c.Unlock()

if len(c.items) == 0 && c.isGCRunning.CompareAndSwap(false, true) {
go c.doGC()
}
c.items[key] = ttlCacheItem{
value: value,
expire: time.Now().Add(ttl),
Expand Down Expand Up @@ -161,6 +165,11 @@ func (c *ttlCache) doGC() {
}
}
}
if len(c.items) == 0 && c.isGCRunning.CompareAndSwap(true, false) {
c.Unlock()
log.Debug("TTL GC items is empty exit")
return
}
c.Unlock()
log.Debug("TTL GC items", zap.Int("count", count))
case <-c.ctx.Done():
Expand Down

0 comments on commit 2940bb5

Please sign in to comment.