filechunks_read_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  5. "math"
  6. "math/rand"
  7. "testing"
  8. )
  9. func TestReadResolvedChunks(t *testing.T) {
  10. chunks := []*filer_pb.FileChunk{
  11. {
  12. FileId: "a",
  13. Offset: 0,
  14. Size: 100,
  15. ModifiedTsNs: 1,
  16. },
  17. {
  18. FileId: "b",
  19. Offset: 50,
  20. Size: 100,
  21. ModifiedTsNs: 2,
  22. },
  23. {
  24. FileId: "c",
  25. Offset: 200,
  26. Size: 50,
  27. ModifiedTsNs: 3,
  28. },
  29. {
  30. FileId: "d",
  31. Offset: 250,
  32. Size: 50,
  33. ModifiedTsNs: 4,
  34. },
  35. {
  36. FileId: "e",
  37. Offset: 175,
  38. Size: 100,
  39. ModifiedTsNs: 5,
  40. },
  41. }
  42. visibles := readResolvedChunks(chunks, 0, math.MaxInt64)
  43. fmt.Printf("resolved to %d visible intervales\n", visibles.Len())
  44. for x := visibles.Front(); x != nil; x = x.Next {
  45. visible := x.Value
  46. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  47. }
  48. }
  49. func TestReadResolvedChunks2(t *testing.T) {
  50. chunks := []*filer_pb.FileChunk{
  51. {
  52. FileId: "c",
  53. Offset: 200,
  54. Size: 50,
  55. ModifiedTsNs: 3,
  56. },
  57. {
  58. FileId: "e",
  59. Offset: 200,
  60. Size: 25,
  61. ModifiedTsNs: 5,
  62. },
  63. }
  64. visibles := readResolvedChunks(chunks, 0, math.MaxInt64)
  65. fmt.Printf("resolved to %d visible intervales\n", visibles.Len())
  66. for x := visibles.Front(); x != nil; x = x.Next {
  67. visible := x.Value
  68. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  69. }
  70. }
  71. func TestRandomizedReadResolvedChunks(t *testing.T) {
  72. var limit int64 = 1024 * 1024
  73. array := make([]int64, limit)
  74. var chunks []*filer_pb.FileChunk
  75. for ts := int64(0); ts < 1024; ts++ {
  76. x := rand.Int63n(limit)
  77. y := rand.Int63n(limit)
  78. size := x - y
  79. if size < 0 {
  80. size = -size
  81. }
  82. if size > 1024 {
  83. size = 1024
  84. }
  85. start := x
  86. if start > y {
  87. start = y
  88. }
  89. chunks = append(chunks, randomWrite(array, start, size, ts))
  90. }
  91. visibles := readResolvedChunks(chunks, 0, math.MaxInt64)
  92. for x := visibles.Front(); x != nil; x = x.Next {
  93. visible := x.Value
  94. for i := visible.start; i < visible.stop; i++ {
  95. if array[i] != visible.modifiedTsNs {
  96. t.Errorf("position %d expected ts %d actual ts %d", i, array[i], visible.modifiedTsNs)
  97. }
  98. }
  99. }
  100. // fmt.Printf("visibles %d", len(visibles))
  101. }
  102. func randomWrite(array []int64, start int64, size int64, ts int64) *filer_pb.FileChunk {
  103. for i := start; i < start+size; i++ {
  104. array[i] = ts
  105. }
  106. // fmt.Printf("write [%d,%d) %d\n", start, start+size, ts)
  107. return &filer_pb.FileChunk{
  108. FileId: "",
  109. Offset: start,
  110. Size: uint64(size),
  111. ModifiedTsNs: ts,
  112. }
  113. }
  114. func TestSequentialReadResolvedChunks(t *testing.T) {
  115. var chunkSize int64 = 1024 * 1024 * 2
  116. var chunks []*filer_pb.FileChunk
  117. for ts := int64(0); ts < 13; ts++ {
  118. chunks = append(chunks, &filer_pb.FileChunk{
  119. FileId: "",
  120. Offset: chunkSize * ts,
  121. Size: uint64(chunkSize),
  122. ModifiedTsNs: 1,
  123. })
  124. }
  125. visibles := readResolvedChunks(chunks, 0, math.MaxInt64)
  126. fmt.Printf("visibles %d", visibles.Len())
  127. }
  128. func TestActualReadResolvedChunks(t *testing.T) {
  129. chunks := []*filer_pb.FileChunk{
  130. {
  131. FileId: "5,e7b96fef48",
  132. Offset: 0,
  133. Size: 2097152,
  134. ModifiedTsNs: 1634447487595823000,
  135. },
  136. {
  137. FileId: "5,e5562640b9",
  138. Offset: 2097152,
  139. Size: 2097152,
  140. ModifiedTsNs: 1634447487595826000,
  141. },
  142. {
  143. FileId: "5,df033e0fe4",
  144. Offset: 4194304,
  145. Size: 2097152,
  146. ModifiedTsNs: 1634447487595827000,
  147. },
  148. {
  149. FileId: "7,eb08148a9b",
  150. Offset: 6291456,
  151. Size: 2097152,
  152. ModifiedTsNs: 1634447487595827000,
  153. },
  154. {
  155. FileId: "7,e0f92d1604",
  156. Offset: 8388608,
  157. Size: 2097152,
  158. ModifiedTsNs: 1634447487595828000,
  159. },
  160. {
  161. FileId: "7,e33cb63262",
  162. Offset: 10485760,
  163. Size: 2097152,
  164. ModifiedTsNs: 1634447487595828000,
  165. },
  166. {
  167. FileId: "5,ea98e40e93",
  168. Offset: 12582912,
  169. Size: 2097152,
  170. ModifiedTsNs: 1634447487595829000,
  171. },
  172. {
  173. FileId: "5,e165661172",
  174. Offset: 14680064,
  175. Size: 2097152,
  176. ModifiedTsNs: 1634447487595829000,
  177. },
  178. {
  179. FileId: "3,e692097486",
  180. Offset: 16777216,
  181. Size: 2097152,
  182. ModifiedTsNs: 1634447487595830000,
  183. },
  184. {
  185. FileId: "3,e28e2e3cbd",
  186. Offset: 18874368,
  187. Size: 2097152,
  188. ModifiedTsNs: 1634447487595830000,
  189. },
  190. {
  191. FileId: "3,e443974d4e",
  192. Offset: 20971520,
  193. Size: 2097152,
  194. ModifiedTsNs: 1634447487595830000,
  195. },
  196. {
  197. FileId: "2,e815bed597",
  198. Offset: 23068672,
  199. Size: 2097152,
  200. ModifiedTsNs: 1634447487595831000,
  201. },
  202. {
  203. FileId: "5,e94715199e",
  204. Offset: 25165824,
  205. Size: 1974736,
  206. ModifiedTsNs: 1634447487595832000,
  207. },
  208. }
  209. visibles := readResolvedChunks(chunks, 0, math.MaxInt64)
  210. for x := visibles.Front(); x != nil; x = x.Next {
  211. visible := x.Value
  212. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  213. }
  214. }
  215. func TestActualReadResolvedChunks2(t *testing.T) {
  216. chunks := []*filer_pb.FileChunk{
  217. {
  218. FileId: "1,e7b96fef48",
  219. Offset: 0,
  220. Size: 184320,
  221. ModifiedTsNs: 1,
  222. },
  223. {
  224. FileId: "2,22562640b9",
  225. Offset: 184320,
  226. Size: 4096,
  227. ModifiedTsNs: 2,
  228. },
  229. {
  230. FileId: "2,33562640b9",
  231. Offset: 184320,
  232. Size: 4096,
  233. ModifiedTsNs: 4,
  234. },
  235. {
  236. FileId: "4,df033e0fe4",
  237. Offset: 188416,
  238. Size: 2097152,
  239. ModifiedTsNs: 3,
  240. },
  241. }
  242. visibles := readResolvedChunks(chunks, 0, math.MaxInt64)
  243. for x := visibles.Front(); x != nil; x = x.Next {
  244. visible := x.Value
  245. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  246. }
  247. }