From 127bd0ff1783aaae2a03b1cc4ac7329335c02993 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Wed, 20 May 2020 20:20:00 +0800 Subject: [PATCH] image cache use lockman --- pkg/cloudcommon/elect/elect.go | 2 +- pkg/hostman/storageman/disk_local.go | 2 +- pkg/hostman/storageman/disk_rbd.go | 2 +- .../storageman/imagecachemanager_base.go | 4 +-- .../storageman/imagecachemanager_local.go | 26 +++++++++---------- .../storageman/imagecachemanager_rbd.go | 25 +++++++++--------- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/pkg/cloudcommon/elect/elect.go b/pkg/cloudcommon/elect/elect.go index 8ba23f77e7..98e94ab881 100644 --- a/pkg/cloudcommon/elect/elect.go +++ b/pkg/cloudcommon/elect/elect.go @@ -97,7 +97,7 @@ func NewElect(config *EtcdConfig, key string) (*Elect, error) { DialOptions: []grpc.DialOption{ grpc.WithBlock(), - grpc.WithTimeout(500 * time.Millisecond), + grpc.WithTimeout(3000 * time.Millisecond), }, DialTimeout: 3 * time.Second, }) diff --git a/pkg/hostman/storageman/disk_local.go b/pkg/hostman/storageman/disk_local.go index e5bd463777..d68b1cea04 100644 --- a/pkg/hostman/storageman/disk_local.go +++ b/pkg/hostman/storageman/disk_local.go @@ -219,7 +219,7 @@ func (d *SLocalDisk) createFromTemplate( ) (jsonutils.JSONObject, error) { imageCache := imageCacheManager.AcquireImage(ctx, imageId, d.GetZoneName(), "", "") if imageCache != nil { - defer imageCacheManager.ReleaseImage(imageId) + defer imageCacheManager.ReleaseImage(ctx, imageId) cacheImagePath := imageCache.GetPath() if fileutils2.Exists(d.GetPath()) { diff --git a/pkg/hostman/storageman/disk_rbd.go b/pkg/hostman/storageman/disk_rbd.go index 68584dd99c..56f9353e7e 100644 --- a/pkg/hostman/storageman/disk_rbd.go +++ b/pkg/hostman/storageman/disk_rbd.go @@ -182,7 +182,7 @@ func (d *SRBDDisk) createFromTemplate(ctx context.Context, imageId, format strin if imageCache == nil { return nil, fmt.Errorf("failed to qcquire image for storage %s", d.Storage.GetStorageName()) } - defer imageCacheManager.ReleaseImage(imageId) + defer imageCacheManager.ReleaseImage(ctx, imageId) storage := d.Storage.(*SRbdStorage) destPool, _ := storage.StorageConf.GetString("pool") storage.deleteImage(destPool, d.Id) //重装系统时,需要删除以前的系统盘 diff --git a/pkg/hostman/storageman/imagecachemanager_base.go b/pkg/hostman/storageman/imagecachemanager_base.go index 3efce15007..a8fc532e11 100644 --- a/pkg/hostman/storageman/imagecachemanager_base.go +++ b/pkg/hostman/storageman/imagecachemanager_base.go @@ -16,7 +16,6 @@ package storageman import ( "context" - "sync" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -53,7 +52,7 @@ type IImageCacheManger interface { DeleteImageCache(ctx context.Context, data interface{}) (jsonutils.JSONObject, error) AcquireImage(ctx context.Context, imageId, zone, srcUrl, format string) IImageCache - ReleaseImage(imageId string) + ReleaseImage(ctx context.Context, imageId string) LoadImageCache(imageId string) } @@ -62,7 +61,6 @@ type SBaseImageCacheManager struct { storagecacaheId string cachePath string cachedImages map[string]IImageCache - mutex *sync.Mutex } func (c *SBaseImageCacheManager) GetPath() string { diff --git a/pkg/hostman/storageman/imagecachemanager_local.go b/pkg/hostman/storageman/imagecachemanager_local.go index 9a044d8e25..c532f49cc4 100644 --- a/pkg/hostman/storageman/imagecachemanager_local.go +++ b/pkg/hostman/storageman/imagecachemanager_local.go @@ -19,11 +19,11 @@ import ( "fmt" "io/ioutil" "os" - "sync" "yunion.io/x/jsonutils" "yunion.io/x/pkg/util/regutils" + "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/hostman/hostutils" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/procutils" @@ -43,20 +43,19 @@ func NewLocalImageCacheManager(manager IStorageManager, cachePath string, storag // imageCacheManager.limit = limit // imageCacheManager.isTemplate = isTemplete imageCacheManager.cachedImages = make(map[string]IImageCache, 0) - imageCacheManager.mutex = new(sync.Mutex) if !fileutils2.Exists(cachePath) { procutils.NewCommand("mkdir", "-p", cachePath).Run() } - imageCacheManager.loadCache() + imageCacheManager.loadCache(context.Background()) return imageCacheManager } -func (c *SLocalImageCacheManager) loadCache() { +func (c *SLocalImageCacheManager) loadCache(ctx context.Context) { if len(c.cachePath) == 0 { return } - c.mutex.Lock() - defer c.mutex.Unlock() + lockman.LockRawObject(ctx, "LOCAL", "image-cache") + defer lockman.ReleaseRawObject(ctx, "LOCAL", "image-cache") files, _ := ioutil.ReadDir(c.cachePath) for _, f := range files { if regutils.MatchUUIDExact(f.Name()) { @@ -73,8 +72,8 @@ func (c *SLocalImageCacheManager) LoadImageCache(imageId string) { } func (c *SLocalImageCacheManager) AcquireImage(ctx context.Context, imageId, zone, srcUrl, format string) IImageCache { - c.mutex.Lock() - defer c.mutex.Unlock() + lockman.LockRawObject(ctx, "image-cache", imageId) + defer lockman.ReleaseRawObject(ctx, "image-cache", imageId) img, ok := c.cachedImages[imageId] if !ok { @@ -88,9 +87,10 @@ func (c *SLocalImageCacheManager) AcquireImage(ctx context.Context, imageId, zon } } -func (c *SLocalImageCacheManager) ReleaseImage(imageId string) { - c.mutex.Lock() - defer c.mutex.Unlock() +func (c *SLocalImageCacheManager) ReleaseImage(ctx context.Context, imageId string) { + lockman.LockRawObject(ctx, "image-cache", imageId) + defer lockman.ReleaseRawObject(ctx, "image-cache", imageId) + if img, ok := c.cachedImages[imageId]; ok { img.Release() } @@ -107,8 +107,8 @@ func (c *SLocalImageCacheManager) DeleteImageCache(ctx context.Context, data int } func (c *SLocalImageCacheManager) removeImage(ctx context.Context, imageId string) error { - c.mutex.Lock() - defer c.mutex.Unlock() + lockman.LockRawObject(ctx, "image-cache", imageId) + defer lockman.ReleaseRawObject(ctx, "image-cache", imageId) if img, ok := c.cachedImages[imageId]; ok { delete(c.cachedImages, imageId) diff --git a/pkg/hostman/storageman/imagecachemanager_rbd.go b/pkg/hostman/storageman/imagecachemanager_rbd.go index 918673f77f..a12982db3e 100644 --- a/pkg/hostman/storageman/imagecachemanager_rbd.go +++ b/pkg/hostman/storageman/imagecachemanager_rbd.go @@ -20,12 +20,12 @@ import ( "context" "fmt" "strings" - "sync" "yunion.io/x/jsonutils" "yunion.io/x/log" api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/hostman/hostutils" ) @@ -51,8 +51,7 @@ func NewRbdImageCacheManager(manager IStorageManager, cachePath string, storage imageCacheManager.Pool, imageCacheManager.Prefix = cachePath, "image_cache_" } imageCacheManager.cachedImages = make(map[string]IImageCache, 0) - imageCacheManager.mutex = new(sync.Mutex) - imageCacheManager.loadCache() + imageCacheManager.loadCache(context.Background()) return imageCacheManager } @@ -71,9 +70,9 @@ func init() { registerimageCacheManagerFactory(&SRbdImageCacheManagerFactory{}) } -func (c *SRbdImageCacheManager) loadCache() { - c.mutex.Lock() - defer c.mutex.Unlock() +func (c *SRbdImageCacheManager) loadCache(ctx context.Context) { + lockman.LockRawObject(ctx, "RBD", "image-cache") + defer lockman.ReleaseRawObject(ctx, "RBD", "image-cache") storage := c.storage.(*SRbdStorage) images, err := storage.listImages(c.Pool) @@ -143,8 +142,8 @@ func (c *SRbdImageCacheManager) DeleteImageCache(ctx context.Context, data inter } func (c *SRbdImageCacheManager) removeImage(ctx context.Context, imageId string) error { - c.mutex.Lock() - defer c.mutex.Unlock() + lockman.LockRawObject(ctx, "image-cache", imageId) + defer lockman.ReleaseRawObject(ctx, "image-cache", imageId) if img, ok := c.cachedImages[imageId]; ok { delete(c.cachedImages, imageId) @@ -154,8 +153,8 @@ func (c *SRbdImageCacheManager) removeImage(ctx context.Context, imageId string) } func (c *SRbdImageCacheManager) AcquireImage(ctx context.Context, imageId, zone, srcUrl, format string) IImageCache { - c.mutex.Lock() - defer c.mutex.Unlock() + lockman.LockRawObject(ctx, "image-cache", imageId) + defer lockman.ReleaseRawObject(ctx, "image-cache", imageId) img, ok := c.cachedImages[imageId] if !ok { @@ -168,9 +167,9 @@ func (c *SRbdImageCacheManager) AcquireImage(ctx context.Context, imageId, zone, return nil } -func (c *SRbdImageCacheManager) ReleaseImage(imageId string) { - c.mutex.Lock() - defer c.mutex.Unlock() +func (c *SRbdImageCacheManager) ReleaseImage(ctx context.Context, imageId string) { + lockman.LockRawObject(ctx, "image-cache", imageId) + defer lockman.ReleaseRawObject(ctx, "image-cache", imageId) if img, ok := c.cachedImages[imageId]; ok { img.Release() }