freephysicalslotmanager_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. err = fpsm.Flush()
  171. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  172. t.Error("Unexpected flush result:", err)
  173. return
  174. }
  175. i, err := fpsm.doFlush(1, 0)
  176. if sfe, ok := err.(*file.StorageFileError); i != 0 || !ok || sfe.Type != file.ErrAlreadyInUse {
  177. t.Error("Unexpected doFlush result:", i, err)
  178. return
  179. }
  180. if err := sf.ReleaseInUseID(1, false); err != nil {
  181. t.Error(err)
  182. return
  183. }
  184. // Check the doFlush error return in Flush when allocating new pages
  185. fpsm.storagefile = shadow
  186. if _, err := shadow.Get(1); err != nil {
  187. t.Error(err)
  188. return
  189. }
  190. err = fpsm.Flush()
  191. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  192. t.Error("Unexpected flush result:", err)
  193. return
  194. }
  195. if err := shadow.ReleaseInUseID(1, false); err != nil {
  196. t.Error(err)
  197. return
  198. }
  199. fpsm.storagefile = sf
  200. // Now do the real flush
  201. if err := fpsm.Flush(); err != nil {
  202. t.Error(err)
  203. return
  204. }
  205. // Count the allocated pages
  206. c, err := paging.CountPages(fpsm.pager, view.TypeFreePhysicalSlotPage)
  207. if c != 15 || err != nil {
  208. t.Error("Unexpected counting result:", c, err)
  209. return
  210. }
  211. // Check lastMaxSlotSize works
  212. if loc, err := fpsm.Get(600); loc != 0 || err != nil {
  213. t.Error("Unexpected Get result:", loc, err)
  214. return
  215. }
  216. if fpsm.lastMaxSlotSize != 499 {
  217. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  218. return
  219. }
  220. // Any subsequent call should fail more quickly
  221. if loc, err := fpsm.Get(600); loc != 0 || err != nil {
  222. t.Error("Unexpected Get result:", loc, err)
  223. return
  224. }
  225. // Free slot for size 499 should be on page 2
  226. if _, err := sf.Get(2); err != nil {
  227. t.Error(err)
  228. return
  229. }
  230. loc, err := fpsm.Get(499)
  231. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  232. t.Error("Unexpected Get result:", loc, err)
  233. return
  234. }
  235. if err := sf.ReleaseInUseID(2, false); err != nil {
  236. t.Error(err)
  237. return
  238. }
  239. loc, err = fpsm.Get(499)
  240. if err != nil || loc != 32702963 {
  241. t.Error("Unexpected Get result:", err, loc)
  242. return
  243. }
  244. if fpsm.lastMaxSlotSize != 0 {
  245. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  246. return
  247. }
  248. // Next free slot for size 499 should be on page 3
  249. if _, err := sf.Get(3); err != nil {
  250. t.Error(err)
  251. return
  252. }
  253. loc, err = fpsm.Get(499)
  254. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  255. t.Error("Unexpected Get result:", loc, err)
  256. return
  257. }
  258. if err := sf.ReleaseInUseID(3, false); err != nil {
  259. t.Error(err)
  260. return
  261. }
  262. // There was an error check that the lastMaxSlotSize was reset
  263. if fpsm.lastMaxSlotSize != 0 {
  264. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  265. return
  266. }
  267. loc, err = fpsm.Get(499)
  268. if err != nil || loc != 65471463 {
  269. t.Error("Unexpected Get result:", err, loc)
  270. return
  271. }
  272. if fpsm.lastMaxSlotSize != 0 {
  273. t.Error("Unexpected lastMaxSlotSize:", fpsm.lastMaxSlotSize)
  274. return
  275. }
  276. fpsm.Add(65471666, 699)
  277. if _, err := sf.Get(2); err != nil {
  278. t.Error(err)
  279. return
  280. }
  281. err = fpsm.Flush()
  282. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  283. t.Error("Unexpected Flush result:", err)
  284. return
  285. }
  286. if err := sf.ReleaseInUseID(2, false); err != nil {
  287. t.Error(err)
  288. return
  289. }
  290. if err := fpsm.Flush(); err != nil {
  291. t.Error(err)
  292. return
  293. }
  294. loc, err = fpsm.Get(698)
  295. if err != nil || loc != 65471666 {
  296. t.Error("Unexpected Get result:", err, loc)
  297. return
  298. }
  299. if err := psf.Close(); err != nil {
  300. t.Error(err)
  301. return
  302. }
  303. if err := shadow.Close(); err != nil {
  304. t.Error(err)
  305. return
  306. }
  307. }