Skip to content

Commit

Permalink
image cache use lockman
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed May 21, 2020
1 parent cd31781 commit 127bd0f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkg/cloudcommon/elect/elect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostman/storageman/disk_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostman/storageman/disk_rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) //重装系统时,需要删除以前的系统盘
Expand Down
4 changes: 1 addition & 3 deletions pkg/hostman/storageman/imagecachemanager_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package storageman

import (
"context"
"sync"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
Expand Down Expand Up @@ -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)
}

Expand All @@ -62,7 +61,6 @@ type SBaseImageCacheManager struct {
storagecacaheId string
cachePath string
cachedImages map[string]IImageCache
mutex *sync.Mutex
}

func (c *SBaseImageCacheManager) GetPath() string {
Expand Down
26 changes: 13 additions & 13 deletions pkg/hostman/storageman/imagecachemanager_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()) {
Expand All @@ -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 {
Expand All @@ -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()
}
Expand All @@ -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)
Expand Down
25 changes: 12 additions & 13 deletions pkg/hostman/storageman/imagecachemanager_rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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()
}
Expand Down

0 comments on commit 127bd0f

Please sign in to comment.