2018-08-26 21:58:58 +02:00
|
|
|
package daemon
|
2017-09-17 23:54:23 +02:00
|
|
|
|
|
|
|
import (
|
2017-11-18 20:34:28 +01:00
|
|
|
"bytes"
|
2017-09-17 23:54:23 +02:00
|
|
|
"context"
|
2017-11-18 20:34:28 +01:00
|
|
|
"encoding/json"
|
2017-09-17 23:54:23 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-11-18 20:34:28 +01:00
|
|
|
"io"
|
2017-09-17 23:54:23 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-08-26 21:58:58 +02:00
|
|
|
"github.com/zrepl/zrepl/cmd/daemon/job"
|
|
|
|
"github.com/zrepl/zrepl/version"
|
|
|
|
"github.com/zrepl/zrepl/cmd/helpers"
|
2017-09-17 23:54:23 +02:00
|
|
|
)
|
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
type controlJob struct {
|
2017-09-17 23:54:23 +02:00
|
|
|
sockaddr *net.UnixAddr
|
2018-08-26 21:58:58 +02:00
|
|
|
jobs *jobs
|
2017-09-17 23:54:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
func newControlJob(sockpath string, jobs *jobs) (j *controlJob, err error) {
|
|
|
|
j = &controlJob{jobs: jobs}
|
2017-09-17 23:54:23 +02:00
|
|
|
|
|
|
|
j.sockaddr, err = net.ResolveUnixAddr("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot resolve unix address")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
func (j *controlJob) Name() string { return jobNameControl }
|
2017-09-17 23:54:23 +02:00
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
func (j *controlJob) Status() interface{} { return nil }
|
2018-04-05 22:22:55 +02:00
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
const (
|
2018-02-17 16:20:10 +01:00
|
|
|
ControlJobEndpointPProf string = "/debug/pprof"
|
2017-11-18 20:34:28 +01:00
|
|
|
ControlJobEndpointVersion string = "/version"
|
2017-12-24 15:35:12 +01:00
|
|
|
ControlJobEndpointStatus string = "/status"
|
2017-09-17 23:54:23 +02:00
|
|
|
)
|
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
func (j *controlJob) Run(ctx context.Context) {
|
2017-09-17 23:54:23 +02:00
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
log := job.GetLogger(ctx)
|
2017-09-23 11:07:08 +02:00
|
|
|
defer log.Info("control job finished")
|
2017-09-17 23:54:23 +02:00
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
l, err := helpers.ListenUnixPrivate(j.sockaddr)
|
2017-09-17 23:54:23 +02:00
|
|
|
if err != nil {
|
2017-09-23 11:07:08 +02:00
|
|
|
log.WithError(err).Error("error listening")
|
2017-09-17 23:54:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:20:10 +01:00
|
|
|
pprofServer := NewPProfServer(ctx)
|
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
mux := http.NewServeMux()
|
2018-02-17 16:20:10 +01:00
|
|
|
mux.Handle(ControlJobEndpointPProf, requestLogger{log: log, handlerFunc: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var msg PprofServerControlMsg
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&msg)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("bad pprof request from client")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
pprofServer.Control(msg)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}})
|
2017-12-24 15:05:07 +01:00
|
|
|
mux.Handle(ControlJobEndpointVersion,
|
|
|
|
requestLogger{log: log, handler: jsonResponder{func() (interface{}, error) {
|
2018-08-26 21:58:58 +02:00
|
|
|
return version.NewZreplVersionInformation(), nil
|
2017-12-24 15:05:07 +01:00
|
|
|
}}})
|
2017-12-24 15:35:12 +01:00
|
|
|
mux.Handle(ControlJobEndpointStatus,
|
|
|
|
requestLogger{log: log, handler: jsonResponder{func() (interface{}, error) {
|
2018-08-26 21:58:58 +02:00
|
|
|
s := j.jobs.status()
|
|
|
|
return s, nil
|
2018-08-16 01:26:09 +02:00
|
|
|
}}})
|
2017-09-17 23:54:23 +02:00
|
|
|
server := http.Server{Handler: mux}
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
|
|
|
|
served := make(chan error)
|
|
|
|
go func() {
|
|
|
|
served <- server.Serve(l)
|
|
|
|
close(served)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-10-05 21:20:01 +02:00
|
|
|
log.WithError(ctx.Err()).Info("context done")
|
2017-09-17 23:54:23 +02:00
|
|
|
server.Shutdown(context.Background())
|
|
|
|
break outer
|
|
|
|
case err = <-served:
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithError(err).Error("error serving")
|
2017-09-17 23:54:23 +02:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:05:07 +01:00
|
|
|
type jsonResponder struct {
|
|
|
|
producer func() (interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j jsonResponder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
res, err := j.producer()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
io.WriteString(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = json.NewEncoder(&buf).Encode(res)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
io.WriteString(w, err.Error())
|
|
|
|
} else {
|
|
|
|
io.Copy(w, &buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
type requestLogger struct {
|
2018-08-10 17:06:00 +02:00
|
|
|
log logger.Logger
|
2017-12-24 15:05:07 +01:00
|
|
|
handler http.Handler
|
|
|
|
handlerFunc http.HandlerFunc
|
2017-09-17 23:54:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l requestLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-09-22 14:13:58 +02:00
|
|
|
log := l.log.WithField("method", r.Method).WithField("url", r.URL)
|
2017-09-23 11:07:08 +02:00
|
|
|
log.Info("start")
|
2017-12-24 15:05:07 +01:00
|
|
|
if l.handlerFunc != nil {
|
|
|
|
l.handlerFunc(w, r)
|
|
|
|
} else if l.handler != nil {
|
|
|
|
l.handler.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
log.Error("no handler or handlerFunc configured")
|
|
|
|
}
|
2017-09-23 11:07:08 +02:00
|
|
|
log.Info("finish")
|
2017-09-17 23:54:23 +02:00
|
|
|
}
|