2021-04-26 13:17:49 +03:00
|
|
|
|
package managedstream
|
2021-04-05 19:04:46 +03:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
2021-06-24 11:07:09 +03:00
|
|
|
|
"fmt"
|
2021-07-30 21:05:39 +03:00
|
|
|
|
"sort"
|
2021-04-05 19:04:46 +03:00
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/live/orgchannel"
|
|
|
|
|
|
|
2021-04-05 19:04:46 +03:00
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
2021-04-09 12:17:22 -07:00
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/live"
|
2021-04-26 13:17:49 +03:00
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2021-04-05 19:04:46 +03:00
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-04-26 13:17:49 +03:00
|
|
|
|
var (
|
|
|
|
|
|
logger = log.New("live.managed_stream")
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
// If message comes from a plugin:
|
|
|
|
|
|
// * it's simply sent to local subscribers without any additional steps
|
|
|
|
|
|
// * if there is RULE then may be processed in some way
|
|
|
|
|
|
// * important to keep a message in the original channel
|
|
|
|
|
|
// * client subscribed to ds/<UID>/xxx
|
|
|
|
|
|
//
|
|
|
|
|
|
// What we want to build:
|
|
|
|
|
|
// * Stream scope not hardcoded and determined by the caller
|
|
|
|
|
|
// * So it's possible to use managed stream from plugins
|
|
|
|
|
|
// * The problem is HA – at moment several plugins on different nodes publish same messages
|
|
|
|
|
|
// * Can use in-memory managed stream for plugins with local subscribers publish, use HA-managed stream for HTTP/WS
|
|
|
|
|
|
// * Eventually maintain a single connection with a plugin over a channel leader selection.
|
|
|
|
|
|
|
|
|
|
|
|
// Runner keeps NamespaceStream per namespace.
|
2021-04-26 13:17:49 +03:00
|
|
|
|
type Runner struct {
|
2021-09-09 19:19:29 +03:00
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
|
streams map[int64]map[string]*NamespaceStream
|
|
|
|
|
|
publisher models.ChannelPublisher
|
|
|
|
|
|
localPublisher LocalPublisher
|
|
|
|
|
|
frameCache FrameCache
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type LocalPublisher interface {
|
|
|
|
|
|
PublishLocal(channel string, data []byte) error
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-26 13:17:49 +03:00
|
|
|
|
// NewRunner creates new Runner.
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func NewRunner(publisher models.ChannelPublisher, localPublisher LocalPublisher, frameCache FrameCache) *Runner {
|
2021-04-26 13:17:49 +03:00
|
|
|
|
return &Runner{
|
2021-09-09 19:19:29 +03:00
|
|
|
|
publisher: publisher,
|
|
|
|
|
|
localPublisher: localPublisher,
|
|
|
|
|
|
streams: map[int64]map[string]*NamespaceStream{},
|
|
|
|
|
|
frameCache: frameCache,
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-24 11:07:09 +03:00
|
|
|
|
func (r *Runner) GetManagedChannels(orgID int64) ([]*ManagedChannel, error) {
|
2021-07-30 21:05:39 +03:00
|
|
|
|
activeChannels, err := r.frameCache.GetActiveChannels(orgID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return []*ManagedChannel{}, fmt.Errorf("error getting active managed stream paths: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
channels := make([]*ManagedChannel, 0, len(activeChannels))
|
|
|
|
|
|
for ch, schema := range activeChannels {
|
|
|
|
|
|
managedChannel := &ManagedChannel{
|
|
|
|
|
|
Channel: ch,
|
|
|
|
|
|
Data: schema,
|
2021-06-24 11:07:09 +03:00
|
|
|
|
}
|
2021-07-30 21:05:39 +03:00
|
|
|
|
// Enrich with minute rate.
|
|
|
|
|
|
channel, _ := live.ParseChannel(managedChannel.Channel)
|
2021-09-09 19:19:29 +03:00
|
|
|
|
prefix := channel.Scope + "/" + channel.Namespace
|
|
|
|
|
|
namespaceStream, ok := r.streams[orgID][prefix]
|
2021-07-30 21:05:39 +03:00
|
|
|
|
if ok {
|
|
|
|
|
|
managedChannel.MinuteRate = namespaceStream.minuteRate(channel.Path)
|
|
|
|
|
|
}
|
|
|
|
|
|
channels = append(channels, managedChannel)
|
2021-06-24 11:07:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hardcode sample streams
|
|
|
|
|
|
frameJSON, err := data.FrameToJSON(data.NewFrame("testdata",
|
|
|
|
|
|
data.NewField("Time", nil, make([]time.Time, 0)),
|
|
|
|
|
|
data.NewField("Value", nil, make([]float64, 0)),
|
|
|
|
|
|
data.NewField("Min", nil, make([]float64, 0)),
|
|
|
|
|
|
data.NewField("Max", nil, make([]float64, 0)),
|
|
|
|
|
|
), data.IncludeSchemaOnly)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
channels = append(channels, &ManagedChannel{
|
2021-07-30 21:05:39 +03:00
|
|
|
|
Channel: "plugin/testdata/random-2s-stream",
|
|
|
|
|
|
Data: frameJSON,
|
|
|
|
|
|
MinuteRate: 30,
|
2021-06-24 11:07:09 +03:00
|
|
|
|
}, &ManagedChannel{
|
2021-07-30 21:05:39 +03:00
|
|
|
|
Channel: "plugin/testdata/random-flakey-stream",
|
|
|
|
|
|
Data: frameJSON,
|
|
|
|
|
|
MinuteRate: 150,
|
2021-06-24 11:07:09 +03:00
|
|
|
|
}, &ManagedChannel{
|
2021-07-30 21:05:39 +03:00
|
|
|
|
Channel: "plugin/testdata/random-20Hz-stream",
|
|
|
|
|
|
Data: frameJSON,
|
|
|
|
|
|
MinuteRate: 1200,
|
2021-06-24 11:07:09 +03:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2021-07-30 21:05:39 +03:00
|
|
|
|
|
|
|
|
|
|
sort.Slice(channels, func(i, j int) bool {
|
|
|
|
|
|
return channels[i].Channel < channels[j].Channel
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2021-06-24 11:07:09 +03:00
|
|
|
|
return channels, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-05 19:04:46 +03:00
|
|
|
|
// GetOrCreateStream -- for now this will create new manager for each key.
|
|
|
|
|
|
// Eventually, the stream behavior will need to be configured explicitly
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func (r *Runner) GetOrCreateStream(orgID int64, scope string, namespace string) (*NamespaceStream, error) {
|
2021-04-05 19:04:46 +03:00
|
|
|
|
r.mu.Lock()
|
|
|
|
|
|
defer r.mu.Unlock()
|
2021-05-11 22:03:04 +03:00
|
|
|
|
_, ok := r.streams[orgID]
|
|
|
|
|
|
if !ok {
|
2021-09-09 19:19:29 +03:00
|
|
|
|
r.streams[orgID] = map[string]*NamespaceStream{}
|
2021-05-11 22:03:04 +03:00
|
|
|
|
}
|
2021-09-09 19:19:29 +03:00
|
|
|
|
prefix := scope + "/" + namespace
|
|
|
|
|
|
s, ok := r.streams[orgID][prefix]
|
2021-04-05 19:04:46 +03:00
|
|
|
|
if !ok {
|
2021-09-09 19:19:29 +03:00
|
|
|
|
s = NewNamespaceStream(orgID, scope, namespace, r.publisher, r.localPublisher, r.frameCache)
|
|
|
|
|
|
r.streams[orgID][prefix] = s
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
return s, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
// NamespaceStream holds the state of a managed stream.
|
|
|
|
|
|
type NamespaceStream struct {
|
|
|
|
|
|
orgID int64
|
|
|
|
|
|
scope string
|
|
|
|
|
|
namespace string
|
|
|
|
|
|
publisher models.ChannelPublisher
|
|
|
|
|
|
localPublisher LocalPublisher
|
|
|
|
|
|
frameCache FrameCache
|
|
|
|
|
|
rateMu sync.RWMutex
|
|
|
|
|
|
rates map[string][60]rateEntry
|
2021-07-30 21:05:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type rateEntry struct {
|
|
|
|
|
|
time uint32
|
|
|
|
|
|
count int32
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-24 11:07:09 +03:00
|
|
|
|
// ManagedChannel represents a managed stream.
|
|
|
|
|
|
type ManagedChannel struct {
|
2021-07-30 21:05:39 +03:00
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
|
|
MinuteRate int64 `json:"minute_rate"`
|
|
|
|
|
|
Data json.RawMessage `json:"data"`
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
// NewNamespaceStream creates new NamespaceStream.
|
|
|
|
|
|
func NewNamespaceStream(orgID int64, scope string, namespace string, publisher models.ChannelPublisher, localPublisher LocalPublisher, schemaUpdater FrameCache) *NamespaceStream {
|
|
|
|
|
|
return &NamespaceStream{
|
|
|
|
|
|
orgID: orgID,
|
|
|
|
|
|
scope: scope,
|
|
|
|
|
|
namespace: namespace,
|
|
|
|
|
|
publisher: publisher,
|
|
|
|
|
|
localPublisher: localPublisher,
|
|
|
|
|
|
frameCache: schemaUpdater,
|
|
|
|
|
|
rates: map[string][60]rateEntry{},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-09 21:06:25 +03:00
|
|
|
|
// Push sends frame to the stream and saves it for later retrieval by subscribers.
|
2021-09-09 19:19:29 +03:00
|
|
|
|
// * Saves the entire frame to cache.
|
|
|
|
|
|
// * If schema has been changed sends entire frame to channel, otherwise only data.
|
|
|
|
|
|
func (s *NamespaceStream) Push(path string, frame *data.Frame) error {
|
2021-06-24 11:07:09 +03:00
|
|
|
|
jsonFrameCache, err := data.FrameToJSONCache(frame)
|
2021-04-05 19:04:46 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2021-05-27 02:55:42 -07:00
|
|
|
|
|
2021-04-05 19:04:46 +03:00
|
|
|
|
// The channel this will be posted into.
|
2021-09-09 19:19:29 +03:00
|
|
|
|
channel := live.Channel{Scope: s.scope, Namespace: s.namespace, Path: path}.String()
|
2021-04-05 19:04:46 +03:00
|
|
|
|
|
2021-07-30 21:05:39 +03:00
|
|
|
|
isUpdated, err := s.frameCache.Update(s.orgID, channel, jsonFrameCache)
|
2021-06-24 11:07:09 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Error("Error updating managed stream schema", "error", err)
|
|
|
|
|
|
return err
|
2021-05-11 22:03:04 +03:00
|
|
|
|
}
|
2021-06-24 11:07:09 +03:00
|
|
|
|
|
|
|
|
|
|
// When the schema has not changed, just send the data.
|
|
|
|
|
|
include := data.IncludeDataOnly
|
|
|
|
|
|
if isUpdated {
|
|
|
|
|
|
// When the schema has been changed, send all.
|
|
|
|
|
|
include = data.IncludeAll
|
2021-05-27 02:55:42 -07:00
|
|
|
|
}
|
2021-06-24 11:07:09 +03:00
|
|
|
|
frameJSON := jsonFrameCache.Bytes(include)
|
|
|
|
|
|
|
|
|
|
|
|
logger.Debug("Publish data to channel", "channel", channel, "dataLength", len(frameJSON))
|
2021-07-30 21:05:39 +03:00
|
|
|
|
s.incRate(path, time.Now().Unix())
|
2021-09-09 19:19:29 +03:00
|
|
|
|
if s.scope == live.ScopeDatasource || s.scope == live.ScopePlugin {
|
|
|
|
|
|
return s.localPublisher.PublishLocal(orgchannel.PrependOrgID(s.orgID, channel), frameJSON)
|
|
|
|
|
|
}
|
2021-07-30 21:05:39 +03:00
|
|
|
|
return s.publisher(s.orgID, channel, frameJSON)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func (s *NamespaceStream) incRate(path string, nowUnix int64) {
|
2021-07-30 21:05:39 +03:00
|
|
|
|
s.rateMu.Lock()
|
|
|
|
|
|
pathRate, ok := s.rates[path]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
pathRate = [60]rateEntry{}
|
|
|
|
|
|
}
|
|
|
|
|
|
now := time.Unix(nowUnix, 0)
|
|
|
|
|
|
slot := now.Second() % 60
|
|
|
|
|
|
if pathRate[slot].time != uint32(nowUnix) {
|
|
|
|
|
|
pathRate[slot].count = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
pathRate[slot].time = uint32(nowUnix)
|
|
|
|
|
|
pathRate[slot].count += 1
|
|
|
|
|
|
s.rates[path] = pathRate
|
|
|
|
|
|
s.rateMu.Unlock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func (s *NamespaceStream) minuteRate(path string) int64 {
|
2021-07-30 21:05:39 +03:00
|
|
|
|
var total int64
|
|
|
|
|
|
s.rateMu.RLock()
|
|
|
|
|
|
defer s.rateMu.RUnlock()
|
|
|
|
|
|
pathRate, ok := s.rates[path]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, val := range pathRate {
|
|
|
|
|
|
if val.time > uint32(time.Now().Unix()-60) {
|
|
|
|
|
|
total += int64(val.count)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return total
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func (s *NamespaceStream) GetHandlerForPath(_ string) (models.ChannelHandler, error) {
|
2021-04-05 19:04:46 +03:00
|
|
|
|
return s, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func (s *NamespaceStream) OnSubscribe(_ context.Context, u *models.SignedInUser, e models.SubscribeEvent) (models.SubscribeReply, backend.SubscribeStreamStatus, error) {
|
2021-04-05 19:04:46 +03:00
|
|
|
|
reply := models.SubscribeReply{}
|
2021-06-24 11:07:09 +03:00
|
|
|
|
frameJSON, ok, err := s.frameCache.GetFrame(u.OrgId, e.Channel)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return reply, 0, err
|
|
|
|
|
|
}
|
2021-04-05 19:04:46 +03:00
|
|
|
|
if ok {
|
2021-06-24 11:07:09 +03:00
|
|
|
|
reply.Data = frameJSON
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
return reply, backend.SubscribeStreamStatusOK, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-09 19:19:29 +03:00
|
|
|
|
func (s *NamespaceStream) OnPublish(_ context.Context, _ *models.SignedInUser, _ models.PublishEvent) (models.PublishReply, backend.PublishStreamStatus, error) {
|
2021-06-24 11:07:09 +03:00
|
|
|
|
return models.PublishReply{}, backend.PublishStreamStatusPermissionDenied, nil
|
2021-04-05 19:04:46 +03:00
|
|
|
|
}
|