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.

114 lines
2.6 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. c.RLock()
  42. defer c.RUnlock()
  43. if data = c.memCache.GetChunk(fileId); data != nil {
  44. return data
  45. }
  46. fid, err := needle.ParseFileIdFromString(fileId)
  47. if err != nil {
  48. glog.Errorf("failed to parse file id %s", fileId)
  49. return nil
  50. }
  51. for _, diskCache := range c.diskCaches {
  52. data, err = diskCache.GetNeedle(fid.Key)
  53. if err == storage.ErrorNotFound {
  54. continue
  55. }
  56. if err != nil {
  57. glog.Errorf("failed to read cache file %s id %s", diskCache.fileName, fileId)
  58. continue
  59. }
  60. if len(data) != 0 {
  61. return
  62. }
  63. }
  64. return nil
  65. }
  66. func (c *ChunkCache) SetChunk(fileId string, data []byte) {
  67. c.Lock()
  68. defer c.Unlock()
  69. c.memCache.SetChunk(fileId, data)
  70. if len(c.diskCaches) == 0 {
  71. return
  72. }
  73. if c.diskCaches[0].fileSize+int64(len(data)) > c.diskCaches[0].sizeLimit {
  74. t, resetErr := c.diskCaches[len(c.diskCaches)-1].Reset()
  75. if resetErr != nil {
  76. glog.Errorf("failed to reset cache file %s", c.diskCaches[len(c.diskCaches)-1].fileName)
  77. return
  78. }
  79. for i := len(c.diskCaches) - 1; i > 0; i-- {
  80. c.diskCaches[i] = c.diskCaches[i-1]
  81. }
  82. c.diskCaches[0] = t
  83. }
  84. fid, err := needle.ParseFileIdFromString(fileId)
  85. if err != nil {
  86. glog.Errorf("failed to parse file id %s", fileId)
  87. return
  88. }
  89. c.diskCaches[0].WriteNeedle(fid.Key, data)
  90. }
  91. func (c *ChunkCache) Shutdown() {
  92. c.Lock()
  93. defer c.Unlock()
  94. for _, diskCache := range c.diskCaches {
  95. diskCache.Shutdown()
  96. }
  97. }