Skip to content

Commit

Permalink
add gc options (#828)
Browse files Browse the repository at this point in the history
Signed-off-by: weedge <[email protected]>
Co-authored-by: disksing <[email protected]>
  • Loading branch information
weedge and disksing authored Jul 14, 2023
1 parent 4ec212d commit 51aab26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
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

0 comments on commit 51aab26

Please sign in to comment.