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

Commit e9a73a1

Browse files
committed
add truncindex
fix #222 Signed-off-by: yanxuean <[email protected]>
1 parent 7d25e7a commit e9a73a1

File tree

17 files changed

+1415
-83
lines changed

17 files changed

+1415
-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"
@@ -97,7 +98,7 @@ func LoadContainer() (Container, error) {
9798
type Store struct {
9899
lock sync.RWMutex
99100
containers map[string]Container
100-
// TODO(random-liu): Add trunc index.
101+
idIndex *truncindex.TruncIndex
101102
}
102103

103104
// LoadStore loads containers from runtime.
@@ -106,7 +107,10 @@ func LoadStore() *Store { return nil }
106107

107108
// NewStore creates a container store.
108109
func NewStore() *Store {
109-
return &Store{containers: make(map[string]Container)}
110+
return &Store{
111+
containers: make(map[string]Container),
112+
idIndex: truncindex.NewTruncIndex([]string{}),
113+
}
110114
}
111115

112116
// Add a container into the store. Returns store.ErrAlreadyExist if the
@@ -117,6 +121,10 @@ func (s *Store) Add(c Container) error {
117121
if _, ok := s.containers[c.ID]; ok {
118122
return store.ErrAlreadyExist
119123
}
124+
125+
if err := s.idIndex.Add(c.ID); err != nil {
126+
return err
127+
}
120128
s.containers[c.ID] = c
121129
return nil
122130
}
@@ -126,6 +134,13 @@ func (s *Store) Add(c Container) error {
126134
func (s *Store) Get(id string) (Container, error) {
127135
s.lock.RLock()
128136
defer s.lock.RUnlock()
137+
id, err := s.idIndex.Get(id)
138+
if err != nil {
139+
if err == truncindex.ErrNotExist {
140+
err = store.ErrNotExist
141+
}
142+
return Container{}, err
143+
}
129144
if c, ok := s.containers[id]; ok {
130145
return c, nil
131146
}
@@ -147,5 +162,10 @@ func (s *Store) List() []Container {
147162
func (s *Store) Delete(id string) {
148163
s.lock.Lock()
149164
defer s.lock.Unlock()
165+
id, err := s.idIndex.Get(id)
166+
if err != nil {
167+
return
168+
}
169+
_ = s.idIndex.Delete(id)
150170
delete(s.containers, id)
151171
}

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",
@@ -66,6 +65,18 @@ func TestContainerStore(t *testing.T) {
6665
},
6766
ImageRef: "TestImage-3",
6867
},
68+
"4abcd": {
69+
ID: "4abcd",
70+
Name: "Container-4abcd",
71+
SandboxID: "Sandbox-4abcd",
72+
Config: &runtime.ContainerConfig{
73+
Metadata: &runtime.ContainerMetadata{
74+
Name: "TestPod-4abcd",
75+
Attempt: 1,
76+
},
77+
},
78+
ImageRef: "TestImage-4abcd",
79+
},
6980
}
7081
statuses := map[string]Status{
7182
"1": {
@@ -96,12 +107,22 @@ func TestContainerStore(t *testing.T) {
96107
Message: "TestMessage-3",
97108
Removing: true,
98109
},
110+
"4abcd": {
111+
Pid: 4,
112+
CreatedAt: time.Now().UnixNano(),
113+
StartedAt: time.Now().UnixNano(),
114+
FinishedAt: time.Now().UnixNano(),
115+
ExitCode: 4,
116+
Reason: "TestReason-4abcd",
117+
Message: "TestMessage-4abcd",
118+
Removing: true,
119+
},
99120
}
100121
assert := assertlib.New(t)
101122
containers := map[string]Container{}
102-
for _, id := range ids {
123+
for id, v := range metadatas {
103124
container, err := NewContainer(
104-
metadatas[id],
125+
v,
105126
WithFakeStatus(statuses[id]),
106127
)
107128
assert.NoError(err)
@@ -116,29 +137,35 @@ func TestContainerStore(t *testing.T) {
116137
}
117138

118139
t.Logf("should be able to get container")
140+
genTruncIndex := func(normalName string) string { return normalName[:(len(normalName)+1)/2] }
119141
for id, c := range containers {
120-
got, err := s.Get(id)
142+
got, err := s.Get(genTruncIndex(id))
121143
assert.NoError(err)
122144
assert.Equal(c, got)
123145
}
124146

125147
t.Logf("should be able to list containers")
126148
cs := s.List()
127-
assert.Len(cs, 3)
149+
assert.Len(cs, len(containers))
150+
151+
cntrNum := len(containers)
152+
for testID, v := range containers {
153+
truncID := genTruncIndex(testID)
128154

129-
testID := "2"
130-
t.Logf("add should return already exists error for duplicated container")
131-
assert.Equal(store.ErrAlreadyExist, s.Add(containers[testID]))
155+
t.Logf("add should return already exists error for duplicated container")
156+
assert.Equal(store.ErrAlreadyExist, s.Add(v))
132157

133-
t.Logf("should be able to delete container")
134-
s.Delete(testID)
135-
cs = s.List()
136-
assert.Len(cs, 2)
158+
t.Logf("should be able to delete container")
159+
s.Delete(truncID)
160+
cntrNum--
161+
cs = s.List()
162+
assert.Len(cs, cntrNum)
137163

138-
t.Logf("get should return not exist error after deletion")
139-
c, err := s.Get(testID)
140-
assert.Equal(Container{}, c)
141-
assert.Equal(store.ErrNotExist, err)
164+
t.Logf("get should return not exist error after deletion")
165+
c, err := s.Get(truncID)
166+
assert.Equal(Container{}, c)
167+
assert.Equal(store.ErrNotExist, err)
168+
}
142169
}
143170

144171
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)