Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix data race at the memChangeHook #625

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/twmb/murmur3 v1.1.3
go.etcd.io/etcd/api/v3 v3.5.2
go.etcd.io/etcd/client/v3 v3.5.2
go.uber.org/atomic v1.9.0
go.uber.org/atomic v1.10.0
go.uber.org/goleak v1.1.12
go.uber.org/zap v1.20.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
Expand Down
2 changes: 1 addition & 1 deletion internal/mockstore/mocktikv/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *pdClient) SetExternalTimestamp(ctx context.Context, newTimestamp uint64
return nil
}

if c.externalTimestamp.CAS(externalTimestamp, newTimestamp) {
if c.externalTimestamp.CompareAndSwap(externalTimestamp, newTimestamp) {
return nil
}
}
Expand Down
5 changes: 2 additions & 3 deletions internal/unionstore/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"math"
"reflect"
"sync"
"sync/atomic"
"unsafe"

tikverr "github.com/tikv/client-go/v2/error"
Expand Down Expand Up @@ -872,8 +871,8 @@ func (db *MemDB) SetMemoryFootprintChangeHook(hook func(uint64)) {
innerHook := func() {
hook(db.allocator.capacity + db.vlog.capacity)
}
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&db.allocator.memChangeHook)), unsafe.Pointer(&innerHook))
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&db.vlog.memChangeHook)), unsafe.Pointer(&innerHook))
db.allocator.memChangeHook.Store(&innerHook)
db.vlog.memChangeHook.Store(&innerHook)
}

// Mem returns the current memory footprint
Expand Down
8 changes: 5 additions & 3 deletions internal/unionstore/memdb_arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

"github.com/tikv/client-go/v2/internal/logutil"
"github.com/tikv/client-go/v2/kv"
"go.uber.org/atomic"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -94,7 +95,7 @@ type memdbArena struct {
// the total size of all blocks, also the approximate memory footprint of the arena.
capacity uint64
// when it enlarges or shrinks, call this function with the current memory footprint (in bytes)
memChangeHook *func()
memChangeHook atomic.Pointer[func()]
}

func (a *memdbArena) alloc(size int, align bool) (memdbArenaAddr, []byte) {
Expand Down Expand Up @@ -132,8 +133,9 @@ func (a *memdbArena) enlarge(allocSize, blockSize int) {
}

func (a *memdbArena) onMemChange() {
if a.memChangeHook != nil {
(*a.memChangeHook)()
hook := a.memChangeHook.Load()
if hook != nil {
(*hook)()
}
}

Expand Down
2 changes: 1 addition & 1 deletion oracle/oracles/local_external_timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (l *localExternalTimestamp) setExternalTimestamp(ctx context.Context, o ora
return nil
}

if l.externalTimestamp.CAS(externalTimestamp, newTimestamp) {
if l.externalTimestamp.CompareAndSwap(externalTimestamp, newTimestamp) {
return nil
}
}
Expand Down