mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
image cache use lockman
This commit is contained in:
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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) //重装系统时,需要删除以前的系统盘
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user