2020-01-28 23:13:59 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
// Package lifecycle allows controlling group of items.
|
|
|
|
package lifecycle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-03 14:53:15 +01:00
|
|
|
"errors"
|
2021-05-24 19:32:06 +01:00
|
|
|
"runtime"
|
2020-10-29 11:58:36 +00:00
|
|
|
"runtime/pprof"
|
2021-05-24 19:32:06 +01:00
|
|
|
"sync"
|
2020-11-03 19:42:22 +00:00
|
|
|
"time"
|
2020-01-28 23:13:59 +00:00
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2020-01-28 23:13:59 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"storj.io/common/errs2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mon = monkit.Package()
|
|
|
|
|
|
|
|
// Group implements a collection of items that have a
|
|
|
|
// concurrent start and are closed in reverse order.
|
|
|
|
type Group struct {
|
|
|
|
log *zap.Logger
|
|
|
|
items []Item
|
2021-05-24 19:32:06 +01:00
|
|
|
|
|
|
|
shutdownStack sync.Once
|
2020-01-28 23:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Item is the lifecycle item that group runs and closes.
|
|
|
|
type Item struct {
|
|
|
|
Name string
|
|
|
|
Run func(ctx context.Context) error
|
|
|
|
Close func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGroup creates a new group.
|
|
|
|
func NewGroup(log *zap.Logger) *Group {
|
|
|
|
return &Group{log: log}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds item to the group.
|
|
|
|
func (group *Group) Add(item Item) {
|
|
|
|
group.items = append(group.items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts all items concurrently under group g.
|
|
|
|
func (group *Group) Run(ctx context.Context, g *errgroup.Group) {
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
|
|
|
|
var started []string
|
|
|
|
for _, item := range group.items {
|
|
|
|
item := item
|
|
|
|
started = append(started, item.Name)
|
|
|
|
if item.Run == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-03 19:42:22 +00:00
|
|
|
|
|
|
|
shutdownCtx, shutdownFinished := context.WithCancel(context.Background())
|
2022-05-06 22:04:07 +01:00
|
|
|
go pprof.Do(ctx, pprof.Labels("name", "slow_shutdown:"+item.Name), func(ctx context.Context) {
|
2020-11-03 19:42:22 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-shutdownCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdownDeadline := time.NewTimer(15 * time.Second)
|
|
|
|
defer shutdownDeadline.Stop()
|
|
|
|
select {
|
|
|
|
case <-shutdownDeadline.C:
|
2021-06-25 17:21:30 +01:00
|
|
|
mon.Event("slow_shutdown") //mon:locked
|
2020-11-03 19:42:22 +00:00
|
|
|
group.log.Warn("service takes long to shutdown", zap.String("name", item.Name))
|
2021-05-24 19:32:06 +01:00
|
|
|
group.logStackTrace()
|
2020-11-03 19:42:22 +00:00
|
|
|
case <-shutdownCtx.Done():
|
|
|
|
}
|
2022-05-06 22:04:07 +01:00
|
|
|
})
|
2020-11-03 19:42:22 +00:00
|
|
|
|
2020-01-28 23:13:59 +00:00
|
|
|
g.Go(func() error {
|
2020-11-03 19:42:22 +00:00
|
|
|
defer shutdownFinished()
|
|
|
|
|
2020-10-29 11:58:36 +00:00
|
|
|
var err error
|
|
|
|
pprof.Do(ctx, pprof.Labels("name", item.Name), func(ctx context.Context) {
|
|
|
|
err = item.Run(ctx)
|
|
|
|
})
|
2020-09-03 14:53:15 +01:00
|
|
|
if errors.Is(ctx.Err(), context.Canceled) {
|
|
|
|
err = errs2.IgnoreCanceled(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-06-25 17:21:30 +01:00
|
|
|
mon.Event("unexpected_shutdown") //mon:locked
|
2020-09-03 14:53:15 +01:00
|
|
|
group.log.Error("unexpected shutdown of a runner", zap.String("name", item.Name), zap.Error(err))
|
|
|
|
}
|
|
|
|
return err
|
2020-01-28 23:13:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
group.log.Debug("started", zap.Strings("items", started))
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:32:06 +01:00
|
|
|
func (group *Group) logStackTrace() {
|
|
|
|
group.shutdownStack.Do(func() {
|
|
|
|
buf := make([]byte, 1024*1024)
|
|
|
|
for {
|
|
|
|
n := runtime.Stack(buf, true)
|
|
|
|
if n < len(buf) {
|
|
|
|
buf = buf[:n]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buf = make([]byte, 2*len(buf))
|
|
|
|
}
|
|
|
|
group.log.Info("slow shutdown", zap.String("stack", string(buf)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:13:59 +00:00
|
|
|
// Close closes all items in reverse order.
|
|
|
|
func (group *Group) Close() error {
|
|
|
|
var errlist errs.Group
|
|
|
|
|
|
|
|
for i := len(group.items) - 1; i >= 0; i-- {
|
|
|
|
item := group.items[i]
|
|
|
|
if item.Close == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
errlist.Add(item.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
return errlist.Err()
|
|
|
|
}
|