Skip to content

Commit

Permalink
[dss/RID] GetISA: optional forUpdate flag to allow for early locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Shastick committed Sep 12, 2024
1 parent 97bbbe5 commit 29c0874
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pkg/rid/application/isa.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (a *app) GetISA(ctx context.Context, id dssmodels.ID) (*ridmodels.Identific
if err != nil {
return nil, stacktrace.Propagate(err, "Unable to interact with store")
}
return repo.GetISA(ctx, id)
return repo.GetISA(ctx, id, false)
}

// SearchISAs for ISA within the volume bounds.
Expand All @@ -64,7 +64,7 @@ func (a *app) DeleteISA(ctx context.Context, id dssmodels.ID, owner dssmodels.Ow
)
// The following will automatically retry TXN retry errors.
err := a.Store.Transact(ctx, func(repo repos.Repository) error {
old, err := repo.GetISA(ctx, id)
old, err := repo.GetISA(ctx, id, true)
switch {
case err != nil:
return stacktrace.Propagate(err, "Error getting ISA")
Expand Down Expand Up @@ -106,7 +106,7 @@ func (a *app) InsertISA(ctx context.Context, isa *ridmodels.IdentificationServic
// The following will automatically retry TXN retry errors.
err := a.Store.Transact(ctx, func(repo repos.Repository) error {
// ensure it doesn't exist yet
old, err := repo.GetISA(ctx, isa.ID)
old, err := repo.GetISA(ctx, isa.ID, false)
if err != nil {
return stacktrace.Propagate(err, "Error getting ISA")
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (a *app) UpdateISA(ctx context.Context, isa *ridmodels.IdentificationServic
err := a.Store.Transact(ctx, func(repo repos.Repository) error {
var err error

old, err := repo.GetISA(ctx, isa.ID)
old, err := repo.GetISA(ctx, isa.ID, true)
switch {
case err != nil:
return stacktrace.Propagate(err, "Error getting ISA")
Expand Down
2 changes: 1 addition & 1 deletion pkg/rid/application/isa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type isaStore struct {
isas map[dssmodels.ID]*ridmodels.IdentificationServiceArea
}

func (store *isaStore) GetISA(ctx context.Context, id dssmodels.ID) (*ridmodels.IdentificationServiceArea, error) {
func (store *isaStore) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error) {
if isa, ok := store.isas[id]; ok {
return isa, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/rid/repos/isa.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// ISA is an interface to a storage layer for the ISA entity
type ISA interface {
// Returns nil, nil if not found
GetISA(ctx context.Context, id dssmodels.ID) (*ridmodels.IdentificationServiceArea, error)
GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error)

// DeleteISA deletes the IdentificationServiceArea identified by "id" and owned by "owner".
// Returns the delete IdentificationServiceArea and all Subscriptions affected by the delete.
Expand Down
5 changes: 3 additions & 2 deletions pkg/rid/store/cockroach/identification_service_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ func (c *isaRepo) processOne(ctx context.Context, query string, args ...interfac

// GetISA returns the isa identified by "id".
// Returns nil, nil if not found
func (c *isaRepo) GetISA(ctx context.Context, id dssmodels.ID) (*ridmodels.IdentificationServiceArea, error) {
func (c *isaRepo) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error) {
var query = fmt.Sprintf(`
SELECT %s FROM
identification_service_areas
WHERE
id = $1`, isaFields)
id = $1
%s`, isaFields, dssql.ForUpdate(forUpdate))
uid, err := id.PgUUID()
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to convert id to PgUUID")
Expand Down
5 changes: 3 additions & 2 deletions pkg/rid/store/cockroach/identification_service_area_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ func (c *isaRepoV3) processOne(ctx context.Context, query string, args ...interf

// GetISA returns the isa identified by "id".
// Returns nil, nil if not found
func (c *isaRepoV3) GetISA(ctx context.Context, id dssmodels.ID) (*ridmodels.IdentificationServiceArea, error) {
func (c *isaRepoV3) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error) {
var query = fmt.Sprintf(`
SELECT %s FROM
identification_service_areas
WHERE
id = $1`, isaFieldsV3)
id = $1
%s`, isaFieldsV3, dssql.ForUpdate(forUpdate))
uid, err := id.PgUUID()
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to convert id to PgUUID")
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ func CellUnionToCellIdsWithValidation(cu s2.CellUnion) ([]int64, error) {
}
return pgCids, nil
}

func ForUpdate(forUpdate bool) string {
if forUpdate {
return "FOR UPDATE"
}
return ""
}

0 comments on commit 29c0874

Please sign in to comment.