2017-09-01 18:55:53 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-12-25 00:20:31 +01:00
|
|
|
"container/list"
|
2017-09-13 23:27:18 +02:00
|
|
|
"context"
|
2017-09-10 16:13:05 +02:00
|
|
|
"fmt"
|
2017-09-13 23:27:18 +02:00
|
|
|
"github.com/spf13/cobra"
|
2017-09-23 18:20:22 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-12-25 00:20:31 +01:00
|
|
|
"io"
|
2017-09-13 23:27:18 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-12-25 00:20:31 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-09-13 23:27:18 +02:00
|
|
|
"syscall"
|
2017-09-23 18:20:22 +02:00
|
|
|
"time"
|
2017-09-01 18:55:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// daemonCmd represents the daemon command
|
|
|
|
var daemonCmd = &cobra.Command{
|
|
|
|
Use: "daemon",
|
2017-09-10 16:13:05 +02:00
|
|
|
Short: "start daemon",
|
2017-09-01 18:55:53 +02:00
|
|
|
Run: doDaemon,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(daemonCmd)
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
type Job interface {
|
|
|
|
JobName() string
|
2017-09-13 23:27:18 +02:00
|
|
|
JobStart(ctxt context.Context)
|
2017-12-24 15:34:41 +01:00
|
|
|
JobStatus(ctxt context.Context) (*JobStatus, error)
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
2017-09-01 18:55:53 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
func doDaemon(cmd *cobra.Command, args []string) {
|
2017-09-17 18:20:05 +02:00
|
|
|
|
2017-09-22 14:02:07 +02:00
|
|
|
conf, err := ParseConfig(rootArgs.configFile)
|
2017-09-17 18:20:05 +02:00
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "error parsing config: %s", err)
|
2017-09-17 18:20:05 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-23 18:20:22 +02:00
|
|
|
log := logger.NewLogger(conf.Global.logging.Outlets, 1*time.Second)
|
|
|
|
|
2017-11-18 20:34:28 +01:00
|
|
|
log.Info(NewZreplVersionInformation().String())
|
2017-09-22 14:13:58 +02:00
|
|
|
log.Debug("starting daemon")
|
|
|
|
ctx := context.WithValue(context.Background(), contextKeyLog, log)
|
2017-09-22 14:02:07 +02:00
|
|
|
ctx = context.WithValue(ctx, contextKeyLog, log)
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
d := NewDaemon(conf)
|
|
|
|
d.Loop(ctx)
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
2017-09-01 18:55:53 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
type contextKey string
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
const (
|
2017-12-24 15:35:12 +01:00
|
|
|
contextKeyLog contextKey = contextKey("log")
|
|
|
|
contextKeyDaemon contextKey = contextKey("daemon")
|
2017-09-13 23:27:18 +02:00
|
|
|
)
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
type Daemon struct {
|
2017-12-24 15:34:41 +01:00
|
|
|
conf *Config
|
|
|
|
startedAt time.Time
|
2017-09-17 18:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDaemon(initialConf *Config) *Daemon {
|
2017-12-24 15:34:41 +01:00
|
|
|
return &Daemon{conf: initialConf}
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
func (d *Daemon) Loop(ctx context.Context) {
|
|
|
|
|
2017-12-24 15:34:41 +01:00
|
|
|
d.startedAt = time.Now()
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-12-24 15:35:12 +01:00
|
|
|
ctx = context.WithValue(ctx, contextKeyDaemon, d)
|
2017-09-17 18:20:05 +02:00
|
|
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
2017-09-13 23:27:18 +02:00
|
|
|
finishs := make(chan Job)
|
2017-09-17 18:20:05 +02:00
|
|
|
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("starting jobs from config")
|
2017-09-13 23:27:18 +02:00
|
|
|
i := 0
|
2017-09-17 18:20:05 +02:00
|
|
|
for _, job := range d.conf.Jobs {
|
2017-09-23 11:24:36 +02:00
|
|
|
logger := log.WithField(logJobField, job.JobName())
|
2017-09-23 17:52:29 +02:00
|
|
|
logger.Info("starting")
|
2017-09-13 23:27:18 +02:00
|
|
|
i++
|
2017-09-17 18:20:05 +02:00
|
|
|
jobCtx := context.WithValue(ctx, contextKeyLog, logger)
|
2017-09-10 16:13:05 +02:00
|
|
|
go func(j Job) {
|
2017-09-17 18:20:05 +02:00
|
|
|
j.JobStart(jobCtx)
|
2017-09-13 23:27:18 +02:00
|
|
|
finishs <- j
|
2017-09-10 16:13:05 +02:00
|
|
|
}(job)
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
finishCount := 0
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
2017-09-23 17:52:29 +02:00
|
|
|
case <-finishs:
|
2017-09-13 23:27:18 +02:00
|
|
|
finishCount++
|
2017-09-17 18:20:05 +02:00
|
|
|
if finishCount == len(d.conf.Jobs) {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("all jobs finished")
|
2017-09-13 23:27:18 +02:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
case sig := <-sigChan:
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithField("signal", sig).Info("received signal")
|
|
|
|
log.Info("cancelling all jobs")
|
2017-09-17 18:20:05 +02:00
|
|
|
cancel()
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signal.Stop(sigChan)
|
2017-09-30 16:39:52 +02:00
|
|
|
cancel() // make go vet happy
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("exiting")
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|
2017-12-25 00:20:31 +01:00
|
|
|
|
2017-12-24 15:34:41 +01:00
|
|
|
// Representation of a Job's status that is composed of Tasks
|
|
|
|
type JobStatus struct {
|
|
|
|
// Statuses of all tasks of this job
|
|
|
|
Tasks []*TaskStatus
|
|
|
|
// Error != "" if JobStatus() returned an error
|
|
|
|
JobStatusError string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Representation of a Daemon's status that is composed of Jobs
|
|
|
|
type DaemonStatus struct {
|
|
|
|
StartedAt time.Time
|
|
|
|
Jobs map[string]*JobStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) Status() (s *DaemonStatus) {
|
|
|
|
|
|
|
|
s = &DaemonStatus{}
|
|
|
|
s.StartedAt = d.startedAt
|
|
|
|
|
|
|
|
s.Jobs = make(map[string]*JobStatus, len(d.conf.Jobs))
|
|
|
|
|
|
|
|
for name, j := range d.conf.Jobs {
|
|
|
|
status, err := j.JobStatus(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
s.Jobs[name] = &JobStatus{nil, err.Error()}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.Jobs[name] = status
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-25 00:20:31 +01:00
|
|
|
// Representation of a Task's status
|
|
|
|
type TaskStatus struct {
|
|
|
|
Name string
|
|
|
|
// Whether the task is idle.
|
|
|
|
Idle bool
|
|
|
|
// The stack of activities the task is currently executing.
|
|
|
|
// The first element is the root activity and equal to Name.
|
|
|
|
ActivityStack []string
|
|
|
|
// Number of bytes received by the task since it last left idle state.
|
|
|
|
ProgressRx int64
|
|
|
|
// Number of bytes sent by the task since it last left idle state.
|
|
|
|
ProgressTx int64
|
|
|
|
// Log entries emitted by the task since it last left idle state.
|
|
|
|
// Only contains the log entries emitted through the task's logger
|
|
|
|
// (provided by Task.Log()).
|
|
|
|
LogEntries []logger.Entry
|
|
|
|
// The maximum log level of LogEntries.
|
|
|
|
// Only valid if len(LogEntries) > 0.
|
|
|
|
MaxLogLevel logger.Level
|
2017-12-27 12:45:33 +01:00
|
|
|
// Last time something about the Task changed
|
|
|
|
LastUpdate time.Time
|
2017-12-25 00:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// An instance of Task tracks a single thread of activity that is part of a Job.
|
|
|
|
type Task struct {
|
|
|
|
// Stack of activities the task is currently in
|
|
|
|
// Members are instances of taskActivity
|
|
|
|
activities *list.List
|
2017-12-27 12:45:33 +01:00
|
|
|
// Last time activities was changed (not the activities inside, the list)
|
|
|
|
activitiesLastUpdate time.Time
|
2017-12-25 00:20:31 +01:00
|
|
|
// Protects Task members from modification
|
|
|
|
rwl sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Structure that describes the progress a Task has made
|
|
|
|
type taskProgress struct {
|
|
|
|
rx int64
|
|
|
|
tx int64
|
|
|
|
lastUpdate time.Time
|
|
|
|
logEntries []logger.Entry
|
|
|
|
mtx sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTaskProgress() (p *taskProgress) {
|
|
|
|
return &taskProgress{
|
|
|
|
logEntries: make([]logger.Entry, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *taskProgress) UpdateIO(drx, dtx int64) {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
|
|
|
p.rx += drx
|
|
|
|
p.tx += dtx
|
|
|
|
p.lastUpdate = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *taskProgress) UpdateLogEntry(entry logger.Entry) {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
|
|
|
// FIXME: ensure maximum size (issue #48)
|
|
|
|
p.logEntries = append(p.logEntries, entry)
|
|
|
|
p.lastUpdate = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *taskProgress) DeepCopy() (out taskProgress) {
|
|
|
|
p.mtx.RLock()
|
|
|
|
defer p.mtx.RUnlock()
|
|
|
|
out.rx, out.tx = p.rx, p.tx
|
2017-12-29 21:42:33 +01:00
|
|
|
out.lastUpdate = p.lastUpdate
|
2017-12-25 00:20:31 +01:00
|
|
|
out.logEntries = make([]logger.Entry, len(p.logEntries))
|
|
|
|
for i := range p.logEntries {
|
|
|
|
out.logEntries[i] = p.logEntries[i]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns a copy of this taskProgress, the mutex carries no semantic value
|
|
|
|
func (p *taskProgress) Read() (out taskProgress) {
|
|
|
|
p.mtx.RLock()
|
|
|
|
defer p.mtx.RUnlock()
|
|
|
|
return p.DeepCopy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Element of a Task's activity stack
|
|
|
|
type taskActivity struct {
|
|
|
|
name string
|
|
|
|
idle bool
|
|
|
|
logger *logger.Logger
|
|
|
|
// The progress of the task that is updated by UpdateIO() and UpdateLogEntry()
|
|
|
|
//
|
|
|
|
// Progress happens on a task-level and is thus global to the task.
|
|
|
|
// That's why progress is just a pointer to the current taskProgress:
|
|
|
|
// we reset progress when leaving the idle root activity
|
|
|
|
progress *taskProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTask(name string, lg *logger.Logger) *Task {
|
|
|
|
t := &Task{
|
|
|
|
activities: list.New(),
|
|
|
|
}
|
|
|
|
rootLogger := lg.ReplaceField(logTaskField, name).
|
|
|
|
WithOutlet(t, logger.Debug)
|
|
|
|
rootAct := &taskActivity{name, true, rootLogger, newTaskProgress()}
|
|
|
|
t.activities.PushFront(rootAct)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// callers must hold t.rwl
|
|
|
|
func (t *Task) cur() *taskActivity {
|
|
|
|
return t.activities.Front().Value.(*taskActivity)
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildActivityStack returns the stack of activity names
|
|
|
|
// t.rwl must be held, but the slice can be returned since strings are immutable
|
|
|
|
func (t *Task) buildActivityStack() []string {
|
|
|
|
comps := make([]string, 0, t.activities.Len())
|
|
|
|
for e := t.activities.Back(); e != nil; e = e.Prev() {
|
|
|
|
act := e.Value.(*taskActivity)
|
|
|
|
comps = append(comps, act.name)
|
|
|
|
}
|
|
|
|
return comps
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a sub-activity.
|
|
|
|
// Must always be matched with a call to t.Finish()
|
|
|
|
// --- consider using defer for this purpose.
|
|
|
|
func (t *Task) Enter(activity string) {
|
|
|
|
t.rwl.Lock()
|
|
|
|
defer t.rwl.Unlock()
|
|
|
|
|
|
|
|
prev := t.cur()
|
|
|
|
if prev.idle {
|
|
|
|
// reset progress when leaving idle task
|
|
|
|
// we leave the old progress dangling to have the user not worry about
|
|
|
|
prev.progress = newTaskProgress()
|
|
|
|
}
|
|
|
|
act := &taskActivity{activity, false, nil, prev.progress}
|
|
|
|
t.activities.PushFront(act)
|
|
|
|
stack := t.buildActivityStack()
|
|
|
|
activityField := strings.Join(stack, ".")
|
|
|
|
act.logger = prev.logger.ReplaceField(logTaskField, activityField)
|
|
|
|
|
2017-12-27 12:45:33 +01:00
|
|
|
t.activitiesLastUpdate = time.Now()
|
2017-12-25 00:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) UpdateProgress(dtx, drx int64) {
|
|
|
|
t.rwl.RLock()
|
|
|
|
p := t.cur().progress // protected by own rwlock
|
|
|
|
t.rwl.RUnlock()
|
|
|
|
p.UpdateIO(dtx, drx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a wrapper io.Reader that updates this task's _current_ progress value.
|
|
|
|
// Progress updates after this task resets its progress value are discarded.
|
|
|
|
func (t *Task) ProgressUpdater(r io.Reader) *IOProgressUpdater {
|
|
|
|
t.rwl.RLock()
|
|
|
|
defer t.rwl.RUnlock()
|
|
|
|
return &IOProgressUpdater{r, t.cur().progress}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) Status() *TaskStatus {
|
|
|
|
t.rwl.RLock()
|
|
|
|
defer t.rwl.RUnlock()
|
|
|
|
// NOTE
|
|
|
|
// do not return any state in TaskStatus that is protected by t.rwl
|
|
|
|
|
|
|
|
cur := t.cur()
|
|
|
|
stack := t.buildActivityStack()
|
|
|
|
prog := cur.progress.Read()
|
|
|
|
|
|
|
|
var maxLevel logger.Level
|
|
|
|
for _, entry := range prog.logEntries {
|
|
|
|
if maxLevel < entry.Level {
|
|
|
|
maxLevel = entry.Level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 12:45:33 +01:00
|
|
|
lastUpdate := prog.lastUpdate
|
|
|
|
if lastUpdate.Before(t.activitiesLastUpdate) {
|
|
|
|
lastUpdate = t.activitiesLastUpdate
|
|
|
|
}
|
|
|
|
|
2017-12-25 00:20:31 +01:00
|
|
|
s := &TaskStatus{
|
|
|
|
Name: stack[0],
|
|
|
|
ActivityStack: stack,
|
|
|
|
Idle: cur.idle,
|
|
|
|
ProgressRx: prog.rx,
|
|
|
|
ProgressTx: prog.tx,
|
|
|
|
LogEntries: prog.logEntries,
|
|
|
|
MaxLogLevel: maxLevel,
|
2017-12-27 12:45:33 +01:00
|
|
|
LastUpdate: lastUpdate,
|
2017-12-25 00:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish a sub-activity.
|
|
|
|
// Corresponds to a preceding call to t.Enter()
|
|
|
|
func (t *Task) Finish() {
|
|
|
|
t.rwl.Lock()
|
|
|
|
defer t.rwl.Unlock()
|
|
|
|
top := t.activities.Front()
|
|
|
|
if top.Next() == nil {
|
|
|
|
return // cannot remove root activity
|
|
|
|
}
|
|
|
|
t.activities.Remove(top)
|
2017-12-27 12:45:33 +01:00
|
|
|
t.activitiesLastUpdate = time.Now()
|
2017-12-25 00:20:31 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a logger derived from the logger passed to the constructor function.
|
|
|
|
// The logger's task field contains the current activity stack joined by '.'.
|
|
|
|
func (t *Task) Log() *logger.Logger {
|
|
|
|
t.rwl.RLock()
|
|
|
|
defer t.rwl.RUnlock()
|
2017-12-27 12:45:33 +01:00
|
|
|
// FIXME should influence TaskStatus's LastUpdate field
|
2017-12-25 00:20:31 +01:00
|
|
|
return t.cur().logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// implement logger.Outlet interface
|
2017-12-29 17:19:07 +01:00
|
|
|
func (t *Task) WriteEntry(entry logger.Entry) error {
|
2017-12-25 00:20:31 +01:00
|
|
|
t.rwl.RLock()
|
|
|
|
defer t.rwl.RUnlock()
|
|
|
|
t.cur().progress.UpdateLogEntry(entry)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IOProgressUpdater struct {
|
|
|
|
r io.Reader
|
|
|
|
p *taskProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *IOProgressUpdater) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = u.r.Read(p)
|
|
|
|
u.p.UpdateIO(int64(n), 0)
|
|
|
|
return
|
2017-12-24 15:34:41 +01:00
|
|
|
|
2017-12-25 00:20:31 +01:00
|
|
|
}
|