Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mutex locks to protect hostPathVolumes and hostPathSnapshots #112

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"os"
"path/filepath"
"sync"

"github.com/golang/glog"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -75,8 +76,10 @@ type hostPathSnapshot struct {
var (
vendorVersion = "dev"

hostPathVolumes map[string]hostPathVolume
hostPathVolumeSnapshots map[string]hostPathSnapshot
hostPathVolumes map[string]hostPathVolume
hostPathVolumesMutex sync.RWMutex
hostPathVolumeSnapshots map[string]hostPathSnapshot
hostPathVolumeSnapshotsMutex sync.RWMutex
)

const (
Expand Down Expand Up @@ -136,13 +139,17 @@ func (hp *hostPath) Run() {
}

func getVolumeByID(volumeID string) (hostPathVolume, error) {
hostPathVolumesMutex.RLock()
defer hostPathVolumesMutex.RUnlock()
if hostPathVol, ok := hostPathVolumes[volumeID]; ok {
return hostPathVol, nil
}
return hostPathVolume{}, fmt.Errorf("volume id %s does not exit in the volumes list", volumeID)
}

func getVolumeByName(volName string) (hostPathVolume, error) {
hostPathVolumesMutex.RLock()
defer hostPathVolumesMutex.RUnlock()
for _, hostPathVol := range hostPathVolumes {
if hostPathVol.VolName == volName {
return hostPathVol, nil
Expand All @@ -152,6 +159,8 @@ func getVolumeByName(volName string) (hostPathVolume, error) {
}

func getSnapshotByName(name string) (hostPathSnapshot, error) {
hostPathVolumeSnapshotsMutex.RLock()
defer hostPathVolumeSnapshotsMutex.RUnlock()
for _, snapshot := range hostPathVolumeSnapshots {
if snapshot.Name == name {
return snapshot, nil
Expand Down Expand Up @@ -207,6 +216,8 @@ func createHostpathVolume(volID, name string, cap int64, volAccessType accessTyp
VolAccessType: volAccessType,
Ephemeral: ephemeral,
}
hostPathVolumesMutex.Lock()
defer hostPathVolumesMutex.Unlock()
hostPathVolumes[volID] = hostpathVol
return &hostpathVol, nil
}
Expand All @@ -219,6 +230,8 @@ func updateHostpathVolume(volID string, volume hostPathVolume) error {
return err
}

hostPathVolumesMutex.Lock()
defer hostPathVolumesMutex.Unlock()
hostPathVolumes[volID] = volume
return nil
}
Expand Down Expand Up @@ -254,6 +267,8 @@ func deleteHostpathVolume(volID string) error {
if err := os.RemoveAll(path); err != nil && !os.IsNotExist(err) {
return err
}
hostPathVolumesMutex.Lock()
defer hostPathVolumesMutex.Unlock()
delete(hostPathVolumes, volID)
return nil
}
Expand All @@ -276,7 +291,9 @@ func hostPathIsEmpty(p string) (bool, error) {

// loadFromSnapshot populates the given destPath with data from the snapshotID
func loadFromSnapshot(snapshotId, destPath string) error {
hostPathVolumeSnapshotsMutex.RLock()
snapshot, ok := hostPathVolumeSnapshots[snapshotId]
hostPathVolumeSnapshotsMutex.RUnlock()
if !ok {
return status.Errorf(codes.NotFound, "cannot find snapshot %v", snapshotId)
}
Expand All @@ -295,7 +312,9 @@ func loadFromSnapshot(snapshotId, destPath string) error {

// loadfromVolume populates the given destPath with data from the srcVolumeID
func loadFromVolume(srcVolumeId, destPath string) error {
hostPathVolumesMutex.RLock()
hostPathVolume, ok := hostPathVolumes[srcVolumeId]
hostPathVolumesMutex.RUnlock()
if !ok {
return status.Error(codes.NotFound, "source volumeId does not exist, are source/destination in the same storage class?")
}
Expand Down