filechunks_read_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  5. "math/rand"
  6. "testing"
  7. )
  8. func TestReadResolvedChunks(t *testing.T) {
  9. chunks := []*filer_pb.FileChunk{
  10. {
  11. FileId: "a",
  12. Offset: 0,
  13. Size: 100,
  14. ModifiedTsNs: 1,
  15. },
  16. {
  17. FileId: "b",
  18. Offset: 50,
  19. Size: 100,
  20. ModifiedTsNs: 2,
  21. },
  22. {
  23. FileId: "c",
  24. Offset: 200,
  25. Size: 50,
  26. ModifiedTsNs: 3,
  27. },
  28. {
  29. FileId: "d",
  30. Offset: 250,
  31. Size: 50,
  32. ModifiedTsNs: 4,
  33. },
  34. {
  35. FileId: "e",
  36. Offset: 175,
  37. Size: 100,
  38. ModifiedTsNs: 5,
  39. },
  40. }
  41. visibles := readResolvedChunks(chunks)
  42. for _, visible := range visibles {
  43. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  44. }
  45. }
  46. func TestRandomizedReadResolvedChunks(t *testing.T) {
  47. var limit int64 = 1024 * 1024
  48. array := make([]int64, limit)
  49. var chunks []*filer_pb.FileChunk
  50. for ts := int64(0); ts < 1024; ts++ {
  51. x := rand.Int63n(limit)
  52. y := rand.Int63n(limit)
  53. size := x - y
  54. if size < 0 {
  55. size = -size
  56. }
  57. if size > 1024 {
  58. size = 1024
  59. }
  60. start := x
  61. if start > y {
  62. start = y
  63. }
  64. chunks = append(chunks, randomWrite(array, start, size, ts))
  65. }
  66. visibles := readResolvedChunks(chunks)
  67. for _, visible := range visibles {
  68. for i := visible.start; i < visible.stop; i++ {
  69. if array[i] != visible.modifiedTsNs {
  70. t.Errorf("position %d expected ts %d actual ts %d", i, array[i], visible.modifiedTsNs)
  71. }
  72. }
  73. }
  74. // fmt.Printf("visibles %d", len(visibles))
  75. }
  76. func randomWrite(array []int64, start int64, size int64, ts int64) *filer_pb.FileChunk {
  77. for i := start; i < start+size; i++ {
  78. array[i] = ts
  79. }
  80. // fmt.Printf("write [%d,%d) %d\n", start, start+size, ts)
  81. return &filer_pb.FileChunk{
  82. FileId: "",
  83. Offset: start,
  84. Size: uint64(size),
  85. ModifiedTsNs: ts,
  86. }
  87. }
  88. func TestSequentialReadResolvedChunks(t *testing.T) {
  89. var chunkSize int64 = 1024 * 1024 * 2
  90. var chunks []*filer_pb.FileChunk
  91. for ts := int64(0); ts < 13; ts++ {
  92. chunks = append(chunks, &filer_pb.FileChunk{
  93. FileId: "",
  94. Offset: chunkSize * ts,
  95. Size: uint64(chunkSize),
  96. ModifiedTsNs: 1,
  97. })
  98. }
  99. visibles := readResolvedChunks(chunks)
  100. fmt.Printf("visibles %d", len(visibles))
  101. }
  102. func TestActualReadResolvedChunks(t *testing.T) {
  103. chunks := []*filer_pb.FileChunk{
  104. {
  105. FileId: "5,e7b96fef48",
  106. Offset: 0,
  107. Size: 2097152,
  108. ModifiedTsNs: 1634447487595823000,
  109. },
  110. {
  111. FileId: "5,e5562640b9",
  112. Offset: 2097152,
  113. Size: 2097152,
  114. ModifiedTsNs: 1634447487595826000,
  115. },
  116. {
  117. FileId: "5,df033e0fe4",
  118. Offset: 4194304,
  119. Size: 2097152,
  120. ModifiedTsNs: 1634447487595827000,
  121. },
  122. {
  123. FileId: "7,eb08148a9b",
  124. Offset: 6291456,
  125. Size: 2097152,
  126. ModifiedTsNs: 1634447487595827000,
  127. },
  128. {
  129. FileId: "7,e0f92d1604",
  130. Offset: 8388608,
  131. Size: 2097152,
  132. ModifiedTsNs: 1634447487595828000,
  133. },
  134. {
  135. FileId: "7,e33cb63262",
  136. Offset: 10485760,
  137. Size: 2097152,
  138. ModifiedTsNs: 1634447487595828000,
  139. },
  140. {
  141. FileId: "5,ea98e40e93",
  142. Offset: 12582912,
  143. Size: 2097152,
  144. ModifiedTsNs: 1634447487595829000,
  145. },
  146. {
  147. FileId: "5,e165661172",
  148. Offset: 14680064,
  149. Size: 2097152,
  150. ModifiedTsNs: 1634447487595829000,
  151. },
  152. {
  153. FileId: "3,e692097486",
  154. Offset: 16777216,
  155. Size: 2097152,
  156. ModifiedTsNs: 1634447487595830000,
  157. },
  158. {
  159. FileId: "3,e28e2e3cbd",
  160. Offset: 18874368,
  161. Size: 2097152,
  162. ModifiedTsNs: 1634447487595830000,
  163. },
  164. {
  165. FileId: "3,e443974d4e",
  166. Offset: 20971520,
  167. Size: 2097152,
  168. ModifiedTsNs: 1634447487595830000,
  169. },
  170. {
  171. FileId: "2,e815bed597",
  172. Offset: 23068672,
  173. Size: 2097152,
  174. ModifiedTsNs: 1634447487595831000,
  175. },
  176. {
  177. FileId: "5,e94715199e",
  178. Offset: 25165824,
  179. Size: 1974736,
  180. ModifiedTsNs: 1634447487595832000,
  181. },
  182. }
  183. visibles := readResolvedChunks(chunks)
  184. for _, visible := range visibles {
  185. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  186. }
  187. }