netstore.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "path/filepath"
  19. "sync"
  20. "time"
  21. "github.com/ethereum/go-ethereum/logger"
  22. "github.com/ethereum/go-ethereum/logger/glog"
  23. )
  24. /*
  25. NetStore is a cloud storage access abstaction layer for swarm
  26. it contains the shared logic of network served chunk store/retrieval requests
  27. both local (coming from DPA api) and remote (coming from peers via bzz protocol)
  28. it implements the ChunkStore interface and embeds LocalStore
  29. It is called by the bzz protocol instances via Depo (the store/retrieve request handler)
  30. a protocol instance is running on each peer, so this is heavily parallelised.
  31. NetStore falls back to a backend (CloudStorage interface)
  32. implemented by bzz/network/forwarder. forwarder or IPFS or IPΞS
  33. */
  34. type NetStore struct {
  35. hashfunc Hasher
  36. localStore *LocalStore
  37. cloud CloudStore
  38. lock sync.Mutex
  39. }
  40. // backend engine for cloud store
  41. // It can be aggregate dispatching to several parallel implementations:
  42. // bzz/network/forwarder. forwarder or IPFS or IPΞS
  43. type CloudStore interface {
  44. Store(*Chunk)
  45. Deliver(*Chunk)
  46. Retrieve(*Chunk)
  47. }
  48. type StoreParams struct {
  49. ChunkDbPath string
  50. DbCapacity uint64
  51. CacheCapacity uint
  52. Radius int
  53. }
  54. func NewStoreParams(path string) (self *StoreParams) {
  55. return &StoreParams{
  56. ChunkDbPath: filepath.Join(path, "chunks"),
  57. DbCapacity: defaultDbCapacity,
  58. CacheCapacity: defaultCacheCapacity,
  59. Radius: defaultRadius,
  60. }
  61. }
  62. // netstore contructor, takes path argument that is used to initialise dbStore,
  63. // the persistent (disk) storage component of LocalStore
  64. // the second argument is the hive, the connection/logistics manager for the node
  65. func NewNetStore(hash Hasher, lstore *LocalStore, cloud CloudStore, params *StoreParams) *NetStore {
  66. return &NetStore{
  67. hashfunc: hash,
  68. localStore: lstore,
  69. cloud: cloud,
  70. }
  71. }
  72. const (
  73. // maximum number of peers that a retrieved message is delivered to
  74. requesterCount = 3
  75. )
  76. var (
  77. // timeout interval before retrieval is timed out
  78. searchTimeout = 3 * time.Second
  79. )
  80. // store logic common to local and network chunk store requests
  81. // ~ unsafe put in localdb no check if exists no extra copy no hash validation
  82. // the chunk is forced to propagate (Cloud.Store) even if locally found!
  83. // caller needs to make sure if that is wanted
  84. func (self *NetStore) Put(entry *Chunk) {
  85. self.localStore.Put(entry)
  86. // handle deliveries
  87. if entry.Req != nil {
  88. glog.V(logger.Detail).Infof("NetStore.Put: localStore.Put %v hit existing request...delivering", entry.Key.Log())
  89. // closing C singals to other routines (local requests)
  90. // that the chunk is has been retrieved
  91. close(entry.Req.C)
  92. // deliver the chunk to requesters upstream
  93. go self.cloud.Deliver(entry)
  94. } else {
  95. glog.V(logger.Detail).Infof("NetStore.Put: localStore.Put %v stored locally", entry.Key.Log())
  96. // handle propagating store requests
  97. // go self.cloud.Store(entry)
  98. go self.cloud.Store(entry)
  99. }
  100. }
  101. // retrieve logic common for local and network chunk retrieval requests
  102. func (self *NetStore) Get(key Key) (*Chunk, error) {
  103. var err error
  104. chunk, err := self.localStore.Get(key)
  105. if err == nil {
  106. if chunk.Req == nil {
  107. glog.V(logger.Detail).Infof("NetStore.Get: %v found locally", key)
  108. } else {
  109. glog.V(logger.Detail).Infof("NetStore.Get: %v hit on an existing request", key)
  110. // no need to launch again
  111. }
  112. return chunk, err
  113. }
  114. // no data and no request status
  115. glog.V(logger.Detail).Infof("NetStore.Get: %v not found locally. open new request", key)
  116. chunk = NewChunk(key, newRequestStatus(key))
  117. self.localStore.memStore.Put(chunk)
  118. go self.cloud.Retrieve(chunk)
  119. return chunk, nil
  120. }