2018-05-07 12:33:16 +00:00
|
|
|
// Package pubsubmon implements a PeerMonitor component for IPFS Cluster that
|
|
|
|
// uses PubSub to send and receive metrics.
|
|
|
|
package pubsubmon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2018-10-29 16:25:52 +00:00
|
|
|
|
2018-05-07 12:33:16 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
2018-05-08 09:38:12 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/monitor/metrics"
|
2018-06-27 04:03:15 +00:00
|
|
|
"go.opencensus.io/trace"
|
2018-05-07 12:33:16 +00:00
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log"
|
2018-10-17 13:28:03 +00:00
|
|
|
rpc "github.com/libp2p/go-libp2p-gorpc"
|
2018-05-07 12:33:16 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2018-10-29 16:25:52 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2018-05-07 12:33:16 +00:00
|
|
|
msgpack "github.com/multiformats/go-multicodec/msgpack"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logger = logging.Logger("monitor")
|
|
|
|
|
|
|
|
// PubsubTopic specifies the topic used to publish Cluster metrics.
|
2018-05-08 09:38:12 +00:00
|
|
|
var PubsubTopic = "monitor.metrics"
|
2018-05-07 12:33:16 +00:00
|
|
|
|
|
|
|
var msgpackHandle = msgpack.DefaultMsgpackHandle()
|
|
|
|
|
|
|
|
// Monitor is a component in charge of monitoring peers, logging
|
|
|
|
// metrics and detecting failures
|
|
|
|
type Monitor struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
rpcClient *rpc.Client
|
|
|
|
rpcReady chan struct{}
|
|
|
|
|
2018-10-29 16:25:52 +00:00
|
|
|
pubsub *pubsub.PubSub
|
|
|
|
subscription *pubsub.Subscription
|
2019-02-20 14:24:25 +00:00
|
|
|
peers PeersFunc
|
2018-05-07 12:33:16 +00:00
|
|
|
|
2018-05-08 09:38:12 +00:00
|
|
|
metrics *metrics.Store
|
|
|
|
checker *metrics.Checker
|
2018-05-07 12:33:16 +00:00
|
|
|
|
|
|
|
config *Config
|
|
|
|
|
|
|
|
shutdownLock sync.Mutex
|
|
|
|
shutdown bool
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
// PeersFunc allows the Monitor to filter and discard metrics
|
|
|
|
// that do not belong to a given peerset.
|
|
|
|
type PeersFunc func(context.Context) ([]peer.ID, error)
|
|
|
|
|
|
|
|
// New creates a new PubSub monitor, using the given host, config and
|
|
|
|
// PeersFunc. The PeersFunc can be nil. In this case, no metric filtering is
|
|
|
|
// done based on peers (any peer is considered part of the peerset).
|
|
|
|
func New(
|
2019-04-26 06:33:01 +00:00
|
|
|
ctx context.Context,
|
2019-02-20 14:24:25 +00:00
|
|
|
cfg *Config,
|
|
|
|
psub *pubsub.PubSub,
|
|
|
|
peers PeersFunc,
|
|
|
|
) (*Monitor, error) {
|
2018-05-07 12:33:16 +00:00
|
|
|
err := cfg.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:33:01 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-05-07 12:33:16 +00:00
|
|
|
|
2018-05-08 09:38:12 +00:00
|
|
|
mtrs := metrics.NewStore()
|
2019-04-26 06:33:01 +00:00
|
|
|
checker := metrics.NewChecker(ctx, mtrs, cfg.FailureThreshold)
|
2018-05-07 12:33:16 +00:00
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
subscription, err := psub.Subscribe(PubsubTopic)
|
2018-05-07 12:33:16 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mon := &Monitor{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
rpcReady: make(chan struct{}, 1),
|
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
pubsub: psub,
|
2018-05-07 12:33:16 +00:00
|
|
|
subscription: subscription,
|
2019-02-20 14:24:25 +00:00
|
|
|
peers: peers,
|
2018-05-07 12:33:16 +00:00
|
|
|
|
2018-05-08 09:38:12 +00:00
|
|
|
metrics: mtrs,
|
|
|
|
checker: checker,
|
|
|
|
config: cfg,
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go mon.run()
|
|
|
|
return mon, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mon *Monitor) run() {
|
|
|
|
select {
|
|
|
|
case <-mon.rpcReady:
|
|
|
|
go mon.logFromPubsub()
|
2019-02-20 14:24:25 +00:00
|
|
|
go mon.checker.Watch(mon.ctx, mon.peers, mon.config.CheckInterval)
|
2018-05-07 12:33:16 +00:00
|
|
|
case <-mon.ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// logFromPubsub logs metrics received in the subscribed topic.
|
|
|
|
func (mon *Monitor) logFromPubsub() {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx, span := trace.StartSpan(mon.ctx, "monitor/pubsub/logFromPubsub")
|
|
|
|
defer span.End()
|
|
|
|
|
2018-05-07 12:33:16 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-06-27 04:03:15 +00:00
|
|
|
case <-ctx.Done():
|
2018-05-07 12:33:16 +00:00
|
|
|
return
|
|
|
|
default:
|
2018-06-27 04:03:15 +00:00
|
|
|
msg, err := mon.subscription.Next(ctx)
|
2018-05-07 12:33:16 +00:00
|
|
|
if err != nil { // context cancelled enters here
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data := msg.GetData()
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
dec := msgpack.Multicodec(msgpackHandle).Decoder(buf)
|
|
|
|
metric := api.Metric{}
|
|
|
|
err = dec.Decode(&metric)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.Debugf(
|
|
|
|
"received pubsub metric '%s' from '%s'",
|
|
|
|
metric.Name,
|
|
|
|
metric.Peer,
|
|
|
|
)
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
err = mon.LogMetric(ctx, &metric)
|
2018-05-07 12:33:16 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClient saves the given rpc.Client for later use
|
|
|
|
func (mon *Monitor) SetClient(c *rpc.Client) {
|
|
|
|
mon.rpcClient = c
|
|
|
|
mon.rpcReady <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the peer monitor. It particular, it will
|
|
|
|
// not deliver any alerts.
|
2018-06-27 04:03:15 +00:00
|
|
|
func (mon *Monitor) Shutdown(ctx context.Context) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "monitor/pubsub/Shutdown")
|
|
|
|
defer span.End()
|
|
|
|
|
2018-05-07 12:33:16 +00:00
|
|
|
mon.shutdownLock.Lock()
|
|
|
|
defer mon.shutdownLock.Unlock()
|
|
|
|
|
|
|
|
if mon.shutdown {
|
|
|
|
logger.Warning("Monitor already shut down")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("stopping Monitor")
|
|
|
|
close(mon.rpcReady)
|
|
|
|
|
|
|
|
mon.cancel()
|
|
|
|
|
|
|
|
mon.wg.Wait()
|
|
|
|
mon.shutdown = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogMetric stores a metric so it can later be retrieved.
|
2019-02-27 17:04:35 +00:00
|
|
|
func (mon *Monitor) LogMetric(ctx context.Context, m *api.Metric) error {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "monitor/pubsub/LogMetric")
|
|
|
|
defer span.End()
|
|
|
|
|
2018-05-08 09:38:12 +00:00
|
|
|
mon.metrics.Add(m)
|
|
|
|
logger.Debugf("pubsub mon logged '%s' metric from '%s'. Expires on %d", m.Name, m.Peer, m.Expire)
|
2018-05-07 12:33:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishMetric broadcasts a metric to all current cluster peers.
|
2019-02-27 17:04:35 +00:00
|
|
|
func (mon *Monitor) PublishMetric(ctx context.Context, m *api.Metric) error {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "monitor/pubsub/PublishMetric")
|
|
|
|
defer span.End()
|
|
|
|
|
2018-05-07 12:33:16 +00:00
|
|
|
if m.Discard() {
|
|
|
|
logger.Warningf("discarding invalid metric: %+v", m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
enc := msgpack.Multicodec(msgpackHandle).Encoder(&b)
|
|
|
|
err := enc.Encode(m)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf(
|
|
|
|
"publishing metric %s to pubsub. Expires: %d",
|
|
|
|
m.Name,
|
|
|
|
m.Expire,
|
|
|
|
)
|
|
|
|
|
|
|
|
err = mon.pubsub.Publish(PubsubTopic, b.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-08 09:38:12 +00:00
|
|
|
// LatestMetrics returns last known VALID metrics of a given type. A metric
|
2019-02-20 14:24:25 +00:00
|
|
|
// is only valid if it has not expired and belongs to a current cluster peer.
|
2019-02-27 17:04:35 +00:00
|
|
|
func (mon *Monitor) LatestMetrics(ctx context.Context, name string) []*api.Metric {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "monitor/pubsub/LatestMetrics")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
latest := mon.metrics.LatestValid(name)
|
|
|
|
|
|
|
|
if mon.peers == nil {
|
|
|
|
return latest
|
|
|
|
}
|
2018-05-08 09:38:12 +00:00
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
// Make sure we only return metrics in the current peerset if we have
|
|
|
|
// a peerset provider.
|
|
|
|
peers, err := mon.peers(ctx)
|
2018-05-07 12:33:16 +00:00
|
|
|
if err != nil {
|
2019-02-27 17:04:35 +00:00
|
|
|
return []*api.Metric{}
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 09:38:12 +00:00
|
|
|
return metrics.PeersetFilter(latest, peers)
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alerts returns a channel on which alerts are sent when the
|
|
|
|
// monitor detects a failure.
|
2019-02-27 17:04:35 +00:00
|
|
|
func (mon *Monitor) Alerts() <-chan *api.Alert {
|
2018-05-08 09:38:12 +00:00
|
|
|
return mon.checker.Alerts()
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|