From 88c0dfcca51f4d301810380dacd119a9f291417c Mon Sep 17 00:00:00 2001 From: Dario Maiocchi Date: Mon, 20 Nov 2017 22:58:08 +0100 Subject: use poolMutexKV for locking resources. --- libvirt/cloudinit_def.go | 4 ++-- libvirt/coreos_ignition_def.go | 4 ++-- libvirt/pool_sync.go | 49 -------------------------------------- libvirt/pool_sync_test.go | 46 ----------------------------------- libvirt/provider.go | 4 ++++ libvirt/resource_libvirt_domain.go | 3 --- libvirt/resource_libvirt_volume.go | 4 ++-- libvirt/utils.go | 4 ++-- 8 files changed, 12 insertions(+), 106 deletions(-) delete mode 100644 libvirt/pool_sync.go delete mode 100644 libvirt/pool_sync_test.go (limited to 'libvirt') diff --git a/libvirt/cloudinit_def.go b/libvirt/cloudinit_def.go index d3426dc5..91b73514 100644 --- a/libvirt/cloudinit_def.go +++ b/libvirt/cloudinit_def.go @@ -65,8 +65,8 @@ func (ci *defCloudInit) CreateAndUpload(virConn *libvirt.Connect) (string, error } defer pool.Free() - PoolSync.AcquireLock(ci.PoolName) - defer PoolSync.ReleaseLock(ci.PoolName) + poolMutexKV.Lock(ci.PoolName) + defer poolMutexKV.Unlock(ci.PoolName) // Refresh the pool of the volume so that libvirt knows it is // not longer in use. diff --git a/libvirt/coreos_ignition_def.go b/libvirt/coreos_ignition_def.go index 4e77b199..bef01d63 100644 --- a/libvirt/coreos_ignition_def.go +++ b/libvirt/coreos_ignition_def.go @@ -38,8 +38,8 @@ func (ign *defIgnition) CreateAndUpload(virConn *libvirt.Connect) (string, error } defer pool.Free() - PoolSync.AcquireLock(ign.PoolName) - defer PoolSync.ReleaseLock(ign.PoolName) + poolMutexKV.Lock(ign.PoolName) + defer poolMutexKV.Unlock(ign.PoolName) // Refresh the pool of the volume so that libvirt knows it is // not longer in use. diff --git a/libvirt/pool_sync.go b/libvirt/pool_sync.go deleted file mode 100644 index a50528bc..00000000 --- a/libvirt/pool_sync.go +++ /dev/null @@ -1,49 +0,0 @@ -package libvirt - -import ( - "sync" -) - -// LVirtPoolSync makes possible to synchronize operations -// against libvirt pools. -// Doing pool.Refresh() operations while uploading or removing -// a volume into the pool causes errors inside of libvirtd -type LVirtPoolSync struct { - PoolLocks map[string]*sync.Mutex - internalMutex sync.Mutex -} - -// NewLVirtPoolSync allocates a new instance of LVirtPoolSync -func NewLVirtPoolSync() LVirtPoolSync { - pool := LVirtPoolSync{} - pool.PoolLocks = make(map[string]*sync.Mutex) - - return pool -} - -// AcquireLock acquires a lock for the specified pool -func (ps LVirtPoolSync) AcquireLock(pool string) { - ps.internalMutex.Lock() - defer ps.internalMutex.Unlock() - - lock, exists := ps.PoolLocks[pool] - if !exists { - lock = new(sync.Mutex) - ps.PoolLocks[pool] = lock - } - - lock.Lock() -} - -// ReleaseLock releases the look for the specified pool -func (ps LVirtPoolSync) ReleaseLock(pool string) { - ps.internalMutex.Lock() - defer ps.internalMutex.Unlock() - - lock, exists := ps.PoolLocks[pool] - if !exists { - return - } - - lock.Unlock() -} diff --git a/libvirt/pool_sync_test.go b/libvirt/pool_sync_test.go deleted file mode 100644 index 90ad4849..00000000 --- a/libvirt/pool_sync_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package libvirt - -import ( - "testing" -) - -func TestAcquireLock(t *testing.T) { - ps := NewLVirtPoolSync() - - ps.AcquireLock("test") - - _, found := ps.PoolLocks["test"] - - if !found { - t.Errorf("lock not found") - } -} - -func TestReleaseLock(t *testing.T) { - ps := NewLVirtPoolSync() - - ps.AcquireLock("test") - - _, found := ps.PoolLocks["test"] - if !found { - t.Errorf("lock not found") - } - - ps.ReleaseLock("test") - _, found = ps.PoolLocks["test"] - if !found { - t.Errorf("lock not found") - } -} - -func TestReleaseNotExistingLock(t *testing.T) { - ps := NewLVirtPoolSync() - - ps.ReleaseLock("test") - _, found := ps.PoolLocks["test"] - if found { - t.Errorf("lock found") - } - // moreover there should be no runtime error because - // we are not trying to unlock a not-locked mutex -} diff --git a/libvirt/provider.go b/libvirt/provider.go index c5f9b09f..e1ebaeb4 100644 --- a/libvirt/provider.go +++ b/libvirt/provider.go @@ -1,10 +1,14 @@ package libvirt import ( + "github.com/hashicorp/terraform/helper/mutexkv" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) +// Global poolMutexKV +var poolMutexKV = mutexkv.NewMutexKV() + // Provider libvirt func Provider() terraform.ResourceProvider { return &schema.Provider{ diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 707cd61e..7266923b 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -26,9 +26,6 @@ type DomainMeta struct { ifaces chan libvirtxml.DomainInterface } -// PoolSync exported pool sync -var PoolSync = NewLVirtPoolSync() - func init() { spew.Config.Indent = "\t" } diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index 958f9598..bc890e3c 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -90,8 +90,8 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error poolName = d.Get("pool").(string) } - PoolSync.AcquireLock(poolName) - defer PoolSync.ReleaseLock(poolName) + poolMutexKV.Lock(poolName) + defer poolMutexKV.Unlock(poolName) pool, err := virConn.LookupStoragePoolByName(poolName) if err != nil { diff --git a/libvirt/utils.go b/libvirt/utils.go index a82417fb..13a34312 100644 --- a/libvirt/utils.go +++ b/libvirt/utils.go @@ -85,8 +85,8 @@ func RemoveVolume(virConn *libvirt.Connect, key string) error { return fmt.Errorf("Error retrieving name of volume: %s", err) } - PoolSync.AcquireLock(poolName) - defer PoolSync.ReleaseLock(poolName) + poolMutexKV.Lock(poolName) + defer poolMutexKV.Unlock(poolName) WaitForSuccess("Error refreshing pool for volume", func() error { return volPool.Refresh(0) -- cgit v1.2.3