-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yan Song <[email protected]>
- Loading branch information
Showing
12 changed files
with
601 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package backend | ||
|
||
import "context" | ||
|
||
const ( | ||
DefaultFileChunkSize = 1024 * 1024 * 1 // 1 MB | ||
DefaultThrottleFileSize = 1024 * 1024 * 2 // 2 MB | ||
) | ||
|
||
type Backend struct { | ||
Type string `json:"type"` | ||
Config map[string]string `json:"config"` | ||
} | ||
|
||
type Result struct { | ||
Chunks []Chunk | ||
Files []string | ||
Backend Backend | ||
} | ||
|
||
type File struct { | ||
RelativePath string | ||
Size int64 | ||
} | ||
|
||
// Handler is the interface for backend handler. | ||
type Handler interface { | ||
// Backend returns the backend information. | ||
Backend(ctx context.Context) (*Backend, error) | ||
// Handle handles the file and returns the object information. | ||
Handle(ctx context.Context, file File) ([]Chunk, error) | ||
} | ||
|
||
type Chunk interface { | ||
ObjectID() uint32 | ||
ObjectContent() interface{} | ||
ObjectOffset() uint64 | ||
} | ||
|
||
// SplitObjectOffsets splits the total size into object offsets | ||
// with the specified chunk size. | ||
func SplitObjectOffsets(totalSize, chunkSize int64) []uint64 { | ||
objectOffsets := []uint64{} | ||
if chunkSize <= 0 { | ||
return objectOffsets | ||
} | ||
|
||
chunkN := totalSize / chunkSize | ||
|
||
for i := int64(0); i < chunkN; i++ { | ||
objectOffsets = append(objectOffsets, uint64(i*chunkSize)) | ||
} | ||
|
||
if totalSize%chunkSize > 0 { | ||
objectOffsets = append(objectOffsets, uint64(chunkN*chunkSize)) | ||
} | ||
|
||
return objectOffsets | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package backend | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLayout(t *testing.T) { | ||
require.Equal(t, fmt.Sprintf("%d", 4096), fmt.Sprintf("%d", unsafe.Sizeof(Header{}))) | ||
require.Equal(t, fmt.Sprintf("%d", 256), fmt.Sprintf("%d", unsafe.Sizeof(ChunkMeta{}))) | ||
require.Equal(t, fmt.Sprintf("%d", 256), fmt.Sprintf("%d", unsafe.Sizeof(ObjectMeta{}))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package backend | ||
|
||
const MetaMagic uint32 = 0x0AF5_E1E2 | ||
const MetaVersion uint32 = 0x0000_0001 | ||
|
||
// Layout | ||
// | ||
// header: magic | version | chunk_meta_offset | object_meta_offset | ||
// chunks: chunk_meta | chunk | chunk | ... | ||
// objects: object_meta | [object_offsets] | object | object | ... | ||
|
||
// 4096 bytes | ||
type Header struct { | ||
Magic uint32 | ||
Version uint32 | ||
|
||
ChunkMetaOffset uint32 | ||
ObjectMetaOffset uint32 | ||
|
||
Reserved2 [4080]byte | ||
} | ||
|
||
// 256 bytes | ||
type ChunkMeta struct { | ||
EntryCount uint32 | ||
EntrySize uint32 | ||
|
||
Reserved [248]byte | ||
} | ||
|
||
// 256 bytes | ||
type ObjectMeta struct { | ||
EntryCount uint32 | ||
// = 0 means indeterminate entry size, and len(object_offsets) > 0. | ||
// > 0 means fixed entry size, and len(object_offsets) == 0. | ||
EntrySize uint32 | ||
|
||
Reserved [248]byte | ||
} | ||
|
||
// 8 bytes | ||
type ChunkOndisk struct { | ||
ObjectIndex uint32 | ||
Reserved [4]byte | ||
ObjectOffset uint64 | ||
} | ||
|
||
// 4 bytes | ||
type ObjectOffset uint32 | ||
|
||
// Size depends on different external backend implementations | ||
type ObjectOndisk struct { | ||
EntrySize uint32 | ||
EncodedData []byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package local | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/containerd/nydus-snapshotter/pkg/external/backend" | ||
) | ||
|
||
type object struct { | ||
Path string `msgpack:"p"` | ||
} | ||
|
||
type chunk struct { | ||
objectID uint32 | ||
objectContent object | ||
objectOffset uint64 | ||
} | ||
|
||
func (c *chunk) ObjectID() uint32 { | ||
return c.objectID | ||
} | ||
|
||
func (c *chunk) ObjectContent() interface{} { | ||
return c.objectContent | ||
} | ||
|
||
func (c *chunk) ObjectOffset() uint64 { | ||
return c.objectOffset | ||
} | ||
|
||
type Handler struct { | ||
root string | ||
objectID uint32 | ||
} | ||
|
||
func NewHandler(root string) *Handler { | ||
return &Handler{ | ||
root: root, | ||
objectID: 0, | ||
} | ||
} | ||
|
||
func (handler *Handler) Handle(_ context.Context, file backend.File) ([]backend.Chunk, error) { | ||
chunks := []backend.Chunk{} | ||
objectOffsets := backend.SplitObjectOffsets(file.Size, backend.DefaultFileChunkSize) | ||
|
||
for _, objectOffset := range objectOffsets { | ||
chunks = append(chunks, &chunk{ | ||
objectID: handler.objectID, | ||
objectContent: object{ | ||
Path: file.RelativePath, | ||
}, | ||
objectOffset: objectOffset, | ||
}) | ||
} | ||
handler.objectID++ | ||
|
||
return chunks, nil | ||
} | ||
|
||
func (handler *Handler) Backend(_ context.Context) (*backend.Backend, error) { | ||
return &backend.Backend{ | ||
Type: "local", | ||
Config: map[string]string{ | ||
"root": handler.root, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package local | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/vmihailenco/msgpack/v5" | ||
) | ||
|
||
type Object2 struct { | ||
Kind string `msgpack:"k"` | ||
Number uint64 `msgpack:"n"` | ||
Path string `msgpack:"p"` | ||
} | ||
|
||
func TestSerializeCompatibility(t *testing.T) { | ||
object1 := object{ | ||
Path: "test1", | ||
} | ||
object2 := Object2{} | ||
buf, err := msgpack.Marshal(&object1) | ||
require.NoError(t, err) | ||
err = msgpack.Unmarshal(buf, &object2) | ||
require.NoError(t, err) | ||
require.Equal(t, object1.Path, object2.Path) | ||
|
||
object1 = object{} | ||
object2 = Object2{ | ||
Kind: "test2", | ||
Number: 123, | ||
Path: "test1", | ||
} | ||
object3 := Object2{} | ||
buf, err = msgpack.Marshal(&object2) | ||
require.NoError(t, err) | ||
err = msgpack.Unmarshal(buf, &object1) | ||
require.NoError(t, err) | ||
require.Equal(t, object2.Path, object1.Path) | ||
|
||
err = msgpack.Unmarshal(buf, &object3) | ||
require.NoError(t, err) | ||
require.Equal(t, object2, object3) | ||
} |
Oops, something went wrong.