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.

135 lines
2.9 KiB

5 years ago
  1. package chunk_cache
  2. import (
  3. "fmt"
  4. "path"
  5. "sort"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/storage"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  10. )
  11. // a global cache for recently accessed file chunks
  12. type ChunkCache struct {
  13. memCache *ChunkCacheInMemory
  14. diskCaches []*ChunkCacheVolume
  15. sync.RWMutex
  16. }
  17. func NewChunkCache(maxEntries int64, dir string, diskSizeMB int64, segmentCount int) *ChunkCache {
  18. c := &ChunkCache{
  19. memCache: NewChunkCacheInMemory(maxEntries),
  20. }
  21. volumeCount, volumeSize := int(diskSizeMB/30000), int64(30000)
  22. if volumeCount < segmentCount {
  23. volumeCount, volumeSize = segmentCount, diskSizeMB/int64(segmentCount)
  24. }
  25. for i := 0; i < volumeCount; i++ {
  26. fileName := path.Join(dir, fmt.Sprintf("cache_%d", i))
  27. diskCache, err := LoadOrCreateChunkCacheVolume(fileName, volumeSize*1024*1024)
  28. if err != nil {
  29. glog.Errorf("failed to add cache %s : %v", fileName, err)
  30. } else {
  31. c.diskCaches = append(c.diskCaches, diskCache)
  32. }
  33. }
  34. // keep newest cache to the front
  35. sort.Slice(c.diskCaches, func(i, j int) bool {
  36. return c.diskCaches[i].lastModTime.After(c.diskCaches[j].lastModTime)
  37. })
  38. return c
  39. }
  40. func (c *ChunkCache) GetChunk(fileId string) (data []byte) {
  41. if c == nil {
  42. return
  43. }
  44. c.RLock()
  45. defer c.RUnlock()
  46. return c.doGetChunk(fileId)
  47. }
  48. func (c *ChunkCache) doGetChunk(fileId string) (data []byte) {
  49. if data = c.memCache.GetChunk(fileId); data != nil {
  50. return data
  51. }
  52. fid, err := needle.ParseFileIdFromString(fileId)
  53. if err != nil {
  54. glog.Errorf("failed to parse file id %s", fileId)
  55. return nil
  56. }
  57. for _, diskCache := range c.diskCaches {
  58. data, err = diskCache.GetNeedle(fid.Key)
  59. if err == storage.ErrorNotFound {
  60. continue
  61. }
  62. if err != nil {
  63. glog.Errorf("failed to read cache file %s id %s", diskCache.fileName, fileId)
  64. continue
  65. }
  66. if len(data) != 0 {
  67. return
  68. }
  69. }
  70. return nil
  71. }
  72. func (c *ChunkCache) SetChunk(fileId string, data []byte) {
  73. if c == nil {
  74. return
  75. }
  76. c.Lock()
  77. defer c.Unlock()
  78. if existingData := c.doGetChunk(fileId); len(existingData)==0{
  79. c.doSetChunk(fileId, data)
  80. }
  81. }
  82. func (c *ChunkCache) doSetChunk(fileId string, data []byte) {
  83. c.memCache.SetChunk(fileId, data)
  84. if len(c.diskCaches) == 0 {
  85. return
  86. }
  87. if c.diskCaches[0].fileSize+int64(len(data)) > c.diskCaches[0].sizeLimit {
  88. t, resetErr := c.diskCaches[len(c.diskCaches)-1].Reset()
  89. if resetErr != nil {
  90. glog.Errorf("failed to reset cache file %s", c.diskCaches[len(c.diskCaches)-1].fileName)
  91. return
  92. }
  93. for i := len(c.diskCaches) - 1; i > 0; i-- {
  94. c.diskCaches[i] = c.diskCaches[i-1]
  95. }
  96. c.diskCaches[0] = t
  97. }
  98. fid, err := needle.ParseFileIdFromString(fileId)
  99. if err != nil {
  100. glog.Errorf("failed to parse file id %s", fileId)
  101. return
  102. }
  103. c.diskCaches[0].WriteNeedle(fid.Key, data)
  104. }
  105. func (c *ChunkCache) Shutdown() {
  106. if c == nil {
  107. return
  108. }
  109. c.Lock()
  110. defer c.Unlock()
  111. for _, diskCache := range c.diskCaches {
  112. diskCache.Shutdown()
  113. }
  114. }