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.

237 lines
4.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. fmt.Printf("resolved to %d visible intervales\n", len(visibles))
  43. for _, visible := range visibles {
  44. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  45. }
  46. }
  47. func TestReadResolvedChunks2(t *testing.T) {
  48. chunks := []*filer_pb.FileChunk{
  49. {
  50. FileId: "c",
  51. Offset: 200,
  52. Size: 50,
  53. ModifiedTsNs: 3,
  54. },
  55. {
  56. FileId: "e",
  57. Offset: 200,
  58. Size: 25,
  59. ModifiedTsNs: 5,
  60. },
  61. }
  62. visibles := readResolvedChunks(chunks)
  63. fmt.Printf("resolved to %d visible intervales\n", len(visibles))
  64. for _, visible := range visibles {
  65. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  66. }
  67. }
  68. func TestRandomizedReadResolvedChunks(t *testing.T) {
  69. var limit int64 = 1024 * 1024
  70. array := make([]int64, limit)
  71. var chunks []*filer_pb.FileChunk
  72. for ts := int64(0); ts < 1024; ts++ {
  73. x := rand.Int63n(limit)
  74. y := rand.Int63n(limit)
  75. size := x - y
  76. if size < 0 {
  77. size = -size
  78. }
  79. if size > 1024 {
  80. size = 1024
  81. }
  82. start := x
  83. if start > y {
  84. start = y
  85. }
  86. chunks = append(chunks, randomWrite(array, start, size, ts))
  87. }
  88. visibles := readResolvedChunks(chunks)
  89. for _, visible := range visibles {
  90. for i := visible.start; i < visible.stop; i++ {
  91. if array[i] != visible.modifiedTsNs {
  92. t.Errorf("position %d expected ts %d actual ts %d", i, array[i], visible.modifiedTsNs)
  93. }
  94. }
  95. }
  96. // fmt.Printf("visibles %d", len(visibles))
  97. }
  98. func randomWrite(array []int64, start int64, size int64, ts int64) *filer_pb.FileChunk {
  99. for i := start; i < start+size; i++ {
  100. array[i] = ts
  101. }
  102. // fmt.Printf("write [%d,%d) %d\n", start, start+size, ts)
  103. return &filer_pb.FileChunk{
  104. FileId: "",
  105. Offset: start,
  106. Size: uint64(size),
  107. ModifiedTsNs: ts,
  108. }
  109. }
  110. func TestSequentialReadResolvedChunks(t *testing.T) {
  111. var chunkSize int64 = 1024 * 1024 * 2
  112. var chunks []*filer_pb.FileChunk
  113. for ts := int64(0); ts < 13; ts++ {
  114. chunks = append(chunks, &filer_pb.FileChunk{
  115. FileId: "",
  116. Offset: chunkSize * ts,
  117. Size: uint64(chunkSize),
  118. ModifiedTsNs: 1,
  119. })
  120. }
  121. visibles := readResolvedChunks(chunks)
  122. fmt.Printf("visibles %d", len(visibles))
  123. }
  124. func TestActualReadResolvedChunks(t *testing.T) {
  125. chunks := []*filer_pb.FileChunk{
  126. {
  127. FileId: "5,e7b96fef48",
  128. Offset: 0,
  129. Size: 2097152,
  130. ModifiedTsNs: 1634447487595823000,
  131. },
  132. {
  133. FileId: "5,e5562640b9",
  134. Offset: 2097152,
  135. Size: 2097152,
  136. ModifiedTsNs: 1634447487595826000,
  137. },
  138. {
  139. FileId: "5,df033e0fe4",
  140. Offset: 4194304,
  141. Size: 2097152,
  142. ModifiedTsNs: 1634447487595827000,
  143. },
  144. {
  145. FileId: "7,eb08148a9b",
  146. Offset: 6291456,
  147. Size: 2097152,
  148. ModifiedTsNs: 1634447487595827000,
  149. },
  150. {
  151. FileId: "7,e0f92d1604",
  152. Offset: 8388608,
  153. Size: 2097152,
  154. ModifiedTsNs: 1634447487595828000,
  155. },
  156. {
  157. FileId: "7,e33cb63262",
  158. Offset: 10485760,
  159. Size: 2097152,
  160. ModifiedTsNs: 1634447487595828000,
  161. },
  162. {
  163. FileId: "5,ea98e40e93",
  164. Offset: 12582912,
  165. Size: 2097152,
  166. ModifiedTsNs: 1634447487595829000,
  167. },
  168. {
  169. FileId: "5,e165661172",
  170. Offset: 14680064,
  171. Size: 2097152,
  172. ModifiedTsNs: 1634447487595829000,
  173. },
  174. {
  175. FileId: "3,e692097486",
  176. Offset: 16777216,
  177. Size: 2097152,
  178. ModifiedTsNs: 1634447487595830000,
  179. },
  180. {
  181. FileId: "3,e28e2e3cbd",
  182. Offset: 18874368,
  183. Size: 2097152,
  184. ModifiedTsNs: 1634447487595830000,
  185. },
  186. {
  187. FileId: "3,e443974d4e",
  188. Offset: 20971520,
  189. Size: 2097152,
  190. ModifiedTsNs: 1634447487595830000,
  191. },
  192. {
  193. FileId: "2,e815bed597",
  194. Offset: 23068672,
  195. Size: 2097152,
  196. ModifiedTsNs: 1634447487595831000,
  197. },
  198. {
  199. FileId: "5,e94715199e",
  200. Offset: 25165824,
  201. Size: 1974736,
  202. ModifiedTsNs: 1634447487595832000,
  203. },
  204. }
  205. visibles := readResolvedChunks(chunks)
  206. for _, visible := range visibles {
  207. fmt.Printf("[%d,%d) %s %d\n", visible.start, visible.stop, visible.fileId, visible.modifiedTsNs)
  208. }
  209. }