dbstore.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. // disk storage layer for the package bzz
  17. // DbStore implements the ChunkStore interface and is used by the DPA as
  18. // persistent storage of chunks
  19. // it implements purging based on access count allowing for external control of
  20. // max capacity
  21. package storage
  22. import (
  23. "archive/tar"
  24. "bytes"
  25. "encoding/binary"
  26. "encoding/hex"
  27. "fmt"
  28. "io"
  29. "io/ioutil"
  30. "sync"
  31. "github.com/ethereum/go-ethereum/log"
  32. "github.com/ethereum/go-ethereum/rlp"
  33. "github.com/syndtr/goleveldb/leveldb"
  34. "github.com/syndtr/goleveldb/leveldb/iterator"
  35. )
  36. const (
  37. defaultDbCapacity = 5000000
  38. defaultRadius = 0 // not yet used
  39. gcArraySize = 10000
  40. gcArrayFreeRatio = 0.1
  41. // key prefixes for leveldb storage
  42. kpIndex = 0
  43. kpData = 1
  44. )
  45. var (
  46. keyAccessCnt = []byte{2}
  47. keyEntryCnt = []byte{3}
  48. keyDataIdx = []byte{4}
  49. keyGCPos = []byte{5}
  50. )
  51. type gcItem struct {
  52. idx uint64
  53. value uint64
  54. idxKey []byte
  55. }
  56. type DbStore struct {
  57. db *LDBDatabase
  58. // this should be stored in db, accessed transactionally
  59. entryCnt, accessCnt, dataIdx, capacity uint64
  60. gcPos, gcStartPos []byte
  61. gcArray []*gcItem
  62. hashfunc Hasher
  63. lock sync.Mutex
  64. }
  65. func NewDbStore(path string, hash Hasher, capacity uint64, radius int) (s *DbStore, err error) {
  66. s = new(DbStore)
  67. s.hashfunc = hash
  68. s.db, err = NewLDBDatabase(path)
  69. if err != nil {
  70. return
  71. }
  72. s.setCapacity(capacity)
  73. s.gcStartPos = make([]byte, 1)
  74. s.gcStartPos[0] = kpIndex
  75. s.gcArray = make([]*gcItem, gcArraySize)
  76. data, _ := s.db.Get(keyEntryCnt)
  77. s.entryCnt = BytesToU64(data)
  78. data, _ = s.db.Get(keyAccessCnt)
  79. s.accessCnt = BytesToU64(data)
  80. data, _ = s.db.Get(keyDataIdx)
  81. s.dataIdx = BytesToU64(data)
  82. s.gcPos, _ = s.db.Get(keyGCPos)
  83. if s.gcPos == nil {
  84. s.gcPos = s.gcStartPos
  85. }
  86. return
  87. }
  88. type dpaDBIndex struct {
  89. Idx uint64
  90. Access uint64
  91. }
  92. func BytesToU64(data []byte) uint64 {
  93. if len(data) < 8 {
  94. return 0
  95. }
  96. return binary.LittleEndian.Uint64(data)
  97. }
  98. func U64ToBytes(val uint64) []byte {
  99. data := make([]byte, 8)
  100. binary.LittleEndian.PutUint64(data, val)
  101. return data
  102. }
  103. func getIndexGCValue(index *dpaDBIndex) uint64 {
  104. return index.Access
  105. }
  106. func (s *DbStore) updateIndexAccess(index *dpaDBIndex) {
  107. index.Access = s.accessCnt
  108. }
  109. func getIndexKey(hash Key) []byte {
  110. HashSize := len(hash)
  111. key := make([]byte, HashSize+1)
  112. key[0] = 0
  113. copy(key[1:], hash[:])
  114. return key
  115. }
  116. func getDataKey(idx uint64) []byte {
  117. key := make([]byte, 9)
  118. key[0] = 1
  119. binary.BigEndian.PutUint64(key[1:9], idx)
  120. return key
  121. }
  122. func encodeIndex(index *dpaDBIndex) []byte {
  123. data, _ := rlp.EncodeToBytes(index)
  124. return data
  125. }
  126. func encodeData(chunk *Chunk) []byte {
  127. return chunk.SData
  128. }
  129. func decodeIndex(data []byte, index *dpaDBIndex) {
  130. dec := rlp.NewStream(bytes.NewReader(data), 0)
  131. dec.Decode(index)
  132. }
  133. func decodeData(data []byte, chunk *Chunk) {
  134. chunk.SData = data
  135. chunk.Size = int64(binary.LittleEndian.Uint64(data[0:8]))
  136. }
  137. func gcListPartition(list []*gcItem, left int, right int, pivotIndex int) int {
  138. pivotValue := list[pivotIndex].value
  139. dd := list[pivotIndex]
  140. list[pivotIndex] = list[right]
  141. list[right] = dd
  142. storeIndex := left
  143. for i := left; i < right; i++ {
  144. if list[i].value < pivotValue {
  145. dd = list[storeIndex]
  146. list[storeIndex] = list[i]
  147. list[i] = dd
  148. storeIndex++
  149. }
  150. }
  151. dd = list[storeIndex]
  152. list[storeIndex] = list[right]
  153. list[right] = dd
  154. return storeIndex
  155. }
  156. func gcListSelect(list []*gcItem, left int, right int, n int) int {
  157. if left == right {
  158. return left
  159. }
  160. pivotIndex := (left + right) / 2
  161. pivotIndex = gcListPartition(list, left, right, pivotIndex)
  162. if n == pivotIndex {
  163. return n
  164. } else {
  165. if n < pivotIndex {
  166. return gcListSelect(list, left, pivotIndex-1, n)
  167. } else {
  168. return gcListSelect(list, pivotIndex+1, right, n)
  169. }
  170. }
  171. }
  172. func (s *DbStore) collectGarbage(ratio float32) {
  173. it := s.db.NewIterator()
  174. it.Seek(s.gcPos)
  175. if it.Valid() {
  176. s.gcPos = it.Key()
  177. } else {
  178. s.gcPos = nil
  179. }
  180. gcnt := 0
  181. for (gcnt < gcArraySize) && (uint64(gcnt) < s.entryCnt) {
  182. if (s.gcPos == nil) || (s.gcPos[0] != kpIndex) {
  183. it.Seek(s.gcStartPos)
  184. if it.Valid() {
  185. s.gcPos = it.Key()
  186. } else {
  187. s.gcPos = nil
  188. }
  189. }
  190. if (s.gcPos == nil) || (s.gcPos[0] != kpIndex) {
  191. break
  192. }
  193. gci := new(gcItem)
  194. gci.idxKey = s.gcPos
  195. var index dpaDBIndex
  196. decodeIndex(it.Value(), &index)
  197. gci.idx = index.Idx
  198. // the smaller, the more likely to be gc'd
  199. gci.value = getIndexGCValue(&index)
  200. s.gcArray[gcnt] = gci
  201. gcnt++
  202. it.Next()
  203. if it.Valid() {
  204. s.gcPos = it.Key()
  205. } else {
  206. s.gcPos = nil
  207. }
  208. }
  209. it.Release()
  210. cutidx := gcListSelect(s.gcArray, 0, gcnt-1, int(float32(gcnt)*ratio))
  211. cutval := s.gcArray[cutidx].value
  212. // fmt.Print(gcnt, " ", s.entryCnt, " ")
  213. // actual gc
  214. for i := 0; i < gcnt; i++ {
  215. if s.gcArray[i].value <= cutval {
  216. s.delete(s.gcArray[i].idx, s.gcArray[i].idxKey)
  217. }
  218. }
  219. // fmt.Println(s.entryCnt)
  220. s.db.Put(keyGCPos, s.gcPos)
  221. }
  222. // Export writes all chunks from the store to a tar archive, returning the
  223. // number of chunks written.
  224. func (s *DbStore) Export(out io.Writer) (int64, error) {
  225. tw := tar.NewWriter(out)
  226. defer tw.Close()
  227. it := s.db.NewIterator()
  228. defer it.Release()
  229. var count int64
  230. for ok := it.Seek([]byte{kpIndex}); ok; ok = it.Next() {
  231. key := it.Key()
  232. if (key == nil) || (key[0] != kpIndex) {
  233. break
  234. }
  235. var index dpaDBIndex
  236. decodeIndex(it.Value(), &index)
  237. data, err := s.db.Get(getDataKey(index.Idx))
  238. if err != nil {
  239. log.Warn(fmt.Sprintf("Chunk %x found but could not be accessed: %v", key[:], err))
  240. continue
  241. }
  242. hdr := &tar.Header{
  243. Name: hex.EncodeToString(key[1:]),
  244. Mode: 0644,
  245. Size: int64(len(data)),
  246. }
  247. if err := tw.WriteHeader(hdr); err != nil {
  248. return count, err
  249. }
  250. if _, err := tw.Write(data); err != nil {
  251. return count, err
  252. }
  253. count++
  254. }
  255. return count, nil
  256. }
  257. // Import reads chunks into the store from a tar archive, returning the number
  258. // of chunks read.
  259. func (s *DbStore) Import(in io.Reader) (int64, error) {
  260. tr := tar.NewReader(in)
  261. var count int64
  262. for {
  263. hdr, err := tr.Next()
  264. if err == io.EOF {
  265. break
  266. } else if err != nil {
  267. return count, err
  268. }
  269. if len(hdr.Name) != 64 {
  270. log.Warn("ignoring non-chunk file", "name", hdr.Name)
  271. continue
  272. }
  273. key, err := hex.DecodeString(hdr.Name)
  274. if err != nil {
  275. log.Warn("ignoring invalid chunk file", "name", hdr.Name, "err", err)
  276. continue
  277. }
  278. data, err := ioutil.ReadAll(tr)
  279. if err != nil {
  280. return count, err
  281. }
  282. s.Put(&Chunk{Key: key, SData: data})
  283. count++
  284. }
  285. return count, nil
  286. }
  287. func (s *DbStore) Cleanup() {
  288. //Iterates over the database and checks that there are no faulty chunks
  289. it := s.db.NewIterator()
  290. startPosition := []byte{kpIndex}
  291. it.Seek(startPosition)
  292. var key []byte
  293. var errorsFound, total int
  294. for it.Valid() {
  295. key = it.Key()
  296. if (key == nil) || (key[0] != kpIndex) {
  297. break
  298. }
  299. total++
  300. var index dpaDBIndex
  301. decodeIndex(it.Value(), &index)
  302. data, err := s.db.Get(getDataKey(index.Idx))
  303. if err != nil {
  304. log.Warn(fmt.Sprintf("Chunk %x found but could not be accessed: %v", key[:], err))
  305. s.delete(index.Idx, getIndexKey(key[1:]))
  306. errorsFound++
  307. } else {
  308. hasher := s.hashfunc()
  309. hasher.Write(data)
  310. hash := hasher.Sum(nil)
  311. if !bytes.Equal(hash, key[1:]) {
  312. log.Warn(fmt.Sprintf("Found invalid chunk. Hash mismatch. hash=%x, key=%x", hash, key[:]))
  313. s.delete(index.Idx, getIndexKey(key[1:]))
  314. errorsFound++
  315. }
  316. }
  317. it.Next()
  318. }
  319. it.Release()
  320. log.Warn(fmt.Sprintf("Found %v errors out of %v entries", errorsFound, total))
  321. }
  322. func (s *DbStore) delete(idx uint64, idxKey []byte) {
  323. batch := new(leveldb.Batch)
  324. batch.Delete(idxKey)
  325. batch.Delete(getDataKey(idx))
  326. s.entryCnt--
  327. batch.Put(keyEntryCnt, U64ToBytes(s.entryCnt))
  328. s.db.Write(batch)
  329. }
  330. func (s *DbStore) Counter() uint64 {
  331. s.lock.Lock()
  332. defer s.lock.Unlock()
  333. return s.dataIdx
  334. }
  335. func (s *DbStore) Put(chunk *Chunk) {
  336. s.lock.Lock()
  337. defer s.lock.Unlock()
  338. ikey := getIndexKey(chunk.Key)
  339. var index dpaDBIndex
  340. if s.tryAccessIdx(ikey, &index) {
  341. if chunk.dbStored != nil {
  342. close(chunk.dbStored)
  343. }
  344. log.Trace(fmt.Sprintf("Storing to DB: chunk already exists, only update access"))
  345. return // already exists, only update access
  346. }
  347. data := encodeData(chunk)
  348. //data := ethutil.Encode([]interface{}{entry})
  349. if s.entryCnt >= s.capacity {
  350. s.collectGarbage(gcArrayFreeRatio)
  351. }
  352. batch := new(leveldb.Batch)
  353. batch.Put(getDataKey(s.dataIdx), data)
  354. index.Idx = s.dataIdx
  355. s.updateIndexAccess(&index)
  356. idata := encodeIndex(&index)
  357. batch.Put(ikey, idata)
  358. batch.Put(keyEntryCnt, U64ToBytes(s.entryCnt))
  359. s.entryCnt++
  360. batch.Put(keyDataIdx, U64ToBytes(s.dataIdx))
  361. s.dataIdx++
  362. batch.Put(keyAccessCnt, U64ToBytes(s.accessCnt))
  363. s.accessCnt++
  364. s.db.Write(batch)
  365. if chunk.dbStored != nil {
  366. close(chunk.dbStored)
  367. }
  368. log.Trace(fmt.Sprintf("DbStore.Put: %v. db storage counter: %v ", chunk.Key.Log(), s.dataIdx))
  369. }
  370. // try to find index; if found, update access cnt and return true
  371. func (s *DbStore) tryAccessIdx(ikey []byte, index *dpaDBIndex) bool {
  372. idata, err := s.db.Get(ikey)
  373. if err != nil {
  374. return false
  375. }
  376. decodeIndex(idata, index)
  377. batch := new(leveldb.Batch)
  378. batch.Put(keyAccessCnt, U64ToBytes(s.accessCnt))
  379. s.accessCnt++
  380. s.updateIndexAccess(index)
  381. idata = encodeIndex(index)
  382. batch.Put(ikey, idata)
  383. s.db.Write(batch)
  384. return true
  385. }
  386. func (s *DbStore) Get(key Key) (chunk *Chunk, err error) {
  387. s.lock.Lock()
  388. defer s.lock.Unlock()
  389. var index dpaDBIndex
  390. if s.tryAccessIdx(getIndexKey(key), &index) {
  391. var data []byte
  392. data, err = s.db.Get(getDataKey(index.Idx))
  393. if err != nil {
  394. log.Trace(fmt.Sprintf("DBStore: Chunk %v found but could not be accessed: %v", key.Log(), err))
  395. s.delete(index.Idx, getIndexKey(key))
  396. return
  397. }
  398. hasher := s.hashfunc()
  399. hasher.Write(data)
  400. hash := hasher.Sum(nil)
  401. if !bytes.Equal(hash, key) {
  402. s.delete(index.Idx, getIndexKey(key))
  403. log.Warn("Invalid Chunk in Database. Please repair with command: 'swarm cleandb'")
  404. }
  405. chunk = &Chunk{
  406. Key: key,
  407. }
  408. decodeData(data, chunk)
  409. } else {
  410. err = notFound
  411. }
  412. return
  413. }
  414. func (s *DbStore) updateAccessCnt(key Key) {
  415. s.lock.Lock()
  416. defer s.lock.Unlock()
  417. var index dpaDBIndex
  418. s.tryAccessIdx(getIndexKey(key), &index) // result_chn == nil, only update access cnt
  419. }
  420. func (s *DbStore) setCapacity(c uint64) {
  421. s.lock.Lock()
  422. defer s.lock.Unlock()
  423. s.capacity = c
  424. if s.entryCnt > c {
  425. ratio := float32(1.01) - float32(c)/float32(s.entryCnt)
  426. if ratio < gcArrayFreeRatio {
  427. ratio = gcArrayFreeRatio
  428. }
  429. if ratio > 1 {
  430. ratio = 1
  431. }
  432. for s.entryCnt > c {
  433. s.collectGarbage(ratio)
  434. }
  435. }
  436. }
  437. func (s *DbStore) Close() {
  438. s.db.Close()
  439. }
  440. // describes a section of the DbStore representing the unsynced
  441. // domain relevant to a peer
  442. // Start - Stop designate a continuous area Keys in an address space
  443. // typically the addresses closer to us than to the peer but not closer
  444. // another closer peer in between
  445. // From - To designates a time interval typically from the last disconnect
  446. // till the latest connection (real time traffic is relayed)
  447. type DbSyncState struct {
  448. Start, Stop Key
  449. First, Last uint64
  450. }
  451. // implements the syncer iterator interface
  452. // iterates by storage index (~ time of storage = first entry to db)
  453. type dbSyncIterator struct {
  454. it iterator.Iterator
  455. DbSyncState
  456. }
  457. // initialises a sync iterator from a syncToken (passed in with the handshake)
  458. func (self *DbStore) NewSyncIterator(state DbSyncState) (si *dbSyncIterator, err error) {
  459. if state.First > state.Last {
  460. return nil, fmt.Errorf("no entries found")
  461. }
  462. si = &dbSyncIterator{
  463. it: self.db.NewIterator(),
  464. DbSyncState: state,
  465. }
  466. si.it.Seek(getIndexKey(state.Start))
  467. return si, nil
  468. }
  469. // walk the area from Start to Stop and returns items within time interval
  470. // First to Last
  471. func (self *dbSyncIterator) Next() (key Key) {
  472. for self.it.Valid() {
  473. dbkey := self.it.Key()
  474. if dbkey[0] != 0 {
  475. break
  476. }
  477. key = Key(make([]byte, len(dbkey)-1))
  478. copy(key[:], dbkey[1:])
  479. if bytes.Compare(key[:], self.Start) <= 0 {
  480. self.it.Next()
  481. continue
  482. }
  483. if bytes.Compare(key[:], self.Stop) > 0 {
  484. break
  485. }
  486. var index dpaDBIndex
  487. decodeIndex(self.it.Value(), &index)
  488. self.it.Next()
  489. if (index.Idx >= self.First) && (index.Idx < self.Last) {
  490. return
  491. }
  492. }
  493. self.it.Release()
  494. return nil
  495. }