memstore.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // memory storage layer for the package blockhash
  17. package storage
  18. import (
  19. "fmt"
  20. "sync"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/metrics"
  23. )
  24. //metrics variables
  25. var (
  26. memstorePutCounter = metrics.NewRegisteredCounter("storage.db.memstore.put.count", nil)
  27. memstoreRemoveCounter = metrics.NewRegisteredCounter("storage.db.memstore.rm.count", nil)
  28. )
  29. const (
  30. memTreeLW = 2 // log2(subtree count) of the subtrees
  31. memTreeFLW = 14 // log2(subtree count) of the root layer
  32. dbForceUpdateAccessCnt = 1000
  33. defaultCacheCapacity = 5000
  34. )
  35. type MemStore struct {
  36. memtree *memTree
  37. entryCnt, capacity uint // stored entries
  38. accessCnt uint64 // access counter; oldest is thrown away when full
  39. dbAccessCnt uint64
  40. dbStore *DbStore
  41. lock sync.Mutex
  42. }
  43. /*
  44. a hash prefix subtree containing subtrees or one storage entry (but never both)
  45. - access[0] stores the smallest (oldest) access count value in this subtree
  46. - if it contains more subtrees and its subtree count is at least 4, access[1:2]
  47. stores the smallest access count in the first and second halves of subtrees
  48. (so that access[0] = min(access[1], access[2])
  49. - likewise, if subtree count is at least 8,
  50. access[1] = min(access[3], access[4])
  51. access[2] = min(access[5], access[6])
  52. (access[] is a binary tree inside the multi-bit leveled hash tree)
  53. */
  54. func NewMemStore(d *DbStore, capacity uint) (m *MemStore) {
  55. m = &MemStore{}
  56. m.memtree = newMemTree(memTreeFLW, nil, 0)
  57. m.dbStore = d
  58. m.setCapacity(capacity)
  59. return
  60. }
  61. type memTree struct {
  62. subtree []*memTree
  63. parent *memTree
  64. parentIdx uint
  65. bits uint // log2(subtree count)
  66. width uint // subtree count
  67. entry *Chunk // if subtrees are present, entry should be nil
  68. lastDBaccess uint64
  69. access []uint64
  70. }
  71. func newMemTree(b uint, parent *memTree, pidx uint) (node *memTree) {
  72. node = new(memTree)
  73. node.bits = b
  74. node.width = 1 << b
  75. node.subtree = make([]*memTree, node.width)
  76. node.access = make([]uint64, node.width-1)
  77. node.parent = parent
  78. node.parentIdx = pidx
  79. if parent != nil {
  80. parent.subtree[pidx] = node
  81. }
  82. return node
  83. }
  84. func (node *memTree) updateAccess(a uint64) {
  85. aidx := uint(0)
  86. var aa uint64
  87. oa := node.access[0]
  88. for node.access[aidx] == oa {
  89. node.access[aidx] = a
  90. if aidx > 0 {
  91. aa = node.access[((aidx-1)^1)+1]
  92. aidx = (aidx - 1) >> 1
  93. } else {
  94. pidx := node.parentIdx
  95. node = node.parent
  96. if node == nil {
  97. return
  98. }
  99. nn := node.subtree[pidx^1]
  100. if nn != nil {
  101. aa = nn.access[0]
  102. } else {
  103. aa = 0
  104. }
  105. aidx = (node.width + pidx - 2) >> 1
  106. }
  107. if (aa != 0) && (aa < a) {
  108. a = aa
  109. }
  110. }
  111. }
  112. func (s *MemStore) setCapacity(c uint) {
  113. s.lock.Lock()
  114. defer s.lock.Unlock()
  115. for c < s.entryCnt {
  116. s.removeOldest()
  117. }
  118. s.capacity = c
  119. }
  120. func (s *MemStore) Counter() uint {
  121. return s.entryCnt
  122. }
  123. // entry (not its copy) is going to be in MemStore
  124. func (s *MemStore) Put(entry *Chunk) {
  125. if s.capacity == 0 {
  126. return
  127. }
  128. s.lock.Lock()
  129. defer s.lock.Unlock()
  130. if s.entryCnt >= s.capacity {
  131. s.removeOldest()
  132. }
  133. s.accessCnt++
  134. memstorePutCounter.Inc(1)
  135. node := s.memtree
  136. bitpos := uint(0)
  137. for node.entry == nil {
  138. l := entry.Key.bits(bitpos, node.bits)
  139. st := node.subtree[l]
  140. if st == nil {
  141. st = newMemTree(memTreeLW, node, l)
  142. bitpos += node.bits
  143. node = st
  144. break
  145. }
  146. bitpos += node.bits
  147. node = st
  148. }
  149. if node.entry != nil {
  150. if node.entry.Key.isEqual(entry.Key) {
  151. node.updateAccess(s.accessCnt)
  152. if entry.SData == nil {
  153. entry.Size = node.entry.Size
  154. entry.SData = node.entry.SData
  155. }
  156. if entry.Req == nil {
  157. entry.Req = node.entry.Req
  158. }
  159. entry.C = node.entry.C
  160. node.entry = entry
  161. return
  162. }
  163. for node.entry != nil {
  164. l := node.entry.Key.bits(bitpos, node.bits)
  165. st := node.subtree[l]
  166. if st == nil {
  167. st = newMemTree(memTreeLW, node, l)
  168. }
  169. st.entry = node.entry
  170. node.entry = nil
  171. st.updateAccess(node.access[0])
  172. l = entry.Key.bits(bitpos, node.bits)
  173. st = node.subtree[l]
  174. if st == nil {
  175. st = newMemTree(memTreeLW, node, l)
  176. }
  177. bitpos += node.bits
  178. node = st
  179. }
  180. }
  181. node.entry = entry
  182. node.lastDBaccess = s.dbAccessCnt
  183. node.updateAccess(s.accessCnt)
  184. s.entryCnt++
  185. }
  186. func (s *MemStore) Get(hash Key) (chunk *Chunk, err error) {
  187. s.lock.Lock()
  188. defer s.lock.Unlock()
  189. node := s.memtree
  190. bitpos := uint(0)
  191. for node.entry == nil {
  192. l := hash.bits(bitpos, node.bits)
  193. st := node.subtree[l]
  194. if st == nil {
  195. return nil, notFound
  196. }
  197. bitpos += node.bits
  198. node = st
  199. }
  200. if node.entry.Key.isEqual(hash) {
  201. s.accessCnt++
  202. node.updateAccess(s.accessCnt)
  203. chunk = node.entry
  204. if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt {
  205. s.dbAccessCnt++
  206. node.lastDBaccess = s.dbAccessCnt
  207. if s.dbStore != nil {
  208. s.dbStore.updateAccessCnt(hash)
  209. }
  210. }
  211. } else {
  212. err = notFound
  213. }
  214. return
  215. }
  216. func (s *MemStore) removeOldest() {
  217. node := s.memtree
  218. for node.entry == nil {
  219. aidx := uint(0)
  220. av := node.access[aidx]
  221. for aidx < node.width/2-1 {
  222. if av == node.access[aidx*2+1] {
  223. node.access[aidx] = node.access[aidx*2+2]
  224. aidx = aidx*2 + 1
  225. } else if av == node.access[aidx*2+2] {
  226. node.access[aidx] = node.access[aidx*2+1]
  227. aidx = aidx*2 + 2
  228. } else {
  229. panic(nil)
  230. }
  231. }
  232. pidx := aidx*2 + 2 - node.width
  233. if (node.subtree[pidx] != nil) && (av == node.subtree[pidx].access[0]) {
  234. if node.subtree[pidx+1] != nil {
  235. node.access[aidx] = node.subtree[pidx+1].access[0]
  236. } else {
  237. node.access[aidx] = 0
  238. }
  239. } else if (node.subtree[pidx+1] != nil) && (av == node.subtree[pidx+1].access[0]) {
  240. if node.subtree[pidx] != nil {
  241. node.access[aidx] = node.subtree[pidx].access[0]
  242. } else {
  243. node.access[aidx] = 0
  244. }
  245. pidx++
  246. } else {
  247. panic(nil)
  248. }
  249. //fmt.Println(pidx)
  250. node = node.subtree[pidx]
  251. }
  252. if node.entry.dbStored != nil {
  253. log.Trace(fmt.Sprintf("Memstore Clean: Waiting for chunk %v to be saved", node.entry.Key.Log()))
  254. <-node.entry.dbStored
  255. log.Trace(fmt.Sprintf("Memstore Clean: Chunk %v saved to DBStore. Ready to clear from mem.", node.entry.Key.Log()))
  256. } else {
  257. log.Trace(fmt.Sprintf("Memstore Clean: Chunk %v already in DB. Ready to delete.", node.entry.Key.Log()))
  258. }
  259. if node.entry.SData != nil {
  260. memstoreRemoveCounter.Inc(1)
  261. node.entry = nil
  262. s.entryCnt--
  263. }
  264. node.access[0] = 0
  265. //---
  266. aidx := uint(0)
  267. for {
  268. aa := node.access[aidx]
  269. if aidx > 0 {
  270. aidx = (aidx - 1) >> 1
  271. } else {
  272. pidx := node.parentIdx
  273. node = node.parent
  274. if node == nil {
  275. return
  276. }
  277. aidx = (node.width + pidx - 2) >> 1
  278. }
  279. if (aa != 0) && ((aa < node.access[aidx]) || (node.access[aidx] == 0)) {
  280. node.access[aidx] = aa
  281. }
  282. }
  283. }
  284. // Close memstore
  285. func (s *MemStore) Close() {}