mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
Merge pull request #557 from seaweedfs/feat/oss-layout-read
layout: resolve an image root -> segment -> chunk
This commit is contained in:
commit
725fecbe6a
3 files changed
+387
-1
No files matched your search
@@ -341,7 +341,10 @@ func DecodeRoot(b []byte) (Root, error) {
|
||||
if r.err != nil {
|
||||
return Root{}, r.err
|
||||
}
|
||||
again, _ := out.Encode()
|
||||
again, err := out.Encode()
|
||||
if err != nil {
|
||||
return Root{}, err
|
||||
}
|
||||
if !bytes.Equal(again, b) {
|
||||
return Root{}, fmt.Errorf("%w: root bytes are not canonical", ErrMalformed)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
)
|
||||
|
||||
// ErrMissing reports an object a root reaches that is not in the store: a
|
||||
// root, a segment or a chunk. It is never read as zeros.
|
||||
var ErrMissing = errors.New("layout: object missing")
|
||||
|
||||
// ErrCorrupt reports an object whose bytes do not match its name.
|
||||
var ErrCorrupt = errors.New("layout: object corrupt")
|
||||
|
||||
// Fetch reads the whole object at p. A missing object is (nil, false, nil);
|
||||
// any other failure is an error.
|
||||
type Fetch func(ctx context.Context, p string) (data []byte, found bool, err error)
|
||||
|
||||
// Reader resolves images of one volume: its roots and segments are read
|
||||
// from <base>/<volume>/, each chunk from the pool of the volume its root
|
||||
// entry names.
|
||||
type Reader struct {
|
||||
base, vol string
|
||||
fetch Fetch
|
||||
}
|
||||
|
||||
// NewReader returns a Reader of vol under base.
|
||||
func NewReader(base, vol string, fetch Fetch) (*Reader, error) {
|
||||
if err := checkOwner(vol); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fetch == nil {
|
||||
return nil, errors.New("layout: reader needs a fetch")
|
||||
}
|
||||
return &Reader{base: base, vol: vol, fetch: fetch}, nil
|
||||
}
|
||||
|
||||
// get fetches the object at rel under volume vol and checks its bytes hash
|
||||
// to want.
|
||||
func (r *Reader) get(ctx context.Context, vol, rel string, want Hash) ([]byte, error) {
|
||||
p := path.Join(r.base, vol, rel)
|
||||
b, ok, err := r.fetch(ctx, p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("layout: read %s: %w", p, err)
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: %s", ErrMissing, p)
|
||||
}
|
||||
if got := Sum(b); got != want {
|
||||
return nil, fmt.Errorf("%w: %s hashes to %s", ErrCorrupt, p, got)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Image is one root of the volume, opened.
|
||||
type Image struct {
|
||||
r *Reader
|
||||
root Root
|
||||
}
|
||||
|
||||
// Open reads and checks the root rn.
|
||||
func (r *Reader) Open(ctx context.Context, rn RootName) (*Image, error) {
|
||||
b, err := r.get(ctx, r.vol, rn.Path(), rn.Hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root, err := DecodeRoot(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: root %s: %v", ErrCorrupt, rn, err)
|
||||
}
|
||||
return &Image{r: r, root: root}, nil
|
||||
}
|
||||
|
||||
// Segment reads the segment of shard; ok is false when the root names no
|
||||
// segment for it (every index of the shard reads as zeros).
|
||||
func (img *Image) Segment(ctx context.Context, shard uint64) (seg Segment, ok bool, err error) {
|
||||
n, ok := img.root.Segments[shard]
|
||||
if !ok {
|
||||
return Segment{}, false, nil
|
||||
}
|
||||
b, err := img.r.get(ctx, img.r.vol, n.Path(), n.Hash)
|
||||
if err != nil {
|
||||
return Segment{}, false, err
|
||||
}
|
||||
seg, err = DecodeSegment(b)
|
||||
if err != nil {
|
||||
return Segment{}, false, fmt.Errorf("%w: segment %s: %v", ErrCorrupt, n, err)
|
||||
}
|
||||
if seg.Shard != shard {
|
||||
return Segment{}, false, fmt.Errorf("%w: segment %s holds shard %d", ErrCorrupt, n, seg.Shard)
|
||||
}
|
||||
return seg, true, nil
|
||||
}
|
||||
|
||||
// Chunk is a resolved chunk: zeros, or the bytes of Ref.
|
||||
type Chunk struct {
|
||||
Zero bool
|
||||
Ref Ref
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// Resolve finds the entry of index: root -> segment -> ref. An index the
|
||||
// root does not name is zeros; a segment the root names but the store lacks
|
||||
// is ErrMissing.
|
||||
func (img *Image) Resolve(ctx context.Context, index uint64) (ref Ref, zero bool, err error) {
|
||||
seg, ok, err := img.Segment(ctx, Shard(index))
|
||||
if err != nil {
|
||||
return Ref{}, false, err
|
||||
}
|
||||
if !ok {
|
||||
return Ref{}, true, nil
|
||||
}
|
||||
ref, ok = seg.Chunks[index]
|
||||
if !ok {
|
||||
return Ref{}, true, nil
|
||||
}
|
||||
return ref, false, nil
|
||||
}
|
||||
|
||||
// Chunk resolves index and reads its bytes from the pool of the volume
|
||||
// that owns it. A chunk the root names but the owner's pool lacks is
|
||||
// ErrMissing.
|
||||
func (img *Image) Chunk(ctx context.Context, index uint64) (Chunk, error) {
|
||||
ref, zero, err := img.Resolve(ctx, index)
|
||||
if err != nil {
|
||||
return Chunk{}, err
|
||||
}
|
||||
if zero {
|
||||
return Chunk{Zero: true}, nil
|
||||
}
|
||||
b, err := img.r.get(ctx, ref.Owner, ref.Chunk.Path(), ref.Chunk.Hash)
|
||||
if err != nil {
|
||||
return Chunk{}, err
|
||||
}
|
||||
return Chunk{Ref: ref, Data: b}, nil
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// objStore is a fake store of whole objects by full path.
|
||||
type objStore struct {
|
||||
obj map[string][]byte
|
||||
fail string // a path whose fetch fails
|
||||
asked []string
|
||||
}
|
||||
|
||||
func (s *objStore) fetch(_ context.Context, p string) ([]byte, bool, error) {
|
||||
s.asked = append(s.asked, p)
|
||||
if p == s.fail {
|
||||
return nil, false, errors.New("filer unavailable")
|
||||
}
|
||||
b, ok := s.obj[p]
|
||||
return b, ok, nil
|
||||
}
|
||||
|
||||
const base = "/swblock/chunks"
|
||||
|
||||
// readFixture: volume "t" (a restore target) whose root names chunk 3
|
||||
// ("own") in its own pool, chunk 10002 ("src data") in the pool of "src",
|
||||
// and nothing else; every object is written.
|
||||
func readFixture(t *testing.T) (*objStore, RootName, map[uint64]Ref) {
|
||||
t.Helper()
|
||||
s := &objStore{obj: map[string][]byte{}}
|
||||
put := func(vol, rel string, b []byte) { s.obj[path.Join(base, vol, rel)] = b }
|
||||
own := NameChunk(3, []byte("own"))
|
||||
foreign := NameChunk(10002, []byte("src data"))
|
||||
put("t", own.Path(), []byte("own"))
|
||||
put("src", foreign.Path(), []byte("src data"))
|
||||
m := map[uint64]Ref{3: {Owner: "t", Chunk: own}, 10002: {Owner: "src", Chunk: foreign}}
|
||||
root, segs, err := Build(m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for n, b := range segs {
|
||||
put("t", n.Path(), b)
|
||||
}
|
||||
rn, rb, err := NameRoot(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
put("t", rn.Path(), rb)
|
||||
return s, rn, m
|
||||
}
|
||||
|
||||
func newReader(t *testing.T, s *objStore) *Reader {
|
||||
t.Helper()
|
||||
r, err := NewReader(base, "t", s.fetch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func TestRead_OwnChunk(t *testing.T) {
|
||||
s, rn, _ := readFixture(t)
|
||||
r := newReader(t, s)
|
||||
img, err := r.Open(context.Background(), rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := img.Chunk(context.Background(), 3)
|
||||
if err != nil || c.Zero || !bytes.Equal(c.Data, []byte("own")) || c.Ref.Owner != "t" {
|
||||
t.Fatalf("chunk 3: %+v %v", c, err)
|
||||
}
|
||||
}
|
||||
|
||||
// An index the root does not name reads as zeros: in a shard with a
|
||||
// segment, and in a shard with none. Zeros never touch the pool.
|
||||
func TestRead_AbsentIndexIsZeros(t *testing.T) {
|
||||
s, rn, _ := readFixture(t)
|
||||
r := newReader(t, s)
|
||||
img, err := r.Open(context.Background(), rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, idx := range []uint64{4, 9999, 10001, 50000} {
|
||||
s.asked = nil
|
||||
c, err := img.Chunk(context.Background(), idx)
|
||||
if err != nil || !c.Zero || c.Data != nil {
|
||||
t.Fatalf("index %d: %+v %v; want zeros", idx, c, err)
|
||||
}
|
||||
for _, p := range s.asked {
|
||||
if strings.Contains(p, "/"+PoolDir+"/") {
|
||||
t.Fatalf("index %d read the pool (%s) for a zero chunk", idx, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A foreign name resolves in the source volume's pool, never in the
|
||||
// reading volume's.
|
||||
func TestRead_ForeignChunkFromSourcePool(t *testing.T) {
|
||||
s, rn, m := readFixture(t)
|
||||
// A same-named object in t's own pool must not be what is read.
|
||||
s.obj[path.Join(base, "t", m[10002].Chunk.Path())] = []byte("decoy")
|
||||
r := newReader(t, s)
|
||||
img, err := r.Open(context.Background(), rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.asked = nil
|
||||
c, err := img.Chunk(context.Background(), 10002)
|
||||
if err != nil || c.Zero || !bytes.Equal(c.Data, []byte("src data")) || c.Ref.Owner != "src" {
|
||||
t.Fatalf("chunk 10002: %+v %v", c, err)
|
||||
}
|
||||
want := path.Join(base, "src", m[10002].Chunk.Path())
|
||||
if got := s.asked[len(s.asked)-1]; got != want {
|
||||
t.Fatalf("read %s; want the source pool path %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A missing segment or chunk is an error, never zeros; so is a missing
|
||||
// root.
|
||||
func TestRead_MissingObjectIsAnErrorNeverZeros(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Run("root", func(t *testing.T) {
|
||||
s, rn, _ := readFixture(t)
|
||||
delete(s.obj, path.Join(base, "t", rn.Path()))
|
||||
if _, err := newReader(t, s).Open(ctx, rn); !errors.Is(err, ErrMissing) {
|
||||
t.Fatalf("open of a missing root: %v; want ErrMissing", err)
|
||||
}
|
||||
})
|
||||
t.Run("segment", func(t *testing.T) {
|
||||
s, rn, _ := readFixture(t)
|
||||
img, err := newReader(t, s).Open(ctx, rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delete(s.obj, path.Join(base, "t", img.root.Segments[1].Path()))
|
||||
for _, idx := range []uint64{10002, 10003} { // named, and not named, in that shard
|
||||
c, err := img.Chunk(ctx, idx)
|
||||
if !errors.Is(err, ErrMissing) || c.Zero {
|
||||
t.Fatalf("index %d with its segment missing: %+v %v; want ErrMissing", idx, c, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("own chunk", func(t *testing.T) {
|
||||
s, rn, m := readFixture(t)
|
||||
delete(s.obj, path.Join(base, "t", m[3].Chunk.Path()))
|
||||
img, err := newReader(t, s).Open(ctx, rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c, err := img.Chunk(ctx, 3); !errors.Is(err, ErrMissing) || c.Zero {
|
||||
t.Fatalf("missing own chunk: %+v %v; want ErrMissing", c, err)
|
||||
}
|
||||
})
|
||||
t.Run("foreign chunk", func(t *testing.T) {
|
||||
s, rn, m := readFixture(t)
|
||||
delete(s.obj, path.Join(base, "src", m[10002].Chunk.Path()))
|
||||
img, err := newReader(t, s).Open(ctx, rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c, err := img.Chunk(ctx, 10002); !errors.Is(err, ErrMissing) || c.Zero {
|
||||
t.Fatalf("missing foreign chunk: %+v %v; want ErrMissing", c, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A failed fetch is an error and not ErrMissing; bytes that do not match
|
||||
// their name are ErrCorrupt.
|
||||
func TestRead_FetchFailureAndCorruption(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, rn, m := readFixture(t)
|
||||
r := newReader(t, s)
|
||||
img, err := r.Open(ctx, rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.fail = path.Join(base, "t", m[3].Chunk.Path())
|
||||
if _, err := img.Chunk(ctx, 3); err == nil || errors.Is(err, ErrMissing) {
|
||||
t.Fatalf("failed fetch: %v; want an error that is not ErrMissing", err)
|
||||
}
|
||||
s.fail = ""
|
||||
for name, p := range map[string]string{
|
||||
"chunk": path.Join(base, "t", m[3].Chunk.Path()),
|
||||
"segment": path.Join(base, "t", img.root.Segments[0].Path()),
|
||||
} {
|
||||
s2, rn2, _ := readFixture(t)
|
||||
s2.obj[p] = append(append([]byte(nil), s2.obj[p]...), 'x')
|
||||
img2, err := newReader(t, s2).Open(ctx, rn2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := img2.Chunk(ctx, 3); !errors.Is(err, ErrCorrupt) {
|
||||
t.Errorf("%s altered: %v; want ErrCorrupt", name, err)
|
||||
}
|
||||
}
|
||||
s3, rn3, _ := readFixture(t)
|
||||
rp := path.Join(base, "t", rn3.Path())
|
||||
s3.obj[rp] = append(append([]byte(nil), s3.obj[rp]...), 0)
|
||||
if _, err := newReader(t, s3).Open(ctx, rn3); !errors.Is(err, ErrCorrupt) {
|
||||
t.Errorf("root altered: %v; want ErrCorrupt", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A segment stored under one shard's name but holding another shard is
|
||||
// refused (its bytes match its hash, so only the shard check catches it).
|
||||
func TestRead_SegmentOfAnotherShardRefused(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := &objStore{obj: map[string][]byte{}}
|
||||
seg := Segment{Shard: 2, Chunks: map[uint64]Ref{20000: {Owner: "t", Chunk: NameChunk(20000, []byte("z"))}}}
|
||||
sn, sb, err := NameSegment(seg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lie := SegName{Shard: 0, Hash: sn.Hash}
|
||||
s.obj[path.Join(base, "t", lie.Path())] = sb
|
||||
root := Root{Segments: map[uint64]SegName{0: lie}}
|
||||
rn, rb, err := NameRoot(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.obj[path.Join(base, "t", rn.Path())] = rb
|
||||
img, err := newReader(t, s).Open(ctx, rn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := img.Chunk(ctx, 5); !errors.Is(err, ErrCorrupt) {
|
||||
t.Fatalf("segment of shard 2 under shard 0: %v; want ErrCorrupt", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewReader_RefusesBadInput(t *testing.T) {
|
||||
f := func(context.Context, string) ([]byte, bool, error) { return nil, false, nil }
|
||||
if _, err := NewReader(base, "a/b", f); err == nil {
|
||||
t.Error("volume with a slash accepted")
|
||||
}
|
||||
if _, err := NewReader(base, "t", nil); err == nil {
|
||||
t.Error("nil fetch accepted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user