Skip to content

Commit bbd7a55

Browse files
authored
fix(host-cleanup): fix locking doesn't work as expected #6725
Signed-off-by: Alexandr Zaytsev <alexandr.zaytsev@flant.com>
1 parent bb2dfc4 commit bbd7a55

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

Diff for: pkg/host_cleaning/host_lock.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,26 @@ import (
55

66
chart "github.com/werf/common-go/pkg/lock"
77
"github.com/werf/lockgate"
8+
"github.com/werf/logboek"
89
)
910

10-
func withHostLock(ctx context.Context, lockName string, fn func() error) error {
11+
// withHostLockOrNothing executes callback function if "soft" (NonBlocking=true) lock is acquired. Otherwise, does nothing.
12+
func withHostLockOrNothing(ctx context.Context, lockName string, callback func() error) (err error) {
1113
lockOptions := lockgate.AcquireOptions{NonBlocking: true}
12-
return chart.WithHostLock(ctx, lockName, lockOptions, fn)
14+
15+
acquired, lock, err := chart.AcquireHostLock(ctx, lockName, lockOptions)
16+
if err != nil {
17+
return err
18+
}
19+
20+
if !acquired {
21+
logboek.Context(ctx).Warn().LogF("Ignore locked %s\n", lockName)
22+
return nil
23+
}
24+
25+
defer func() {
26+
err = chart.ReleaseHostLock(lock)
27+
}()
28+
29+
return callback()
1330
}

Diff for: pkg/host_cleaning/host_lock_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package host_cleaning
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = Describe("withHostLockOrNothing", func() {
11+
var ctx context.Context
12+
var spy *spyHostLockCallback
13+
BeforeEach(func() {
14+
ctx = context.Background()
15+
spy = &spyHostLockCallback{}
16+
})
17+
It("should call callback function if lock is acquired", func() {
18+
lockName := "test"
19+
err := withHostLockOrNothing(ctx, lockName, spy.Method)
20+
Expect(err).To(Succeed())
21+
Expect(spy.callsCount).To(Equal(1))
22+
})
23+
It("should not call callback function if lock isn't acquired", func() {
24+
lockName := "test"
25+
err := withHostLockOrNothing(ctx, lockName, func() error {
26+
return withHostLockOrNothing(ctx, lockName, spy.Method) // lock is already acquired in parent function
27+
})
28+
Expect(err).To(Succeed())
29+
Expect(spy.callsCount).To(Equal(0))
30+
})
31+
})
32+
33+
type spyHostLockCallback struct {
34+
callsCount int
35+
}
36+
37+
func (s *spyHostLockCallback) Method() error {
38+
s.callsCount++
39+
return nil
40+
}

Diff for: pkg/host_cleaning/local_backend_cleaner.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ func (cleaner *LocalBackendCleaner) doSafeCleanupWerfContainers(ctx context.Cont
570570
continue
571571
}
572572

573-
err := withHostLock(ctx, container_backend.ContainerLockName(containerName), func() error {
573+
err := withHostLockOrNothing(ctx, container_backend.ContainerLockName(containerName), func() error {
574574
err := cleaner.backend.Rm(ctx, container.ID, container_backend.RmOpts{
575575
Force: options.Force,
576576
})
@@ -693,7 +693,7 @@ func (cleaner *LocalBackendCleaner) removeImageByRepoTags(ctx context.Context, o
693693
unRemovedCount++
694694
}
695695
} else {
696-
err := withHostLock(ctx, container_backend.ImageLockName(ref), func() error {
696+
err := withHostLockOrNothing(ctx, container_backend.ImageLockName(ref), func() error {
697697
err := cleaner.backend.Rmi(ctx, ref, container_backend.RmiOpts{
698698
Force: options.Force,
699699
})

Diff for: pkg/host_cleaning/suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
. "github.com/onsi/gomega"
88
)
99

10-
func TestStage(t *testing.T) {
10+
func TestHostCleaning(t *testing.T) {
1111
RegisterFailHandler(Fail)
1212
RunSpecs(t, "Host Cleaning Suite")
1313
}

0 commit comments

Comments
 (0)