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

SA: Improve concurrency robustness of CRL leasing transactions #8030

Merged
merged 6 commits into from
Mar 3, 2025
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
49 changes: 31 additions & 18 deletions sa/sa.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ func (ssa *SQLStorageAuthority) leaseOldestCRLShard(ctx context.Context, req *sa

// Determine which shard index we want to lease.
var shardIdx int
var needToInsert bool
if len(shards) < (int(req.MaxShardIdx + 1 - req.MinShardIdx)) {
// Some expected shards are missing (i.e. never-before-produced), so we
// pick one at random.
Expand All @@ -1107,7 +1106,17 @@ func (ssa *SQLStorageAuthority) leaseOldestCRLShard(ctx context.Context, req *sa
shardIdx = idx
break
}
needToInsert = true

_, err = tx.ExecContext(ctx,
`INSERT INTO crlShards (issuerID, idx, leasedUntil)
VALUES (?, ?, ?)`,
req.IssuerNameID,
shardIdx,
req.Until.AsTime(),
)
if err != nil {
return -1, fmt.Errorf("inserting selected shard: %w", err)
}
} else {
// We got all the shards we expect, so we pick the oldest unleased shard.
var oldest *crlShardModel
Expand All @@ -1125,34 +1134,29 @@ func (ssa *SQLStorageAuthority) leaseOldestCRLShard(ctx context.Context, req *sa
return -1, fmt.Errorf("issuer %d has no unleased shards in range %d-%d", req.IssuerNameID, req.MinShardIdx, req.MaxShardIdx)
}
shardIdx = oldest.Idx
needToInsert = false
}

if needToInsert {
_, err = tx.ExecContext(ctx,
`INSERT INTO crlShards (issuerID, idx, leasedUntil)
VALUES (?, ?, ?)`,
req.IssuerNameID,
shardIdx,
req.Until.AsTime(),
)
if err != nil {
return -1, fmt.Errorf("inserting selected shard: %w", err)
}
} else {
_, err = tx.ExecContext(ctx,
res, err := tx.ExecContext(ctx,
`UPDATE crlShards
SET leasedUntil = ?
WHERE issuerID = ?
AND idx = ?
AND leasedUntil = ?
LIMIT 1`,
req.Until.AsTime(),
req.IssuerNameID,
shardIdx,
oldest.LeasedUntil,
)
if err != nil {
return -1, fmt.Errorf("updating selected shard: %w", err)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return -1, fmt.Errorf("confirming update of selected shard: %w", err)
}
if rowsAffected != 1 {
return -1, errors.New("failed to lease shard")
}
}

return shardIdx, err
Expand Down Expand Up @@ -1208,19 +1212,28 @@ func (ssa *SQLStorageAuthority) leaseSpecificCRLShard(ctx context.Context, req *
return nil, fmt.Errorf("inserting selected shard: %w", err)
}
} else {
_, err = tx.ExecContext(ctx,
res, err := tx.ExecContext(ctx,
`UPDATE crlShards
SET leasedUntil = ?
WHERE issuerID = ?
AND idx = ?
AND leasedUntil = ?
LIMIT 1`,
req.Until.AsTime(),
req.IssuerNameID,
req.MinShardIdx,
shardModel.LeasedUntil,
)
if err != nil {
return nil, fmt.Errorf("updating selected shard: %w", err)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return -1, fmt.Errorf("confirming update of selected shard: %w", err)
}
if rowsAffected != 1 {
return -1, errors.New("failed to lease shard")
}
}

return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion test/integration/crl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestCRLPipeline(t *testing.T) {
resp.Body.Close()
}

func TestTemporalAndExplicitShardingCoexist(t *testing.T) {
func TestCRLTemporalAndExplicitShardingCoexist(t *testing.T) {
db, err := sql.Open("mysql", vars.DBConnSAIntegrationFullPerms)
if err != nil {
t.Fatalf("sql.Open: %s", err)
Expand Down
Loading