From 11cde7ed714414bed0cfe9b082352ce599bed47f Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 12 Dec 2018 13:44:06 +0100 Subject: [PATCH] renames main lock function --- pkg/infra/serverlock/serverlock.go | 9 ++++++--- pkg/infra/serverlock/serverlock_integration_test.go | 12 ++++++------ pkg/services/cleanup/cleanup.go | 5 ++--- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pkg/infra/serverlock/serverlock.go b/pkg/infra/serverlock/serverlock.go index 0cdb94ca627..77d2d7173fc 100644 --- a/pkg/infra/serverlock/serverlock.go +++ b/pkg/infra/serverlock/serverlock.go @@ -13,7 +13,10 @@ func init() { registry.RegisterService(&ServerLockService{}) } -// ServerLockService allows servers in HA mode to execute function once over in the group +// DistributedLockService + +// ServerLockService allows servers in HA mode to claim a lock +// and execute an function if the server was granted the lock type ServerLockService struct { SQLStore *sqlstore.SqlStore `inject:""` log log.Logger @@ -25,10 +28,10 @@ func (sl *ServerLockService) Init() error { return nil } -// OncePerServerGroup try to create a lock for this server and only executes the +// LockAndExecute try to create a lock for this server and only executes the // `fn` function when successful. This should not be used at low internal. But services // that needs to be run once every ex 10m. -func (sl *ServerLockService) OncePerServerGroup(ctx context.Context, actionName string, maxInterval time.Duration, fn func()) error { +func (sl *ServerLockService) LockAndExecute(ctx context.Context, actionName string, maxInterval time.Duration, fn func()) error { // gets or creates a lockable row rowLock, err := sl.getOrCreate(ctx, actionName) if err != nil { diff --git a/pkg/infra/serverlock/serverlock_integration_test.go b/pkg/infra/serverlock/serverlock_integration_test.go index 335f540b2e3..8bcd9c2ca25 100644 --- a/pkg/infra/serverlock/serverlock_integration_test.go +++ b/pkg/infra/serverlock/serverlock_integration_test.go @@ -21,19 +21,19 @@ func TestServerLok(t *testing.T) { ctx := context.Background() //this time `fn` should be executed - So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) + So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) //this should not execute `fn` - So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) - So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) - So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) - So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) + So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) + So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) + So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) + So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) // wait 5 second. <-time.After(atInterval * 2) // now `fn` should be executed again - err = sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter) + err = sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter) So(err, ShouldBeNil) So(counter, ShouldEqual, 2) }) diff --git a/pkg/services/cleanup/cleanup.go b/pkg/services/cleanup/cleanup.go index 9338e3afab3..63d829bccec 100644 --- a/pkg/services/cleanup/cleanup.go +++ b/pkg/services/cleanup/cleanup.go @@ -7,9 +7,8 @@ import ( "path" "time" - "github.com/grafana/grafana/pkg/infra/serverlock" - "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/infra/serverlock" "github.com/grafana/grafana/pkg/log" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/registry" @@ -41,7 +40,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error { srv.cleanUpTmpFiles() srv.deleteExpiredSnapshots() srv.deleteExpiredDashboardVersions() - srv.ServerLockService.OncePerServerGroup(ctx, "delete old login attempts", time.Minute*10, func() { + srv.ServerLockService.LockAndExecute(ctx, "delete old login attempts", time.Minute*10, func() { srv.deleteOldLoginAttempts() })