-
Notifications
You must be signed in to change notification settings - Fork 257
/
stats.go
102 lines (86 loc) · 2.93 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package spanner
import (
"context"
"fmt"
"math/rand"
"time"
"cloud.google.com/go/spanner"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/codes"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/datastore"
)
var (
queryRelationshipEstimate = fmt.Sprintf("SELECT SUM(%s) FROM %s", colCount, tableCounters)
rng = rand.NewSource(time.Now().UnixNano())
)
func (sd spannerDatastore) Statistics(ctx context.Context) (datastore.Stats, error) {
var uniqueID string
if err := sd.client.Single().Read(
context.Background(),
tableMetadata,
spanner.AllKeys(),
[]string{colUniqueID},
).Do(func(r *spanner.Row) error {
return r.Columns(&uniqueID)
}); err != nil {
return datastore.Stats{}, fmt.Errorf("unable to read unique ID: %w", err)
}
iter := sd.client.Single().Read(
ctx,
tableNamespace,
spanner.AllKeys(),
[]string{colNamespaceConfig, colNamespaceTS},
)
defer iter.Stop()
allNamespaces, err := readAllNamespaces(iter, trace.SpanFromContext(ctx))
if err != nil {
return datastore.Stats{}, fmt.Errorf("unable to read namespaces: %w", err)
}
var estimate spanner.NullInt64
if err := sd.client.Single().Query(ctx, spanner.Statement{SQL: queryRelationshipEstimate}).Do(func(r *spanner.Row) error {
return r.Columns(&estimate)
}); err != nil {
return datastore.Stats{}, fmt.Errorf("unable to read row counts: %w", err)
}
return datastore.Stats{
UniqueID: uniqueID,
ObjectTypeStatistics: datastore.ComputeObjectTypeStats(allNamespaces),
EstimatedRelationshipCount: uint64(estimate.Int64),
}, nil
}
func updateCounter(ctx context.Context, rwt *spanner.ReadWriteTransaction, change int64) error {
newValue := change
counterID := make([]byte, 2)
// nolint:gosec
// G404 use of non cryptographically secure random number generator is not concern here,
// as this is only used to randomly distributed the counters across multiple rows and reduce write contention
_, err := rand.New(rng).Read(counterID)
if err != nil {
return fmt.Errorf("unable to select random counter: %w", err)
}
counterRow, err := rwt.ReadRow(ctx, tableCounters, spanner.Key{counterID}, []string{colCount})
if err != nil {
if spanner.ErrCode(err) != codes.NotFound {
return fmt.Errorf("unable to read counter value: %w", err)
}
// In this branch we leave newValue alone because the counter doesn't exist
} else {
var currentValue int64
if err := counterRow.Columns(¤tValue); err != nil {
return fmt.Errorf("unable to decode counter value: %w", err)
}
newValue += currentValue
}
log.Ctx(ctx).Trace().
Bytes("counterID", counterID).
Int64("newValue", newValue).
Int64("change", change).
Msg("updating counter")
if err := rwt.BufferWrite([]*spanner.Mutation{
spanner.InsertOrUpdate(tableCounters, []string{colID, colCount}, []any{counterID, newValue}),
}); err != nil {
return fmt.Errorf("unable to buffer update to counter: %w", err)
}
return nil
}