status: infra for reporting jobs instead of just replication.Report

This commit is contained in:
Christian Schwarz
2018-09-23 21:08:03 +02:00
parent 4a6160baf3
commit 9446b51a1f
7 changed files with 116 additions and 25 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ func newControlJob(sockpath string, jobs *jobs) (j *controlJob, err error) {
func (j *controlJob) Name() string { return jobNameControl }
func (j *controlJob) Status() interface{} { return nil }
func (j *controlJob) Status() *job.Status { return &job.Status{Type: job.TypeInternal} }
var promControl struct {
requestBegin *prometheus.CounterVec
+3 -3
View File
@@ -125,13 +125,13 @@ func (s *jobs) wait() <-chan struct{} {
return ch
}
func (s *jobs) status() map[string]interface{} {
func (s *jobs) status() map[string]*job.Status {
s.m.RLock()
defer s.m.RUnlock()
type res struct {
name string
status interface{}
status *job.Status
}
var wg sync.WaitGroup
c := make(chan res, len(s.jobs))
@@ -144,7 +144,7 @@ func (s *jobs) status() map[string]interface{} {
}
wg.Wait()
close(c)
ret := make(map[string]interface{}, len(s.jobs))
ret := make(map[string]*job.Status, len(s.jobs))
for res := range c {
ret[res.name] = res.status
}
+66 -1
View File
@@ -2,7 +2,9 @@ package job
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/zrepl/zrepl/logger"
)
@@ -47,10 +49,73 @@ func WithWakeup(ctx context.Context) (context.Context, WakeupFunc) {
type Job interface {
Name() string
Run(ctx context.Context)
Status() interface{}
Status() *Status
RegisterMetrics(registerer prometheus.Registerer)
}
type Type string
const (
TypeInternal Type = "internal"
TypePush Type = "push"
TypeSink Type = "sink"
)
type Status struct {
Type Type
JobSpecific interface{}
}
func (s *Status) MarshalJSON() ([]byte, error) {
typeJson, err := json.Marshal(s.Type)
if err != nil {
return nil, err
}
jobJSON, err := json.Marshal(s.JobSpecific)
if err != nil {
return nil, err
}
m := map[string]json.RawMessage {
"type": typeJson,
string(s.Type): jobJSON,
}
return json.Marshal(m)
}
func (s *Status) UnmarshalJSON(in []byte) (err error) {
var m map[string]json.RawMessage
if err := json.Unmarshal(in, &m); err != nil {
return err
}
tJSON, ok := m["type"]
if !ok {
return fmt.Errorf("field 'type' not found")
}
if err := json.Unmarshal(tJSON, &s.Type); err != nil {
return err
}
key := string(s.Type)
jobJSON, ok := m[key]
if !ok {
return fmt.Errorf("field '%s', not found", key)
}
switch s.Type {
case TypePush:
var st PushStatus
err = json.Unmarshal(jobJSON, &st)
s.JobSpecific = &st
case TypeSink:
var st SinkStatus
err = json.Unmarshal(jobJSON, &st)
s.JobSpecific = &st
case TypeInternal:
// internal jobs do not report specifics
default:
err = fmt.Errorf("unknown job type '%s'", key)
}
return err
}
func WaitWakeup(ctx context.Context) <-chan struct{} {
wc, ok := ctx.Value(contextKeyWakeup).(chan struct{})
if !ok {
+9 -3
View File
@@ -89,7 +89,11 @@ func (j *Push) RegisterMetrics(registerer prometheus.Registerer) {
func (j *Push) Name() string { return j.name }
func (j *Push) Status() interface{} {
type PushStatus struct {
Replication *replication.Report
}
func (j *Push) Status() *Status {
rep := func() *replication.Replication {
j.mtx.Lock()
defer j.mtx.Unlock()
@@ -98,10 +102,12 @@ func (j *Push) Status() interface{} {
}
return j.replication
}()
s := &PushStatus{}
if rep == nil {
return nil
return &Status{Type: TypePush, JobSpecific: s}
}
return rep.Report()
s.Replication = rep.Report()
return &Status{Type: TypePush, JobSpecific: s}
}
func (j *Push) Run(ctx context.Context) {
+4 -3
View File
@@ -41,9 +41,10 @@ func SinkFromConfig(g *config.Global, in *config.SinkJob) (s *Sink, err error) {
func (j *Sink) Name() string { return j.name }
func (*Sink) Status() interface{} {
// FIXME
return nil
type SinkStatus struct {}
func (*Sink) Status() *Status {
return &Status{Type: TypeSink} // FIXME SinkStatus
}
func (*Sink) RegisterMetrics(registerer prometheus.Registerer) {}
+1 -1
View File
@@ -39,7 +39,7 @@ func init() {
func (j *prometheusJob) Name() string { return jobNamePrometheus }
func (j *prometheusJob) Status() interface{} { return nil }
func (j *prometheusJob) Status() *job.Status { return &job.Status{Type: job.TypeInternal} }
func (j *prometheusJob) RegisterMetrics(registerer prometheus.Registerer) {}