mirror of https://github.com/tikv/client-go.git
fix: avoid data race when setting memdb footprint hook (#621)
Signed-off-by: ekexium <eke@fastmail.com>
This commit is contained in:
parent
e9db9e6a8a
commit
92f0a82e1a
|
|
@ -39,6 +39,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
tikverr "github.com/tikv/client-go/v2/error"
|
tikverr "github.com/tikv/client-go/v2/error"
|
||||||
|
|
@ -871,8 +872,8 @@ func (db *MemDB) SetMemoryFootprintChangeHook(hook func(uint64)) {
|
||||||
innerHook := func() {
|
innerHook := func() {
|
||||||
hook(db.allocator.capacity + db.vlog.capacity)
|
hook(db.allocator.capacity + db.vlog.capacity)
|
||||||
}
|
}
|
||||||
db.allocator.memChangeHook = innerHook
|
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&db.allocator.memChangeHook)), unsafe.Pointer(&innerHook))
|
||||||
db.vlog.memChangeHook = innerHook
|
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&db.vlog.memChangeHook)), unsafe.Pointer(&innerHook))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mem returns the current memory footprint
|
// Mem returns the current memory footprint
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ type memdbArena struct {
|
||||||
// the total size of all blocks, also the approximate memory footprint of the arena.
|
// the total size of all blocks, also the approximate memory footprint of the arena.
|
||||||
capacity uint64
|
capacity uint64
|
||||||
// when it enlarges or shrinks, call this function with the current memory footprint (in bytes)
|
// when it enlarges or shrinks, call this function with the current memory footprint (in bytes)
|
||||||
memChangeHook func()
|
memChangeHook *func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *memdbArena) alloc(size int, align bool) (memdbArenaAddr, []byte) {
|
func (a *memdbArena) alloc(size int, align bool) (memdbArenaAddr, []byte) {
|
||||||
|
|
@ -133,7 +133,7 @@ func (a *memdbArena) enlarge(allocSize, blockSize int) {
|
||||||
|
|
||||||
func (a *memdbArena) onMemChange() {
|
func (a *memdbArena) onMemChange() {
|
||||||
if a.memChangeHook != nil {
|
if a.memChangeHook != nil {
|
||||||
a.memChangeHook()
|
(*a.memChangeHook)()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -257,11 +257,6 @@ func (s *KVStore) Begin(opts ...TxnOption) (txn *transaction.KVTxn, err error) {
|
||||||
opt(options)
|
opt(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if err == nil && txn != nil && options.MemoryFootprintChangeHook != nil {
|
|
||||||
txn.SetMemoryFootprintChangeHook(options.MemoryFootprintChangeHook)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if options.TxnScope == "" {
|
if options.TxnScope == "" {
|
||||||
options.TxnScope = oracle.GlobalTxnScope
|
options.TxnScope = oracle.GlobalTxnScope
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,6 @@ type SchemaAmender interface {
|
||||||
type TxnOptions struct {
|
type TxnOptions struct {
|
||||||
TxnScope string
|
TxnScope string
|
||||||
StartTS *uint64
|
StartTS *uint64
|
||||||
MemoryFootprintChangeHook func(uint64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// KVTxn contains methods to interact with a TiKV transaction.
|
// KVTxn contains methods to interact with a TiKV transaction.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue