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.

113 lines
2.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package chunk_cache
  2. import (
  3. "sync"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  6. )
  7. const (
  8. memCacheSizeLimit = 1024 * 1024
  9. onDiskCacheSizeLimit0 = memCacheSizeLimit
  10. onDiskCacheSizeLimit1 = 4 * memCacheSizeLimit
  11. )
  12. // a global cache for recently accessed file chunks
  13. type ChunkCache struct {
  14. memCache *ChunkCacheInMemory
  15. diskCaches []*OnDiskCacheLayer
  16. sync.RWMutex
  17. }
  18. func NewChunkCache(maxEntries int64, dir string, diskSizeMB int64) *ChunkCache {
  19. c := &ChunkCache{
  20. memCache: NewChunkCacheInMemory(maxEntries),
  21. }
  22. c.diskCaches = make([]*OnDiskCacheLayer, 3)
  23. c.diskCaches[0] = NewOnDiskCacheLayer(dir, "c0_1", diskSizeMB/4, 4)
  24. c.diskCaches[1] = NewOnDiskCacheLayer(dir, "c1_4", diskSizeMB/4, 4)
  25. c.diskCaches[2] = NewOnDiskCacheLayer(dir, "cache", diskSizeMB/2, 4)
  26. return c
  27. }
  28. func (c *ChunkCache) GetChunk(fileId string, chunkSize uint64) (data []byte) {
  29. if c == nil {
  30. return
  31. }
  32. c.RLock()
  33. defer c.RUnlock()
  34. return c.doGetChunk(fileId, chunkSize)
  35. }
  36. func (c *ChunkCache) doGetChunk(fileId string, chunkSize uint64) (data []byte) {
  37. if chunkSize < memCacheSizeLimit {
  38. if data = c.memCache.GetChunk(fileId); data != nil {
  39. return data
  40. }
  41. }
  42. fid, err := needle.ParseFileIdFromString(fileId)
  43. if err != nil {
  44. glog.Errorf("failed to parse file id %s", fileId)
  45. return nil
  46. }
  47. for _, diskCache := range c.diskCaches {
  48. data := diskCache.getChunk(fid.Key)
  49. if len(data) != 0 {
  50. return data
  51. }
  52. }
  53. return nil
  54. }
  55. func (c *ChunkCache) SetChunk(fileId string, data []byte) {
  56. if c == nil {
  57. return
  58. }
  59. c.Lock()
  60. defer c.Unlock()
  61. c.doSetChunk(fileId, data)
  62. }
  63. func (c *ChunkCache) doSetChunk(fileId string, data []byte) {
  64. if len(data) < memCacheSizeLimit {
  65. c.memCache.SetChunk(fileId, data)
  66. }
  67. fid, err := needle.ParseFileIdFromString(fileId)
  68. if err != nil {
  69. glog.Errorf("failed to parse file id %s", fileId)
  70. return
  71. }
  72. if len(data) < onDiskCacheSizeLimit0 {
  73. c.diskCaches[0].setChunk(fid.Key, data)
  74. } else if len(data) < onDiskCacheSizeLimit1 {
  75. c.diskCaches[1].setChunk(fid.Key, data)
  76. } else {
  77. c.diskCaches[2].setChunk(fid.Key, data)
  78. }
  79. }
  80. func (c *ChunkCache) Shutdown() {
  81. if c == nil {
  82. return
  83. }
  84. c.Lock()
  85. defer c.Unlock()
  86. for _, diskCache := range c.diskCaches {
  87. diskCache.shutdown()
  88. }
  89. }