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: exit faster #1893

Merged
merged 3 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 8 additions & 7 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var (
playgroundReport *telemetry.PlaygroundReport
options = &BootOptions{}
tag string
deleteWhenExit bool
tiupDataDir string
dataDir string
log = logprinter.NewLogger("")
Expand Down Expand Up @@ -167,16 +168,17 @@ Examples:
switch {
case tag != "":
dataDir = filepath.Join(tiupHome, localdata.DataParentDir, tag)
err := os.MkdirAll(dataDir, os.ModePerm)
if err != nil {
return err
}
case tiupDataDir != "":
dataDir = tiupDataDir
tag = dataDir[strings.LastIndex(dataDir, "/")+1:]
default:
tag = utils.Base62Tag()
dataDir = filepath.Join(tiupHome, localdata.DataParentDir, tag)
deleteWhenExit = true
}
err := os.MkdirAll(dataDir, os.ModePerm)
if err != nil {
return err
}
fmt.Printf("\033]0;TiUP Playground: %s\a", tag)
return nil
Expand Down Expand Up @@ -257,7 +259,6 @@ Examples:
sig = (<-sc).(syscall.Signal)
atomic.StoreInt32(&p.curSig, int32(syscall.SIGKILL))
if sig == syscall.SIGINT {
removeData()
p.terminate(syscall.SIGKILL)
}
}()
Expand Down Expand Up @@ -630,6 +631,7 @@ func main() {
fmt.Println(color.RedString("Error: %v", err))
code = 1
}
removeData()

if reportEnabled {
f := func() {
Expand Down Expand Up @@ -681,8 +683,7 @@ func main() {
}

func removeData() {
// remove if not set tag when run at standalone mode
if tiupDataDir == "" && tag == "" {
if deleteWhenExit {
os.RemoveAll(dataDir)
}
}
48 changes: 31 additions & 17 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -959,11 +959,14 @@ func (p *Playground) wait() error {
}

func (p *Playground) terminate(sig syscall.Signal) {
kill := func(pid int, wait func() error) {
if sig != syscall.SIGINT {
_ = syscall.Kill(pid, sig)
kill := func(name string, pid int, wait func() error) {
if sig == syscall.SIGKILL {
fmt.Printf("Force %s(%d) to quit...\n", name, pid)
} else if atomic.LoadInt32(&p.curSig) == int32(sig) { // In case of double ctr+c
fmt.Printf("Wait %s(%d) to quit...\n", name, pid)
}

_ = syscall.Kill(pid, sig)
timer := time.AfterFunc(forceKillAfterDuration, func() {
_ = syscall.Kill(pid, syscall.SIGKILL)
})
Expand All @@ -972,27 +975,38 @@ func (p *Playground) terminate(sig syscall.Signal) {
timer.Stop()
}

for i := len(p.startedInstances); i > 0; i-- {
inst := p.startedInstances[i-1]
if sig == syscall.SIGKILL {
fmt.Printf("Force %s(%d) to quit...\n", inst.Component(), inst.Pid())
} else if atomic.LoadInt32(&p.curSig) == int32(sig) { // In case of double ctr+c
fmt.Printf("Wait %s(%d) to quit...\n", inst.Component(), inst.Pid())
}

kill(inst.Pid(), inst.Wait)
}

if p.monitor != nil {
kill(p.monitor.cmd.Process.Pid, p.monitor.wait)
go kill("prometheus", p.monitor.cmd.Process.Pid, p.monitor.wait)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it be possible to remain as zombie process if it hangs on kill?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entire terminate function are in a goroutine and there are another wait goroutine.

}

if p.ngmonitoring != nil {
kill(p.ngmonitoring.cmd.Process.Pid, p.ngmonitoring.wait)
go kill("ng-monitoring", p.ngmonitoring.cmd.Process.Pid, p.ngmonitoring.wait)
}

if p.grafana != nil {
kill(p.grafana.cmd.Process.Pid, p.grafana.wait)
go kill("grafana", p.grafana.cmd.Process.Pid, p.grafana.wait)
}
for _, inst := range p.tiflashs {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
for _, inst := range p.ticdcs {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
for _, inst := range p.drainers {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
// tidb must exit earlier then pd
for _, inst := range p.tidbs {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
for _, inst := range p.pumps {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
for _, inst := range p.tikvs {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
for _, inst := range p.pds {
kill(inst.Component(), inst.Pid(), inst.Wait)
}
}

Expand Down