ldbstore_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. ch "github.com/ethereum/go-ethereum/swarm/chunk"
  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, 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, n, chunksize, t)
  81. }
  82. func testDbStoreCorrect(n 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, n, chunksize, t)
  89. }
  90. func TestDbStoreRandom_1(t *testing.T) {
  91. testDbStoreRandom(1, 0, false, t)
  92. }
  93. func TestDbStoreCorrect_1(t *testing.T) {
  94. testDbStoreCorrect(1, 4096, false, t)
  95. }
  96. func TestDbStoreRandom_1k(t *testing.T) {
  97. testDbStoreRandom(1000, 0, false, t)
  98. }
  99. func TestDbStoreCorrect_1k(t *testing.T) {
  100. testDbStoreCorrect(1000, 4096, false, t)
  101. }
  102. func TestMockDbStoreRandom_1(t *testing.T) {
  103. testDbStoreRandom(1, 0, true, t)
  104. }
  105. func TestMockDbStoreCorrect_1(t *testing.T) {
  106. testDbStoreCorrect(1, 4096, true, t)
  107. }
  108. func TestMockDbStoreRandom_1k(t *testing.T) {
  109. testDbStoreRandom(1000, 0, true, t)
  110. }
  111. func TestMockDbStoreCorrect_1k(t *testing.T) {
  112. testDbStoreCorrect(1000, 4096, true, t)
  113. }
  114. func testDbStoreNotFound(t *testing.T, mock bool) {
  115. db, cleanup, err := newTestDbStore(mock, false)
  116. defer cleanup()
  117. if err != nil {
  118. t.Fatalf("init dbStore failed: %v", err)
  119. }
  120. _, err = db.Get(context.TODO(), ZeroAddr)
  121. if err != ErrChunkNotFound {
  122. t.Errorf("Expected ErrChunkNotFound, got %v", err)
  123. }
  124. }
  125. func TestDbStoreNotFound(t *testing.T) {
  126. testDbStoreNotFound(t, false)
  127. }
  128. func TestMockDbStoreNotFound(t *testing.T) {
  129. testDbStoreNotFound(t, true)
  130. }
  131. func testIterator(t *testing.T, mock bool) {
  132. var chunkcount int = 32
  133. var i int
  134. var poc uint
  135. chunkkeys := NewAddressCollection(chunkcount)
  136. chunkkeys_results := NewAddressCollection(chunkcount)
  137. db, cleanup, err := newTestDbStore(mock, false)
  138. defer cleanup()
  139. if err != nil {
  140. t.Fatalf("init dbStore failed: %v", err)
  141. }
  142. chunks := GenerateRandomChunks(ch.DefaultSize, chunkcount)
  143. for i = 0; i < len(chunks); i++ {
  144. chunkkeys[i] = chunks[i].Address()
  145. err := db.Put(context.TODO(), chunks[i])
  146. if err != nil {
  147. t.Fatalf("dbStore.Put failed: %v", err)
  148. }
  149. }
  150. for i = 0; i < len(chunkkeys); i++ {
  151. log.Trace(fmt.Sprintf("Chunk array pos %d/%d: '%v'", i, chunkcount, chunkkeys[i]))
  152. }
  153. i = 0
  154. for poc = 0; poc <= 255; poc++ {
  155. err := db.SyncIterator(0, uint64(chunkkeys.Len()), uint8(poc), func(k Address, n uint64) bool {
  156. log.Trace(fmt.Sprintf("Got key %v number %d poc %d", k, n, uint8(poc)))
  157. chunkkeys_results[n] = k
  158. i++
  159. return true
  160. })
  161. if err != nil {
  162. t.Fatalf("Iterator call failed: %v", err)
  163. }
  164. }
  165. for i = 0; i < chunkcount; i++ {
  166. if !bytes.Equal(chunkkeys[i], chunkkeys_results[i]) {
  167. t.Fatalf("Chunk put #%d key '%v' does not match iterator's key '%v'", i, chunkkeys[i], chunkkeys_results[i])
  168. }
  169. }
  170. }
  171. func TestIterator(t *testing.T) {
  172. testIterator(t, false)
  173. }
  174. func TestMockIterator(t *testing.T) {
  175. testIterator(t, true)
  176. }
  177. func benchmarkDbStorePut(n int, processors int, chunksize int64, mock bool, b *testing.B) {
  178. db, cleanup, err := newTestDbStore(mock, true)
  179. defer cleanup()
  180. if err != nil {
  181. b.Fatalf("init dbStore failed: %v", err)
  182. }
  183. benchmarkStorePut(db, n, chunksize, b)
  184. }
  185. func benchmarkDbStoreGet(n int, processors int, chunksize int64, mock bool, b *testing.B) {
  186. db, cleanup, err := newTestDbStore(mock, true)
  187. defer cleanup()
  188. if err != nil {
  189. b.Fatalf("init dbStore failed: %v", err)
  190. }
  191. benchmarkStoreGet(db, n, chunksize, b)
  192. }
  193. func BenchmarkDbStorePut_1_500(b *testing.B) {
  194. benchmarkDbStorePut(500, 1, 4096, false, b)
  195. }
  196. func BenchmarkDbStorePut_8_500(b *testing.B) {
  197. benchmarkDbStorePut(500, 8, 4096, false, b)
  198. }
  199. func BenchmarkDbStoreGet_1_500(b *testing.B) {
  200. benchmarkDbStoreGet(500, 1, 4096, false, b)
  201. }
  202. func BenchmarkDbStoreGet_8_500(b *testing.B) {
  203. benchmarkDbStoreGet(500, 8, 4096, false, b)
  204. }
  205. func BenchmarkMockDbStorePut_1_500(b *testing.B) {
  206. benchmarkDbStorePut(500, 1, 4096, true, b)
  207. }
  208. func BenchmarkMockDbStorePut_8_500(b *testing.B) {
  209. benchmarkDbStorePut(500, 8, 4096, true, b)
  210. }
  211. func BenchmarkMockDbStoreGet_1_500(b *testing.B) {
  212. benchmarkDbStoreGet(500, 1, 4096, true, b)
  213. }
  214. func BenchmarkMockDbStoreGet_8_500(b *testing.B) {
  215. benchmarkDbStoreGet(500, 8, 4096, true, b)
  216. }
  217. // TestLDBStoreWithoutCollectGarbage tests that we can put a number of random chunks in the LevelDB store, and
  218. // retrieve them, provided we don't hit the garbage collection
  219. func TestLDBStoreWithoutCollectGarbage(t *testing.T) {
  220. capacity := 50
  221. n := 10
  222. ldb, cleanup := newLDBStore(t)
  223. ldb.setCapacity(uint64(capacity))
  224. defer cleanup()
  225. chunks, err := mputRandomChunks(ldb, n, int64(ch.DefaultSize))
  226. if err != nil {
  227. t.Fatal(err.Error())
  228. }
  229. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  230. for _, ch := range chunks {
  231. ret, err := ldb.Get(context.TODO(), ch.Address())
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if !bytes.Equal(ret.Data(), ch.Data()) {
  236. t.Fatal("expected to get the same data back, but got smth else")
  237. }
  238. }
  239. if ldb.entryCnt != uint64(n) {
  240. t.Fatalf("expected entryCnt to be equal to %v, but got %v", n, ldb.entryCnt)
  241. }
  242. if ldb.accessCnt != uint64(2*n) {
  243. t.Fatalf("expected accessCnt to be equal to %v, but got %v", 2*n, ldb.accessCnt)
  244. }
  245. }
  246. // TestLDBStoreCollectGarbage tests that we can put more chunks than LevelDB's capacity, and
  247. // retrieve only some of them, because garbage collection must have cleared some of them
  248. func TestLDBStoreCollectGarbage(t *testing.T) {
  249. capacity := 500
  250. n := 2000
  251. ldb, cleanup := newLDBStore(t)
  252. ldb.setCapacity(uint64(capacity))
  253. defer cleanup()
  254. chunks, err := mputRandomChunks(ldb, n, int64(ch.DefaultSize))
  255. if err != nil {
  256. t.Fatal(err.Error())
  257. }
  258. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  259. // wait for garbage collection to kick in on the responsible actor
  260. time.Sleep(1 * time.Second)
  261. var missing int
  262. for _, ch := range chunks {
  263. ret, err := ldb.Get(context.Background(), ch.Address())
  264. if err == ErrChunkNotFound || err == ldberrors.ErrNotFound {
  265. missing++
  266. continue
  267. }
  268. if err != nil {
  269. t.Fatal(err)
  270. }
  271. if !bytes.Equal(ret.Data(), ch.Data()) {
  272. t.Fatal("expected to get the same data back, but got smth else")
  273. }
  274. log.Trace("got back chunk", "chunk", ret)
  275. }
  276. if missing < n-capacity {
  277. t.Fatalf("gc failure: expected to miss %v chunks, but only %v are actually missing", n-capacity, missing)
  278. }
  279. log.Info("ldbstore", "total", n, "missing", missing, "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  280. }
  281. // TestLDBStoreAddRemove tests that we can put and then delete a given chunk
  282. func TestLDBStoreAddRemove(t *testing.T) {
  283. ldb, cleanup := newLDBStore(t)
  284. ldb.setCapacity(200)
  285. defer cleanup()
  286. n := 100
  287. chunks, err := mputRandomChunks(ldb, n, int64(ch.DefaultSize))
  288. if err != nil {
  289. t.Fatalf(err.Error())
  290. }
  291. for i := 0; i < n; i++ {
  292. // delete all even index chunks
  293. if i%2 == 0 {
  294. ldb.Delete(chunks[i].Address())
  295. }
  296. }
  297. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  298. for i := 0; i < n; i++ {
  299. ret, err := ldb.Get(nil, chunks[i].Address())
  300. if i%2 == 0 {
  301. // expect even chunks to be missing
  302. if err == nil {
  303. // if err != ErrChunkNotFound {
  304. t.Fatal("expected chunk to be missing, but got no error")
  305. }
  306. } else {
  307. // expect odd chunks to be retrieved successfully
  308. if err != nil {
  309. t.Fatalf("expected no error, but got %s", err)
  310. }
  311. if !bytes.Equal(ret.Data(), chunks[i].Data()) {
  312. t.Fatal("expected to get the same data back, but got smth else")
  313. }
  314. }
  315. }
  316. }
  317. // TestLDBStoreRemoveThenCollectGarbage tests that we can delete chunks and that we can trigger garbage collection
  318. func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
  319. capacity := 11
  320. surplus := 4
  321. ldb, cleanup := newLDBStore(t)
  322. ldb.setCapacity(uint64(capacity))
  323. n := capacity
  324. chunks := []Chunk{}
  325. for i := 0; i < n+surplus; i++ {
  326. c := GenerateRandomChunk(ch.DefaultSize)
  327. chunks = append(chunks, c)
  328. log.Trace("generate random chunk", "idx", i, "chunk", c)
  329. }
  330. for i := 0; i < n; i++ {
  331. ldb.Put(context.TODO(), chunks[i])
  332. }
  333. // delete all chunks
  334. for i := 0; i < n; i++ {
  335. ldb.Delete(chunks[i].Address())
  336. }
  337. log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
  338. if ldb.entryCnt != 0 {
  339. t.Fatalf("ldb.entrCnt expected 0 got %v", ldb.entryCnt)
  340. }
  341. expAccessCnt := uint64(n * 2)
  342. if ldb.accessCnt != expAccessCnt {
  343. t.Fatalf("ldb.accessCnt expected %v got %v", expAccessCnt, ldb.entryCnt)
  344. }
  345. cleanup()
  346. ldb, cleanup = newLDBStore(t)
  347. capacity = 10
  348. ldb.setCapacity(uint64(capacity))
  349. defer cleanup()
  350. n = capacity + surplus
  351. for i := 0; i < n; i++ {
  352. ldb.Put(context.TODO(), chunks[i])
  353. }
  354. // wait for garbage collection
  355. time.Sleep(1 * time.Second)
  356. // expect first surplus chunks to be missing, because they have the smallest access value
  357. for i := 0; i < surplus; i++ {
  358. _, err := ldb.Get(context.TODO(), chunks[i].Address())
  359. if err == nil {
  360. t.Fatal("expected surplus chunk to be missing, but got no error")
  361. }
  362. }
  363. // expect last chunks to be present, as they have the largest access value
  364. for i := surplus; i < surplus+capacity; i++ {
  365. ret, err := ldb.Get(context.TODO(), chunks[i].Address())
  366. if err != nil {
  367. t.Fatalf("chunk %v: expected no error, but got %s", i, err)
  368. }
  369. if !bytes.Equal(ret.Data(), chunks[i].Data()) {
  370. t.Fatal("expected to get the same data back, but got smth else")
  371. }
  372. }
  373. }