diff --git a/pkg/cloudcommon/db/lockman/base.go b/pkg/cloudcommon/db/lockman/base.go new file mode 100644 index 0000000000..1c034a6135 --- /dev/null +++ b/pkg/cloudcommon/db/lockman/base.go @@ -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) +} diff --git a/pkg/cloudcommon/db/lockman/etcd.go b/pkg/cloudcommon/db/lockman/etcd.go index 64462421da..a9c4733731 100644 --- a/pkg/cloudcommon/db/lockman/etcd.go +++ b/pkg/cloudcommon/db/lockman/etcd.go @@ -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 } diff --git a/pkg/cloudcommon/db/lockman/inmemory.go b/pkg/cloudcommon/db/lockman/inmemory.go index a9aa0ce51e..0d941f1eb0 100644 --- a/pkg/cloudcommon/db/lockman/inmemory.go +++ b/pkg/cloudcommon/db/lockman/inmemory.go @@ -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 } diff --git a/pkg/cloudcommon/db/lockman/lockman.go b/pkg/cloudcommon/db/lockman/lockman.go index f71439260b..b9c9bbf7e1 100644 --- a/pkg/cloudcommon/db/lockman/lockman.go +++ b/pkg/cloudcommon/db/lockman/lockman.go @@ -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) } diff --git a/pkg/cloudcommon/db/lockman/noop.go b/pkg/cloudcommon/db/lockman/noop.go index 1fb04068bf..0e238e4d29 100644 --- a/pkg/cloudcommon/db/lockman/noop.go +++ b/pkg/cloudcommon/db/lockman/noop.go @@ -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 } diff --git a/pkg/hostman/guestman/guestman.go b/pkg/hostman/guestman/guestman.go index 0c26b76720..e49e36061f 100644 --- a/pkg/hostman/guestman/guestman.go +++ b/pkg/hostman/guestman/guestman.go @@ -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() diff --git a/pkg/hostman/storageman/imagecachemanager_local.go b/pkg/hostman/storageman/imagecachemanager_local.go index c532f49cc4..8c4268e56a 100644 --- a/pkg/hostman/storageman/imagecachemanager_local.go +++ b/pkg/hostman/storageman/imagecachemanager_local.go @@ -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) diff --git a/pkg/hostman/storageman/storage_rbd.go b/pkg/hostman/storageman/storage_rbd.go index d01df639fa..564d564451 100644 --- a/pkg/hostman/storageman/storage_rbd.go +++ b/pkg/hostman/storageman/storage_rbd.go @@ -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 +}