freephysicalslotmanager_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package slotting
  11. import (
  12. "flag"
  13. "fmt"
  14. "os"
  15. "testing"
  16. "devt.de/krotik/common/fileutil"
  17. "devt.de/krotik/eliasdb/storage/file"
  18. "devt.de/krotik/eliasdb/storage/paging"
  19. "devt.de/krotik/eliasdb/storage/paging/view"
  20. "devt.de/krotik/eliasdb/storage/slotting/pageview"
  21. "devt.de/krotik/eliasdb/storage/util"
  22. )
  23. const DBDIR = "buckettest"
  24. // Main function for all tests in this package
  25. func TestMain(m *testing.M) {
  26. flag.Parse()
  27. // Setup
  28. if res, _ := fileutil.PathExists(DBDIR); res {
  29. os.RemoveAll(DBDIR)
  30. }
  31. err := os.Mkdir(DBDIR, 0770)
  32. if err != nil {
  33. fmt.Print("Could not create test directory:", err.Error())
  34. os.Exit(1)
  35. }
  36. // Run the tests
  37. res := m.Run()
  38. // Teardown
  39. err = os.RemoveAll(DBDIR)
  40. if err != nil {
  41. fmt.Print("Could not remove test directory:", err.Error())
  42. }
  43. os.Exit(res)
  44. }
  45. func TestFreePhysicalSlotManager(t *testing.T) {
  46. sf, err := file.NewDefaultStorageFile(DBDIR+"/test1", false)
  47. if err != nil {
  48. t.Error(err.Error())
  49. return
  50. }
  51. psf, err := paging.NewPagedStorageFile(sf)
  52. if err != nil {
  53. t.Error(err)
  54. return
  55. }
  56. fpsm := NewFreePhysicalSlotManager(psf, false)
  57. // Add some locations
  58. fpsm.Add(util.PackLocation(5, 22), 30)
  59. fpsm.Add(util.PackLocation(6, 23), 35)
  60. out := fpsm.String()
  61. if out != "FreePhysicalSlotManager: buckettest/test1 (onlyAppend:false lastMaxSlotSize:0)\n"+
  62. "Ids :[327702 393239]\n"+
  63. "Sizes:[30 35]" {
  64. t.Error("Unexpected output of FreePhysicalSlotManager:", out)
  65. }
  66. if err = fpsm.Flush(); err != nil {
  67. t.Error(nil)
  68. return
  69. }
  70. if len(fpsm.slots) != 0 || len(fpsm.sizes) != 0 {
  71. t.Error("Nothing should be left in the slot cache after a flush")
  72. return
  73. }
  74. // Check pages are allocated
  75. cursor := paging.NewPageCursor(fpsm.pager, view.TypeFreePhysicalSlotPage, 0)
  76. if page, err := cursor.Next(); page != 1 || err != nil {
  77. t.Error("Unexpected free physical slot page:", page, err)
  78. return
  79. }
  80. if page, err := cursor.Next(); page != 0 || err != nil {
  81. t.Error("Unexpected free physical slot page:", page, err)
  82. return
  83. }
  84. page := fpsm.pager.First(view.TypeFreePhysicalSlotPage)
  85. if page != 1 {
  86. t.Error("Unexpected first free physical slot page")
  87. return
  88. }
  89. fpspRec, err := sf.Get(1)
  90. if err != nil {
  91. t.Error(err)
  92. }
  93. fpsp := pageview.NewFreePhysicalSlotPage(fpspRec)
  94. if fsc := fpsp.FreeSlotCount(); fsc != 2 {
  95. t.Error("Unexpected number of stored free slots", fsc)
  96. }
  97. // Check that both slotinfos have been written
  98. if fpsp.SlotInfoLocation(0) != util.PackLocation(5, 22) {
  99. t.Error("Unexpected free slot info")
  100. return
  101. }
  102. if fpsp.SlotInfoLocation(1) != util.PackLocation(6, 23) {
  103. t.Error("Unexpected free slot info")
  104. return
  105. }
  106. sf.ReleaseInUse(fpspRec)
  107. // Check that we can find them
  108. loc, err := fpsm.Get(31)
  109. if err != nil {
  110. t.Error(err)
  111. return
  112. }
  113. if util.LocationRecord(loc) != 6 || util.LocationOffset(loc) != 23 {
  114. t.Error("Unexpected location was found", util.LocationRecord(loc), util.LocationOffset(loc))
  115. return
  116. }
  117. if fsc := fpsp.FreeSlotCount(); fsc != 1 {
  118. t.Error("Unexpected number of stored free slots", fsc)
  119. }
  120. // Test only append flag
  121. fpsm.onlyAppend = true
  122. loc, err = fpsm.Get(29)
  123. if err != nil || loc != 0 {
  124. t.Error("Unexpected onlyAppend result:", loc, err)
  125. }
  126. fpsm.onlyAppend = false
  127. loc, err = fpsm.Get(29)
  128. if err != nil {
  129. t.Error(err)
  130. return
  131. }
  132. if util.LocationRecord(loc) != 5 || util.LocationOffset(loc) != 22 {
  133. t.Error("Unexpected location was found", util.LocationRecord(loc), util.LocationOffset(loc))
  134. return
  135. }
  136. if fsc := fpsp.FreeSlotCount(); fsc != 0 {
  137. t.Error("Unexpected number of stored free slots", fsc)
  138. }
  139. if err := psf.Close(); err != nil {
  140. t.Error(err)
  141. return
  142. }
  143. }
  144. func TestFreePhysicalSlotManagerScale(t *testing.T) {
  145. sf, err := file.NewDefaultStorageFile(DBDIR+"/test2", false)
  146. if err != nil {
  147. t.Error(err.Error())
  148. return
  149. }
  150. shadow, err := file.NewDefaultStorageFile(DBDIR+"/test2_", false)
  151. if err != nil {
  152. t.Error(err.Error())
  153. return
  154. }
  155. psf, err := paging.NewPagedStorageFile(sf)
  156. if err != nil {
  157. t.Error(err)
  158. return
  159. }
  160. fpsm := NewFreePhysicalSlotManager(psf, false)
  161. // Add a lot of locations
  162. for i := 0; i < 5000; i++ {
  163. fpsm.Add(util.PackLocation(uint64(i), uint16(i%1000)), uint32(i%500))
  164. }
  165. // Check Flush and low level doFlush if a page can't be accessed
  166. if _, err := sf.Get(1); err != nil {
  167. t.Error(err)
  168. return
  169. }
  170. if err := fpsm.Flush(); err != file.ErrAlreadyInUse {
  171. t.Error("Unexpected flush result:", err)
  172. return
  173. }
  174. if i, err := fpsm.doFlush(1, 0); i != 0 || err != file.ErrAlreadyInUse {
  175. t.Error("Unexpected doFlush result:", i, err)
  176. return
  177. }
  178. if err := sf.ReleaseInUseID(1, false); err != nil {
  179. t.Error(err)
  180. return
  181. }
  182. // Check the doFlush error return in Flush when allocating new pages
  183. fpsm.storagefile = shadow
  184. if _, err := shadow.Get(1); err != nil {
  185. t.Error(err)
  186. return
  187. }
  188. if err := fpsm.Flush(); err != file.ErrAlreadyInUse {
  189. t.Error("Unexpected flush result:", err)
  190. return
  191. }
  192. if err := shadow.ReleaseInUseID(1, false); err != nil {
  193. t.Error(err)
  194. return
  195. }
  196. fpsm.storagefile = sf
  197. // Now do the real flush
  198. if err := fpsm.Flush(); err != nil {
  199. t.Error(err)
  200. return
  201. }
  202. // Count the allocated pages
  203. c, err := paging.CountPages(fpsm.pager, view.TypeFreePhysicalSlotPage)
  204. if c != 15 || err != nil {
  205. t.Error("Unexpected counting result:", c, err)
  206. return
  207. }
  208. // Check lastMaxSlotSize works
  209. if loc, err := fpsm.Get(600); loc != 0 || err != nil {
  210. t.Error("Unexpected Get result:", loc, err)
  211. return
  212. }
  213. if fpsm.lastMaxSlotSize != 499 {
  214. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  215. return
  216. }
  217. // Any subsequent call should fail more quickly
  218. if loc, err := fpsm.Get(600); loc != 0 || err != nil {
  219. t.Error("Unexpected Get result:", loc, err)
  220. return
  221. }
  222. // Free slot for size 499 should be on page 2
  223. if _, err := sf.Get(2); err != nil {
  224. t.Error(err)
  225. return
  226. }
  227. if loc, err := fpsm.Get(499); err != file.ErrAlreadyInUse {
  228. t.Error("Unexpected Get result:", loc, err)
  229. return
  230. }
  231. if err := sf.ReleaseInUseID(2, false); err != nil {
  232. t.Error(err)
  233. return
  234. }
  235. loc, err := fpsm.Get(499)
  236. if err != nil || loc != 32702963 {
  237. t.Error("Unexpected Get result:", err, loc)
  238. return
  239. }
  240. if fpsm.lastMaxSlotSize != 0 {
  241. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  242. return
  243. }
  244. // Next free slot for size 499 should be on page 3
  245. if _, err := sf.Get(3); err != nil {
  246. t.Error(err)
  247. return
  248. }
  249. if loc, err := fpsm.Get(499); err != file.ErrAlreadyInUse {
  250. t.Error("Unexpected Get result:", loc, err)
  251. return
  252. }
  253. if err := sf.ReleaseInUseID(3, false); err != nil {
  254. t.Error(err)
  255. return
  256. }
  257. // There was an error check that the lastMaxSlotSize was reset
  258. if fpsm.lastMaxSlotSize != 0 {
  259. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  260. return
  261. }
  262. loc, err = fpsm.Get(499)
  263. if err != nil || loc != 65471463 {
  264. t.Error("Unexpected Get result:", err, loc)
  265. return
  266. }
  267. if fpsm.lastMaxSlotSize != 0 {
  268. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  269. return
  270. }
  271. fpsm.Add(65471666, 699)
  272. if _, err := sf.Get(2); err != nil {
  273. t.Error(err)
  274. return
  275. }
  276. if err := fpsm.Flush(); err != file.ErrAlreadyInUse {
  277. t.Error("Unexpected Flush result:", err)
  278. return
  279. }
  280. if err := sf.ReleaseInUseID(2, false); err != nil {
  281. t.Error(err)
  282. return
  283. }
  284. if err := fpsm.Flush(); err != nil {
  285. t.Error(err)
  286. return
  287. }
  288. loc, err = fpsm.Get(698)
  289. if err != nil || loc != 65471666 {
  290. t.Error("Unexpected Get result:", err, loc)
  291. return
  292. }
  293. if err := psf.Close(); err != nil {
  294. t.Error(err)
  295. return
  296. }
  297. if err := shadow.Close(); err != nil {
  298. t.Error(err)
  299. return
  300. }
  301. }