imagecachemanager_base.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storageman
  15. import (
  16. "context"
  17. "sync"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. )
  22. type IImageCacheMangerFactory interface {
  23. NewImageCacheManager(manager *SStorageManager, cachePath string, storage IStorage, storagecacheId string) IImageCacheManger
  24. StorageType() string
  25. }
  26. var (
  27. imageCacheManagerFactories = make(map[string]IImageCacheMangerFactory)
  28. )
  29. func registerimageCacheManagerFactory(factory IImageCacheMangerFactory) {
  30. imageCacheManagerFactories[factory.StorageType()] = factory
  31. }
  32. func NewImageCacheManager(manager *SStorageManager, cachePath string, storage IStorage, storagecacheId string, storageType string) IImageCacheManger {
  33. if factory, ok := imageCacheManagerFactories[storageType]; ok {
  34. return factory.NewImageCacheManager(manager, cachePath, storage, storagecacheId)
  35. }
  36. log.Errorf("no image cache manager driver for %s found", storageType)
  37. return nil
  38. }
  39. type IImageCacheManger interface {
  40. GetId() string
  41. GetPath() string
  42. SetStoragecacheId(string)
  43. Lvmlockd() bool
  44. IsLocal() bool
  45. GetStorageType() string
  46. // for diskhandler
  47. PrefetchImageCache(ctx context.Context, data interface{}) (jsonutils.JSONObject, error)
  48. DeleteImageCache(ctx context.Context, data interface{}) (jsonutils.JSONObject, error)
  49. RemoveImage(ctx context.Context, imageId string) error
  50. AcquireImage(ctx context.Context, input api.CacheImageInput, callback func(progress, progressMbps float64, totalSizeMb int64)) (IImageCache, error)
  51. ReleaseImage(ctx context.Context, imageId string)
  52. LoadImageCache(imageId string)
  53. CleanImageCachefiles(ctx context.Context)
  54. }
  55. // IImageCacheManagerGetter is used to get image cache from image cache manager
  56. type IImageCacheManagerGetter interface {
  57. IImageCacheManger
  58. // Try to use AcquireImage first, only using GetImage for readonly operations
  59. GetImage(imageId string) IImageCache
  60. }
  61. type SBaseImageCacheManager struct {
  62. storageManager IStorageManager
  63. storagecacaheId string
  64. cachePath string
  65. cachedImages *sync.Map // map[string]IImageCache
  66. }
  67. func (c *SBaseImageCacheManager) GetPath() string {
  68. return c.cachePath
  69. }
  70. func (c *SBaseImageCacheManager) GetId() string {
  71. return c.storagecacaheId
  72. }
  73. func (c *SBaseImageCacheManager) SetStoragecacheId(scid string) {
  74. c.storagecacaheId = scid
  75. }
  76. func (c *SBaseImageCacheManager) GetStorageManager() IStorageManager {
  77. return c.storageManager
  78. }
  79. func (c *SBaseImageCacheManager) Lvmlockd() bool {
  80. return false
  81. }