Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playground: add support of ng-monitoring #1648

Merged
merged 4 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
99 changes: 99 additions & 0 deletions components/playground/ngmonitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2021 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 main

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/components/playground/instance"
"github.com/pingcap/tiup/pkg/environment"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

type ngMonitoring struct {
host string
port int
cmd *exec.Cmd

waitErr error
waitOnce sync.Once
}

func (m *ngMonitoring) wait() error {
m.waitOnce.Do(func() {
m.waitErr = m.cmd.Wait()
})

return m.waitErr
}

// the cmd is not started after return
func newNGMonitoring(ctx context.Context, version string, host, dir string, pds []*instance.PDInstance) (*ngMonitoring, error) {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, errors.AddStack(err)
}

port, err := utils.GetFreePort(host, 12020)
if err != nil {
return nil, err
}

m := new(ngMonitoring)
var endpoints []string
for _, pd := range pds {
endpoints = append(endpoints, fmt.Sprintf("%s:%d", pd.Host, pd.StatusPort))
}
args := []string{
fmt.Sprintf("--pd.endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--address=%s:%d", host, port),
fmt.Sprintf("--advertise-address=%s:%d", host, port),
fmt.Sprintf("--storage.path=%s", filepath.Join(dir, "data")),
fmt.Sprintf("--log.path=%s", filepath.Join(dir, "logs")),
}

env := environment.GlobalEnv()
binDir, err := env.Profile().ComponentInstalledPath("prometheus", utils.Version(version))
if err != nil {
return nil, err
}
params := &tiupexec.PrepareCommandParams{
Ctx: ctx,
Component: "prometheus",
BinPath: filepath.Join(binDir, "ng-monitoring-server"),
Version: utils.Version(version),
InstanceDir: dir,
WD: dir,
Args: args,
SysProcAttr: instance.SysProcAttr,
Env: env,
}
cmd, err := tiupexec.PrepareCommand(params)
if err != nil {
return nil, err
}

m.port = port
m.cmd = cmd
m.host = host

return m, nil
}
116 changes: 79 additions & 37 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ type Playground struct {
// not nil iff we start the exec.Cmd successfully.
// we should and can safely call wait() to make sure the process quit
// before playground quit.
monitor *monitor
grafana *grafana
monitor *monitor
ngmonitoring *ngMonitoring
grafana *grafana
}

// MonitorInfo represent the monitor
Expand Down Expand Up @@ -800,41 +801,6 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme

fmt.Println("Playground Bootstrapping...")

var monitorInfo *MonitorInfo
if options.Monitor {
var err error

p.monitor, monitorInfo, err = p.bootMonitor(ctx, env)
if err != nil {
return err
}

p.instanceWaiter.Go(func() error {
err := p.monitor.wait()
if err != nil && atomic.LoadInt32(&p.curSig) == 0 {
fmt.Printf("Prometheus quit: %v\n", err)
} else {
fmt.Println("prometheus quit")
}
return err
})

p.grafana, err = p.bootGrafana(ctx, env, monitorInfo)
if err != nil {
return err
}

p.instanceWaiter.Go(func() error {
err := p.grafana.wait()
if err != nil && atomic.LoadInt32(&p.curSig) == 0 {
fmt.Printf("Grafana quit: %v\n", err)
} else {
fmt.Println("Grafana quit")
}
return err
})
}

anyPumpReady := false
// Start all instance except tiflash.
err := p.WalkInstances(func(cid string, ins instance.Instance) error {
Expand Down Expand Up @@ -868,6 +834,26 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme

succ := p.waitAllTidbUp()

var monitorInfo *MonitorInfo
if options.Monitor {
var err error

p.monitor, monitorInfo, err = p.bootMonitor(ctx, env)
if err != nil {
return err
}

p.ngmonitoring, err = p.bootNGMonitoring(ctx, env)
if err != nil {
return err
}

p.grafana, err = p.bootGrafana(ctx, env, monitorInfo)
if err != nil {
return err
}
}

if len(succ) > 0 {
// start TiFlash after at least one TiDB is up.
var started []*instance.TiFlashInstance
Expand Down Expand Up @@ -1040,9 +1026,55 @@ func (p *Playground) bootMonitor(ctx context.Context, env *environment.Environme
return nil, nil, err
}

p.instanceWaiter.Go(func() error {
err := p.monitor.wait()
if err != nil && atomic.LoadInt32(&p.curSig) == 0 {
fmt.Printf("Prometheus quit: %v\n", err)
} else {
fmt.Println("prometheus quit")
}
return err
})

return monitor, monitorInfo, nil
}

// return not error iff the Cmd is started successfully.
// user must and can safely wait the Cmd
func (p *Playground) bootNGMonitoring(ctx context.Context, env *environment.Environment) (*ngMonitoring, error) {
options := p.bootOptions

dataDir := p.dataDir
promDir := filepath.Join(dataDir, "prometheus")

ngm, err := newNGMonitoring(ctx, options.Version, options.Host, promDir, p.pds)
if err != nil {
return nil, err
}

// ng-monitoring only exists when tidb >= 5.3.0
_, err = os.Stat(ngm.cmd.Path)
if os.IsNotExist(err) {
return nil, nil
}

if err := ngm.cmd.Start(); err != nil {
return nil, err
}

p.instanceWaiter.Go(func() error {
err := p.ngmonitoring.wait()
if err != nil && atomic.LoadInt32(&p.curSig) == 0 {
fmt.Printf("ng-monitoring quit: %v\n", err)
} else {
fmt.Println("ng-monitoring quit")
}
return err
})

return ngm, nil
}

// return not error iff the Cmd is started successfully.
func (p *Playground) bootGrafana(ctx context.Context, env *environment.Environment, monitorInfo *MonitorInfo) (*grafana, error) {
// set up grafana
Expand Down Expand Up @@ -1099,6 +1131,16 @@ func (p *Playground) bootGrafana(ctx context.Context, env *environment.Environme
return nil, err
}

p.instanceWaiter.Go(func() error {
err := p.grafana.wait()
if err != nil && atomic.LoadInt32(&p.curSig) == 0 {
fmt.Printf("Grafana quit: %v\n", err)
} else {
fmt.Println("Grafana quit")
}
return err
})

return grafana, nil
}

Expand Down