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.

327 lines
8.6 KiB

more solid weed mount (#4089) * compare chunks by timestamp * fix slab clearing error * fix test compilation * move oldest chunk to sealed, instead of by fullness * lock on fh.entryViewCache * remove verbose logs * revert slat clearing * less logs * less logs * track write and read by timestamp * remove useless logic * add entry lock on file handle release * use mem chunk only, swap file chunk has problems * comment out code that maybe used later * add debug mode to compare data read and write * more efficient readResolvedChunks with linked list * small optimization * fix test compilation * minor fix on writer * add SeparateGarbageChunks * group chunks into sections * turn off debug mode * fix tests * fix tests * tmp enable swap file chunk * Revert "tmp enable swap file chunk" This reverts commit 985137ec472924e4815f258189f6ca9f2168a0a7. * simple refactoring * simple refactoring * do not re-use swap file chunk. Sealed chunks should not be re-used. * comment out debugging facilities * either mem chunk or swap file chunk is fine now * remove orderedMutex as *semaphore.Weighted not found impactful * optimize size calculation for changing large files * optimize performance to avoid going through the long list of chunks * still problems with swap file chunk * rename * tiny optimization * swap file chunk save only successfully read data * fix * enable both mem and swap file chunk * resolve chunks with range * rename * fix chunk interval list * also change file handle chunk group when adding chunks * pick in-active chunk with time-decayed counter * fix compilation * avoid nil with empty fh.entry * refactoring * rename * rename * refactor visible intervals to *list.List * refactor chunkViews to *list.List * add IntervalList for generic interval list * change visible interval to use IntervalList in generics * cahnge chunkViews to *IntervalList[*ChunkView] * use NewFileChunkSection to create * rename variables * refactor * fix renaming leftover * renaming * renaming * add insert interval * interval list adds lock * incrementally add chunks to readers Fixes: 1. set start and stop offset for the value object 2. clone the value object 3. use pointer instead of copy-by-value when passing to interval.Value 4. use insert interval since adding chunk could be out of order * fix tests compilation * fix tests compilation
2 years ago
  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. type IntervalInt int
  8. func (i IntervalInt) SetStartStop(start, stop int64) {
  9. }
  10. func (i IntervalInt) Clone() IntervalValue {
  11. return i
  12. }
  13. func TestIntervalList_Overlay(t *testing.T) {
  14. list := NewIntervalList[IntervalInt]()
  15. list.Overlay(0, 100, 1, 1)
  16. list.Overlay(50, 150, 2, 2)
  17. list.Overlay(200, 250, 3, 3)
  18. list.Overlay(225, 250, 4, 4)
  19. list.Overlay(175, 210, 5, 5)
  20. list.Overlay(0, 25, 6, 6)
  21. for p := list.Front(); p != nil; p = p.Next {
  22. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  23. }
  24. assert.Equal(t, 6, list.Len())
  25. println()
  26. list.Overlay(50, 150, 7, 7)
  27. for p := list.Front(); p != nil; p = p.Next {
  28. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  29. }
  30. assert.Equal(t, 6, list.Len())
  31. }
  32. func TestIntervalList_Overlay2(t *testing.T) {
  33. list := NewIntervalList[IntervalInt]()
  34. list.Overlay(50, 100, 1, 1)
  35. list.Overlay(0, 50, 2, 2)
  36. for p := list.Front(); p != nil; p = p.Next {
  37. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  38. }
  39. }
  40. func TestIntervalList_Overlay3(t *testing.T) {
  41. list := NewIntervalList[IntervalInt]()
  42. list.Overlay(50, 100, 1, 1)
  43. assert.Equal(t, 1, list.Len())
  44. list.Overlay(0, 60, 2, 2)
  45. for p := list.Front(); p != nil; p = p.Next {
  46. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  47. }
  48. assert.Equal(t, 2, list.Len())
  49. }
  50. func TestIntervalList_Overlay4(t *testing.T) {
  51. list := NewIntervalList[IntervalInt]()
  52. list.Overlay(50, 100, 1, 1)
  53. list.Overlay(0, 100, 2, 2)
  54. for p := list.Front(); p != nil; p = p.Next {
  55. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  56. }
  57. assert.Equal(t, 1, list.Len())
  58. }
  59. func TestIntervalList_Overlay5(t *testing.T) {
  60. list := NewIntervalList[IntervalInt]()
  61. list.Overlay(50, 100, 1, 1)
  62. list.Overlay(0, 110, 2, 2)
  63. for p := list.Front(); p != nil; p = p.Next {
  64. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  65. }
  66. assert.Equal(t, 1, list.Len())
  67. }
  68. func TestIntervalList_Overlay6(t *testing.T) {
  69. list := NewIntervalList[IntervalInt]()
  70. list.Overlay(50, 100, 1, 1)
  71. list.Overlay(50, 110, 2, 2)
  72. for p := list.Front(); p != nil; p = p.Next {
  73. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  74. }
  75. assert.Equal(t, 1, list.Len())
  76. }
  77. func TestIntervalList_Overlay7(t *testing.T) {
  78. list := NewIntervalList[IntervalInt]()
  79. list.Overlay(50, 100, 1, 1)
  80. list.Overlay(50, 90, 2, 2)
  81. for p := list.Front(); p != nil; p = p.Next {
  82. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  83. }
  84. assert.Equal(t, 2, list.Len())
  85. }
  86. func TestIntervalList_Overlay8(t *testing.T) {
  87. list := NewIntervalList[IntervalInt]()
  88. list.Overlay(50, 100, 1, 1)
  89. list.Overlay(60, 90, 2, 2)
  90. for p := list.Front(); p != nil; p = p.Next {
  91. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  92. }
  93. assert.Equal(t, 3, list.Len())
  94. }
  95. func TestIntervalList_Overlay9(t *testing.T) {
  96. list := NewIntervalList[IntervalInt]()
  97. list.Overlay(50, 100, 1, 1)
  98. list.Overlay(60, 100, 2, 2)
  99. for p := list.Front(); p != nil; p = p.Next {
  100. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  101. }
  102. assert.Equal(t, 2, list.Len())
  103. }
  104. func TestIntervalList_Overlay10(t *testing.T) {
  105. list := NewIntervalList[IntervalInt]()
  106. list.Overlay(50, 100, 1, 1)
  107. list.Overlay(60, 110, 2, 2)
  108. for p := list.Front(); p != nil; p = p.Next {
  109. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  110. }
  111. assert.Equal(t, 2, list.Len())
  112. }
  113. func TestIntervalList_Overlay11(t *testing.T) {
  114. list := NewIntervalList[IntervalInt]()
  115. list.Overlay(0, 100, 1, 1)
  116. list.Overlay(100, 110, 2, 2)
  117. list.Overlay(0, 90, 3, 3)
  118. list.Overlay(0, 80, 4, 4)
  119. list.Overlay(0, 90, 5, 5)
  120. list.Overlay(90, 90, 6, 6)
  121. for p := list.Front(); p != nil; p = p.Next {
  122. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  123. }
  124. assert.Equal(t, 3, list.Len())
  125. }
  126. func TestIntervalList_insertInterval1(t *testing.T) {
  127. list := NewIntervalList[IntervalInt]()
  128. list.InsertInterval(50, 150, 2, 2)
  129. list.InsertInterval(200, 250, 3, 3)
  130. for p := list.Front(); p != nil; p = p.Next {
  131. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  132. }
  133. assert.Equal(t, 2, list.Len())
  134. }
  135. func TestIntervalList_insertInterval2(t *testing.T) {
  136. list := NewIntervalList[IntervalInt]()
  137. list.InsertInterval(50, 150, 2, 2)
  138. list.InsertInterval(0, 25, 3, 3)
  139. for p := list.Front(); p != nil; p = p.Next {
  140. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  141. }
  142. assert.Equal(t, 2, list.Len())
  143. }
  144. func TestIntervalList_insertInterval3(t *testing.T) {
  145. list := NewIntervalList[IntervalInt]()
  146. list.InsertInterval(50, 150, 2, 2)
  147. list.InsertInterval(200, 250, 4, 4)
  148. list.InsertInterval(0, 75, 3, 3)
  149. for p := list.Front(); p != nil; p = p.Next {
  150. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  151. }
  152. assert.Equal(t, 3, list.Len())
  153. }
  154. func TestIntervalList_insertInterval4(t *testing.T) {
  155. list := NewIntervalList[IntervalInt]()
  156. list.InsertInterval(200, 250, 4, 4)
  157. list.InsertInterval(0, 225, 3, 3)
  158. for p := list.Front(); p != nil; p = p.Next {
  159. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  160. }
  161. assert.Equal(t, 2, list.Len())
  162. }
  163. func TestIntervalList_insertInterval5(t *testing.T) {
  164. list := NewIntervalList[IntervalInt]()
  165. list.InsertInterval(200, 250, 4, 4)
  166. list.InsertInterval(0, 225, 5, 5)
  167. for p := list.Front(); p != nil; p = p.Next {
  168. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  169. }
  170. assert.Equal(t, 2, list.Len())
  171. }
  172. func TestIntervalList_insertInterval6(t *testing.T) {
  173. list := NewIntervalList[IntervalInt]()
  174. list.InsertInterval(50, 150, 2, 2)
  175. list.InsertInterval(200, 250, 4, 4)
  176. list.InsertInterval(0, 275, 1, 1)
  177. for p := list.Front(); p != nil; p = p.Next {
  178. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  179. }
  180. assert.Equal(t, 5, list.Len())
  181. }
  182. func TestIntervalList_insertInterval7(t *testing.T) {
  183. list := NewIntervalList[IntervalInt]()
  184. list.InsertInterval(50, 150, 2, 2)
  185. list.InsertInterval(200, 250, 4, 4)
  186. list.InsertInterval(75, 275, 1, 1)
  187. for p := list.Front(); p != nil; p = p.Next {
  188. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  189. }
  190. assert.Equal(t, 4, list.Len())
  191. }
  192. func TestIntervalList_insertInterval8(t *testing.T) {
  193. list := NewIntervalList[IntervalInt]()
  194. list.InsertInterval(50, 150, 2, 2)
  195. list.InsertInterval(200, 250, 4, 4)
  196. list.InsertInterval(75, 275, 3, 3)
  197. for p := list.Front(); p != nil; p = p.Next {
  198. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  199. }
  200. assert.Equal(t, 4, list.Len())
  201. }
  202. func TestIntervalList_insertInterval9(t *testing.T) {
  203. list := NewIntervalList[IntervalInt]()
  204. list.InsertInterval(50, 150, 2, 2)
  205. list.InsertInterval(200, 250, 4, 4)
  206. list.InsertInterval(50, 150, 3, 3)
  207. for p := list.Front(); p != nil; p = p.Next {
  208. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  209. }
  210. assert.Equal(t, 2, list.Len())
  211. }
  212. func TestIntervalList_insertInterval10(t *testing.T) {
  213. list := NewIntervalList[IntervalInt]()
  214. list.InsertInterval(50, 100, 2, 2)
  215. list.InsertInterval(200, 300, 4, 4)
  216. list.InsertInterval(100, 200, 5, 5)
  217. for p := list.Front(); p != nil; p = p.Next {
  218. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  219. }
  220. assert.Equal(t, 3, list.Len())
  221. }
  222. func TestIntervalList_insertInterval11(t *testing.T) {
  223. list := NewIntervalList[IntervalInt]()
  224. list.InsertInterval(0, 64, 1, 1)
  225. list.InsertInterval(72, 136, 3, 3)
  226. list.InsertInterval(64, 128, 2, 2)
  227. list.InsertInterval(68, 72, 4, 4)
  228. for p := list.Front(); p != nil; p = p.Next {
  229. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  230. }
  231. assert.Equal(t, 4, list.Len())
  232. }
  233. type IntervalStruct struct {
  234. x int
  235. start int64
  236. stop int64
  237. }
  238. func newIntervalStruct(i int) IntervalStruct {
  239. return IntervalStruct{
  240. x: i,
  241. }
  242. }
  243. func (i IntervalStruct) SetStartStop(start, stop int64) {
  244. i.start, i.stop = start, stop
  245. }
  246. func (i IntervalStruct) Clone() IntervalValue {
  247. return &IntervalStruct{
  248. x: i.x,
  249. start: i.start,
  250. stop: i.stop,
  251. }
  252. }
  253. func TestIntervalList_insertIntervalStruct(t *testing.T) {
  254. list := NewIntervalList[IntervalStruct]()
  255. list.InsertInterval(0, 64, 1, newIntervalStruct(1))
  256. list.InsertInterval(64, 72, 2, newIntervalStruct(2))
  257. list.InsertInterval(72, 136, 3, newIntervalStruct(3))
  258. list.InsertInterval(64, 68, 4, newIntervalStruct(4))
  259. for p := list.Front(); p != nil; p = p.Next {
  260. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  261. }
  262. assert.Equal(t, 4, list.Len())
  263. }