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

Commit 204f8e6

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

File tree

17 files changed

+1414
-83
lines changed

17 files changed

+1414
-83
lines changed

pkg/server/container_attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (c *criContainerdService) Attach(ctx context.Context, r *runtime.AttachRequ
3838

3939
cntr, err := c.containerStore.Get(r.GetContainerId())
4040
if err != nil {
41-
return nil, fmt.Errorf("failed to find container in store: %v", err)
41+
return nil, fmt.Errorf("failed to find container %q in store: %v", r.GetContainerId(), err)
4242
}
4343
state := cntr.Status.Get().State()
4444
if state != runtime.ContainerState_CONTAINER_RUNNING {
@@ -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/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: 22 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,10 @@ func (s *Store) Add(c Container) error {
112116
if _, ok := s.containers[c.ID]; ok {
113117
return store.ErrAlreadyExist
114118
}
119+
120+
if err := s.idIndex.Add(c.ID); err != nil {
121+
return err
122+
}
115123
s.containers[c.ID] = c
116124
return nil
117125
}
@@ -121,6 +129,13 @@ func (s *Store) Add(c Container) error {
121129
func (s *Store) Get(id string) (Container, error) {
122130
s.lock.RLock()
123131
defer s.lock.RUnlock()
132+
id, err := s.idIndex.Get(id)
133+
if err != nil {
134+
if err == truncindex.ErrNotExist {
135+
err = store.ErrNotExist
136+
}
137+
return Container{}, err
138+
}
124139
if c, ok := s.containers[id]; ok {
125140
return c, nil
126141
}
@@ -142,5 +157,10 @@ func (s *Store) List() []Container {
142157
func (s *Store) Delete(id string) {
143158
s.lock.Lock()
144159
defer s.lock.Unlock()
160+
id, err := s.idIndex.Get(id)
161+
if err != nil {
162+
return
163+
}
164+
_ = s.idIndex.Delete(id)
145165
delete(s.containers, id)
146166
}

pkg/store/container/container_test.go

Lines changed: 43 additions & 16 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",
@@ -69,6 +68,18 @@ func TestContainerStore(t *testing.T) {
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": {
@@ -99,12 +110,22 @@ func TestContainerStore(t *testing.T) {
99110
Message: "TestMessage-3",
100111
Removing: true,
101112
},
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",
121+
Removing: true,
122+
},
102123
}
103124
assert := assertlib.New(t)
104125
containers := map[string]Container{}
105-
for _, id := range ids {
126+
for id, v := range metadatas {
106127
container, err := NewContainer(
107-
metadatas[id],
128+
v,
108129
WithFakeStatus(statuses[id]),
109130
)
110131
assert.NoError(err)
@@ -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: 33 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,51 @@ 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+
72+
if _, err := s.idIndex.Get(img.ID); err != nil {
73+
if err != truncindex.ErrNotExist {
74+
return err
75+
}
76+
if err := s.idIndex.Add(img.ID); err != nil {
77+
return err
78+
}
79+
}
80+
6781
i, ok := s.images[img.ID]
6882
if !ok {
6983
// If the image doesn't exist, add it.
7084
s.images[img.ID] = img
71-
return
85+
return nil
7286
}
7387
// Or else, merge the repo tags/digests.
7488
i.RepoTags = mergeStringSlices(i.RepoTags, img.RepoTags)
7589
i.RepoDigests = mergeStringSlices(i.RepoDigests, img.RepoDigests)
7690
s.images[img.ID] = i
91+
return nil
7792
}
7893

7994
// Get returns the image with specified id. Returns store.ErrNotExist if the
8095
// image doesn't exist.
8196
func (s *Store) Get(id string) (Image, error) {
8297
s.lock.RLock()
8398
defer s.lock.RUnlock()
99+
id, err := s.idIndex.Get(id)
100+
if err != nil {
101+
if err == truncindex.ErrNotExist {
102+
err = store.ErrNotExist
103+
}
104+
return Image{}, err
105+
}
84106
if i, ok := s.images[id]; ok {
85107
return i, nil
86108
}
@@ -102,6 +124,11 @@ func (s *Store) List() []Image {
102124
func (s *Store) Delete(id string) {
103125
s.lock.Lock()
104126
defer s.lock.Unlock()
127+
id, err := s.idIndex.Get(id)
128+
if err != nil {
129+
return
130+
}
131+
_ = s.idIndex.Delete(id)
105132
delete(s.images, id)
106133
}
107134

0 commit comments

Comments
 (0)