mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
- fix local storage init local man
- fix rbd set storage info
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package lockman
|
||||
|
||||
import "context"
|
||||
|
||||
type SBaseLockManager struct {
|
||||
manager ILockManager
|
||||
}
|
||||
|
||||
func NewBaseLockManger(m ILockManager) *SBaseLockManager {
|
||||
return &SBaseLockManager{manager: m}
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) LockClass(ctx context.Context, manager ILockedClass, projectId string) {
|
||||
key := getClassKey(manager, projectId)
|
||||
m.manager.LockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) ReleaseClass(ctx context.Context, manager ILockedClass, projectId string) {
|
||||
key := getClassKey(manager, projectId)
|
||||
m.manager.UnlockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) LockObject(ctx context.Context, model ILockedObject) {
|
||||
key := getObjectKey(model)
|
||||
m.manager.LockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) ReleaseObject(ctx context.Context, model ILockedObject) {
|
||||
key := getObjectKey(model)
|
||||
m.manager.UnlockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) LockRawObject(ctx context.Context, resName string, resId string) {
|
||||
key := getRawObjectKey(resName, resId)
|
||||
m.manager.LockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) ReleaseRawObject(ctx context.Context, resName string, resId string) {
|
||||
key := getRawObjectKey(resName, resId)
|
||||
m.manager.UnlockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) LockJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject) {
|
||||
key := getJointObjectKey(model, model2)
|
||||
m.manager.LockKey(ctx, key)
|
||||
}
|
||||
|
||||
func (m *SBaseLockManager) ReleaseJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject) {
|
||||
key := getJointObjectKey(model, model2)
|
||||
m.manager.UnlockKey(ctx, key)
|
||||
}
|
||||
@@ -167,6 +167,7 @@ type SLockTableIndex struct {
|
||||
}
|
||||
|
||||
type SEtcdLockManager struct {
|
||||
*SBaseLockManager
|
||||
tableLock *sync.Mutex
|
||||
lockTable map[SLockTableIndex]*SEtcdLockRecord
|
||||
|
||||
@@ -203,6 +204,7 @@ func NewEtcdLockManager(config *SEtcdLockManagerConfig) (ILockManager, error) {
|
||||
Value: lockman,
|
||||
Func: atexit.ExitHandlerFunc(lockman.destroyAtExit),
|
||||
})
|
||||
lockman.SBaseLockManager = NewBaseLockManger(&lockman)
|
||||
return &lockman, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -135,12 +135,17 @@ func (rec *SInMemoryLockRecord) unlockContext(ctx context.Context) (needClean bo
|
||||
}
|
||||
|
||||
type SInMemoryLockManager struct {
|
||||
*SBaseLockManager
|
||||
tableLock *sync.Mutex
|
||||
lockTable map[string]*SInMemoryLockRecord
|
||||
}
|
||||
|
||||
func NewInMemoryLockManager() ILockManager {
|
||||
lockMan := SInMemoryLockManager{tableLock: &sync.Mutex{}, lockTable: make(map[string]*SInMemoryLockRecord)}
|
||||
lockMan := SInMemoryLockManager{
|
||||
tableLock: &sync.Mutex{},
|
||||
lockTable: make(map[string]*SInMemoryLockRecord),
|
||||
}
|
||||
lockMan.SBaseLockManager = NewBaseLockManger(&lockMan)
|
||||
return &lockMan
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,15 @@ type ILockedObject interface {
|
||||
type ILockManager interface {
|
||||
LockKey(ctx context.Context, key string)
|
||||
UnlockKey(ctx context.Context, key string)
|
||||
|
||||
LockClass(ctx context.Context, manager ILockedClass, projectId string)
|
||||
ReleaseClass(ctx context.Context, manager ILockedClass, projectId string)
|
||||
LockObject(ctx context.Context, model ILockedObject)
|
||||
ReleaseObject(ctx context.Context, model ILockedObject)
|
||||
LockRawObject(ctx context.Context, resName string, resId string)
|
||||
ReleaseRawObject(ctx context.Context, resName string, resId string)
|
||||
LockJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject)
|
||||
ReleaseJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject)
|
||||
}
|
||||
|
||||
func getClassKey(manager ILockedClass, projectId string) string {
|
||||
@@ -64,41 +73,33 @@ func Init(man ILockManager) {
|
||||
}
|
||||
|
||||
func LockClass(ctx context.Context, manager ILockedClass, projectId string) {
|
||||
key := getClassKey(manager, projectId)
|
||||
_lockman.LockKey(ctx, key)
|
||||
_lockman.LockClass(ctx, manager, projectId)
|
||||
}
|
||||
|
||||
func ReleaseClass(ctx context.Context, manager ILockedClass, projectId string) {
|
||||
key := getClassKey(manager, projectId)
|
||||
_lockman.UnlockKey(ctx, key)
|
||||
_lockman.ReleaseClass(ctx, manager, projectId)
|
||||
}
|
||||
|
||||
func LockObject(ctx context.Context, model ILockedObject) {
|
||||
key := getObjectKey(model)
|
||||
_lockman.LockKey(ctx, key)
|
||||
_lockman.LockObject(ctx, model)
|
||||
}
|
||||
|
||||
func ReleaseObject(ctx context.Context, model ILockedObject) {
|
||||
key := getObjectKey(model)
|
||||
_lockman.UnlockKey(ctx, key)
|
||||
_lockman.ReleaseObject(ctx, model)
|
||||
}
|
||||
|
||||
func LockRawObject(ctx context.Context, resName string, resId string) {
|
||||
key := getRawObjectKey(resName, resId)
|
||||
_lockman.LockKey(ctx, key)
|
||||
_lockman.LockRawObject(ctx, resName, resId)
|
||||
}
|
||||
|
||||
func ReleaseRawObject(ctx context.Context, resName string, resId string) {
|
||||
key := getRawObjectKey(resName, resId)
|
||||
_lockman.UnlockKey(ctx, key)
|
||||
_lockman.ReleaseRawObject(ctx, resName, resId)
|
||||
}
|
||||
|
||||
func LockJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject) {
|
||||
key := getJointObjectKey(model, model2)
|
||||
_lockman.LockKey(ctx, key)
|
||||
_lockman.LockJointObject(ctx, model, model2)
|
||||
}
|
||||
|
||||
func ReleaseJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject) {
|
||||
key := getJointObjectKey(model, model2)
|
||||
_lockman.UnlockKey(ctx, key)
|
||||
_lockman.ReleaseJointObject(ctx, model, model2)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
)
|
||||
|
||||
type SNoopLockManager struct {
|
||||
*SBaseLockManager
|
||||
}
|
||||
|
||||
func (lockman *SNoopLockManager) LockKey(ctx context.Context, key string) {
|
||||
@@ -33,5 +34,6 @@ func (lockman *SNoopLockManager) UnlockKey(ctx context.Context, key string) {
|
||||
|
||||
func NewNoopLockManager() ILockManager {
|
||||
lockMan := SNoopLockManager{}
|
||||
lockMan.SBaseLockManager = NewBaseLockManger(&lockMan)
|
||||
return &lockMan
|
||||
}
|
||||
|
||||
@@ -501,7 +501,7 @@ func (m *SGuestManager) GuestStart(ctx context.Context, sid string, body jsonuti
|
||||
var data *jsonutils.JSONDict
|
||||
params, err := body.Get("params")
|
||||
if err != nil {
|
||||
data = params.(*jsonutils.JSONDict)
|
||||
data, _ = params.(*jsonutils.JSONDict)
|
||||
}
|
||||
guest.StartGuest(ctx, data)
|
||||
res := jsonutils.NewDict()
|
||||
|
||||
@@ -33,10 +33,12 @@ type SLocalImageCacheManager struct {
|
||||
SBaseImageCacheManager
|
||||
// limit int
|
||||
// isTemplate bool
|
||||
lock lockman.ILockManager
|
||||
}
|
||||
|
||||
func NewLocalImageCacheManager(manager IStorageManager, cachePath string, storagecacheId string) *SLocalImageCacheManager {
|
||||
imageCacheManager := new(SLocalImageCacheManager)
|
||||
imageCacheManager.lock = lockman.NewInMemoryLockManager()
|
||||
imageCacheManager.storageManager = manager
|
||||
imageCacheManager.storagecacaheId = storagecacheId
|
||||
imageCacheManager.cachePath = cachePath
|
||||
@@ -54,8 +56,8 @@ func (c *SLocalImageCacheManager) loadCache(ctx context.Context) {
|
||||
if len(c.cachePath) == 0 {
|
||||
return
|
||||
}
|
||||
lockman.LockRawObject(ctx, "LOCAL", "image-cache")
|
||||
defer lockman.ReleaseRawObject(ctx, "LOCAL", "image-cache")
|
||||
c.lock.LockRawObject(ctx, "LOCAL", "image-cache")
|
||||
defer c.lock.ReleaseRawObject(ctx, "LOCAL", "image-cache")
|
||||
files, _ := ioutil.ReadDir(c.cachePath)
|
||||
for _, f := range files {
|
||||
if regutils.MatchUUIDExact(f.Name()) {
|
||||
@@ -72,8 +74,8 @@ func (c *SLocalImageCacheManager) LoadImageCache(imageId string) {
|
||||
}
|
||||
|
||||
func (c *SLocalImageCacheManager) AcquireImage(ctx context.Context, imageId, zone, srcUrl, format string) IImageCache {
|
||||
lockman.LockRawObject(ctx, "image-cache", imageId)
|
||||
defer lockman.ReleaseRawObject(ctx, "image-cache", imageId)
|
||||
c.lock.LockRawObject(ctx, "image-cache", imageId)
|
||||
defer c.lock.ReleaseRawObject(ctx, "image-cache", imageId)
|
||||
|
||||
img, ok := c.cachedImages[imageId]
|
||||
if !ok {
|
||||
@@ -88,8 +90,8 @@ func (c *SLocalImageCacheManager) AcquireImage(ctx context.Context, imageId, zon
|
||||
}
|
||||
|
||||
func (c *SLocalImageCacheManager) ReleaseImage(ctx context.Context, imageId string) {
|
||||
lockman.LockRawObject(ctx, "image-cache", imageId)
|
||||
defer lockman.ReleaseRawObject(ctx, "image-cache", imageId)
|
||||
c.lock.LockRawObject(ctx, "image-cache", imageId)
|
||||
defer c.lock.ReleaseRawObject(ctx, "image-cache", imageId)
|
||||
|
||||
if img, ok := c.cachedImages[imageId]; ok {
|
||||
img.Release()
|
||||
@@ -107,8 +109,8 @@ func (c *SLocalImageCacheManager) DeleteImageCache(ctx context.Context, data int
|
||||
}
|
||||
|
||||
func (c *SLocalImageCacheManager) removeImage(ctx context.Context, imageId string) error {
|
||||
lockman.LockRawObject(ctx, "image-cache", imageId)
|
||||
defer lockman.ReleaseRawObject(ctx, "image-cache", imageId)
|
||||
c.lock.LockRawObject(ctx, "image-cache", imageId)
|
||||
defer c.lock.ReleaseRawObject(ctx, "image-cache", imageId)
|
||||
|
||||
if img, ok := c.cachedImages[imageId]; ok {
|
||||
delete(c.cachedImages, imageId)
|
||||
|
||||
@@ -758,3 +758,12 @@ func (s *SRbdStorage) CreateDiskFromSnapshot(
|
||||
)
|
||||
return disk.CreateFromRbdSnapshot(ctx, snapshotUrl, srcDiskId, srcPool)
|
||||
}
|
||||
|
||||
func (s *SRbdStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error {
|
||||
s.StorageId = storageId
|
||||
s.StorageName = storageName
|
||||
if dconf, ok := conf.(*jsonutils.JSONDict); ok {
|
||||
s.StorageConf = dconf
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user