Skip to content

Commit

Permalink
playground: add support of ng-monitoring (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar authored Dec 6, 2021
1 parent e7f9871 commit 592b46c
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 37 deletions.
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
}
120 changes: 83 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 @@ -980,6 +966,10 @@ func (p *Playground) terminate(sig syscall.Signal) {
kill(p.monitor.cmd.Process.Pid, p.monitor.wait)
}

if p.ngmonitoring != nil {
kill(p.ngmonitoring.cmd.Process.Pid, p.ngmonitoring.wait)
}

if p.grafana != nil {
kill(p.grafana.cmd.Process.Pid, p.grafana.wait)
}
Expand Down Expand Up @@ -1040,9 +1030,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 +1135,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

0 comments on commit 592b46c

Please sign in to comment.