-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Neil Shen <overvenus@gmail.com>
- Loading branch information
Showing
10 changed files
with
244 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
"github.com/pingcap/errors" | ||
pd "github.com/pingcap/pd/v4/client" | ||
) | ||
|
||
var minPDVersion *semver.Version = semver.New("4.0.0-rc.1") | ||
var minTiKVVersion *semver.Version = semver.New("4.0.0-rc.1") | ||
|
||
func removeV(v string) string { | ||
if v == "" { | ||
return v | ||
} | ||
return strings.TrimPrefix(v, "v") | ||
} | ||
|
||
// CheckClusterVersion check TiKV and PD version. | ||
func CheckClusterVersion(ctx context.Context, client pd.Client, pdHTTP string) error { | ||
stores, err := client.GetAllStores(ctx, pd.WithExcludeTombstone()) | ||
if err != nil { | ||
return err | ||
} | ||
for _, s := range stores { | ||
ver, err := semver.NewVersion(removeV(s.Version)) | ||
if err != nil { | ||
return err | ||
} | ||
ord := ver.Compare(*minTiKVVersion) | ||
if ord < 0 { | ||
return errors.NotSupportedf("TiKV %s is not supported, require minimal version %s", | ||
removeV(s.Version), minTiKVVersion) | ||
} | ||
} | ||
// See more: https://github.com/pingcap/pd/blob/v4.0.0-rc.1/server/api/version.go | ||
pdVer := struct { | ||
Version string `json:"version"` | ||
}{} | ||
req, err := http.NewRequestWithContext( | ||
ctx, http.MethodGet, fmt.Sprintf("%s/pd/api/v1/version", pdHTTP), nil) | ||
if err != nil { | ||
return errors.Annotate(err, "fail to request PD") | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return errors.Annotate(err, "fail to request PD") | ||
} | ||
if resp.StatusCode < 200 && resp.StatusCode >= 300 { | ||
return errors.BadRequestf("fail to requet PD %s", resp.Status) | ||
} | ||
content, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return errors.Annotate(err, "fail to request PD") | ||
} | ||
err = json.Unmarshal(content, &pdVer) | ||
if err != nil { | ||
return errors.Annotate(err, "fail to request PD") | ||
} | ||
err = resp.Body.Close() | ||
if err != nil { | ||
return errors.Annotate(err, "fail to request PD") | ||
} | ||
ver, err := semver.NewVersion(removeV(pdVer.Version)) | ||
if err != nil { | ||
return err | ||
} | ||
ord := ver.Compare(*minPDVersion) | ||
if ord < 0 { | ||
return errors.NotSupportedf("PD %s is not supported, require minimal version %s", | ||
removeV(pdVer.Version), minPDVersion) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
"github.com/pingcap/check" | ||
"github.com/pingcap/kvproto/pkg/metapb" | ||
pd "github.com/pingcap/pd/v4/client" | ||
"github.com/pingcap/pd/v4/pkg/tempurl" | ||
) | ||
|
||
type checkSuite struct{} | ||
|
||
var _ = check.Suite(&checkSuite{}) | ||
|
||
type mockPDClient struct { | ||
pd.Client | ||
getAllStores func() []*metapb.Store | ||
getVersion func() string | ||
} | ||
|
||
func (m *mockPDClient) GetAllStores(ctx context.Context, opts ...pd.GetStoreOption) ([]*metapb.Store, error) { | ||
if m.getAllStores != nil { | ||
return m.getAllStores(), nil | ||
} | ||
return []*metapb.Store{}, nil | ||
} | ||
|
||
func (m *mockPDClient) ServeHTTP(resp http.ResponseWriter, _ *http.Request) { | ||
if m.getVersion != nil { | ||
_, _ = resp.Write([]byte(fmt.Sprintf(`{"version":"%s"}`, m.getVersion()))) | ||
} | ||
} | ||
|
||
func (s *checkSuite) TestCheckClusterVersion(c *check.C) { | ||
mock := mockPDClient{ | ||
Client: nil, | ||
} | ||
pdURL, _ := url.Parse(tempurl.Alloc()) | ||
pdHTTP := fmt.Sprintf("http://%s", pdURL.Host) | ||
svr := http.Server{Addr: pdURL.Host, Handler: &mock} | ||
go func() { | ||
c.Assert(svr.ListenAndServe(), check.IsNil) | ||
}() | ||
defer svr.Close() | ||
for i := 0; i < 20; i++ { | ||
time.Sleep(100 * time.Millisecond) | ||
_, err := http.Get(pdHTTP) | ||
if err == nil { | ||
break | ||
} | ||
c.Error(err) | ||
if i == 199 { | ||
c.Fatal("http server timeout", err) | ||
} | ||
} | ||
|
||
{ | ||
mock.getVersion = func() string { | ||
return minPDVersion.String() | ||
} | ||
mock.getAllStores = func() []*metapb.Store { | ||
return []*metapb.Store{{Version: minTiKVVersion.String()}} | ||
} | ||
err := CheckClusterVersion(context.Background(), &mock, pdHTTP) | ||
c.Assert(err, check.IsNil) | ||
} | ||
|
||
{ | ||
mock.getVersion = func() string { | ||
return `v1.0.0-alpha-271-g824ae7fd` | ||
} | ||
mock.getAllStores = func() []*metapb.Store { | ||
return []*metapb.Store{{Version: minTiKVVersion.String()}} | ||
} | ||
err := CheckClusterVersion(context.Background(), &mock, pdHTTP) | ||
c.Assert(err, check.ErrorMatches, "PD .* is not supported.*") | ||
} | ||
|
||
{ | ||
mock.getVersion = func() string { | ||
return minPDVersion.String() | ||
} | ||
mock.getAllStores = func() []*metapb.Store { | ||
// TiKV does not include 'v'. | ||
return []*metapb.Store{{Version: `1.0.0-alpha-271-g824ae7fd`}} | ||
} | ||
err := CheckClusterVersion(context.Background(), &mock, pdHTTP) | ||
c.Assert(err, check.ErrorMatches, "TiKV .* is not supported.*") | ||
} | ||
} | ||
|
||
func (s *checkSuite) TestCompareVersion(c *check.C) { | ||
c.Assert(semver.New("4.0.0-rc").Compare(*semver.New("4.0.0-rc.2")), check.Equals, -1) | ||
c.Assert(semver.New("4.0.0-rc.1").Compare(*semver.New("4.0.0-rc.2")), check.Equals, -1) | ||
// BUG it should be "<" instead of ">". | ||
// c.Assert(semver.New("4.0.0-rc-35-g31dae220").Compare(*semver.New("4.0.0-rc.2")), check.Equals, -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters