This commit is contained in:
Georges Chaudy 2024-07-08 10:19:01 +02:00
parent 26a2f947e8
commit 396979bcf6
No known key found for this signature in database
GPG Key ID: 0EE887FFCA1DB6EF
2 changed files with 4 additions and 95 deletions

View File

@ -10,15 +10,12 @@ import (
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
"github.com/grafana/grafana/pkg/services/store/entity/sqlstash"
"github.com/grafana/grafana/pkg/storage/unified/resource"
"github.com/grafana/grafana/pkg/storage/unified/sql/db"
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
@ -26,34 +23,6 @@ import (
const trace_prefix = "sql.resource."
// Package-level errors.
var (
ErrNotImplementedYet = errors.New("not implemented yet (sqlnext)")
)
func ProvideSQLResourceServer(db db.ResourceDBInterface, tracer tracing.Tracer) (resource.ResourceServer, error) {
ctx, cancel := context.WithCancel(context.Background())
store := &backend{
db: db,
log: log.New("sql-resource-server"),
ctx: ctx,
cancel: cancel,
tracer: tracer,
}
if err := prometheus.Register(sqlstash.NewStorageMetrics()); err != nil {
return nil, err
}
return resource.NewResourceServer(resource.ResourceServerOptions{
Tracer: tracer,
Backend: store,
Diagnostics: store,
Lifecycle: store,
})
}
type backendOptions struct {
DB db.ResourceDBInterface
Tracer trace.Tracer
@ -201,13 +170,7 @@ func (b *backend) create(ctx context.Context, event resource.WriteEvent) (int64,
return fmt.Errorf("insert into resource history: %w", err)
}
// // 3. Rebuild the whole folder tree structure if we're creating a folder
// TODO: We need a better way to do this that does not require a custom table
// if newEntity.Group == folder.GROUP && newEntity.Resource == folder.RESOURCE {
// if err = s.updateFolderTree(ctx, tx, key.Namespace); err != nil {
// return fmt.Errorf("rebuild folder tree structure: %w", err)
// }
// }
// 3. TODO: Rebuild the whole folder tree structure if we're creating a folder
// 4. Atomically increpement resource version for this kind
rv, err := resourceVersionAtomicInc(ctx, tx, b.sqlDialect, event.Key)
@ -270,13 +233,7 @@ func (b *backend) update(ctx context.Context, event resource.WriteEvent) (int64,
return fmt.Errorf("insert into resource history: %w", err)
}
// // 3. Rebuild the whole folder tree structure if we're creating a folder
// TODO: We need a better way to do this that does not require a custom table
// if newEntity.Group == folder.GROUP && newEntity.Resource == folder.RESOURCE {
// if err = s.updateFolderTree(ctx, tx, key.Namespace); err != nil {
// return fmt.Errorf("rebuild folder tree structure: %w", err)
// }
// }
// 3. TODO: Rebuild the whole folder tree structure if we're creating a folder
// 4. Atomically increpement resource version for this kind
rv, err := resourceVersionAtomicInc(ctx, tx, b.sqlDialect, event.Key)
@ -339,13 +296,7 @@ func (b *backend) delete(ctx context.Context, event resource.WriteEvent) (int64,
return fmt.Errorf("insert into resource history: %w", err)
}
// // 3. Rebuild the whole folder tree structure if we're creating a folder
// TODO: We need a better way to do this that does not require a custom table
// if newEntity.Group == folder.GROUP && newEntity.Resource == folder.RESOURCE {
// if err = s.updateFolderTree(ctx, tx, key.Namespace); err != nil {
// return fmt.Errorf("rebuild folder tree structure: %w", err)
// }
// }
// 3. TODO: Rebuild the whole folder tree structure if we're creating a folder
// 4. Atomically increpement resource version for this kind
newVersion, err = resourceVersionAtomicInc(ctx, tx, b.sqlDialect, event.Key)

View File

@ -4,7 +4,6 @@ import (
"database/sql"
"embed"
"fmt"
"strings"
"text/template"
"github.com/grafana/grafana/pkg/storage/unified/resource"
@ -16,12 +15,7 @@ var (
//go:embed data/*.sql
sqlTemplatesFS embed.FS
// all templates
helpers = template.FuncMap{
"listSep": helperListSep,
"join": helperJoin,
}
sqlTemplates = template.Must(template.New("sql").Funcs(helpers).ParseFS(sqlTemplatesFS, `data/*.sql`))
sqlTemplates = template.Must(template.New("sql").ParseFS(sqlTemplatesFS, `data/*.sql`))
)
func mustTemplate(filename string) *template.Template {
@ -121,14 +115,6 @@ func (r sqlResourceHistoryPollRequest) Validate() error {
// sqlResourceReadRequest can be used to retrieve a row fromthe "resource" tables.
type key struct {
resource.ReadResponse
}
type readRequest struct {
resource.ReadResponse
}
type readResponse struct {
resource.ReadResponse
}
@ -188,31 +174,3 @@ type sqlResourceVersionRequest struct {
func (r sqlResourceVersionRequest) Validate() error {
return nil // TODO
}
// Template helpers.
// helperListSep is a helper that helps writing simpler loops in SQL templates.
// Example usage:
//
// {{ $comma := listSep ", " }}
// {{ range .Values }}
// {{/* here we put "-" on each end to remove extra white space */}}
// {{- call $comma -}}
// {{ .Value }}
// {{ end }}
func helperListSep(sep string) func() string {
var addSep bool
return func() string {
if addSep {
return sep
}
addSep = true
return ""
}
}
func helperJoin(sep string, elems ...string) string {
return strings.Join(elems, sep)
}