Skip to content

Commit

Permalink
Measure lock acquisition delay on update methods (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
seaneganx authored Jun 17, 2022
1 parent 2f9a84a commit 426d476
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/registry/actions_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package registry
import (
"context"
"sync"
"time"

"github.com/apigee/registry/log"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/internal/storage"
"github.com/apigee/registry/server/registry/internal/storage/models"
Expand Down Expand Up @@ -200,6 +202,7 @@ func (s *RegistryServer) UpdateApi(ctx context.Context, req *rpc.UpdateApiReques
}

if req.GetAllowMissing() {
before := time.Now()
// Prevent a race condition that can occur when two updates are made
// to the same non-existent resource. The db.Get...() call returns
// NotFound for both updates, and after one creates the resource,
Expand All @@ -208,6 +211,7 @@ func (s *RegistryServer) UpdateApi(ctx context.Context, req *rpc.UpdateApiReques
// with improvements closer to the database level.
updateApiMutex.Lock()
defer updateApiMutex.Unlock()
log.Debugf(ctx, "Acquired lock after blocking for %v", time.Since(before))
}

api, err := db.GetApi(ctx, name)
Expand Down
4 changes: 4 additions & 0 deletions server/registry/actions_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package registry
import (
"context"
"sync"
"time"

"github.com/apigee/registry/log"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/internal/storage"
"github.com/apigee/registry/server/registry/internal/storage/models"
Expand Down Expand Up @@ -225,6 +227,7 @@ func (s *RegistryServer) UpdateApiDeployment(ctx context.Context, req *rpc.Updat
}

if req.GetAllowMissing() {
before := time.Now()
// Prevent a race condition that can occur when two updates are made
// to the same non-existent resource. The db.Get...() call returns
// NotFound for both updates, and after one creates the resource,
Expand All @@ -233,6 +236,7 @@ func (s *RegistryServer) UpdateApiDeployment(ctx context.Context, req *rpc.Updat
// with improvements closer to the database level.
updateDeploymentMutex.Lock()
defer updateDeploymentMutex.Unlock()
log.Debugf(ctx, "Acquired lock after blocking for %v", time.Since(before))
}

deployment, err := db.GetDeployment(ctx, name)
Expand Down
4 changes: 4 additions & 0 deletions server/registry/actions_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package registry
import (
"context"
"sync"
"time"

"github.com/apigee/registry/log"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/internal/storage"
"github.com/apigee/registry/server/registry/internal/storage/models"
Expand Down Expand Up @@ -168,6 +170,7 @@ func (s *RegistryServer) UpdateProject(ctx context.Context, req *rpc.UpdateProje
}

if req.GetAllowMissing() {
before := time.Now()
// Prevent a race condition that can occur when two updates are made
// to the same non-existent resource. The db.Get...() call returns
// NotFound for both updates, and after one creates the resource,
Expand All @@ -176,6 +179,7 @@ func (s *RegistryServer) UpdateProject(ctx context.Context, req *rpc.UpdateProje
// with improvements closer to the database level.
updateProjectMutex.Lock()
defer updateProjectMutex.Unlock()
log.Debugf(ctx, "Acquired lock after blocking for %v", time.Since(before))
}

project, err := db.GetProject(ctx, name)
Expand Down
4 changes: 4 additions & 0 deletions server/registry/actions_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"context"
"strings"
"sync"
"time"

"github.com/apigee/registry/log"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/internal/storage"
"github.com/apigee/registry/server/registry/internal/storage/models"
Expand Down Expand Up @@ -278,6 +280,7 @@ func (s *RegistryServer) UpdateApiSpec(ctx context.Context, req *rpc.UpdateApiSp
}

if req.GetAllowMissing() {
before := time.Now()
// Prevent a race condition that can occur when two updates are made
// to the same non-existent resource. The db.Get...() call returns
// NotFound for both updates, and after one creates the resource,
Expand All @@ -286,6 +289,7 @@ func (s *RegistryServer) UpdateApiSpec(ctx context.Context, req *rpc.UpdateApiSp
// with improvements closer to the database level.
updateSpecMutex.Lock()
defer updateSpecMutex.Unlock()
log.Debugf(ctx, "Acquired lock after blocking for %v", time.Since(before))
}

spec, err := db.GetSpec(ctx, name)
Expand Down
4 changes: 4 additions & 0 deletions server/registry/actions_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package registry
import (
"context"
"sync"
"time"

"github.com/apigee/registry/log"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/internal/storage"
"github.com/apigee/registry/server/registry/internal/storage/models"
Expand Down Expand Up @@ -200,6 +202,7 @@ func (s *RegistryServer) UpdateApiVersion(ctx context.Context, req *rpc.UpdateAp
}

if req.GetAllowMissing() {
before := time.Now()
// Prevent a race condition that can occur when two updates are made
// to the same non-existent resource. The db.Get...() call returns
// NotFound for both updates, and after one creates the resource,
Expand All @@ -208,6 +211,7 @@ func (s *RegistryServer) UpdateApiVersion(ctx context.Context, req *rpc.UpdateAp
// with improvements closer to the database level.
updateVersionMutex.Lock()
defer updateVersionMutex.Unlock()
log.Debugf(ctx, "Acquired lock after blocking for %v", time.Since(before))
}

version, err := db.GetVersion(ctx, name)
Expand Down

0 comments on commit 426d476

Please sign in to comment.