scheduler.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2017 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 bloombits
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common/gopool"
  20. )
  21. // request represents a bloom retrieval task to prioritize and pull from the local
  22. // database or remotely from the network.
  23. type request struct {
  24. section uint64 // Section index to retrieve the a bit-vector from
  25. bit uint // Bit index within the section to retrieve the vector of
  26. }
  27. // response represents the state of a requested bit-vector through a scheduler.
  28. type response struct {
  29. cached []byte // Cached bits to dedup multiple requests
  30. done chan struct{} // Channel to allow waiting for completion
  31. }
  32. // scheduler handles the scheduling of bloom-filter retrieval operations for
  33. // entire section-batches belonging to a single bloom bit. Beside scheduling the
  34. // retrieval operations, this struct also deduplicates the requests and caches
  35. // the results to minimize network/database overhead even in complex filtering
  36. // scenarios.
  37. type scheduler struct {
  38. bit uint // Index of the bit in the bloom filter this scheduler is responsible for
  39. responses map[uint64]*response // Currently pending retrieval requests or already cached responses
  40. lock sync.Mutex // Lock protecting the responses from concurrent access
  41. }
  42. // newScheduler creates a new bloom-filter retrieval scheduler for a specific
  43. // bit index.
  44. func newScheduler(idx uint) *scheduler {
  45. return &scheduler{
  46. bit: idx,
  47. responses: make(map[uint64]*response),
  48. }
  49. }
  50. // run creates a retrieval pipeline, receiving section indexes from sections and
  51. // returning the results in the same order through the done channel. Concurrent
  52. // runs of the same scheduler are allowed, leading to retrieval task deduplication.
  53. func (s *scheduler) run(sections chan uint64, dist chan *request, done chan []byte, quit chan struct{}, wg *sync.WaitGroup) {
  54. // Create a forwarder channel between requests and responses of the same size as
  55. // the distribution channel (since that will block the pipeline anyway).
  56. pend := make(chan uint64, cap(dist))
  57. // Start the pipeline schedulers to forward between user -> distributor -> user
  58. wg.Add(2)
  59. gopool.Submit(func() {
  60. s.scheduleRequests(sections, dist, pend, quit, wg)
  61. })
  62. gopool.Submit(func() {
  63. s.scheduleDeliveries(pend, done, quit, wg)
  64. })
  65. }
  66. // reset cleans up any leftovers from previous runs. This is required before a
  67. // restart to ensure the no previously requested but never delivered state will
  68. // cause a lockup.
  69. func (s *scheduler) reset() {
  70. s.lock.Lock()
  71. defer s.lock.Unlock()
  72. for section, res := range s.responses {
  73. if res.cached == nil {
  74. delete(s.responses, section)
  75. }
  76. }
  77. }
  78. // scheduleRequests reads section retrieval requests from the input channel,
  79. // deduplicates the stream and pushes unique retrieval tasks into the distribution
  80. // channel for a database or network layer to honour.
  81. func (s *scheduler) scheduleRequests(reqs chan uint64, dist chan *request, pend chan uint64, quit chan struct{}, wg *sync.WaitGroup) {
  82. // Clean up the goroutine and pipeline when done
  83. defer wg.Done()
  84. defer close(pend)
  85. // Keep reading and scheduling section requests
  86. for {
  87. select {
  88. case <-quit:
  89. return
  90. case section, ok := <-reqs:
  91. // New section retrieval requested
  92. if !ok {
  93. return
  94. }
  95. // Deduplicate retrieval requests
  96. unique := false
  97. s.lock.Lock()
  98. if s.responses[section] == nil {
  99. s.responses[section] = &response{
  100. done: make(chan struct{}),
  101. }
  102. unique = true
  103. }
  104. s.lock.Unlock()
  105. // Schedule the section for retrieval and notify the deliverer to expect this section
  106. if unique {
  107. select {
  108. case <-quit:
  109. return
  110. case dist <- &request{bit: s.bit, section: section}:
  111. }
  112. }
  113. select {
  114. case <-quit:
  115. return
  116. case pend <- section:
  117. }
  118. }
  119. }
  120. }
  121. // scheduleDeliveries reads section acceptance notifications and waits for them
  122. // to be delivered, pushing them into the output data buffer.
  123. func (s *scheduler) scheduleDeliveries(pend chan uint64, done chan []byte, quit chan struct{}, wg *sync.WaitGroup) {
  124. // Clean up the goroutine and pipeline when done
  125. defer wg.Done()
  126. defer close(done)
  127. // Keep reading notifications and scheduling deliveries
  128. for {
  129. select {
  130. case <-quit:
  131. return
  132. case idx, ok := <-pend:
  133. // New section retrieval pending
  134. if !ok {
  135. return
  136. }
  137. // Wait until the request is honoured
  138. s.lock.Lock()
  139. res := s.responses[idx]
  140. s.lock.Unlock()
  141. select {
  142. case <-quit:
  143. return
  144. case <-res.done:
  145. }
  146. // Deliver the result
  147. select {
  148. case <-quit:
  149. return
  150. case done <- res.cached:
  151. }
  152. }
  153. }
  154. }
  155. // deliver is called by the request distributor when a reply to a request arrives.
  156. func (s *scheduler) deliver(sections []uint64, data [][]byte) {
  157. s.lock.Lock()
  158. defer s.lock.Unlock()
  159. for i, section := range sections {
  160. if res := s.responses[section]; res != nil && res.cached == nil { // Avoid non-requests and double deliveries
  161. res.cached = data[i]
  162. close(res.done)
  163. }
  164. }
  165. }