You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
4.6 KiB

  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
  5. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  6. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  7. "sync"
  8. "time"
  9. )
  10. type ReaderCache struct {
  11. chunkCache chunk_cache.ChunkCache
  12. lookupFileIdFn wdclient.LookupFileIdFunctionType
  13. sync.Mutex
  14. downloaders map[string]*SingleChunkCacher
  15. limit int
  16. }
  17. type SingleChunkCacher struct {
  18. sync.Mutex
  19. cond *sync.Cond
  20. parent *ReaderCache
  21. chunkFileId string
  22. data []byte
  23. err error
  24. cipherKey []byte
  25. isGzipped bool
  26. chunkSize int
  27. shouldCache bool
  28. wg sync.WaitGroup
  29. completedTime time.Time
  30. }
  31. func newReaderCache(limit int, chunkCache chunk_cache.ChunkCache, lookupFileIdFn wdclient.LookupFileIdFunctionType) *ReaderCache {
  32. return &ReaderCache{
  33. limit: limit,
  34. chunkCache: chunkCache,
  35. lookupFileIdFn: lookupFileIdFn,
  36. downloaders: make(map[string]*SingleChunkCacher),
  37. }
  38. }
  39. func (rc *ReaderCache) MaybeCache(chunkViews []*ChunkView) {
  40. if rc.lookupFileIdFn == nil {
  41. return
  42. }
  43. rc.Lock()
  44. defer rc.Unlock()
  45. for _, chunkView := range chunkViews {
  46. if _, found := rc.downloaders[chunkView.FileId]; found {
  47. continue
  48. }
  49. if len(rc.downloaders) >= rc.limit {
  50. // if still no slots, return
  51. return
  52. }
  53. // glog.V(4).Infof("prefetch %s offset %d", chunkView.FileId, chunkView.LogicOffset)
  54. // cache this chunk if not yet
  55. cacher := newSingleChunkCacher(rc, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int(chunkView.ChunkSize), false)
  56. cacher.wg.Add(1)
  57. go cacher.startCaching()
  58. cacher.wg.Wait()
  59. rc.downloaders[chunkView.FileId] = cacher
  60. }
  61. return
  62. }
  63. func (rc *ReaderCache) ReadChunkAt(buffer []byte, fileId string, cipherKey []byte, isGzipped bool, offset int64, chunkSize int, shouldCache bool) (int, error) {
  64. rc.Lock()
  65. defer rc.Unlock()
  66. if cacher, found := rc.downloaders[fileId]; found {
  67. if n, err := cacher.readChunkAt(buffer, offset); n != 0 && err == nil {
  68. return n, err
  69. }
  70. }
  71. if shouldCache || rc.lookupFileIdFn == nil {
  72. n, err := rc.chunkCache.ReadChunkAt(buffer, fileId, uint64(offset))
  73. if n > 0 {
  74. return n, err
  75. }
  76. }
  77. if len(rc.downloaders) >= rc.limit {
  78. oldestFid, oldestTime := "", time.Now()
  79. for fid, downloader := range rc.downloaders {
  80. if !downloader.completedTime.IsZero() {
  81. if downloader.completedTime.Before(oldestTime) {
  82. oldestFid, oldestTime = fid, downloader.completedTime
  83. }
  84. }
  85. }
  86. if oldestFid != "" {
  87. oldDownloader := rc.downloaders[oldestFid]
  88. delete(rc.downloaders, oldestFid)
  89. oldDownloader.destroy()
  90. }
  91. }
  92. // glog.V(4).Infof("cache1 %s", fileId)
  93. cacher := newSingleChunkCacher(rc, fileId, cipherKey, isGzipped, chunkSize, shouldCache)
  94. cacher.wg.Add(1)
  95. go cacher.startCaching()
  96. cacher.wg.Wait()
  97. rc.downloaders[fileId] = cacher
  98. return cacher.readChunkAt(buffer, offset)
  99. }
  100. func (rc *ReaderCache) UnCache(fileId string) {
  101. rc.Lock()
  102. defer rc.Unlock()
  103. // glog.V(4).Infof("uncache %s", fileId)
  104. if downloader, found := rc.downloaders[fileId]; found {
  105. downloader.destroy()
  106. delete(rc.downloaders, fileId)
  107. }
  108. }
  109. func (rc *ReaderCache) destroy() {
  110. rc.Lock()
  111. defer rc.Unlock()
  112. for _, downloader := range rc.downloaders {
  113. downloader.destroy()
  114. }
  115. }
  116. func newSingleChunkCacher(parent *ReaderCache, fileId string, cipherKey []byte, isGzipped bool, chunkSize int, shouldCache bool) *SingleChunkCacher {
  117. t := &SingleChunkCacher{
  118. parent: parent,
  119. chunkFileId: fileId,
  120. cipherKey: cipherKey,
  121. isGzipped: isGzipped,
  122. chunkSize: chunkSize,
  123. shouldCache: shouldCache,
  124. }
  125. t.cond = sync.NewCond(t)
  126. return t
  127. }
  128. func (s *SingleChunkCacher) startCaching() {
  129. s.Lock()
  130. defer s.Unlock()
  131. s.wg.Done() // means this has been started
  132. urlStrings, err := s.parent.lookupFileIdFn(s.chunkFileId)
  133. if err != nil {
  134. s.err = fmt.Errorf("operation LookupFileId %s failed, err: %v", s.chunkFileId, err)
  135. return
  136. }
  137. s.data = mem.Allocate(s.chunkSize)
  138. _, s.err = retriedFetchChunkData(s.data, urlStrings, s.cipherKey, s.isGzipped, true, 0)
  139. if s.err != nil {
  140. mem.Free(s.data)
  141. s.data = nil
  142. return
  143. }
  144. s.completedTime = time.Now()
  145. if s.shouldCache {
  146. s.parent.chunkCache.SetChunk(s.chunkFileId, s.data)
  147. }
  148. s.cond.Broadcast()
  149. return
  150. }
  151. func (s *SingleChunkCacher) destroy() {
  152. s.Lock()
  153. defer s.Unlock()
  154. if s.data != nil {
  155. mem.Free(s.data)
  156. s.data = nil
  157. }
  158. }
  159. func (s *SingleChunkCacher) readChunkAt(buf []byte, offset int64) (int, error) {
  160. s.Lock()
  161. defer s.Unlock()
  162. for s.completedTime.IsZero() {
  163. s.cond.Wait()
  164. }
  165. if s.err != nil {
  166. return 0, s.err
  167. }
  168. if len(s.data) == 0 {
  169. return 0, nil
  170. }
  171. return copy(buf, s.data[offset:]), nil
  172. }