Skip to content

Commit

Permalink
Fix pvc resize support for tidb (#3891)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonly authored Apr 6, 2021
1 parent 449ed11 commit 2286554
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
46 changes: 33 additions & 13 deletions pkg/manager/member/pvc_resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type PVCResizerInterface interface {

var (
pdRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.PDLabelVal})
tidbRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.TiDBLabelVal})
tikvRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.TiKVLabelVal})
tiflashRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.TiFlashLabelVal})
pumpRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.PumpLabelVal})
Expand All @@ -86,14 +87,14 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error {
return err
}

// First we compose a map, with PVC name prefix for current component as keys,
// For each component, we compose a map called pvcPrefix2Quantity, with PVC name prefix for current component as keys,
// for example "pd-${tcName}-pd" (for tc.Spec.PD.Requests) or "pd-log-${tcName}-pd" (for tc.Spec.PD.storageVolumes elements with name "log").
// Reference implementation of BuildStorageVolumeAndVolumeMount().
// Note: for TiFlash, it is currently "data0-${tcName}-tiflash" (for tc.Spec.TiFlash.StorageClaims elements, in list definition order)
pvcPrefix2Quantity := make(map[string]resource.Quantity)

// patch PD PVCs
if tc.Spec.PD != nil {
pvcPrefix2Quantity := make(map[string]resource.Quantity)
pdMemberType := v1alpha1.PDMemberType.String()
if quantity, ok := tc.Spec.PD.Requests[corev1.ResourceStorage]; ok {
key := fmt.Sprintf("%s-%s-%s", pdMemberType, tc.Name, pdMemberType)
Expand All @@ -111,8 +112,25 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error {
return err
}
}
// patch TiDB PVCs
if tc.Spec.TiDB != nil {
pvcPrefix2Quantity := make(map[string]resource.Quantity)
tidbMemberType := v1alpha1.TiDBMemberType.String()
for _, sv := range tc.Spec.TiDB.StorageVolumes {
key := fmt.Sprintf("%s-%s-%s-%s", tidbMemberType, sv.Name, tc.Name, tidbMemberType)
if quantity, err := resource.ParseQuantity(sv.StorageSize); err == nil {
pvcPrefix2Quantity[key] = quantity
} else {
klog.Warningf("StorageVolume %q in %s/%s .Spec.TiDB is invalid", sv.Name, ns, tc.Name)
}
}
if err := p.patchPVCs(ns, selector.Add(*tidbRequirement), pvcPrefix2Quantity); err != nil {
return err
}
}
// patch TiKV PVCs
if tc.Spec.TiKV != nil {
pvcPrefix2Quantity := make(map[string]resource.Quantity)
tikvMemberType := v1alpha1.TiKVMemberType.String()
if quantity, ok := tc.Spec.TiKV.Requests[corev1.ResourceStorage]; ok {
key := fmt.Sprintf("%s-%s-%s", tikvMemberType, tc.Name, tikvMemberType)
Expand All @@ -132,6 +150,7 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error {
}
// patch TiFlash PVCs
if tc.Spec.TiFlash != nil {
pvcPrefix2Quantity := make(map[string]resource.Quantity)
tiflashMemberType := v1alpha1.TiFlashMemberType.String()
for i, claim := range tc.Spec.TiFlash.StorageClaims {
key := fmt.Sprintf("data%d-%s-%s", i, tc.Name, tiflashMemberType)
Expand All @@ -145,6 +164,7 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error {
}
// patch Pump PVCs
if tc.Spec.Pump != nil {
pvcPrefix2Quantity := make(map[string]resource.Quantity)
pumpMemberType := v1alpha1.PumpMemberType.String()
if quantity, ok := tc.Spec.Pump.Requests[corev1.ResourceStorage]; ok {
key := fmt.Sprintf("data-%s-%s", tc.Name, pumpMemberType)
Expand All @@ -164,20 +184,23 @@ func (p *pvcResizer) ResizeDM(dc *v1alpha1.DMCluster) error {
if err != nil {
return err
}
pvcPrefix2Quantity := make(map[string]resource.Quantity)

// patch dm-master PVCs
if quantity, err := resource.ParseQuantity(dc.Spec.Master.StorageSize); err == nil {
dmMasterMemberType := v1alpha1.DMMasterMemberType.String()
key := fmt.Sprintf("%s-%s-%s", dmMasterMemberType, dc.Name, dmMasterMemberType)
pvcPrefix2Quantity[key] = quantity
}
if err := p.patchPVCs(ns, selector.Add(*dmMasterRequirement), pvcPrefix2Quantity); err != nil {
return err
{
pvcPrefix2Quantity := make(map[string]resource.Quantity)
if quantity, err := resource.ParseQuantity(dc.Spec.Master.StorageSize); err == nil {
dmMasterMemberType := v1alpha1.DMMasterMemberType.String()
key := fmt.Sprintf("%s-%s-%s", dmMasterMemberType, dc.Name, dmMasterMemberType)
pvcPrefix2Quantity[key] = quantity
}
if err := p.patchPVCs(ns, selector.Add(*dmMasterRequirement), pvcPrefix2Quantity); err != nil {
return err
}
}

// patch dm-worker PVCs
if dc.Spec.Worker != nil {
pvcPrefix2Quantity := make(map[string]resource.Quantity)
if quantity, err := resource.ParseQuantity(dc.Spec.Worker.StorageSize); err == nil {
dmWorkerMemberType := v1alpha1.DMWorkerMemberType.String()
key := fmt.Sprintf("%s-%s-%s", dmWorkerMemberType, dc.Name, dmWorkerMemberType)
Expand Down Expand Up @@ -211,9 +234,6 @@ func (p *pvcResizer) patchPVCs(ns string, selector labels.Selector, pvcQuantityI
return err
}

if err != nil {
return err
}
// the PVC name for StatefulSet will be ${pvcNameInTemplate}-${stsName}-${ordinal}, here we want to drop the ordinal
rePvcPrefix := regexp.MustCompile(`^(.+)-\d+$`)
for _, pvc := range pvcs {
Expand Down
30 changes: 30 additions & 0 deletions pkg/manager/member/pvc_resizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ func TestPVCResizer(t *testing.T) {
newPVCWithStorage("pd-log-tc-pd-2", label.PDLabelVal, "sc", "2Gi"),
},
},
{
name: "resize TiDB PVCs",
tc: &v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.NamespaceDefault,
Name: "tc",
},
Spec: v1alpha1.TidbClusterSpec{
TiDB: &v1alpha1.TiDBSpec{
StorageVolumes: []v1alpha1.StorageVolume{
{
Name: "log",
StorageSize: "2Gi",
},
},
},
},
},
sc: newStorageClass("sc", true),
pvcs: []*v1.PersistentVolumeClaim{
newPVCWithStorage("tidb-log-tc-tidb-0", label.TiDBLabelVal, "sc", "1Gi"),
newPVCWithStorage("tidb-log-tc-tidb-1", label.TiDBLabelVal, "sc", "1Gi"),
newPVCWithStorage("tidb-log-tc-tidb-2", label.TiDBLabelVal, "sc", "1Gi"),
},
wantPVCs: []*v1.PersistentVolumeClaim{
newPVCWithStorage("tidb-log-tc-tidb-0", label.TiDBLabelVal, "sc", "2Gi"),
newPVCWithStorage("tidb-log-tc-tidb-1", label.TiDBLabelVal, "sc", "2Gi"),
newPVCWithStorage("tidb-log-tc-tidb-2", label.TiDBLabelVal, "sc", "2Gi"),
},
},
{
name: "resize TiKV PVCs",
tc: &v1alpha1.TidbCluster{
Expand Down

0 comments on commit 2286554

Please sign in to comment.