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

implement external timestamp for mock pd #611

Merged
merged 2 commits into from
Oct 31, 2022
Merged
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
32 changes: 32 additions & 0 deletions internal/mockstore/mocktikv/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pkg/errors"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
"go.uber.org/atomic"
)

// Use global variables to prevent pdClients from creating duplicate timestamps.
Expand All @@ -63,6 +65,8 @@ type pdClient struct {
// in GC.
serviceSafePoints map[string]uint64
gcSafePointMu sync.Mutex

externalTimestamp atomic.Uint64
}

// NewPDClient creates a mock pd.Client that uses local timestamp and meta data
Expand Down Expand Up @@ -116,6 +120,34 @@ func (c *pdClient) GetLocalTSAsync(ctx context.Context, dcLocation string) pd.TS
return c.GetTSAsync(ctx)
}

func (c *pdClient) SetExternalTimestamp(ctx context.Context, newTimestamp uint64) error {
p, l, err := c.GetTS(ctx)
if err != nil {
return err
}

currentTSO := oracle.ComposeTS(p, l)
if newTimestamp > currentTSO {
return errors.New("external timestamp is greater than global tso")
}
for {
externalTimestamp := c.externalTimestamp.Load()
if externalTimestamp > newTimestamp {
return errors.New("cannot decrease the external timestamp")
} else if externalTimestamp == newTimestamp {
return nil
}

if c.externalTimestamp.CAS(externalTimestamp, newTimestamp) {
return nil
}
}
}

func (c *pdClient) GetExternalTimestamp(ctx context.Context) (uint64, error) {
return c.externalTimestamp.Load(), nil
}

type mockTSFuture struct {
pdc *pdClient
ctx context.Context
Expand Down