From df816a58fe0b1c2519eb1c95b08368265fcb0818 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 23 Aug 2020 14:09:25 -0700 Subject: [PATCH] add tests --- weed/filer2/filechunks2_test.go | 52 +++++++++++++++++++++++++++++++++ weed/filer2/filechunks_test.go | 46 ++++++++++++++++++++++++++--- 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 weed/filer2/filechunks2_test.go diff --git a/weed/filer2/filechunks2_test.go b/weed/filer2/filechunks2_test.go new file mode 100644 index 000000000..b87af6a6c --- /dev/null +++ b/weed/filer2/filechunks2_test.go @@ -0,0 +1,52 @@ +package filer2 + +import ( + "sort" + "testing" + + "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" +) + +func TestCompactFileChunksRealCase(t *testing.T) { + + chunks := []*filer_pb.FileChunk{ + {FileId:"2,512f31f2c0700a", Offset: 0, Size: 25- 0, Mtime: 5320497}, + {FileId:"6,512f2c2e24e9e8", Offset: 868352, Size: 917585- 868352, Mtime: 5320492}, + {FileId:"7,514468dd5954ca", Offset: 884736, Size: 901120- 884736, Mtime: 5325928}, + {FileId:"5,5144463173fe77", Offset: 917504, Size: 2297856- 917504, Mtime: 5325894}, + {FileId:"4,51444c7ab54e2d", Offset: 2301952, Size: 2367488-2301952, Mtime: 5325900}, + {FileId:"4,514450e643ad22", Offset: 2371584, Size: 2420736-2371584, Mtime: 5325904}, + {FileId:"1,51446489116dbb", Offset: 2424832, Size: 2445312-2424832, Mtime: 5325924}, + {FileId:"6,514456a5e9e4d7", Offset: 2449408, Size: 2490368-2449408, Mtime: 5325910}, + {FileId:"3,51444f8d53eebe", Offset: 2494464, Size: 2555904-2494464, Mtime: 5325903}, + {FileId:"4,5144578b097c7e", Offset: 2560000, Size: 2596864-2560000, Mtime: 5325911}, + {FileId:"4,51446563a1d91a", Offset: 2600960, Size: 2617344-2600960, Mtime: 5325925}, + {FileId:"7,514471a77e7b61", Offset: 2621440, Size: 2633728-2621440, Mtime: 5325937}, + {FileId:"3,51445500b6b4ac", Offset: 2637824, Size: 2678784-2637824, Mtime: 5325909}, + {FileId:"2,514476409139dc", Offset: 2682880, Size: 2691072-2682880, Mtime: 5325942}, + {FileId:"1,51446285e52a61", Offset: 2695168, Size: 2715648-2695168, Mtime: 5325922}, + {FileId:"6,51447234faf940", Offset: 2719744, Size: 2730531-2719744, Mtime: 5325938}, + {FileId:"5,514473558a90ed", Offset: 2736128, Size: 2744590-2736128, Mtime: 5325939}, + } + + printChunks("before", chunks) + + compacted, garbage := CompactFileChunks(nil, chunks) + + printChunks("compacted", compacted) + printChunks("garbage", garbage) + +} + +func printChunks(name string, chunks []*filer_pb.FileChunk) { + sort.Slice(chunks, func(i, j int) bool { + if chunks[i].Offset == chunks[j].Offset { + return chunks[i].Mtime < chunks[j].Mtime + } + return chunks[i].Offset < chunks[j].Offset + }) + for _, chunk := range chunks { + glog.V(0).Infof("%s chunk %s [%10d,%10d)", name, chunk.GetFileIdString(), chunk.Offset, chunk.Offset+int64(chunk.Size)) + } +} \ No newline at end of file diff --git a/weed/filer2/filechunks_test.go b/weed/filer2/filechunks_test.go index 70da6e16c..cc1fd1912 100644 --- a/weed/filer2/filechunks_test.go +++ b/weed/filer2/filechunks_test.go @@ -4,6 +4,8 @@ import ( "fmt" "log" "math" + "math/rand" + "strconv" "testing" "github.com/stretchr/testify/assert" @@ -62,6 +64,42 @@ func TestCompactFileChunks2(t *testing.T) { } } +func TestRandomFileChunksCompact(t *testing.T) { + + data := make([]byte, 1024) + + var chunks []*filer_pb.FileChunk + for i := 0; i < 15; i++ { + start, stop := rand.Intn(len(data)), rand.Intn(len(data)) + if start > stop { + start, stop = stop, start + } + if start + 16 < stop { + stop = start + 16 + } + chunk := &filer_pb.FileChunk{ + FileId: strconv.Itoa(i), + Offset: int64(start), + Size: uint64(stop - start), + Mtime: int64(i), + Fid: &filer_pb.FileId{FileKey: uint64(i)}, + } + chunks = append(chunks, chunk) + for x := start; x < stop; x++ { + data[x] = byte(i) + } + } + + visibles, _ := NonOverlappingVisibleIntervals(nil, chunks) + + for _, v := range visibles { + for x := v.start; x < v.stop; x++ { + assert.Equal(t, strconv.Itoa(int(data[x])), v.fileId) + } + } + +} + func TestIntervalMerging(t *testing.T) { testcases := []struct { @@ -142,12 +180,12 @@ func TestIntervalMerging(t *testing.T) { // case 6: same updates { Chunks: []*filer_pb.FileChunk{ - {Offset: 0, Size: 100, FileId: "abc", Mtime: 123}, - {Offset: 0, Size: 100, FileId: "abc", Mtime: 123}, - {Offset: 0, Size: 100, FileId: "abc", Mtime: 123}, + {Offset: 0, Size: 100, FileId: "abc", Fid: &filer_pb.FileId{FileKey: 1}, Mtime: 123}, + {Offset: 0, Size: 100, FileId: "axf", Fid: &filer_pb.FileId{FileKey: 2}, Mtime: 123}, + {Offset: 0, Size: 100, FileId: "xyz", Fid: &filer_pb.FileId{FileKey: 3}, Mtime: 123}, }, Expected: []*VisibleInterval{ - {start: 0, stop: 100, fileId: "abc"}, + {start: 0, stop: 100, fileId: "xyz"}, }, }, // case 7: real updates