Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 67d5ae6

Browse files
committed
add truncindex
fix #222 Signed-off-by: yanxuean <[email protected]>
1 parent 0e6e593 commit 67d5ae6

File tree

18 files changed

+1441
-120
lines changed

18 files changed

+1441
-120
lines changed

pkg/server/container_attach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (c *criContainerdService) attachContainer(ctx context.Context, id string, s
5252
// Get container from our container store.
5353
cntr, err := c.containerStore.Get(id)
5454
if err != nil {
55-
return fmt.Errorf("failed to find container in store: %v", err)
55+
return fmt.Errorf("failed to find container %q in store: %v", id, err)
5656
}
5757
id = cntr.ID
5858

pkg/server/container_exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
func (c *criContainerdService) Exec(ctx context.Context, r *runtime.ExecRequest) (*runtime.ExecResponse, error) {
2828
cntr, err := c.containerStore.Get(r.GetContainerId())
2929
if err != nil {
30-
return nil, fmt.Errorf("failed to find container in store: %v", err)
30+
return nil, fmt.Errorf("failed to find container %q in store: %v", r.GetContainerId(), err)
3131
}
3232
state := cntr.Status.Get().State()
3333
if state != runtime.ContainerState_CONTAINER_RUNNING {

pkg/server/container_execsync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (c *criContainerdService) execInContainer(ctx context.Context, id string, o
7878
// Get container from our container store.
7979
cntr, err := c.containerStore.Get(id)
8080
if err != nil {
81-
return nil, fmt.Errorf("failed to find container in store: %v", err)
81+
return nil, fmt.Errorf("failed to find container %q in store: %v", id, err)
8282
}
8383
id = cntr.ID
8484

pkg/server/image_pull.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma
142142
img.RepoTags = []string{repoTag}
143143
}
144144

145-
c.imageStore.Add(img)
145+
if err := c.imageStore.Add(img); err != nil {
146+
return nil, fmt.Errorf("failed to add image %q into store: %v", img.ID, err)
147+
}
146148

147149
// NOTE(random-liu): the actual state in containerd is the source of truth, even we maintain
148150
// in-memory image store, it's only for in-memory indexing. The image could be removed

pkg/server/restart.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ func (c *criContainerdService) recover(ctx context.Context) error {
101101
return fmt.Errorf("failed to load images: %v", err)
102102
}
103103
for _, image := range images {
104-
c.imageStore.Add(image)
105104
glog.V(4).Infof("Loaded image %+v", image)
105+
if err := c.imageStore.Add(image); err != nil {
106+
return fmt.Errorf("failed to add image %q to store: %v", image.ID, err)
107+
}
106108
}
107109

108110
// It's possible that containerd containers are deleted unexpectedly. In that case,

pkg/server/sandbox_portforward.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *criContainerdService) PortForward(ctx context.Context, r *runtime.PortF
3535
// TODO(random-liu): Run a socat container inside the sandbox to do portforward.
3636
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
3737
if err != nil {
38-
return nil, fmt.Errorf("failed to find sandbox: %v", err)
38+
return nil, fmt.Errorf("failed to find sandbox %q: %v", r.GetPodSandboxId(), err)
3939
}
4040

4141
t, err := sandbox.Container.Task(ctx, nil)
@@ -59,7 +59,7 @@ func (c *criContainerdService) PortForward(ctx context.Context, r *runtime.PortF
5959
func (c *criContainerdService) portForward(id string, port int32, stream io.ReadWriteCloser) error {
6060
s, err := c.sandboxStore.Get(id)
6161
if err != nil {
62-
return fmt.Errorf("failed to find sandbox in store: %v", err)
62+
return fmt.Errorf("failed to find sandbox %q in store: %v", id, err)
6363
}
6464
t, err := s.Container.Task(context.Background(), nil)
6565
if err != nil {

pkg/store/container/container.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sync"
2121

2222
"github.com/containerd/containerd"
23+
"github.com/docker/docker/pkg/truncindex"
2324

2425
cio "github.com/kubernetes-incubator/cri-containerd/pkg/server/io"
2526
"github.com/kubernetes-incubator/cri-containerd/pkg/store"
@@ -92,7 +93,7 @@ func (c *Container) Delete() error {
9293
type Store struct {
9394
lock sync.RWMutex
9495
containers map[string]Container
95-
// TODO(random-liu): Add trunc index.
96+
idIndex *truncindex.TruncIndex
9697
}
9798

9899
// LoadStore loads containers from runtime.
@@ -101,7 +102,10 @@ func LoadStore() *Store { return nil }
101102

102103
// NewStore creates a container store.
103104
func NewStore() *Store {
104-
return &Store{containers: make(map[string]Container)}
105+
return &Store{
106+
containers: make(map[string]Container),
107+
idIndex: truncindex.NewTruncIndex([]string{}),
108+
}
105109
}
106110

107111
// Add a container into the store. Returns store.ErrAlreadyExist if the
@@ -112,6 +116,9 @@ func (s *Store) Add(c Container) error {
112116
if _, ok := s.containers[c.ID]; ok {
113117
return store.ErrAlreadyExist
114118
}
119+
if err := s.idIndex.Add(c.ID); err != nil {
120+
return err
121+
}
115122
s.containers[c.ID] = c
116123
return nil
117124
}
@@ -121,6 +128,13 @@ func (s *Store) Add(c Container) error {
121128
func (s *Store) Get(id string) (Container, error) {
122129
s.lock.RLock()
123130
defer s.lock.RUnlock()
131+
id, err := s.idIndex.Get(id)
132+
if err != nil {
133+
if err == truncindex.ErrNotExist {
134+
err = store.ErrNotExist
135+
}
136+
return Container{}, err
137+
}
124138
if c, ok := s.containers[id]; ok {
125139
return c, nil
126140
}
@@ -142,5 +156,7 @@ func (s *Store) List() []Container {
142156
func (s *Store) Delete(id string) {
143157
s.lock.Lock()
144158
defer s.lock.Unlock()
159+
id, _ = s.idIndex.Get(id) // nolint: errcheck
160+
_ = s.idIndex.Delete(id)
145161
delete(s.containers, id)
146162
}

pkg/store/container/container_test.go

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
)
2929

3030
func TestContainerStore(t *testing.T) {
31-
ids := []string{"1", "2", "3"}
3231
metadatas := map[string]Metadata{
3332
"1": {
3433
ID: "1",
@@ -43,32 +42,44 @@ func TestContainerStore(t *testing.T) {
4342
ImageRef: "TestImage-1",
4443
LogPath: "/test/log/path/1",
4544
},
46-
"2": {
47-
ID: "2",
48-
Name: "Container-2",
49-
SandboxID: "Sandbox-2",
45+
"2abcd": {
46+
ID: "2abcd",
47+
Name: "Container-2abcd",
48+
SandboxID: "Sandbox-2abcd",
5049
Config: &runtime.ContainerConfig{
5150
Metadata: &runtime.ContainerMetadata{
52-
Name: "TestPod-2",
51+
Name: "TestPod-2abcd",
5352
Attempt: 2,
5453
},
5554
},
5655
ImageRef: "TestImage-2",
5756
LogPath: "/test/log/path/2",
5857
},
59-
"3": {
60-
ID: "3",
61-
Name: "Container-3",
62-
SandboxID: "Sandbox-3",
58+
"4a333": {
59+
ID: "4a333",
60+
Name: "Container-4a333",
61+
SandboxID: "Sandbox-4a333",
6362
Config: &runtime.ContainerConfig{
6463
Metadata: &runtime.ContainerMetadata{
65-
Name: "TestPod-3",
64+
Name: "TestPod-4a333",
6665
Attempt: 3,
6766
},
6867
},
6968
ImageRef: "TestImage-3",
7069
LogPath: "/test/log/path/3",
7170
},
71+
"4abcd": {
72+
ID: "4abcd",
73+
Name: "Container-4abcd",
74+
SandboxID: "Sandbox-4abcd",
75+
Config: &runtime.ContainerConfig{
76+
Metadata: &runtime.ContainerMetadata{
77+
Name: "TestPod-4abcd",
78+
Attempt: 1,
79+
},
80+
},
81+
ImageRef: "TestImage-4abcd",
82+
},
7283
}
7384
statuses := map[string]Status{
7485
"1": {
@@ -80,29 +91,39 @@ func TestContainerStore(t *testing.T) {
8091
Reason: "TestReason-1",
8192
Message: "TestMessage-1",
8293
},
83-
"2": {
94+
"2abcd": {
8495
Pid: 2,
8596
CreatedAt: time.Now().UnixNano(),
8697
StartedAt: time.Now().UnixNano(),
8798
FinishedAt: time.Now().UnixNano(),
8899
ExitCode: 2,
89-
Reason: "TestReason-2",
90-
Message: "TestMessage-2",
100+
Reason: "TestReason-2abcd",
101+
Message: "TestMessage-2abcd",
91102
},
92-
"3": {
103+
"4a333": {
93104
Pid: 3,
94105
CreatedAt: time.Now().UnixNano(),
95106
StartedAt: time.Now().UnixNano(),
96107
FinishedAt: time.Now().UnixNano(),
97108
ExitCode: 3,
98-
Reason: "TestReason-3",
99-
Message: "TestMessage-3",
109+
Reason: "TestReason-4a333",
110+
Message: "TestMessage-4a333",
111+
Removing: true,
112+
},
113+
"4abcd": {
114+
Pid: 4,
115+
CreatedAt: time.Now().UnixNano(),
116+
StartedAt: time.Now().UnixNano(),
117+
FinishedAt: time.Now().UnixNano(),
118+
ExitCode: 4,
119+
Reason: "TestReason-4abcd",
120+
Message: "TestMessage-4abcd",
100121
Removing: true,
101122
},
102123
}
103124
assert := assertlib.New(t)
104125
containers := map[string]Container{}
105-
for _, id := range ids {
126+
for id := range metadatas {
106127
container, err := NewContainer(
107128
metadatas[id],
108129
WithFakeStatus(statuses[id]),
@@ -119,29 +140,35 @@ func TestContainerStore(t *testing.T) {
119140
}
120141

121142
t.Logf("should be able to get container")
143+
genTruncIndex := func(normalName string) string { return normalName[:(len(normalName)+1)/2] }
122144
for id, c := range containers {
123-
got, err := s.Get(id)
145+
got, err := s.Get(genTruncIndex(id))
124146
assert.NoError(err)
125147
assert.Equal(c, got)
126148
}
127149

128150
t.Logf("should be able to list containers")
129151
cs := s.List()
130-
assert.Len(cs, 3)
152+
assert.Len(cs, len(containers))
153+
154+
cntrNum := len(containers)
155+
for testID, v := range containers {
156+
truncID := genTruncIndex(testID)
131157

132-
testID := "2"
133-
t.Logf("add should return already exists error for duplicated container")
134-
assert.Equal(store.ErrAlreadyExist, s.Add(containers[testID]))
158+
t.Logf("add should return already exists error for duplicated container")
159+
assert.Equal(store.ErrAlreadyExist, s.Add(v))
135160

136-
t.Logf("should be able to delete container")
137-
s.Delete(testID)
138-
cs = s.List()
139-
assert.Len(cs, 2)
161+
t.Logf("should be able to delete container")
162+
s.Delete(truncID)
163+
cntrNum--
164+
cs = s.List()
165+
assert.Len(cs, cntrNum)
140166

141-
t.Logf("get should return not exist error after deletion")
142-
c, err := s.Get(testID)
143-
assert.Equal(Container{}, c)
144-
assert.Equal(store.ErrNotExist, err)
167+
t.Logf("get should return not exist error after deletion")
168+
c, err := s.Get(truncID)
169+
assert.Equal(Container{}, c)
170+
assert.Equal(store.ErrNotExist, err)
171+
}
145172
}
146173

147174
func TestWithContainerIO(t *testing.T) {

pkg/store/image/image.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sync"
2121

2222
"github.com/containerd/containerd"
23+
"github.com/docker/docker/pkg/truncindex"
2324
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2425

2526
"github.com/kubernetes-incubator/cri-containerd/pkg/store"
@@ -46,9 +47,9 @@ type Image struct {
4647

4748
// Store stores all images.
4849
type Store struct {
49-
lock sync.RWMutex
50-
images map[string]Image
51-
// TODO(random-liu): Add trunc index.
50+
lock sync.RWMutex
51+
images map[string]Image
52+
idIndex *truncindex.TruncIndex
5253
}
5354

5455
// LoadStore loads images from runtime.
@@ -57,30 +58,50 @@ func LoadStore() *Store { return nil }
5758

5859
// NewStore creates an image store.
5960
func NewStore() *Store {
60-
return &Store{images: make(map[string]Image)}
61+
return &Store{
62+
images: make(map[string]Image),
63+
idIndex: truncindex.NewTruncIndex([]string{}),
64+
}
6165
}
6266

6367
// Add an image into the store.
64-
func (s *Store) Add(img Image) {
68+
func (s *Store) Add(img Image) error {
6569
s.lock.Lock()
6670
defer s.lock.Unlock()
71+
if _, err := s.idIndex.Get(img.ID); err != nil {
72+
if err != truncindex.ErrNotExist {
73+
return err
74+
}
75+
if err := s.idIndex.Add(img.ID); err != nil {
76+
return err
77+
}
78+
}
79+
6780
i, ok := s.images[img.ID]
6881
if !ok {
6982
// If the image doesn't exist, add it.
7083
s.images[img.ID] = img
71-
return
84+
return nil
7285
}
7386
// Or else, merge the repo tags/digests.
7487
i.RepoTags = mergeStringSlices(i.RepoTags, img.RepoTags)
7588
i.RepoDigests = mergeStringSlices(i.RepoDigests, img.RepoDigests)
7689
s.images[img.ID] = i
90+
return nil
7791
}
7892

7993
// Get returns the image with specified id. Returns store.ErrNotExist if the
8094
// image doesn't exist.
8195
func (s *Store) Get(id string) (Image, error) {
8296
s.lock.RLock()
8397
defer s.lock.RUnlock()
98+
id, err := s.idIndex.Get(id)
99+
if err != nil {
100+
if err == truncindex.ErrNotExist {
101+
err = store.ErrNotExist
102+
}
103+
return Image{}, err
104+
}
84105
if i, ok := s.images[id]; ok {
85106
return i, nil
86107
}
@@ -102,6 +123,8 @@ func (s *Store) List() []Image {
102123
func (s *Store) Delete(id string) {
103124
s.lock.Lock()
104125
defer s.lock.Unlock()
126+
id, _ = s.idIndex.Get(id) // nolint: errcheck
127+
_ = s.idIndex.Delete(id)
105128
delete(s.images, id)
106129
}
107130

0 commit comments

Comments
 (0)