Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

checksum: fix pd service id is empty #460

Merged
merged 2 commits into from
Nov 10, 2020
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
9 changes: 9 additions & 0 deletions lightning/restore/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync/atomic"
"time"

"github.com/google/uuid"

"github.com/pingcap/br/pkg/checksum"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
Expand Down Expand Up @@ -324,6 +326,13 @@ type gcTTLManager struct {
started uint32
}

func newGCTTLManager(pdClient pd.Client) gcTTLManager {
return gcTTLManager{
pdClient: pdClient,
serviceID: fmt.Sprintf("lightning-%s", uuid.New()),
}
}

func (m *gcTTLManager) addOneJob(ctx context.Context, table string, ts uint64) error {
// start gc ttl loop if not started yet.
if atomic.CompareAndSwapUint32(&m.started, 0, 1) {
Expand Down
20 changes: 17 additions & 3 deletions lightning/restore/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (s *checksumSuite) TestDoChecksumWithTikv(c *C) {
pdClient := &testPDClient{}
resp := tipb.ChecksumResponse{Checksum: 123, TotalKvs: 10, TotalBytes: 1000}
kvClient := &mockChecksumKVClient{checksum: resp, respDur: time.Second * 5}
checksumExec := &tikvChecksumManager{manager: gcTTLManager{pdClient: pdClient}, client: kvClient}
checksumExec := &tikvChecksumManager{manager: newGCTTLManager(pdClient), client: kvClient}

// mock a table info
p := parser.New()
Expand Down Expand Up @@ -229,6 +229,9 @@ func (c *testPDClient) currentSafePoint() uint64 {
}

func (c *testPDClient) UpdateServiceGCSafePoint(ctx context.Context, serviceID string, ttl int64, safePoint uint64) (uint64, error) {
if serviceID == "" {
panic("service ID must not be empty")
}
atomic.AddInt32(&c.count, 1)
c.Lock()
idx := sort.Search(len(c.gcSafePoint), func(i int) bool {
Expand All @@ -253,7 +256,8 @@ func (c *testPDClient) UpdateServiceGCSafePoint(ctx context.Context, serviceID s

func (s *checksumSuite) TestGcTTLManagerSingle(c *C) {
pdClient := &testPDClient{}
manager := gcTTLManager{pdClient: pdClient}
manager := newGCTTLManager(pdClient)
c.Assert(manager.serviceID != "", IsTrue)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
oldTTL := serviceSafePointTTL
Expand All @@ -280,7 +284,7 @@ func (s *checksumSuite) TestGcTTLManagerSingle(c *C) {
}

func (s *checksumSuite) TestGcTTLManagerMulti(c *C) {
manager := gcTTLManager{pdClient: &testPDClient{}}
manager := newGCTTLManager(&testPDClient{})
ctx := context.Background()

for i := uint64(1); i <= 5; i++ {
Expand All @@ -305,6 +309,16 @@ func (s *checksumSuite) TestGcTTLManagerMulti(c *C) {
c.Assert(manager.currentTs, Equals, uint64(0))
}

func (s *checksumSuite) TestPdServiceID(c *C) {
pdCli := &testPDClient{}
gcTTLManager1 := newGCTTLManager(pdCli)
c.Assert(gcTTLManager1.serviceID != "", IsTrue)
gcTTLManager2 := newGCTTLManager(pdCli)
c.Assert(gcTTLManager2.serviceID != "", IsTrue)

c.Assert(gcTTLManager1.serviceID != gcTTLManager2.serviceID, IsTrue)
}

type mockResponse struct {
finished bool
data []byte
Expand Down