unified-storage: Skip query when ListModifiedSince cannot return anything. (#110338)

* Skip query when ListModifiedSince cannot return anything.

* Only save listRV if it's different than sinceRV. This saves a disk access if not needed.

* Add test for ListModifiedSince with same RV.
This commit is contained in:
Peter Štibraný
2025-08-29 14:49:20 +02:00
committed by GitHub
parent e3f5a65372
commit 3a3ba483b1
3 changed files with 44 additions and 10 deletions
+19 -5
View File
@@ -11,7 +11,6 @@ import (
"time"
"github.com/go-sql-driver/mysql"
"github.com/grafana/grafana/pkg/util/sqlite"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
@@ -20,6 +19,8 @@ import (
"google.golang.org/protobuf/proto"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/grafana/grafana/pkg/util/sqlite"
"github.com/grafana/grafana-app-sdk/logging"
"github.com/grafana/grafana/pkg/storage/unified/resource"
@@ -642,21 +643,34 @@ func (b *backend) ListModifiedSince(ctx context.Context, key resource.Namespaced
}
}
rollbackOnDefer := true
defer func() {
if rollbackOnDefer {
if terr := tx.Rollback(); terr != nil {
b.log.Warn("Error rolling back transaction in ListModifiedSince", "error", terr)
}
}
}()
// Fetch latest RV within the transaction
latestRv, err := b.fetchLatestRV(ctx, tx, b.dialect, key.Group, key.Resource)
if err != nil {
terr := tx.Rollback()
if terr != nil {
b.log.Warn("Error rolling back transaction in ListModifiedSince", "error", terr)
}
return 0, func(yield func(*resource.ModifiedResource, error) bool) {
yield(nil, err)
}
}
// If latest RV is the same as request RV, there's nothing to report, and we can avoid running another query.
if latestRv == sinceRv {
return 0, func(yield func(*resource.ModifiedResource, error) bool) { /* nothing to return */ }
}
// since results are sorted by name ASC and rv DESC, we can get away with tracking the last seen
lastSeen := ""
// We will rollback after iteration has finished.
rollbackOnDefer = false
// rollback transaction if iterator not called within 30 seconds
rollbackTimer := time.AfterFunc(30*time.Second, func() {
if err := tx.Rollback(); err != nil && !errors.Is(err, sql.ErrTxDone) {