You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package filer2
  2. import (
  3. "testing"
  4. "log"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. )
  7. func TestCompactFileChunks(t *testing.T) {
  8. chunks := []*filer_pb.FileChunk{
  9. {Offset:10, Size:100, FileId:"abc", Mtime:50},
  10. {Offset:100, Size:100, FileId:"def", Mtime:100},
  11. {Offset:200, Size:100, FileId:"ghi", Mtime:200},
  12. {Offset:110, Size:200, FileId:"jkl", Mtime:300},
  13. }
  14. compacted, garbarge := CompactFileChunks(chunks)
  15. log.Printf("Compacted: %+v", compacted)
  16. log.Printf("Garbage : %+v", garbarge)
  17. if len(compacted) != 3 {
  18. t.Fatalf("unexpected compacted: %d", len(compacted))
  19. }
  20. if len(garbarge) != 1 {
  21. t.Fatalf("unexpected garbarge: %d", len(garbarge))
  22. }
  23. }
  24. func TestIntervalMerging(t *testing.T) {
  25. testcases := []struct {
  26. Chunks []*filer_pb.FileChunk
  27. Expected []*visibleInterval
  28. }{
  29. // case 0: normal
  30. {
  31. Chunks: []*filer_pb.FileChunk{
  32. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  33. {Offset: 100, Size: 100, FileId: "asdf", Mtime: 134},
  34. {Offset: 200, Size: 100, FileId: "fsad", Mtime: 353},
  35. },
  36. Expected: []*visibleInterval{
  37. {start: 0, stop: 100, fileId: "abc"},
  38. {start: 100, stop: 200, fileId: "asdf"},
  39. {start: 200, stop: 300, fileId: "fsad"},
  40. },
  41. },
  42. // case 1: updates overwrite full chunks
  43. {
  44. Chunks: []*filer_pb.FileChunk{
  45. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  46. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  47. },
  48. Expected: []*visibleInterval{
  49. {start: 0, stop: 200, fileId: "asdf"},
  50. },
  51. },
  52. // case 2: updates overwrite part of previous chunks
  53. {
  54. Chunks: []*filer_pb.FileChunk{
  55. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  56. {Offset: 0, Size: 50, FileId: "asdf", Mtime: 134},
  57. },
  58. Expected: []*visibleInterval{
  59. {start: 0, stop: 50, fileId: "asdf"},
  60. {start: 50, stop: 100, fileId: "abc"},
  61. },
  62. },
  63. // case 3: updates overwrite full chunks
  64. {
  65. Chunks: []*filer_pb.FileChunk{
  66. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  67. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  68. {Offset: 50, Size: 250, FileId: "xxxx", Mtime: 154},
  69. },
  70. Expected: []*visibleInterval{
  71. {start: 0, stop: 50, fileId: "asdf"},
  72. {start: 50, stop: 300, fileId: "xxxx"},
  73. },
  74. },
  75. // case 4: updates far away from prev chunks
  76. {
  77. Chunks: []*filer_pb.FileChunk{
  78. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  79. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  80. {Offset: 250, Size: 250, FileId: "xxxx", Mtime: 154},
  81. },
  82. Expected: []*visibleInterval{
  83. {start: 0, stop: 200, fileId: "asdf"},
  84. {start: 250, stop: 500, fileId: "xxxx"},
  85. },
  86. },
  87. // case 5: updates overwrite full chunks
  88. {
  89. Chunks: []*filer_pb.FileChunk{
  90. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  91. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 184},
  92. {Offset: 70, Size: 150, FileId: "abc", Mtime: 143},
  93. {Offset: 80, Size: 100, FileId: "xxxx", Mtime: 134},
  94. },
  95. Expected: []*visibleInterval{
  96. {start: 0, stop: 200, fileId: "asdf"},
  97. {start: 200, stop: 220, fileId: "abc"},
  98. },
  99. },
  100. // case 6: same updates
  101. {
  102. Chunks: []*filer_pb.FileChunk{
  103. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  104. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  105. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  106. },
  107. Expected: []*visibleInterval{
  108. {start: 0, stop: 100, fileId: "abc"},
  109. },
  110. },
  111. }
  112. for i, testcase := range testcases {
  113. log.Printf("++++++++++ merged test case %d ++++++++++++++++++++", i)
  114. intervals := nonOverlappingVisibleIntervals(testcase.Chunks)
  115. for x, interval := range intervals {
  116. log.Printf("test case %d, interval %d, start=%d, stop=%d, fileId=%s",
  117. i, x, interval.start, interval.stop, interval.fileId)
  118. if interval.start != testcase.Expected[x].start {
  119. t.Fatalf("failed on test case %d, interval %d, start %d, expect %d",
  120. i, x, interval.start, testcase.Expected[x].start)
  121. }
  122. if interval.stop != testcase.Expected[x].stop {
  123. t.Fatalf("failed on test case %d, interval %d, stop %d, expect %d",
  124. i, x, interval.stop, testcase.Expected[x].stop)
  125. }
  126. if interval.fileId != testcase.Expected[x].fileId {
  127. t.Fatalf("failed on test case %d, interval %d, chunkId %s, expect %s",
  128. i, x, interval.fileId, testcase.Expected[x].fileId)
  129. }
  130. }
  131. if len(intervals) != len(testcase.Expected) {
  132. t.Fatalf("failed to compact test case %d, len %d expected %d", i, len(intervals), len(testcase.Expected))
  133. }
  134. }
  135. }