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

[dss/RID] GetISA: optional forUpdate flag to allow for early locking #1117

Merged
merged 1 commit into from
Sep 20, 2024
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
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
4 changes: 2 additions & 2 deletions pkg/rid/store/cockroach/garbage_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ func TestDeleteExpiredISAs(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, saOut)

ret, err := repo.GetISA(ctx, serviceArea.ID)
ret, err := repo.GetISA(ctx, serviceArea.ID, false)
require.NoError(t, err)
require.NotNil(t, ret)

gc := NewGarbageCollector(repo, writer)
err = gc.DeleteRIDExpiredRecords(ctx)
require.NoError(t, err)

ret, err = repo.GetISA(ctx, serviceArea.ID)
ret, err = repo.GetISA(ctx, serviceArea.ID, false)
require.NoError(t, err)
require.Nil(t, ret)
}
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
6 changes: 3 additions & 3 deletions pkg/rid/store/cockroach/identification_service_area_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestStoreExpiredISA(t *testing.T) {
require.NoError(t, err)
require.Len(t, serviceAreas, 1)

ret, err := repo.GetISA(ctx, serviceArea.ID)
ret, err := repo.GetISA(ctx, serviceArea.ID, false)
require.NoError(t, err)
require.NotNil(t, ret)

Expand All @@ -199,7 +199,7 @@ func TestStoreExpiredISA(t *testing.T) {
require.Len(t, serviceAreas, 0)

// A get should work even if it is expired.
ret, err = repo.GetISA(ctx, serviceArea.ID)
ret, err = repo.GetISA(ctx, serviceArea.ID, false)
require.NoError(t, err)
require.NotNil(t, ret)
}
Expand All @@ -222,7 +222,7 @@ func TestStoreDeleteISAs(t *testing.T) {

// Delete the ISA.
// Ensure a fresh Get, then delete still updates the sub indexes
isa, err = repo.GetISA(ctx, isa.ID)
isa, err = repo.GetISA(ctx, isa.ID, false)
require.NoError(t, err)

serviceAreaOut, err := repo.DeleteISA(ctx, isa)
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
2 changes: 1 addition & 1 deletion pkg/rid/store/cockroach/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestTxnRetrier(t *testing.T) {
repo, err := store.Interact(ctx)
require.NoError(t, err)

isa, err := repo.GetISA(ctx, serviceArea.ID)
isa, err := repo.GetISA(ctx, serviceArea.ID, false)
require.NoError(t, err)
require.NotNil(t, isa)

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 ""
}
Loading