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.

501 lines
15 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
  1. package filer2
  2. import (
  3. "fmt"
  4. "log"
  5. "math"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func TestCompactFileChunks(t *testing.T) {
  11. chunks := []*filer_pb.FileChunk{
  12. {Offset: 10, Size: 100, FileId: "abc", Mtime: 50},
  13. {Offset: 100, Size: 100, FileId: "def", Mtime: 100},
  14. {Offset: 200, Size: 100, FileId: "ghi", Mtime: 200},
  15. {Offset: 110, Size: 200, FileId: "jkl", Mtime: 300},
  16. }
  17. compacted, garbage := CompactFileChunks(nil, chunks)
  18. if len(compacted) != 3 {
  19. t.Fatalf("unexpected compacted: %d", len(compacted))
  20. }
  21. if len(garbage) != 1 {
  22. t.Fatalf("unexpected garbage: %d", len(garbage))
  23. }
  24. }
  25. func TestCompactFileChunks2(t *testing.T) {
  26. chunks := []*filer_pb.FileChunk{
  27. {Offset: 0, Size: 100, FileId: "abc", Mtime: 50},
  28. {Offset: 100, Size: 100, FileId: "def", Mtime: 100},
  29. {Offset: 200, Size: 100, FileId: "ghi", Mtime: 200},
  30. {Offset: 0, Size: 100, FileId: "abcf", Mtime: 300},
  31. {Offset: 50, Size: 100, FileId: "fhfh", Mtime: 400},
  32. {Offset: 100, Size: 100, FileId: "yuyu", Mtime: 500},
  33. }
  34. k := 3
  35. for n := 0; n < k; n++ {
  36. chunks = append(chunks, &filer_pb.FileChunk{
  37. Offset: int64(n * 100), Size: 100, FileId: fmt.Sprintf("fileId%d", n), Mtime: int64(n),
  38. })
  39. chunks = append(chunks, &filer_pb.FileChunk{
  40. Offset: int64(n * 50), Size: 100, FileId: fmt.Sprintf("fileId%d", n+k), Mtime: int64(n + k),
  41. })
  42. }
  43. compacted, garbage := CompactFileChunks(nil, chunks)
  44. if len(compacted) != 4 {
  45. t.Fatalf("unexpected compacted: %d", len(compacted))
  46. }
  47. if len(garbage) != 8 {
  48. t.Fatalf("unexpected garbage: %d", len(garbage))
  49. }
  50. }
  51. func TestIntervalMerging(t *testing.T) {
  52. testcases := []struct {
  53. Chunks []*filer_pb.FileChunk
  54. Expected []*VisibleInterval
  55. }{
  56. // case 0: normal
  57. {
  58. Chunks: []*filer_pb.FileChunk{
  59. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  60. {Offset: 100, Size: 100, FileId: "asdf", Mtime: 134},
  61. {Offset: 200, Size: 100, FileId: "fsad", Mtime: 353},
  62. },
  63. Expected: []*VisibleInterval{
  64. {start: 0, stop: 100, fileId: "abc"},
  65. {start: 100, stop: 200, fileId: "asdf"},
  66. {start: 200, stop: 300, fileId: "fsad"},
  67. },
  68. },
  69. // case 1: updates overwrite full chunks
  70. {
  71. Chunks: []*filer_pb.FileChunk{
  72. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  73. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  74. },
  75. Expected: []*VisibleInterval{
  76. {start: 0, stop: 200, fileId: "asdf"},
  77. },
  78. },
  79. // case 2: updates overwrite part of previous chunks
  80. {
  81. Chunks: []*filer_pb.FileChunk{
  82. {Offset: 0, Size: 100, FileId: "a", Mtime: 123},
  83. {Offset: 0, Size: 70, FileId: "b", Mtime: 134},
  84. },
  85. Expected: []*VisibleInterval{
  86. {start: 0, stop: 70, fileId: "b"},
  87. {start: 70, stop: 100, fileId: "a", chunkOffset: 70},
  88. },
  89. },
  90. // case 3: updates overwrite full chunks
  91. {
  92. Chunks: []*filer_pb.FileChunk{
  93. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  94. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  95. {Offset: 50, Size: 250, FileId: "xxxx", Mtime: 154},
  96. },
  97. Expected: []*VisibleInterval{
  98. {start: 0, stop: 50, fileId: "asdf"},
  99. {start: 50, stop: 300, fileId: "xxxx"},
  100. },
  101. },
  102. // case 4: updates far away from prev chunks
  103. {
  104. Chunks: []*filer_pb.FileChunk{
  105. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  106. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  107. {Offset: 250, Size: 250, FileId: "xxxx", Mtime: 154},
  108. },
  109. Expected: []*VisibleInterval{
  110. {start: 0, stop: 200, fileId: "asdf"},
  111. {start: 250, stop: 500, fileId: "xxxx"},
  112. },
  113. },
  114. // case 5: updates overwrite full chunks
  115. {
  116. Chunks: []*filer_pb.FileChunk{
  117. {Offset: 0, Size: 100, FileId: "a", Mtime: 123},
  118. {Offset: 0, Size: 200, FileId: "d", Mtime: 184},
  119. {Offset: 70, Size: 150, FileId: "c", Mtime: 143},
  120. {Offset: 80, Size: 100, FileId: "b", Mtime: 134},
  121. },
  122. Expected: []*VisibleInterval{
  123. {start: 0, stop: 200, fileId: "d"},
  124. {start: 200, stop: 220, fileId: "c", chunkOffset: 130},
  125. },
  126. },
  127. // case 6: same updates
  128. {
  129. Chunks: []*filer_pb.FileChunk{
  130. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  131. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  132. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  133. },
  134. Expected: []*VisibleInterval{
  135. {start: 0, stop: 100, fileId: "abc"},
  136. },
  137. },
  138. // case 7: real updates
  139. {
  140. Chunks: []*filer_pb.FileChunk{
  141. {Offset: 0, Size: 2097152, FileId: "7,0294cbb9892b", Mtime: 123},
  142. {Offset: 0, Size: 3145728, FileId: "3,029565bf3092", Mtime: 130},
  143. {Offset: 2097152, Size: 3145728, FileId: "6,029632f47ae2", Mtime: 140},
  144. {Offset: 5242880, Size: 3145728, FileId: "2,029734c5aa10", Mtime: 150},
  145. {Offset: 8388608, Size: 3145728, FileId: "5,02982f80de50", Mtime: 160},
  146. {Offset: 11534336, Size: 2842193, FileId: "7,0299ad723803", Mtime: 170},
  147. },
  148. Expected: []*VisibleInterval{
  149. {start: 0, stop: 2097152, fileId: "3,029565bf3092"},
  150. {start: 2097152, stop: 5242880, fileId: "6,029632f47ae2"},
  151. {start: 5242880, stop: 8388608, fileId: "2,029734c5aa10"},
  152. {start: 8388608, stop: 11534336, fileId: "5,02982f80de50"},
  153. {start: 11534336, stop: 14376529, fileId: "7,0299ad723803"},
  154. },
  155. },
  156. // case 8: real bug
  157. {
  158. Chunks: []*filer_pb.FileChunk{
  159. {Offset: 0, Size: 77824, FileId: "4,0b3df938e301", Mtime: 123},
  160. {Offset: 471040, Size: 472225 - 471040, FileId: "6,0b3e0650019c", Mtime: 130},
  161. {Offset: 77824, Size: 208896 - 77824, FileId: "4,0b3f0c7202f0", Mtime: 140},
  162. {Offset: 208896, Size: 339968 - 208896, FileId: "2,0b4031a72689", Mtime: 150},
  163. {Offset: 339968, Size: 471040 - 339968, FileId: "3,0b416a557362", Mtime: 160},
  164. },
  165. Expected: []*VisibleInterval{
  166. {start: 0, stop: 77824, fileId: "4,0b3df938e301"},
  167. {start: 77824, stop: 208896, fileId: "4,0b3f0c7202f0"},
  168. {start: 208896, stop: 339968, fileId: "2,0b4031a72689"},
  169. {start: 339968, stop: 471040, fileId: "3,0b416a557362"},
  170. {start: 471040, stop: 472225, fileId: "6,0b3e0650019c"},
  171. },
  172. },
  173. }
  174. for i, testcase := range testcases {
  175. log.Printf("++++++++++ merged test case %d ++++++++++++++++++++", i)
  176. intervals, _ := NonOverlappingVisibleIntervals(nil, testcase.Chunks)
  177. for x, interval := range intervals {
  178. log.Printf("test case %d, interval %d, start=%d, stop=%d, fileId=%s",
  179. i, x, interval.start, interval.stop, interval.fileId)
  180. }
  181. for x, interval := range intervals {
  182. if interval.start != testcase.Expected[x].start {
  183. t.Fatalf("failed on test case %d, interval %d, start %d, expect %d",
  184. i, x, interval.start, testcase.Expected[x].start)
  185. }
  186. if interval.stop != testcase.Expected[x].stop {
  187. t.Fatalf("failed on test case %d, interval %d, stop %d, expect %d",
  188. i, x, interval.stop, testcase.Expected[x].stop)
  189. }
  190. if interval.fileId != testcase.Expected[x].fileId {
  191. t.Fatalf("failed on test case %d, interval %d, chunkId %s, expect %s",
  192. i, x, interval.fileId, testcase.Expected[x].fileId)
  193. }
  194. if interval.chunkOffset != testcase.Expected[x].chunkOffset {
  195. t.Fatalf("failed on test case %d, interval %d, chunkOffset %d, expect %d",
  196. i, x, interval.chunkOffset, testcase.Expected[x].chunkOffset)
  197. }
  198. }
  199. if len(intervals) != len(testcase.Expected) {
  200. t.Fatalf("failed to compact test case %d, len %d expected %d", i, len(intervals), len(testcase.Expected))
  201. }
  202. }
  203. }
  204. func TestChunksReading(t *testing.T) {
  205. testcases := []struct {
  206. Chunks []*filer_pb.FileChunk
  207. Offset int64
  208. Size int64
  209. Expected []*ChunkView
  210. }{
  211. // case 0: normal
  212. {
  213. Chunks: []*filer_pb.FileChunk{
  214. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  215. {Offset: 100, Size: 100, FileId: "asdf", Mtime: 134},
  216. {Offset: 200, Size: 100, FileId: "fsad", Mtime: 353},
  217. },
  218. Offset: 0,
  219. Size: 250,
  220. Expected: []*ChunkView{
  221. {Offset: 0, Size: 100, FileId: "abc", LogicOffset: 0},
  222. {Offset: 0, Size: 100, FileId: "asdf", LogicOffset: 100},
  223. {Offset: 0, Size: 50, FileId: "fsad", LogicOffset: 200},
  224. },
  225. },
  226. // case 1: updates overwrite full chunks
  227. {
  228. Chunks: []*filer_pb.FileChunk{
  229. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  230. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  231. },
  232. Offset: 50,
  233. Size: 100,
  234. Expected: []*ChunkView{
  235. {Offset: 50, Size: 100, FileId: "asdf", LogicOffset: 50},
  236. },
  237. },
  238. // case 2: updates overwrite part of previous chunks
  239. {
  240. Chunks: []*filer_pb.FileChunk{
  241. {Offset: 3, Size: 100, FileId: "a", Mtime: 123},
  242. {Offset: 10, Size: 50, FileId: "b", Mtime: 134},
  243. },
  244. Offset: 30,
  245. Size: 40,
  246. Expected: []*ChunkView{
  247. {Offset: 20, Size: 30, FileId: "b", LogicOffset: 30},
  248. {Offset: 57, Size: 10, FileId: "a", LogicOffset: 60},
  249. },
  250. },
  251. // case 3: updates overwrite full chunks
  252. {
  253. Chunks: []*filer_pb.FileChunk{
  254. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  255. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  256. {Offset: 50, Size: 250, FileId: "xxxx", Mtime: 154},
  257. },
  258. Offset: 0,
  259. Size: 200,
  260. Expected: []*ChunkView{
  261. {Offset: 0, Size: 50, FileId: "asdf", LogicOffset: 0},
  262. {Offset: 0, Size: 150, FileId: "xxxx", LogicOffset: 50},
  263. },
  264. },
  265. // case 4: updates far away from prev chunks
  266. {
  267. Chunks: []*filer_pb.FileChunk{
  268. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  269. {Offset: 0, Size: 200, FileId: "asdf", Mtime: 134},
  270. {Offset: 250, Size: 250, FileId: "xxxx", Mtime: 154},
  271. },
  272. Offset: 0,
  273. Size: 400,
  274. Expected: []*ChunkView{
  275. {Offset: 0, Size: 200, FileId: "asdf", LogicOffset: 0},
  276. {Offset: 0, Size: 150, FileId: "xxxx", LogicOffset: 250},
  277. },
  278. },
  279. // case 5: updates overwrite full chunks
  280. {
  281. Chunks: []*filer_pb.FileChunk{
  282. {Offset: 0, Size: 100, FileId: "a", Mtime: 123},
  283. {Offset: 0, Size: 200, FileId: "c", Mtime: 184},
  284. {Offset: 70, Size: 150, FileId: "b", Mtime: 143},
  285. {Offset: 80, Size: 100, FileId: "xxxx", Mtime: 134},
  286. },
  287. Offset: 0,
  288. Size: 220,
  289. Expected: []*ChunkView{
  290. {Offset: 0, Size: 200, FileId: "c", LogicOffset: 0},
  291. {Offset: 130, Size: 20, FileId: "b", LogicOffset: 200},
  292. },
  293. },
  294. // case 6: same updates
  295. {
  296. Chunks: []*filer_pb.FileChunk{
  297. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  298. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  299. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  300. },
  301. Offset: 0,
  302. Size: 100,
  303. Expected: []*ChunkView{
  304. {Offset: 0, Size: 100, FileId: "abc", LogicOffset: 0},
  305. },
  306. },
  307. // case 7: edge cases
  308. {
  309. Chunks: []*filer_pb.FileChunk{
  310. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  311. {Offset: 100, Size: 100, FileId: "asdf", Mtime: 134},
  312. {Offset: 200, Size: 100, FileId: "fsad", Mtime: 353},
  313. },
  314. Offset: 0,
  315. Size: 200,
  316. Expected: []*ChunkView{
  317. {Offset: 0, Size: 100, FileId: "abc", LogicOffset: 0},
  318. {Offset: 0, Size: 100, FileId: "asdf", LogicOffset: 100},
  319. },
  320. },
  321. // case 8: edge cases
  322. {
  323. Chunks: []*filer_pb.FileChunk{
  324. {Offset: 0, Size: 100, FileId: "abc", Mtime: 123},
  325. {Offset: 90, Size: 200, FileId: "asdf", Mtime: 134},
  326. {Offset: 190, Size: 300, FileId: "fsad", Mtime: 353},
  327. },
  328. Offset: 0,
  329. Size: 300,
  330. Expected: []*ChunkView{
  331. {Offset: 0, Size: 90, FileId: "abc", LogicOffset: 0},
  332. {Offset: 0, Size: 100, FileId: "asdf", LogicOffset: 90},
  333. {Offset: 0, Size: 110, FileId: "fsad", LogicOffset: 190},
  334. },
  335. },
  336. // case 9: edge cases
  337. {
  338. Chunks: []*filer_pb.FileChunk{
  339. {Offset: 0, Size: 43175947, FileId: "2,111fc2cbfac1", Mtime: 1},
  340. {Offset: 43175936, Size: 52981771 - 43175936, FileId: "2,112a36ea7f85", Mtime: 2},
  341. {Offset: 52981760, Size: 72564747 - 52981760, FileId: "4,112d5f31c5e7", Mtime: 3},
  342. {Offset: 72564736, Size: 133255179 - 72564736, FileId: "1,113245f0cdb6", Mtime: 4},
  343. {Offset: 133255168, Size: 137269259 - 133255168, FileId: "3,1141a70733b5", Mtime: 5},
  344. {Offset: 137269248, Size: 153578836 - 137269248, FileId: "1,114201d5bbdb", Mtime: 6},
  345. },
  346. Offset: 0,
  347. Size: 153578836,
  348. Expected: []*ChunkView{
  349. {Offset: 0, Size: 43175936, FileId: "2,111fc2cbfac1", LogicOffset: 0},
  350. {Offset: 0, Size: 52981760 - 43175936, FileId: "2,112a36ea7f85", LogicOffset: 43175936},
  351. {Offset: 0, Size: 72564736 - 52981760, FileId: "4,112d5f31c5e7", LogicOffset: 52981760},
  352. {Offset: 0, Size: 133255168 - 72564736, FileId: "1,113245f0cdb6", LogicOffset: 72564736},
  353. {Offset: 0, Size: 137269248 - 133255168, FileId: "3,1141a70733b5", LogicOffset: 133255168},
  354. {Offset: 0, Size: 153578836 - 137269248, FileId: "1,114201d5bbdb", LogicOffset: 137269248},
  355. },
  356. },
  357. }
  358. for i, testcase := range testcases {
  359. if i != 2 {
  360. // continue
  361. }
  362. log.Printf("++++++++++ read test case %d ++++++++++++++++++++", i)
  363. chunks := ViewFromChunks(nil, testcase.Chunks, testcase.Offset, testcase.Size)
  364. for x, chunk := range chunks {
  365. log.Printf("read case %d, chunk %d, offset=%d, size=%d, fileId=%s",
  366. i, x, chunk.Offset, chunk.Size, chunk.FileId)
  367. if chunk.Offset != testcase.Expected[x].Offset {
  368. t.Fatalf("failed on read case %d, chunk %s, Offset %d, expect %d",
  369. i, chunk.FileId, chunk.Offset, testcase.Expected[x].Offset)
  370. }
  371. if chunk.Size != testcase.Expected[x].Size {
  372. t.Fatalf("failed on read case %d, chunk %s, Size %d, expect %d",
  373. i, chunk.FileId, chunk.Size, testcase.Expected[x].Size)
  374. }
  375. if chunk.FileId != testcase.Expected[x].FileId {
  376. t.Fatalf("failed on read case %d, chunk %d, FileId %s, expect %s",
  377. i, x, chunk.FileId, testcase.Expected[x].FileId)
  378. }
  379. if chunk.LogicOffset != testcase.Expected[x].LogicOffset {
  380. t.Fatalf("failed on read case %d, chunk %d, LogicOffset %d, expect %d",
  381. i, x, chunk.LogicOffset, testcase.Expected[x].LogicOffset)
  382. }
  383. }
  384. if len(chunks) != len(testcase.Expected) {
  385. t.Fatalf("failed to read test case %d, len %d expected %d", i, len(chunks), len(testcase.Expected))
  386. }
  387. }
  388. }
  389. func BenchmarkCompactFileChunks(b *testing.B) {
  390. var chunks []*filer_pb.FileChunk
  391. k := 1024
  392. for n := 0; n < k; n++ {
  393. chunks = append(chunks, &filer_pb.FileChunk{
  394. Offset: int64(n * 100), Size: 100, FileId: fmt.Sprintf("fileId%d", n), Mtime: int64(n),
  395. })
  396. chunks = append(chunks, &filer_pb.FileChunk{
  397. Offset: int64(n * 50), Size: 100, FileId: fmt.Sprintf("fileId%d", n+k), Mtime: int64(n + k),
  398. })
  399. }
  400. for n := 0; n < b.N; n++ {
  401. CompactFileChunks(nil, chunks)
  402. }
  403. }
  404. func TestViewFromVisibleIntervals(t *testing.T) {
  405. visibles := []VisibleInterval{
  406. {
  407. start: 0,
  408. stop: 25,
  409. fileId: "fid1",
  410. },
  411. {
  412. start: 4096,
  413. stop: 8192,
  414. fileId: "fid2",
  415. },
  416. {
  417. start: 16384,
  418. stop: 18551,
  419. fileId: "fid3",
  420. },
  421. }
  422. views := ViewFromVisibleIntervals(visibles, 0, math.MaxInt32)
  423. if len(views) != len(visibles) {
  424. assert.Equal(t, len(visibles), len(views), "ViewFromVisibleIntervals error")
  425. }
  426. }
  427. func TestViewFromVisibleIntervals2(t *testing.T) {
  428. visibles := []VisibleInterval{
  429. {
  430. start: 344064,
  431. stop: 348160,
  432. fileId: "fid1",
  433. },
  434. {
  435. start: 348160,
  436. stop: 356352,
  437. fileId: "fid2",
  438. },
  439. }
  440. views := ViewFromVisibleIntervals(visibles, 0, math.MaxInt32)
  441. if len(views) != len(visibles) {
  442. assert.Equal(t, len(visibles), len(views), "ViewFromVisibleIntervals error")
  443. }
  444. }
  445. func TestViewFromVisibleIntervals3(t *testing.T) {
  446. visibles := []VisibleInterval{
  447. {
  448. start: 1000,
  449. stop: 2000,
  450. fileId: "fid1",
  451. },
  452. {
  453. start: 3000,
  454. stop: 4000,
  455. fileId: "fid2",
  456. },
  457. }
  458. views := ViewFromVisibleIntervals(visibles, 1700, 1500)
  459. if len(views) != len(visibles) {
  460. assert.Equal(t, len(visibles), len(views), "ViewFromVisibleIntervals error")
  461. }
  462. }