Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CockroachDB: Optimize quota manager by using follower reads #2853

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion quota/crdbqm/crdb_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const (

// TODO(jaosorior): Come up with a more optimal solution for CRDB, as this is
// linear and too costly.
countFromUnsequencedTable = "SELECT COUNT(*) FROM Unsequenced"
// Using a follower read here to reduce latency on the query. While this will
// slightly less accurate than a read from the leader, it should be good enough.
countFromUnsequencedTable = "SELECT COUNT(*) FROM Unsequenced AS OF SYSTEM TIME follower_read_timestamp()"
)

// ErrTooManyUnsequencedRows is returned when tokens are requested but Unsequenced has grown
Expand Down
9 changes: 9 additions & 0 deletions quota/crdbqm/crdb_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func TestQuotaManager_GetTokens(t *testing.T) {
testdb.SkipIfNoCockroachDB(t)
ctx := context.Background()

// This allows for CockroachDB to persist the transaction for the record.
// We're mostly just interested in testing that the quota is managed at all,
// but running this in production will need to tolerate that
// quota management won't be exact.
// The 5 second time was gotten from https://www.cockroachlabs.com/docs/stable/follower-reads.html#when-to-use-exact-staleness-reads
const transactionBackoff = 5 * time.Second

db, done, err := testdb.NewTrillianDB(ctx, testdb.DriverCockroachDB)
if err != nil {
t.Fatalf("GetTestDB() returned err = %v", err)
Expand Down Expand Up @@ -103,6 +110,8 @@ func TestQuotaManager_GetTokens(t *testing.T) {
return
}

time.Sleep(transactionBackoff)

qm := &QuotaManager{DB: db, MaxUnsequencedRows: test.maxUnsequencedRows}

err = qm.GetTokens(ctx, test.numTokens, test.specs)
Expand Down