CRI: use (snapshotter_id, snapshot_key) to uniquely identify snapshots

Before snapshotter per runtime, CRI only supports a global snapshotter.
So a snapshot can be uniquely identified by `snapshot_key`. With snapshotter
per runtime enabled, there may be multiple snapshotters used by CRI. So only
(snapshotter_id, snapshot_key) can uniquely identify a snapshot.
Also extends CRI/store/snapshot/Store to support multiple snapshotters.

Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
This commit is contained in:
Jiang Liu
2023-10-15 21:06:39 +08:00
parent a7333657af
commit 5ad6f34329
11 changed files with 174 additions and 70 deletions

View File

@@ -23,10 +23,17 @@ import (
snapshot "github.com/containerd/containerd/snapshots"
)
type Key struct {
// Key is the key of the snapshot
Key string
// Snapshotter is the name of the snapshotter managing the snapshot
Snapshotter string
}
// Snapshot contains the information about the snapshot.
type Snapshot struct {
// Key is the key of the snapshot
Key string
Key Key
// Kind is the kind of the snapshot (active, committed, view)
Kind snapshot.Kind
// Size is the size of the snapshot in bytes.
@@ -41,12 +48,12 @@ type Snapshot struct {
// Store stores all snapshots.
type Store struct {
lock sync.RWMutex
snapshots map[string]Snapshot
snapshots map[Key]Snapshot
}
// NewStore creates a snapshot store.
func NewStore() *Store {
return &Store{snapshots: make(map[string]Snapshot)}
return &Store{snapshots: make(map[Key]Snapshot)}
}
// Add a snapshot into the store.
@@ -58,7 +65,7 @@ func (s *Store) Add(snapshot Snapshot) {
// Get returns the snapshot with specified key. Returns errdefs.ErrNotFound if the
// snapshot doesn't exist.
func (s *Store) Get(key string) (Snapshot, error) {
func (s *Store) Get(key Key) (Snapshot, error) {
s.lock.RLock()
defer s.lock.RUnlock()
if sn, ok := s.snapshots[key]; ok {
@@ -79,7 +86,7 @@ func (s *Store) List() []Snapshot {
}
// Delete deletes the snapshot with specified key.
func (s *Store) Delete(key string) {
func (s *Store) Delete(key Key) {
s.lock.Lock()
defer s.lock.Unlock()
delete(s.snapshots, key)

View File

@@ -27,23 +27,35 @@ import (
)
func TestSnapshotStore(t *testing.T) {
snapshots := map[string]Snapshot{
"key1": {
Key: "key1",
key1 := Key{
Key: "key1",
Snapshotter: "snapshotter1",
}
key2 := Key{
Key: "key2",
Snapshotter: "snapshotter1",
}
key3 := Key{
Key: "key1",
Snapshotter: "snapshotter2",
}
snapshots := map[Key]Snapshot{
key1: {
Key: key1,
Kind: snapshot.KindActive,
Size: 10,
Inodes: 100,
Timestamp: time.Now().UnixNano(),
},
"key2": {
Key: "key2",
key2: {
Key: key2,
Kind: snapshot.KindCommitted,
Size: 20,
Inodes: 200,
Timestamp: time.Now().UnixNano(),
},
"key3": {
Key: "key3",
key3: {
Key: key3,
Kind: snapshot.KindView,
Size: 0,
Inodes: 0,
@@ -70,7 +82,19 @@ func TestSnapshotStore(t *testing.T) {
sns := s.List()
assert.Len(sns, 3)
testKey := "key2"
invalidKey := Key{
Key: "key2",
Snapshotter: "snapshotter2",
}
t.Logf("should not delete snapshot with invalid key")
s.Delete(invalidKey)
sns = s.List()
assert.Len(sns, 3)
testKey := Key{
Key: "key2",
Snapshotter: "snapshotter1",
}
t.Logf("should be able to delete snapshot")
s.Delete(testKey)