X Tutup
Skip to content

Commit c95d71c

Browse files
author
Kazuyoshi Kato
committed
content: include the staleness of the lock when tryLock() fails
When multiple clients are pulling the same image, we may have this lock error. Short-lived locks are probably fine, but long-lived locks may indicate that containerd has some issues. Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
1 parent 9561d93 commit c95d71c

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

content/local/locks.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,33 @@ package local
1818

1919
import (
2020
"sync"
21+
"time"
2122

2223
"github.com/containerd/containerd/errdefs"
2324
"github.com/pkg/errors"
2425
)
2526

2627
// Handles locking references
2728

29+
type lock struct {
30+
since time.Time
31+
}
32+
2833
var (
2934
// locks lets us lock in process
30-
locks = map[string]struct{}{}
35+
locks = make(map[string]*lock)
3136
locksMu sync.Mutex
3237
)
3338

3439
func tryLock(ref string) error {
3540
locksMu.Lock()
3641
defer locksMu.Unlock()
3742

38-
if _, ok := locks[ref]; ok {
39-
return errors.Wrapf(errdefs.ErrUnavailable, "ref %s locked", ref)
43+
if v, ok := locks[ref]; ok {
44+
return errors.Wrapf(errdefs.ErrUnavailable, "ref %s locked since %s", ref, v.since)
4045
}
4146

42-
locks[ref] = struct{}{}
47+
locks[ref] = &lock{time.Now()}
4348
return nil
4449
}
4550

content/local/locks_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package local
18+
19+
import (
20+
"testing"
21+
22+
"gotest.tools/v3/assert"
23+
)
24+
25+
func TestTryLock(t *testing.T) {
26+
err := tryLock("testref")
27+
assert.NilError(t, err)
28+
defer unlock("testref")
29+
30+
err = tryLock("testref")
31+
assert.ErrorContains(t, err, "ref testref locked since ")
32+
}

0 commit comments

Comments
 (0)
X Tutup