scheduler.go 5.6 KB

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