Skip to content
Open
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ package instance

import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/tidbver"
Expand Down Expand Up @@ -236,3 +239,40 @@ func (inst *PDInstance) LogFile() string {
func (inst *PDInstance) Addr() string {
return utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)
}

// Ready returns nil when PD is ready to serve.
func (inst *PDInstance) Ready(ctx context.Context) error {
url := fmt.Sprintf("http://%s/pd/api/v1/members", inst.Addr())

var r struct {
Header struct {
ClusterID uint64 `json:"cluster_id"`
} `json:"header"`
}

ready := func() bool {
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
err = json.NewDecoder(resp.Body).Decode(&r)
return err == nil && r.Header.ClusterID != 0
}
return false
}

for {
if ready() {
return nil
}

select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
// retry
}
}
}
14 changes: 14 additions & 0 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme

anyPumpReady := false
allDMMasterReady := false
allPDReady := false
// Start all instance except tiflash.
err := p.WalkInstances(func(cid string, ins instance.Instance) error {
if cid == spec.ComponentTiFlash {
Expand All @@ -1296,6 +1297,19 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme
allDMMasterReady = true
}

// wait all PD ready before starting TiDB to avoid cluster ID mismatch
if cid == spec.ComponentTiDB && !allPDReady {
for _, pd := range p.pds {
pdCtx, cancel := context.WithTimeout(ctx, time.Second*120)
err := pd.Ready(pdCtx)
cancel()
if err != nil {
return errors.Annotatef(err, "failed to wait PD %s to be ready", pd.Addr())
}
}
allPDReady = true
}

err := p.startInstance(ctx, ins)
if err != nil {
return err
Expand Down
Loading