Skip to content

Commit

Permalink
Support drive anti-affinity for volumes
Browse files Browse the repository at this point in the history
Some optimal setups would require volumes to be allocated on unique disks.
ie, not more than one volume per disk.

By default, the volume scheduling algorithm choses the drive based on most
free capacity. This will end up allocating more than one volumes per disk.

This PR provides a way for such optimal setups, by using the storage class
parameters. Using a storage class with `directpv.min.io/unique-alloc-id: XXX`
parameter enables unique allocation for PVCs. This unique allocation id enables
the one-to-one cardinality for the drives and volumes.
  • Loading branch information
Praveenrajmani committed Aug 14, 2023
1 parent 8efdfac commit ff60dee
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 20 deletions.
39 changes: 21 additions & 18 deletions docs/volume-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ DirectPV CSI controller selects suitable drive for `CreateVolume` request like b
3. As no `DirectPVDrive` CRD object has the requested volume, each drive is selected by
a. By requested capacity
b. By access-tier if requested
c. By topology constraints if requested
c. If unique id doesn't match
d. By topology constraints if requested
4. In the process of step (3), if more than one drive is selected, the maximum free capacity drive is picked.
5. If step (4) picks up more than one drive, a drive is randomly selected.
6. Finally the selected drive is updated with requested volume information.
Expand Down Expand Up @@ -42,23 +43,25 @@ DirectPV CSI controller selects suitable drive for `CreateVolume` request like b
│ drive │ │ matched? │ | No │ Match by │
└─────^─────┘ ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯ |<----│ access-tier │
| | Yes | │ if requested? │
| ┌───────V───────┐ | ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
| │ Filter drives │ | | Yes
| │ by maximum │ | ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╮
| │ free capacity │ | │ Match by │
| └───────────────┘ | No │ topology │
| | |<----│ constraints │
| ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╮ | │ if requested? │
| No │ Is more than │ | ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
+-----------│ one drive │ | | Yes
│ matched? │ | ┌───────V───────┐
╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯ | │ Append to │
| Yes +<----│ matched drives│
┌───────V───────┐ └───────────────┘
│ Return │
│ Randomly │
│ selected drive│
└───────────────┘
| ┌───────V───────┐ | ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
| │ Filter drives │ | |
| │ by maximum │ | ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╌╌╮
| │ free capacity │ | Yes | Match by |
| └───────────────┘ |<----| unique-alloc-id |
| | | | if requested? |
| ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╮ | ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
| No │ Is more than │ | | No
+-----------│ one drive │ | ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╮
| matched? │ | │ Match by │
╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯ | No │ topology │
| Yes |<----│ constraints │
┌───────V───────┐ | │ if requested? │
│ Return │ | ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
│ Randomly │ | | Yes
│ selected drive│ | ┌───────V───────┐
└───────────────┘ | │ Append to │
+<----│ matched drives│
└───────────────┘
```

## Customizing drive selection
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/directpv.min.io/types/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ const (

// RequestIDLabelKey label key for request ID
RequestIDLabelKey LabelKey = consts.GroupName + "/request-id"

// UniqueAllocIDLabelKey label key to denote the unique allocation of drives for volumes
UniqueAllocIDLabelKey LabelKey = consts.GroupName + "/unique-alloc-id"

// UniqueAllocIDLabelKeyPrefix label key prefix for unique allocation id to be set on the drive
UniqueAllocIDLabelKeyPrefix = consts.GroupName + "/unique-alloc-id-"
)

// LabelValue is a type definition for label value
Expand Down
19 changes: 19 additions & 0 deletions pkg/apis/directpv.min.io/v1beta1/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package v1beta1

import (
"strconv"
"strings"

"github.com/minio/directpv/pkg/apis/directpv.min.io/types"
Expand Down Expand Up @@ -221,6 +222,24 @@ func (drive DirectPVDrive) GetNodeID() types.NodeID {
return types.NodeID(drive.getLabel(types.NodeLabelKey))
}

// HasUniqueAllocID checks if the provided allocation id is set on the drive.
func (drive *DirectPVDrive) HasUniqueAllocID(allocID string) bool {
if v, ok := drive.GetLabels()[types.UniqueAllocIDLabelKeyPrefix+allocID]; ok && v == strconv.FormatBool(true) {
return true
}
return false
}

// SetUniqueAllocID sets the provided allocation id on the drive.
func (drive *DirectPVDrive) SetUniqueAllocID(allocID string) {
drive.SetLabel(types.LabelKey(types.UniqueAllocIDLabelKeyPrefix+allocID), types.LabelValue(strconv.FormatBool(true)))
}

// RemoveUniqueAllocID removes the unique alloc ID label.
func (drive *DirectPVDrive) RemoveUniqueAllocID(allocID string) {
drive.RemoveLabel(types.LabelKey(types.UniqueAllocIDLabelKeyPrefix + allocID))
}

// SetLabel sets label to this drive.
func (drive *DirectPVDrive) SetLabel(key types.LabelKey, value types.LabelValue) bool {
values := drive.GetLabels()
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/directpv.min.io/v1beta1/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ func (volume DirectPVVolume) GetTenantName() string {
return string(volume.getLabel(types.LabelKey(Group + "/tenant")))
}

// SetUniqueAllocID sets the provided allocation id on the volume.
func (volume *DirectPVVolume) SetUniqueAllocID(allocID string) {
volume.SetLabel(types.UniqueAllocIDLabelKey, types.LabelValue(allocID))
}

// GetUniqueAllocID gets the provided allocation id on the volume.
func (volume *DirectPVVolume) GetUniqueAllocID() string {
return string(volume.getLabel(types.UniqueAllocIDLabelKey))
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// DirectPVVolumeList denotes list of volumes.
Expand Down
15 changes: 14 additions & 1 deletion pkg/csi/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,18 @@ func (c *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
return nil, status.Errorf(codes.InvalidArgument, "unsupported filesystem type %v for volume %v", req.GetVolumeCapabilities()[0].GetMount().GetFsType(), name)
}

var uniqueAllocID string
for key, value := range req.GetParameters() {
if key == string(directpvtypes.AccessTierLabelKey) {
switch key {
case string(directpvtypes.AccessTierLabelKey):
if _, err := directpvtypes.StringsToAccessTiers(value); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unknown access-tier %v for volume %v; %v", value, name, err)
}
case string(directpvtypes.UniqueAllocIDLabelKey):
if err := validAllocID(value); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid %v value; %v", directpvtypes.UniqueAllocIDLabelKey, err)
}
uniqueAllocID = value
}
}

Expand Down Expand Up @@ -177,6 +184,9 @@ func (c *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
drive.GetDriveName(),
size,
)
if uniqueAllocID != "" {
newVolume.SetUniqueAllocID(uniqueAllocID)
}

if _, err := client.VolumeClient().Create(ctx, newVolume, metav1.CreateOptions{}); err != nil {
if !errors.IsAlreadyExists(err) {
Expand Down Expand Up @@ -206,6 +216,9 @@ func (c *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}

if drive.AddVolumeFinalizer(req.GetName()) {
if uniqueAllocID != "" {
drive.SetUniqueAllocID(uniqueAllocID)
}
drive.Status.FreeCapacity -= size
drive.Status.AllocatedCapacity += size

Expand Down
13 changes: 13 additions & 0 deletions pkg/csi/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func matchDrive(drive *types.Drive, req *csi.CreateVolumeRequest) bool {
if len(accessTiers) > 0 && drive.GetAccessTier() != accessTiers[0] {
return false
}
case string(directpvtypes.UniqueAllocIDLabelKey):
if value != "" && drive.HasUniqueAllocID(value) {
// Do not allocate another volume with this unique alloc id
return false
}
default:
if labels[key] != value {
return false
Expand Down Expand Up @@ -159,3 +164,11 @@ func selectDrive(ctx context.Context, req *csi.CreateVolumeRequest) (*types.Driv

return &maxFreeCapacityDrives[n.Int64()], nil
}

func validAllocID(allocID string) error {
_, err := directpvtypes.NewLabelKey(directpvtypes.UniqueAllocIDLabelKeyPrefix + allocID)
if err == nil {
_, err = directpvtypes.NewLabelValue(allocID)
}
return err
}
4 changes: 3 additions & 1 deletion pkg/volume/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ func (handler *volumeEventHandler) releaseVolume(ctx context.Context, volume *ty

drive.Status.FreeCapacity += volume.Status.TotalCapacity
drive.Status.AllocatedCapacity = drive.Status.TotalCapacity - drive.Status.FreeCapacity

if uniqueAllocID := volume.GetUniqueAllocID(); uniqueAllocID != "" {
drive.RemoveUniqueAllocID(uniqueAllocID)
}
_, err = client.DriveClient().Update(
ctx, drive, metav1.UpdateOptions{TypeMeta: types.NewDriveTypeMeta()},
)
Expand Down

0 comments on commit ff60dee

Please sign in to comment.