Live: persisting last message in cache for broadcast scope (#32938)

This commit is contained in:
Alexander Emelin
2021-04-30 21:06:33 +03:00
committed by GitHub
parent c25eab4eda
commit 0c2bcbf2bc
10 changed files with 436 additions and 19 deletions

View File

@@ -0,0 +1,24 @@
package database
import "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
// For now disable migration. For now we are using local cache as storage to evaluate ideas.
// This will be turned on soon though.
func AddLiveChannelMigrations(mg *migrator.Migrator) {
//liveMessage := migrator.Table{
// Name: "live_message",
// Columns: []*migrator.Column{
// {Name: "id", Type: migrator.DB_BigInt, Nullable: false, IsPrimaryKey: true, IsAutoIncrement: true},
// {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},
// {Name: "channel", Type: migrator.DB_NVarchar, Length: 189, Nullable: false},
// {Name: "data", Type: migrator.DB_Text, Nullable: false},
// {Name: "published", Type: migrator.DB_DateTime, Nullable: false},
// },
// Indices: []*migrator.Index{
// {Cols: []string{"org_id", "channel"}, Type: migrator.UniqueIndex},
// },
//}
//
//mg.AddMigration("create live message table", migrator.NewAddTableMigration(liveMessage))
//mg.AddMigration("add index live_message.org_id_channel_unique", migrator.NewAddIndexMigration(liveMessage, liveMessage.Indices[0]))
}

View File

@@ -0,0 +1,66 @@
package database
import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
type Storage struct {
store *sqlstore.SQLStore
cache *localcache.CacheService
}
func NewStorage(store *sqlstore.SQLStore, cache *localcache.CacheService) *Storage {
return &Storage{store: store, cache: cache}
}
func getLiveMessageCacheKey(orgID int64, channel string) string {
return fmt.Sprintf("live_message_%d_%s", orgID, channel)
}
func (s *Storage) SaveLiveMessage(query *models.SaveLiveMessageQuery) error {
// Come back to saving into database after evaluating database structure.
//err := s.store.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
// params := []interface{}{query.OrgId, query.Channel, query.Data, time.Now()}
// upsertSQL := s.store.Dialect.UpsertSQL(
// "live_message",
// []string{"org_id", "channel"},
// []string{"org_id", "channel", "data", "published"})
// _, err := sess.SQL(upsertSQL, params...).Query()
// return err
//})
// return err
s.cache.Set(getLiveMessageCacheKey(query.OrgId, query.Channel), models.LiveMessage{
Id: 0, // Not used actually.
OrgId: query.OrgId,
Channel: query.Channel,
Data: query.Data,
Published: time.Now(),
}, 0)
return nil
}
func (s *Storage) GetLiveMessage(query *models.GetLiveMessageQuery) (models.LiveMessage, bool, error) {
// Come back to saving into database after evaluating database structure.
//var msg models.LiveMessage
//var exists bool
//err := s.store.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
// var err error
// exists, err = sess.Where("org_id=? AND channel=?", query.OrgId, query.Channel).Get(&msg)
// return err
//})
//return msg, exists, err
m, ok := s.cache.Get(getLiveMessageCacheKey(query.OrgId, query.Channel))
if !ok {
return models.LiveMessage{}, false, nil
}
msg, ok := m.(models.LiveMessage)
if !ok {
return models.LiveMessage{}, false, fmt.Errorf("unexpected live message type in cache: %T", m)
}
return msg, true, nil
}

View File

@@ -0,0 +1,44 @@
package tests
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/live"
"github.com/grafana/grafana/pkg/services/live/database"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
)
// SetupTestStorage initializes a storage to used by the integration tests.
// This is required to properly register and execute migrations.
func SetupTestStorage(t *testing.T) *database.Storage {
cfg := setting.NewCfg()
// Live is disabled by default and only if it's enabled its database migrations run
// and the related database tables are created.
cfg.FeatureToggles = map[string]bool{"live": true}
gLive := live.NewGrafanaLive()
gLive.Cfg = cfg
// Hook for initialising the service after the Cfg is populated
// so that database migrations will run.
overrideServiceFunc := func(descriptor registry.Descriptor) (*registry.Descriptor, bool) {
if _, ok := descriptor.Instance.(*live.GrafanaLive); ok {
return &registry.Descriptor{
Name: descriptor.Name,
Instance: gLive,
InitPriority: descriptor.InitPriority,
}, true
}
return nil, false
}
registry.RegisterOverride(overrideServiceFunc)
// Now we can use sql.Store.
sqlStore := sqlstore.InitTestDB(t)
localCache := localcache.New(time.Hour, time.Hour)
return database.NewStorage(sqlStore, localCache)
}

View File

@@ -0,0 +1,61 @@
// +build integration
package tests
import (
"encoding/json"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/require"
)
func TestLiveMessage(t *testing.T) {
storage := SetupTestStorage(t)
getQuery := &models.GetLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
}
_, ok, err := storage.GetLiveMessage(getQuery)
require.NoError(t, err)
require.False(t, ok)
saveQuery := &models.SaveLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
Data: []byte(`{}`),
}
err = storage.SaveLiveMessage(saveQuery)
require.NoError(t, err)
msg, ok, err := storage.GetLiveMessage(getQuery)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, int64(1), msg.OrgId)
require.Equal(t, "test_channel", msg.Channel)
require.Equal(t, json.RawMessage(`{}`), msg.Data)
require.NotZero(t, msg.Published)
// try saving again, should be replaced.
saveQuery2 := &models.SaveLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
Data: []byte(`{"input": "hello"}`),
}
err = storage.SaveLiveMessage(saveQuery2)
require.NoError(t, err)
getQuery2 := &models.GetLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
}
msg2, ok, err := storage.GetLiveMessage(getQuery2)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, int64(1), msg2.OrgId)
require.Equal(t, "test_channel", msg2.Channel)
require.Equal(t, json.RawMessage(`{"input": "hello"}`), msg2.Data)
require.NotZero(t, msg2.Published)
}