Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
pd: fix parse version (#620) (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Nov 27, 2020
1 parent dc034a6 commit d311228
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
35 changes: 22 additions & 13 deletions pkg/pdutil/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,7 @@ func NewPdController(
return nil, errors.Annotatef(berrors.ErrPDUpdateFailed, "pd address (%s) not available, please check network", pdAddrs)
}

version, err := semver.NewVersion(string(versionBytes))
if err != nil {
log.Warn("fail back to v0.0.0 version",
zap.ByteString("version", versionBytes), zap.Error(err))
version = &semver.Version{Major: 0, Minor: 0, Patch: 0}
}
failpoint.Inject("PDEnabledPauseConfig", func(val failpoint.Value) {
if val.(bool) {
// test pause config is enable
version = &semver.Version{Major: 5, Minor: 0, Patch: 0}
}
})
version := parseVersion(versionBytes)
maxCallMsgSize := []grpc.DialOption{
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(maxMsgSize)),
Expand All @@ -224,6 +213,26 @@ func NewPdController(
}, nil
}

func parseVersion(versionBytes []byte) *semver.Version {
// we need trim space or semver will parse failed
v := strings.TrimSpace(string(versionBytes))
v = strings.Trim(v, "\"")
v = strings.TrimPrefix(v, "v")
version, err := semver.NewVersion(v)
if err != nil {
log.Warn("fail back to v0.0.0 version",
zap.ByteString("version", versionBytes), zap.Error(err))
version = &semver.Version{Major: 0, Minor: 0, Patch: 0}
}
failpoint.Inject("PDEnabledPauseConfig", func(val failpoint.Value) {
if val.(bool) {
// test pause config is enable
version = &semver.Version{Major: 5, Minor: 0, Patch: 0}
}
})
return version
}

func (p *PdController) isPauseConfigEnabled() bool {
return p.version.Compare(pauseConfigVersion) >= 0
}
Expand Down Expand Up @@ -504,7 +513,7 @@ func restoreSchedulers(ctx context.Context, pd *PdController, clusterCfg cluster
prefix := make([]string, 0, 1)
if pd.isPauseConfigEnabled() {
// set config's ttl to zero, make temporary config invalid immediately.
prefix = append(prefix, fmt.Sprintf("%s?ttlSecond=%d", schedulerPrefix, 0))
prefix = append(prefix, fmt.Sprintf("%s?ttlSecond=%d", scheduleConfigPrefix, 0))
}
// reset config with previous value.
if err := pd.doUpdatePDScheduleConfig(ctx, mergeCfg, pdRequest, prefix...); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/pdutil/pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"testing"

"github.com/coreos/go-semver/semver"
. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb/util/codec"
Expand Down Expand Up @@ -157,3 +158,12 @@ func (s *testPDControllerSuite) TestRegionCount(c *C) {
c.Assert(err, IsNil)
c.Assert(resp, Equals, 2)
}

func (s *testPDControllerSuite) TestPDVersion(c *C) {
v := []byte("\"v4.1.0-alpha1\"\n")
r := parseVersion(v)
expectV := semver.New("4.1.0-alpha1")
c.Assert(r.Major, Equals, expectV.Major)
c.Assert(r.Minor, Equals, expectV.Minor)
c.Assert(r.PreRelease, Equals, expectV.PreRelease)
}

0 comments on commit d311228

Please sign in to comment.