experiment: use read replica for Get and Find Dashboards (#91706)

This commit is contained in:
Kristin Laemmert 2024-08-08 14:22:55 -04:00 committed by GitHub
parent f463d622d0
commit 54177ca619
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -512,6 +512,7 @@ func (d *dashboardStore) GetDashboardsByPluginID(ctx context.Context, query *das
} }
return dashboards, nil return dashboards, nil
} }
func (d *dashboardStore) GetSoftDeletedDashboard(ctx context.Context, orgID int64, uid string) (*dashboards.Dashboard, error) { func (d *dashboardStore) GetSoftDeletedDashboard(ctx context.Context, orgID int64, uid string) (*dashboards.Dashboard, error) {
if orgID == 0 || uid == "" { if orgID == 0 || uid == "" {
return nil, dashboards.ErrDashboardIdentifierNotSet return nil, dashboards.ErrDashboardIdentifierNotSet
@ -736,7 +737,7 @@ func createEntityEvent(dashboard *dashboards.Dashboard, eventType store.EntityEv
func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.GetDashboardQuery) (*dashboards.Dashboard, error) { func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.GetDashboardQuery) (*dashboards.Dashboard, error) {
var queryResult *dashboards.Dashboard var queryResult *dashboards.Dashboard
err := d.store.DB().WithDbSession(ctx, func(sess *db.Session) error { err := d.store.ReadReplica().WithDbSession(ctx, func(sess *db.Session) error {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc() metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck // nolint:staticcheck
if query.ID == 0 && len(query.UID) == 0 && (query.Title == nil || (query.FolderID == nil && query.FolderUID == nil)) { if query.ID == 0 && len(query.UID) == 0 && (query.Title == nil || (query.FolderID == nil && query.FolderUID == nil)) {
@ -796,7 +797,7 @@ func (d *dashboardStore) GetDashboardUIDByID(ctx context.Context, query *dashboa
func (d *dashboardStore) GetDashboards(ctx context.Context, query *dashboards.GetDashboardsQuery) ([]*dashboards.Dashboard, error) { func (d *dashboardStore) GetDashboards(ctx context.Context, query *dashboards.GetDashboardsQuery) ([]*dashboards.Dashboard, error) {
var dashboards = make([]*dashboards.Dashboard, 0) var dashboards = make([]*dashboards.Dashboard, 0)
err := d.store.DB().WithDbSession(ctx, func(sess *db.Session) error { err := d.store.ReadReplica().WithDbSession(ctx, func(sess *db.Session) error {
if len(query.DashboardIDs) == 0 && len(query.DashboardUIDs) == 0 { if len(query.DashboardIDs) == 0 && len(query.DashboardUIDs) == 0 {
return star.ErrCommandValidationFailed return star.ErrCommandValidationFailed
} }
@ -823,7 +824,7 @@ func (d *dashboardStore) GetDashboards(ctx context.Context, query *dashboards.Ge
} }
func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.FindPersistedDashboardsQuery) ([]dashboards.DashboardSearchProjection, error) { func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.FindPersistedDashboardsQuery) ([]dashboards.DashboardSearchProjection, error) {
recursiveQueriesAreSupported, err := d.store.DB().RecursiveQueriesAreSupported() recursiveQueriesAreSupported, err := d.store.ReadReplica().RecursiveQueriesAreSupported()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -856,11 +857,11 @@ func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.F
} }
if len(query.Title) > 0 { if len(query.Title) > 0 {
filters = append(filters, searchstore.TitleFilter{Dialect: d.store.DB().GetDialect(), Title: query.Title}) filters = append(filters, searchstore.TitleFilter{Dialect: d.store.ReadReplica().GetDialect(), Title: query.Title})
} }
if len(query.Type) > 0 { if len(query.Type) > 0 {
filters = append(filters, searchstore.TypeFilter{Dialect: d.store.DB().GetDialect(), Type: query.Type}) filters = append(filters, searchstore.TypeFilter{Dialect: d.store.ReadReplica().GetDialect(), Type: query.Type})
} }
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc() metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck // nolint:staticcheck
@ -870,7 +871,7 @@ func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.F
if len(query.FolderUIDs) > 0 { if len(query.FolderUIDs) > 0 {
filters = append(filters, searchstore.FolderUIDFilter{ filters = append(filters, searchstore.FolderUIDFilter{
Dialect: d.store.DB().GetDialect(), Dialect: d.store.ReadReplica().GetDialect(),
OrgID: orgID, OrgID: orgID,
UIDs: query.FolderUIDs, UIDs: query.FolderUIDs,
NestedFoldersEnabled: d.features.IsEnabled(ctx, featuremgmt.FlagNestedFolders), NestedFoldersEnabled: d.features.IsEnabled(ctx, featuremgmt.FlagNestedFolders),
@ -887,7 +888,7 @@ func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.F
filters = append(filters, searchstore.DeletedFilter{Deleted: query.IsDeleted}) filters = append(filters, searchstore.DeletedFilter{Deleted: query.IsDeleted})
var res []dashboards.DashboardSearchProjection var res []dashboards.DashboardSearchProjection
sb := &searchstore.Builder{Dialect: d.store.DB().GetDialect(), Filters: filters, Features: d.features} sb := &searchstore.Builder{Dialect: d.store.ReadReplica().GetDialect(), Filters: filters, Features: d.features}
limit := query.Limit limit := query.Limit
if limit < 1 { if limit < 1 {
@ -901,7 +902,7 @@ func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.F
sql, params := sb.ToSQL(limit, page) sql, params := sb.ToSQL(limit, page)
err = d.store.DB().WithDbSession(ctx, func(sess *db.Session) error { err = d.store.ReadReplica().WithDbSession(ctx, func(sess *db.Session) error {
return sess.SQL(sql, params...).Find(&res) return sess.SQL(sql, params...).Find(&res)
}) })
@ -993,7 +994,7 @@ func (d *dashboardStore) DeleteDashboardsInFolders(
func (d *dashboardStore) GetAllDashboards(ctx context.Context) ([]*dashboards.Dashboard, error) { func (d *dashboardStore) GetAllDashboards(ctx context.Context) ([]*dashboards.Dashboard, error) {
var dashboards = make([]*dashboards.Dashboard, 0) var dashboards = make([]*dashboards.Dashboard, 0)
err := d.store.DB().WithDbSession(ctx, func(session *db.Session) error { err := d.store.ReadReplica().WithDbSession(ctx, func(session *db.Session) error {
err := session.Find(&dashboards) err := session.Find(&dashboards)
return err return err
}) })