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.

199 lines
4.5 KiB

  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/util/chunk_cache"
  5. "github.com/chrislusf/seaweedfs/weed/util/mem"
  6. "github.com/chrislusf/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.RWMutex
  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. return cacher.readChunkAt(buffer, offset)
  68. }
  69. if shouldCache || rc.lookupFileIdFn == nil {
  70. n, err := rc.chunkCache.ReadChunkAt(buffer, fileId, uint64(offset))
  71. if n > 0 {
  72. return n, err
  73. }
  74. }
  75. if len(rc.downloaders) >= rc.limit {
  76. oldestFid, oldestTime := "", time.Now()
  77. for fid, downloader := range rc.downloaders {
  78. if !downloader.completedTime.IsZero() {
  79. if downloader.completedTime.Before(oldestTime) {
  80. oldestFid, oldestTime = fid, downloader.completedTime
  81. }
  82. }
  83. }
  84. if oldestFid != "" {
  85. oldDownloader := rc.downloaders[oldestFid]
  86. delete(rc.downloaders, oldestFid)
  87. oldDownloader.destroy()
  88. }
  89. }
  90. // glog.V(4).Infof("cache1 %s", fileId)
  91. cacher := newSingleChunkCacher(rc, fileId, cipherKey, isGzipped, chunkSize, shouldCache)
  92. cacher.wg.Add(1)
  93. go cacher.startCaching()
  94. cacher.wg.Wait()
  95. rc.downloaders[fileId] = cacher
  96. return cacher.readChunkAt(buffer, offset)
  97. }
  98. func (rc *ReaderCache) UnCache(fileId string) {
  99. rc.Lock()
  100. defer rc.Unlock()
  101. // glog.V(4).Infof("uncache %s", fileId)
  102. if downloader, found := rc.downloaders[fileId]; found {
  103. downloader.destroy()
  104. delete(rc.downloaders, fileId)
  105. }
  106. }
  107. func (rc *ReaderCache) destroy() {
  108. rc.Lock()
  109. defer rc.Unlock()
  110. for _, downloader := range rc.downloaders {
  111. downloader.destroy()
  112. }
  113. }
  114. func newSingleChunkCacher(parent *ReaderCache, fileId string, cipherKey []byte, isGzipped bool, chunkSize int, shouldCache bool) *SingleChunkCacher {
  115. t := &SingleChunkCacher{
  116. parent: parent,
  117. chunkFileId: fileId,
  118. cipherKey: cipherKey,
  119. isGzipped: isGzipped,
  120. chunkSize: chunkSize,
  121. shouldCache: shouldCache,
  122. }
  123. t.cond = sync.NewCond(t)
  124. return t
  125. }
  126. func (s *SingleChunkCacher) startCaching() {
  127. s.Lock()
  128. defer s.Unlock()
  129. s.wg.Done() // means this has been started
  130. urlStrings, err := s.parent.lookupFileIdFn(s.chunkFileId)
  131. if err != nil {
  132. s.err = fmt.Errorf("operation LookupFileId %s failed, err: %v", s.chunkFileId, err)
  133. return
  134. }
  135. s.data = mem.Allocate(s.chunkSize)
  136. _, s.err = retriedFetchChunkData(s.data, urlStrings, s.cipherKey, s.isGzipped, true, 0)
  137. if s.err != nil {
  138. mem.Free(s.data)
  139. s.data = nil
  140. return
  141. }
  142. s.completedTime = time.Now()
  143. if s.shouldCache {
  144. s.parent.chunkCache.SetChunk(s.chunkFileId, s.data)
  145. }
  146. s.cond.Broadcast()
  147. return
  148. }
  149. func (s *SingleChunkCacher) destroy() {
  150. if s.data != nil {
  151. mem.Free(s.data)
  152. s.data = nil
  153. }
  154. }
  155. func (s *SingleChunkCacher) readChunkAt(buf []byte, offset int64) (int, error) {
  156. s.RLock()
  157. defer s.RUnlock()
  158. for s.completedTime.IsZero() {
  159. s.cond.Wait()
  160. }
  161. if s.err != nil {
  162. return 0, s.err
  163. }
  164. return copy(buf, s.data[offset:]), nil
  165. }