Skip to content

Commit

Permalink
add a switch in cfg to enable the read ts validator
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <[email protected]>
  • Loading branch information
ekexium committed Feb 25, 2025
1 parent ddec823 commit 3727527
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ type Config struct {
// RegionsRefreshInterval indicates the interval of loading regions info, the unit is second, if RegionsRefreshInterval == 0, it will be disabled.
RegionsRefreshInterval uint64
// EnablePreload indicates whether to preload region info when initializing the client.
EnablePreload bool
EnablePreload bool
EnableReadTSValidator bool
}

// DefaultConfig returns the default configuration.
Expand All @@ -99,6 +100,7 @@ func DefaultConfig() Config {
TxnScope: "",
EnableAsyncCommit: false,
Enable1PC: false,
EnableReadTSValidator: true,
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"sync/atomic"
"time"

"github.com/tikv/client-go/v2/config"

"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -402,6 +404,9 @@ func (s *ReplicaAccessStats) String() string {

// NewRegionRequestSender creates a new sender.
func NewRegionRequestSender(regionCache *RegionCache, client client.Client, readTSValidator oracle.ReadTSValidator) *RegionRequestSender {
if !config.GetGlobalConfig().EnableReadTSValidator {
readTSValidator = oracle.NoopReadTSValidator{}
}
return &RegionRequestSender{
regionCache: regionCache,
apiVersion: regionCache.codec.GetAPIVersion(),
Expand Down
6 changes: 6 additions & 0 deletions oracle/oracles/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/tikv/client-go/v2/internal/logutil"
"github.com/tikv/client-go/v2/metrics"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/util"
pd "github.com/tikv/pd/client"
"github.com/tikv/pd/client/clients/tso"
"go.uber.org/zap"
Expand Down Expand Up @@ -661,6 +662,10 @@ func (o *pdOracle) getCurrentTSForValidation(ctx context.Context, opt *oracle.Op
}

func (o *pdOracle) ValidateReadTS(ctx context.Context, readTS uint64, isStaleRead bool, opt *oracle.Option) (errRet error) {
if val, err := util.EvalFailpoint("failOnValidateReadTS"); err == nil {
return errors.New(val.(string))
}

if readTS == math.MaxUint64 {
if isStaleRead {
return oracle.ErrLatestStaleRead{}
Expand Down Expand Up @@ -695,6 +700,7 @@ func (o *pdOracle) ValidateReadTS(ctx context.Context, readTS uint64, isStaleRea
o.adjustUpdateLowResolutionTSIntervalWithRequestedStaleness(readTS, estimatedCurrentTS, time.Now())
}
}

return nil
}

Expand Down

0 comments on commit 3727527

Please sign in to comment.