ldbstore_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package storage
  17. import (
  18. "bytes"
  19. "context"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "sync"
  24. "testing"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/swarm/log"
  28. "github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
  29. ldberrors "github.com/syndtr/goleveldb/leveldb/errors"
  30. )
  31. type testDbStore struct {
  32. *LDBStore
  33. dir string
  34. }
  35. func newTestDbStore(mock bool, trusted bool) (*testDbStore, func(), error) {
  36. dir, err := ioutil.TempDir("", "bzz-storage-test")
  37. if err != nil {
  38. return nil, func() {}, err
  39. }
  40. var db *LDBStore
  41. storeparams := NewDefaultStoreParams()
  42. params := NewLDBStoreParams(storeparams, dir)
  43. params.Po = testPoFunc
  44. if mock {
  45. globalStore := mem.NewGlobalStore()
  46. addr := common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
  47. mockStore := globalStore.NewNodeStore(addr)
  48. db, err = NewMockDbStore(params, mockStore)
  49. } else {
  50. db, err = NewLDBStore(params)
  51. }
  52. cleanup := func() {
  53. if db != nil {
  54. db.Close()
  55. }
  56. err = os.RemoveAll(dir)
  57. if err != nil {
  58. panic(fmt.Sprintf("db cleanup failed: %v", err))
  59. }
  60. }
  61. return &testDbStore{db, dir}, cleanup, err
  62. }
  63. func testPoFunc(k Address) (ret uint8) {
  64. basekey := make([]byte, 32)
  65. return uint8(Proximity(basekey[:], k[:]))
  66. }
  67. func (db *testDbStore) close() {
  68. db.Close()
  69. err := os.RemoveAll(db.dir)
  70. if err != nil {
  71. panic(err)
  72. }
  73. }
  74. func testDbStoreRandom(n int, processors int, chunksize int64, mock bool, t *testing.T) {
  75. db, cleanup, err := newTestDbStore(mock, true)
  76. defer cleanup()
  77. if err != nil {
  78. t.Fatalf("init dbStore failed: %v", err)
  79. }
  80. testStoreRandom(db, processors, n, chunksize, t)
  81. }
  82. func testDbStoreCorrect(n int, processors int, chunksize int64, mock bool, t *testing.T) {
  83. db, cleanup, err := newTestDbStore(mock, false)
  84. defer cleanup()
  85. if err != nil {
  86. t.Fatalf("init dbStore failed: %v", err)
  87. }
  88. testStoreCorrect(db, processors, n, chunksize, t)
  89. }
  90. func TestDbStoreRandom_1(t *testing.T) {
  91. testDbStoreRandom(1, 1, 0, false, t)
  92. }
  93. func TestDbStoreCorrect_1(t *testing.T) {
  94. testDbStoreCorrect(1, 1, 4096, false, t)
  95. }
  96. func TestDbStoreRandom_1_5k(t *testing.T) {
  97. testDbStoreRandom(8, 5000, 0, false, t)
  98. }
  99. func TestDbStoreRandom_8_5k(t *testing.T) {
  100. testDbStoreRandom(8, 5000, 0, false, t)
  101. }
  102. func TestDbStoreCorrect_1_5k(t *testing.T) {
  103. testDbStoreCorrect(1, 5000, 4096, false, t)
  104. }
  105. func TestDbStoreCorrect_8_5k(t *testing.T) {
  106. testDbStoreCorrect(8, 5000, 4096, false, t)
  107. }
  108. func TestMockDbStoreRandom_1(t *testing.T) {
  109. testDbStoreRandom(1, 1, 0, true, t)
  110. }
  111. func TestMockDbStoreCorrect_1(t *testing.T) {
  112. testDbStoreCorrect(1, 1, 4096, true, t)
  113. }
  114. func TestMockDbStoreRandom_1_5k(t *testing.T) {
  115. testDbStoreRandom(8, 5000, 0, true, t)
  116. }
  117. func TestMockDbStoreRandom_8_5k(t *testing.T) {
  118. testDbStoreRandom(8, 5000, 0, true, t)
  119. }
  120. func TestMockDbStoreCorrect_1_5k(t *testing.T) {
  121. testDbStoreCorrect(1, 5000, 4096, true, t)
  122. }
  123. func TestMockDbStoreCorrect_8_5k(t *testing.T) {
  124. testDbStoreCorrect(8, 5000, 4096, true, t)
  125. }
  126. func testDbStoreNotFound(t *testing.T, mock bool) {
  127. db, cleanup, err := newTestDbStore(mock, false)
  128. defer cleanup()
  129. if err != nil {
  130. t.Fatalf("init dbStore failed: %v", err)
  131. }
  132. _, err = db.Get(context.TODO(), ZeroAddr)
  133. if err != ErrChunkNotFound {
  134. t.Errorf("Expected ErrChunkNotFound, got %v", err)
  135. }
  136. }
  137. func TestDbStoreNotFound(t *testing.T) {
  138. testDbStoreNotFound(t, false)
  139. }
  140. func TestMockDbStoreNotFound(t *testing.T) {
  141. testDbStoreNotFound(t, true)
  142. }
  143. func testIterator(t *testing.T, mock bool) {
  144. var chunkcount int = 32
  145. var i int
  146. var poc uint
  147. chunkkeys := NewAddressCollection(chunkcount)
  148. chunkkeys_results := NewAddressCollection(chunkcount)
  149. db, cleanup, err := newTestDbStore(mock, false)
  150. defer cleanup()
  151. if err != nil {
  152. t.Fatalf("init dbStore failed: %v", err)
  153. }
  154. chunks := GenerateRandomChunks(DefaultChunkSize, chunkcount)
  155. wg := &sync.WaitGroup{}
  156. wg.Add(len(chunks))
  157. for i = 0; i < len(chunks); i++ {
  158. db.Put(context.TODO(), chunks[i])
  159. chunkkeys[i] = chunks[i].Addr
  160. j := i
  161. go func() {
  162. defer wg.Done()
  163. <-chunks[j].dbStoredC
  164. }()
  165. }
  166. //testSplit(m, l, 128, chunkkeys, t)
  167. for i = 0; i < len(chunkkeys); i++ {
  168. log.Trace(fmt.Sprintf("Chunk array pos %d/%d: '%v'", i, chunkcount, chunkkeys[i]))
  169. }
  170. wg.Wait()
  171. i = 0
  172. for poc = 0; poc <= 255; poc++ {
  173. err := db.SyncIterator(0, uint64(chunkkeys.Len()), uint8(poc), func(k Address, n uint64) bool {
  174. log.Trace(fmt.Sprintf("Got key %v number %d poc %d", k, n, uint8(poc)))
  175. chunkkeys_results[n-1] = k
  176. i++
  177. return true
  178. })
  179. if err != nil {
  180. t.Fatalf("Iterator call failed: %v", err)
  181. }
  182. }
  183. for i = 0; i < chunkcount; i++ {
  184. if !bytes.Equal(chunkkeys[i], chunkkeys_results[i]) {
  185. t.Fatalf("Chunk put #%d key '%v' does not match iterator's key '%v'", i, chunkkeys[i], chunkkeys_results[i])
  186. }
  187. }
  188. }
  189. func TestIterator(t *testing.T) {
  190. testIterator(t, false)
  191. }
  192. func TestMockIterator(t *testing.T) {
  193. testIterator(t, true)
  194. }
  195. func benchmarkDbStorePut(n int, processors int, chunksize int64, mock bool, b *testing.B) {
  196. db, cleanup, err := newTestDbStore(mock, true)
  197. defer cleanup()
  198. if err != nil {
  199. b.Fatalf("init dbStore failed: %v", err)
  200. }
  201. benchmarkStorePut(db, processors, n, chunksize, b)
  202. }
  203. func benchmarkDbStoreGet(n int, processors int, chunksize int64, mock bool, b *testing.B) {
  204. db, cleanup, err := newTestDbStore(mock, true)
  205. defer cleanup()
  206. if err != nil {
  207. b.Fatalf("init dbStore failed: %v", err)
  208. }
  209. benchmarkStoreGet(db, processors, n, chunksize, b)
  210. }
  211. func BenchmarkDbStorePut_1_500(b *testing.B) {
  212. benchmarkDbStorePut(500, 1, 4096, false, b)
  213. }
  214. func BenchmarkDbStorePut_8_500(b *testing.B) {
  215. benchmarkDbStorePut(500, 8, 4096, false, b)
  216. }
  217. func BenchmarkDbStoreGet_1_500(b *testing.B) {
  218. benchmarkDbStoreGet(500, 1, 4096, false, b)
  219. }
  220. func BenchmarkDbStoreGet_8_500(b *testing.B) {
  221. benchmarkDbStoreGet(500, 8, 4096, false, b)
  222. }
  223. func BenchmarkMockDbStorePut_1_500(b *testing.B) {
  224. benchmarkDbStorePut(500, 1, 4096, true, b)
  225. }
  226. func BenchmarkMockDbStorePut_8_500(b *testing.B) {
  227. benchmarkDbStorePut(500, 8, 4096, true, b)
  228. }
  229. func BenchmarkMockDbStoreGet_1_500(b *testing.B) {
  230. benchmarkDbStoreGet(500, 1, 4096, true, b)
  231. }
  232. func BenchmarkMockDbStoreGet_8_500(b *testing.B) {
  233. benchmarkDbStoreGet(500, 8, 4096, true, b)
  234. }
  235. // TestLDBStoreWithoutCollectGarbage tests that we can put a number of random chunks in the LevelDB store, and
  236. // retrieve them, provided we don't hit the garbage collection
  237. func TestLDBStoreWithoutCollectGarbage(t *testing.T) {
  238. capacity := 50
  239. n := 10
  240. ldb, cleanup := newLDBStore(t)
  241. ldb.setCapacity(uint64(capacity))
  242. defer cleanup()
  243. chunks := []*Chunk{}
  244. for i := 0; i < n; i++ {
  245. c := GenerateRandomChunk(DefaultChunkSize)
  246. chunks = append(chunks, c)
  247. log.Trace("generate random chunk", "idx", i, "chunk", c)
  248. }
  249. for i := 0; i < n; i++ {
  250. go ldb.Put(context.TODO(), chunks[i])
  251. }
  252. // wait for all chunks to be stored
  253. for i := 0; i < n; i++ {
  254. <-chunks[i].dbStoredC
  255. }
  256. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  257. for i := 0; i < n; i++ {
  258. ret, err := ldb.Get(context.TODO(), chunks[i].Addr)
  259. if err != nil {
  260. t.Fatal(err)
  261. }
  262. if !bytes.Equal(ret.SData, chunks[i].SData) {
  263. t.Fatal("expected to get the same data back, but got smth else")
  264. }
  265. log.Info("got back chunk", "chunk", ret)
  266. }
  267. if ldb.entryCnt != uint64(n+1) {
  268. t.Fatalf("expected entryCnt to be equal to %v, but got %v", n+1, ldb.entryCnt)
  269. }
  270. if ldb.accessCnt != uint64(2*n+1) {
  271. t.Fatalf("expected accessCnt to be equal to %v, but got %v", n+1, ldb.accessCnt)
  272. }
  273. }
  274. // TestLDBStoreCollectGarbage tests that we can put more chunks than LevelDB's capacity, and
  275. // retrieve only some of them, because garbage collection must have cleared some of them
  276. func TestLDBStoreCollectGarbage(t *testing.T) {
  277. capacity := 500
  278. n := 2000
  279. ldb, cleanup := newLDBStore(t)
  280. ldb.setCapacity(uint64(capacity))
  281. defer cleanup()
  282. chunks := []*Chunk{}
  283. for i := 0; i < n; i++ {
  284. c := GenerateRandomChunk(DefaultChunkSize)
  285. chunks = append(chunks, c)
  286. log.Trace("generate random chunk", "idx", i, "chunk", c)
  287. }
  288. for i := 0; i < n; i++ {
  289. ldb.Put(context.TODO(), chunks[i])
  290. }
  291. // wait for all chunks to be stored
  292. for i := 0; i < n; i++ {
  293. <-chunks[i].dbStoredC
  294. }
  295. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  296. // wait for garbage collection to kick in on the responsible actor
  297. time.Sleep(5 * time.Second)
  298. var missing int
  299. for i := 0; i < n; i++ {
  300. ret, err := ldb.Get(context.TODO(), chunks[i].Addr)
  301. if err == ErrChunkNotFound || err == ldberrors.ErrNotFound {
  302. missing++
  303. continue
  304. }
  305. if err != nil {
  306. t.Fatal(err)
  307. }
  308. if !bytes.Equal(ret.SData, chunks[i].SData) {
  309. t.Fatal("expected to get the same data back, but got smth else")
  310. }
  311. log.Trace("got back chunk", "chunk", ret)
  312. }
  313. if missing < n-capacity {
  314. t.Fatalf("gc failure: expected to miss %v chunks, but only %v are actually missing", n-capacity, missing)
  315. }
  316. log.Info("ldbstore", "total", n, "missing", missing, "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  317. }
  318. // TestLDBStoreAddRemove tests that we can put and then delete a given chunk
  319. func TestLDBStoreAddRemove(t *testing.T) {
  320. ldb, cleanup := newLDBStore(t)
  321. ldb.setCapacity(200)
  322. defer cleanup()
  323. n := 100
  324. chunks := []*Chunk{}
  325. for i := 0; i < n; i++ {
  326. c := GenerateRandomChunk(DefaultChunkSize)
  327. chunks = append(chunks, c)
  328. log.Trace("generate random chunk", "idx", i, "chunk", c)
  329. }
  330. for i := 0; i < n; i++ {
  331. go ldb.Put(context.TODO(), chunks[i])
  332. }
  333. // wait for all chunks to be stored before continuing
  334. for i := 0; i < n; i++ {
  335. <-chunks[i].dbStoredC
  336. }
  337. for i := 0; i < n; i++ {
  338. // delete all even index chunks
  339. if i%2 == 0 {
  340. key := chunks[i].Addr
  341. ikey := getIndexKey(key)
  342. var indx dpaDBIndex
  343. ldb.tryAccessIdx(ikey, &indx)
  344. ldb.delete(indx.Idx, ikey, ldb.po(key))
  345. }
  346. }
  347. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  348. for i := 0; i < n; i++ {
  349. ret, err := ldb.Get(context.TODO(), chunks[i].Addr)
  350. if i%2 == 0 {
  351. // expect even chunks to be missing
  352. if err == nil || ret != nil {
  353. t.Fatal("expected chunk to be missing, but got no error")
  354. }
  355. } else {
  356. // expect odd chunks to be retrieved successfully
  357. if err != nil {
  358. t.Fatalf("expected no error, but got %s", err)
  359. }
  360. if !bytes.Equal(ret.SData, chunks[i].SData) {
  361. t.Fatal("expected to get the same data back, but got smth else")
  362. }
  363. }
  364. }
  365. }
  366. // TestLDBStoreRemoveThenCollectGarbage tests that we can delete chunks and that we can trigger garbage collection
  367. func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
  368. capacity := 10
  369. ldb, cleanup := newLDBStore(t)
  370. ldb.setCapacity(uint64(capacity))
  371. n := 7
  372. chunks := []*Chunk{}
  373. for i := 0; i < capacity; i++ {
  374. c := GenerateRandomChunk(DefaultChunkSize)
  375. chunks = append(chunks, c)
  376. log.Trace("generate random chunk", "idx", i, "chunk", c)
  377. }
  378. for i := 0; i < n; i++ {
  379. ldb.Put(context.TODO(), chunks[i])
  380. }
  381. // wait for all chunks to be stored before continuing
  382. for i := 0; i < n; i++ {
  383. <-chunks[i].dbStoredC
  384. }
  385. // delete all chunks
  386. for i := 0; i < n; i++ {
  387. key := chunks[i].Addr
  388. ikey := getIndexKey(key)
  389. var indx dpaDBIndex
  390. ldb.tryAccessIdx(ikey, &indx)
  391. ldb.delete(indx.Idx, ikey, ldb.po(key))
  392. }
  393. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  394. cleanup()
  395. ldb, cleanup = newLDBStore(t)
  396. ldb.setCapacity(uint64(capacity))
  397. n = 10
  398. for i := 0; i < n; i++ {
  399. ldb.Put(context.TODO(), chunks[i])
  400. }
  401. // wait for all chunks to be stored before continuing
  402. for i := 0; i < n; i++ {
  403. <-chunks[i].dbStoredC
  404. }
  405. // expect for first chunk to be missing, because it has the smallest access value
  406. idx := 0
  407. ret, err := ldb.Get(context.TODO(), chunks[idx].Addr)
  408. if err == nil || ret != nil {
  409. t.Fatal("expected first chunk to be missing, but got no error")
  410. }
  411. // expect for last chunk to be present, as it has the largest access value
  412. idx = 9
  413. ret, err = ldb.Get(context.TODO(), chunks[idx].Addr)
  414. if err != nil {
  415. t.Fatalf("expected no error, but got %s", err)
  416. }
  417. if !bytes.Equal(ret.SData, chunks[idx].SData) {
  418. t.Fatal("expected to get the same data back, but got smth else")
  419. }
  420. }