Skip to content

Commit

Permalink
implement reboot_pd case
Browse files Browse the repository at this point in the history
Signed-off-by: okJiang <[email protected]>
  • Loading branch information
okJiang committed Oct 21, 2024
1 parent e01a7dd commit cd64c93
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 77 deletions.
56 changes: 25 additions & 31 deletions tests/integrations/realcluster/real_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -67,7 +68,9 @@ func (s *realClusterSuite) TearDownSuite() {
func (s *realClusterSuite) startRealCluster(t *testing.T) {
log.Info("start to deploy a real cluster")

s.deploy(t)
tag := s.tag()
deployTiupPlayground(t, tag)
waitTiupReady(t, tag)
s.clusterCnt++
}

Expand All @@ -83,33 +86,26 @@ func (s *realClusterSuite) tag() string {
return fmt.Sprintf("pd_real_cluster_test_%s_%d", s.suiteName, s.clusterCnt)
}

// func restartTiUP() {
// log.Info("start to restart TiUP")
// cmd := exec.Command("make", "deploy")
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
// err := cmd.Run()
// if err != nil {
// panic(err)
// }
// log.Info("TiUP restart success")
// }

func (s *realClusterSuite) deploy(t *testing.T) {
func (s *realClusterSuite) restart() {
tag := s.tag()
deployTiupPlayground(t, tag)
waitTiupReady(t, tag)
log.Info("start to restart", zap.String("tag", tag))
s.stopRealCluster(s.T())
s.startRealCluster(s.T())
log.Info("TiUP restart success")
}

func destroy(t *testing.T, tag string) {
cmdStr := fmt.Sprintf("ps -ef | grep 'tiup playground' | grep %s | awk '{print $2}' | head -n 1", tag)
cmdStr := fmt.Sprintf("ps -ef | grep %s | awk '{print $2}'", tag)
cmd := exec.Command("sh", "-c", cmdStr)
bytes, err := cmd.Output()
require.NoError(t, err)
pid := string(bytes)
// nolint:errcheck
runCommand("sh", "-c", "kill -9 "+pid)
log.Info("destroy success", zap.String("pid", pid))
pids := string(bytes)
pidArr := strings.Split(pids, "\n")
for _, pid := range pidArr {
// nolint:errcheck
runCommand("sh", "-c", "kill -9 "+pid)
}
log.Info("destroy success", zap.String("tag", tag))
}

func deployTiupPlayground(t *testing.T, tag string) {
Expand All @@ -133,16 +129,14 @@ func deployTiupPlayground(t *testing.T, tag string) {
require.NoError(t, os.Mkdir(filepath.Join(curPath, "playground"), 0755))
}
// nolint:errcheck
go runCommand("sh", "-c",
tiupBin+` playground nightly --kv 3 --tiflash 1 --db 1 --pd 3 \
--without-monitor --tag `+tag+` --pd.binpath ./bin/pd-server \
// --kv.binpath ./third_bin/tikv-server \
// --db.binpath ./third_bin/tidb-server --tiflash.binpath ./third_bin/tiflash \
--kv.binpath ./bin/tikv-server \
--db.binpath ./bin/tidb-server --tiflash.binpath ./bin/tiflash \
--pd.config ./tests/integrations/realcluster/pd.toml \
> `+filepath.Join(curPath, "playground", tag+".log")+` 2>&1 & `)

go func() {
runCommand("sh", "-c",
tiupBin+` playground nightly --kv 3 --tiflash 1 --db 1 --pd 3 \
--without-monitor --tag `+tag+` --pd.binpath ./bin/pd-server \
--kv.binpath ./third_bin/tikv-server \
--db.binpath ./third_bin/tidb-server --tiflash.binpath ./third_bin/tiflash \
> `+filepath.Join(curPath, "playground", tag+".log")+` 2>&1 & `)
}()
// Avoid to change the dir before execute `tiup playground`.
time.Sleep(10 * time.Second)
require.NoError(t, os.Chdir(curPath))
Expand Down
105 changes: 64 additions & 41 deletions tests/integrations/realcluster/reboot_pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,68 @@

package realcluster

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/client/http"
)

type rebootPDSuite struct {
realClusterSuite
}

func TestRebootPD(t *testing.T) {
suite.Run(t, &rebootPDSuite{
realClusterSuite: realClusterSuite{
suiteName: "reboot_pd",
},
})
}

// https://github.com/tikv/pd/issues/6467
// func TestReloadLabel(t *testing.T) {
// re := require.New(t)
// ctx := context.Background()

// resp, err := pdHTTPCli.GetStores(ctx)
// re.NoError(err)
// re.NotEmpty(resp.Stores)
// firstStore := resp.Stores[0]
// // TiFlash labels will be ["engine": "tiflash"]
// // So we need to merge the labels
// storeLabels := map[string]string{
// "zone": "zone1",
// }
// for _, label := range firstStore.Store.Labels {
// storeLabels[label.Key] = label.Value
// }
// re.NoError(pdHTTPCli.SetStoreLabels(ctx, firstStore.Store.ID, storeLabels))
// defer func() {
// re.NoError(pdHTTPCli.DeleteStoreLabel(ctx, firstStore.Store.ID, "zone"))
// }()

// checkLabelsAreEqual := func() {
// resp, err := pdHTTPCli.GetStore(ctx, uint64(firstStore.Store.ID))
// re.NoError(err)

// labelsMap := make(map[string]string)
// for _, label := range resp.Store.Labels {
// re.NotNil(label)
// labelsMap[label.Key] = label.Value
// }

// for key, value := range storeLabels {
// re.Equal(value, labelsMap[key])
// }
// }
// // Check the label is set
// checkLabelsAreEqual()
// // Restart TiUP to reload the label
// restartTiUP()
// checkLabelsAreEqual()
// }
func (s *rebootPDSuite) TestReloadLabel() {
re := require.New(s.T())
ctx := context.Background()

pdHTTPCli := http.NewClient("pd-real-cluster-test", getPDEndpoints(s.T()))
resp, err := pdHTTPCli.GetStores(ctx)
re.NoError(err)
re.NotEmpty(resp.Stores)
firstStore := resp.Stores[0]
// TiFlash labels will be ["engine": "tiflash"]
// So we need to merge the labels
storeLabels := map[string]string{
"zone": "zone1",
}
for _, label := range firstStore.Store.Labels {
storeLabels[label.Key] = label.Value
}
re.NoError(pdHTTPCli.SetStoreLabels(ctx, firstStore.Store.ID, storeLabels))
defer func() {
re.NoError(pdHTTPCli.DeleteStoreLabel(ctx, firstStore.Store.ID, "zone"))
}()

checkLabelsAreEqual := func() {
resp, err := pdHTTPCli.GetStore(ctx, uint64(firstStore.Store.ID))
re.NoError(err)

labelsMap := make(map[string]string)
for _, label := range resp.Store.Labels {
re.NotNil(label)
labelsMap[label.Key] = label.Value
}

for key, value := range storeLabels {
re.Equal(value, labelsMap[key])
}
}
// Check the label is set
checkLabelsAreEqual()
// Restart to reload the label
s.restart()
pdHTTPCli = http.NewClient("pd-real-cluster-test", getPDEndpoints(s.T()))
checkLabelsAreEqual()
}
5 changes: 0 additions & 5 deletions tests/integrations/realcluster/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (

const physicalShiftBits = 18

var (
// pdAddrs = []string{"http://127.0.0.1:2379"}
// pdHTTPCli = http.NewClient("pd-real-cluster-test", pdAddrs)
)

// GetTimeFromTS extracts time.Time from a timestamp.
func GetTimeFromTS(ts uint64) time.Time {
ms := ExtractPhysical(ts)
Expand Down

0 comments on commit cd64c93

Please sign in to comment.