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

add gc options #828

Merged
merged 7 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion examples/gcworker/gcworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/txnkv"
)

Expand All @@ -36,10 +37,15 @@ func main() {
panic(err)
}

sysSafepoint, err := client.GC(context.Background(), *safepoint)

sysSafepoint, err := client.GC(context.Background(), *safepoint, tikv.WithConcurrency(10))
if err != nil {
panic(err)
}
fmt.Printf("Finished GC, expect safepoint:%d(%+v),real safepoint:%d(+%v)\n", *safepoint, oracle.GetTimeFromTS(*safepoint), sysSafepoint, oracle.GetTimeFromTS(sysSafepoint))

err = client.Close()
if err != nil {
panic(err)
}
}
25 changes: 23 additions & 2 deletions tikv/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,36 @@ import (
//
// GC is a simplified version of [GC in TiDB](https://docs.pingcap.com/tidb/stable/garbage-collection-overview).
// We skip the second step "delete ranges" which is an optimization for TiDB.
func (s *KVStore) GC(ctx context.Context, safepoint uint64) (newSafePoint uint64, err error) {
err = s.resolveLocks(ctx, safepoint, 8)
func (s *KVStore) GC(ctx context.Context, safepoint uint64, opts ...GCOpt) (newSafePoint uint64, err error) {
// default concurrency 8
opt := &gcOption{concurrency: 8}
// Apply gc options.
for _, o := range opts {
o(opt)
}

err = s.resolveLocks(ctx, safepoint, opt.concurrency)
if err != nil {
return
}

return s.pdClient.UpdateGCSafePoint(ctx, safepoint)
}

type gcOption struct {
concurrency int
}

// GCOpt gc options
type GCOpt func(*gcOption)

// WithConcurrency is used to set gc RangeTaskRunner concurrency.
func WithConcurrency(concurrency int) GCOpt {
return func(opt *gcOption) {
opt.concurrency = concurrency
}
}

func (s *KVStore) resolveLocks(ctx context.Context, safePoint uint64, concurrency int) error {
handler := func(ctx context.Context, r kv.KeyRange) (rangetask.TaskStat, error) {
return s.resolveLocksForRange(ctx, safePoint, r.StartKey, r.EndKey)
Expand Down
2 changes: 1 addition & 1 deletion txnkv/transaction/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (txn *KVTxn) GetVars() *tikv.Variables {
func (txn *KVTxn) Get(ctx context.Context, k []byte) ([]byte, error) {
ret, err := txn.us.Get(ctx, k)
if tikverr.IsErrNotFound(err) {
return nil, err
return nil, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

em, why change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in rawkv.go https://github.com/tikv/client-go/blob/master/rawkv/rawkv.go#L287 get not found key return nil, nil;
so want txnkv return nil, nil when get not found key

Copy link
Contributor Author

@weedge weedge Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR don't care get not found key case ; so change back 85c6599

}
if err != nil {
return nil, err
Expand Down