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

fix delete volume issues #937

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ var (
)

type controllerService struct {
juicefs juicefs.Interface
vols map[string]int64
juicefs juicefs.Interface
vols map[string]int64
volLocks *util.VolumeLocks
}

func newControllerService(k8sClient *k8sclient.K8sClient) (controllerService, error) {
jfs := juicefs.NewJfsProvider(nil, k8sClient)

return controllerService{
juicefs: jfs,
vols: make(map[string]int64),
juicefs: jfs,
vols: make(map[string]int64),
volLocks: util.NewVolumeLocks(),
}, nil
}

Expand Down Expand Up @@ -142,6 +144,12 @@ func (d *controllerService) DeleteVolume(ctx context.Context, req *csi.DeleteVol
return &csi.DeleteVolumeResponse{}, nil
}

if acquired := d.volLocks.TryAcquire(volumeID); !acquired {
klog.Errorf("DeleteVolume: Volume %q is being used by another operation", volumeID)
return nil, status.Errorf(codes.Aborted, "DeleteVolume: Volume %q is being used by another operation", volumeID)
}
defer d.volLocks.Release(volumeID)

klog.V(5).Infof("DeleteVolume: Deleting volume %q", volumeID)
err = d.juicefs.JfsDeleteVol(ctx, volumeID, volumeID, secrets, nil, nil)
if err != nil {
Expand Down
17 changes: 11 additions & 6 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/juicedata/juicefs-csi-driver/pkg/juicefs"
"github.com/juicedata/juicefs-csi-driver/pkg/juicefs/mocks"
k8s "github.com/juicedata/juicefs-csi-driver/pkg/k8sclient"
"github.com/juicedata/juicefs-csi-driver/pkg/util"
)

func TestNewControllerService(t *testing.T) {
Expand Down Expand Up @@ -287,6 +288,7 @@ func TestDeleteVolume(t *testing.T) {
vols: map[string]int64{
volumeId: int64(1),
},
volLocks: util.NewVolumeLocks(),
}

_, err := juicefsDriver.DeleteVolume(ctx, req)
Expand Down Expand Up @@ -314,8 +316,9 @@ func TestDeleteVolume(t *testing.T) {
ctx := context.Background()

juicefsDriver := controllerService{
juicefs: nil,
vols: make(map[string]int64),
juicefs: nil,
vols: make(map[string]int64),
volLocks: util.NewVolumeLocks(),
}

_, err := juicefsDriver.DeleteVolume(ctx, req)
Expand Down Expand Up @@ -349,8 +352,9 @@ func TestDeleteVolume(t *testing.T) {
mockJuicefs.EXPECT().JfsDeleteVol(context.TODO(), volumeId, volumeId, secret, nil, nil).Return(errors.New("test"))

juicefsDriver := controllerService{
juicefs: mockJuicefs,
vols: map[string]int64{volumeId: int64(1)},
juicefs: mockJuicefs,
vols: map[string]int64{volumeId: int64(1)},
volLocks: util.NewVolumeLocks(),
}

_, err := juicefsDriver.DeleteVolume(ctx, req)
Expand Down Expand Up @@ -378,8 +382,9 @@ func TestDeleteVolume(t *testing.T) {
ctx := context.Background()

juicefsDriver := controllerService{
juicefs: nil,
vols: make(map[string]int64),
juicefs: nil,
vols: make(map[string]int64),
volLocks: util.NewVolumeLocks(),
}

_, err := juicefsDriver.DeleteVolume(ctx, req)
Expand Down
4 changes: 4 additions & 0 deletions pkg/juicefs/mount/builder/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (r *JobBuilder) newJob(jobName string) *batchv1.Job {
Exec: &corev1.ExecAction{Command: []string{"sh", "-c", "umount /mnt/jfs -l && rmdir /mnt/jfs"}},
},
}
// set node name to empty to let k8s scheduler to choose a node
podTemplate.Spec.NodeName = ""
// set priority class name to empty to make job use default priority class
podTemplate.Spec.PriorityClassName = ""
Copy link
Member Author

@zxh326 zxh326 Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

large number of cleanup/delvol jobs may preempt normal/application pods, which is not as expected

podTemplate.Spec.RestartPolicy = corev1.RestartPolicyOnFailure
job := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand Down
27 changes: 27 additions & 0 deletions pkg/util/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package util
import (
"context"
"fmt"
"sync"

corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
Expand Down Expand Up @@ -102,3 +103,29 @@ func getVol(ctx context.Context, client *k8sclient.K8sClient, pod *corev1.Pod, n
}
return
}

type VolumeLocks struct {
locks sync.Map
mux sync.Mutex
}

func NewVolumeLocks() *VolumeLocks {
return &VolumeLocks{}
}

func (vl *VolumeLocks) TryAcquire(volumeID string) bool {
zxh326 marked this conversation as resolved.
Show resolved Hide resolved
vl.mux.Lock()
defer vl.mux.Unlock()
if _, ok := vl.locks.Load(volumeID); ok {
return false
}
vl.locks.Store(volumeID, nil)
return true
}

// Release deletes the lock on volumeID.
func (vl *VolumeLocks) Release(volumeID string) {
vl.mux.Lock()
defer vl.mux.Unlock()
vl.locks.Delete(volumeID)
}
Loading