Skip to content

Commit 3421797

Browse files
author
jiuyu
committed
Enhancement: add hashCache for namespacedName related labels
Signed-off-by: jiuyu <guotongyu.gty@alibaba-inc.com>
1 parent 58ce2ea commit 3421797

File tree

83 files changed

+614
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+614
-490
lines changed

pkg/common/label.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const (
4646
// i.e. fluid.io/managed-by
4747
LabelAnnotationManagedBy = LabelAnnotationPrefix + "managed-by"
4848

49+
LabelAnnotationNamespacedNameHashAlias = LabelAnnotationDataset + ".namespaced-name-alias"
50+
4951
// fluid adminssion webhook inject flag
5052
// i.e. fluid.io/enable-injection
5153
EnableFluidInjectionFlag = LabelAnnotationPrefix + "enable-injection"

pkg/controllers/runtime_controller.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ func NewRuntimeReconciler(reconciler RuntimeReconcilerInterface, client client.C
7676

7777
// ReconcileInternal handles the logic of reconcile runtime
7878
func (r *RuntimeReconciler) ReconcileInternal(ctx cruntime.ReconcileRequestContext) (ctrl.Result, error) {
79-
8079
// 0. Set context time limit
8180
ctxWithTimeout, cancel := context.WithTimeout(ctx.Context, reconcileTimeout)
8281
defer cancel()
@@ -139,6 +138,11 @@ func (r *RuntimeReconciler) ReconcileInternal(ctx cruntime.ReconcileRequestConte
139138
if !utils.ContainsOwners(objectMeta.GetOwnerReferences(), dataset) {
140139
return r.AddOwnerAndRequeue(ctx, dataset)
141140
}
141+
142+
if err := r.AddNamespacedNameHashAliasLabelOndemand(ctx, dataset); err != nil {
143+
return utils.RequeueIfError(err)
144+
}
145+
142146
if !dataset.CanbeBound(ctx.Name, ctx.Namespace, ctx.Category) {
143147
ctx.Log.Info("the dataset can't be bound to the runtime, because it's already bound to another runtime ",
144148
"dataset", dataset.Name)
@@ -340,6 +344,49 @@ func (r *RuntimeReconciler) AddOwnerAndRequeue(ctx cruntime.ReconcileRequestCont
340344
return utils.RequeueImmediately()
341345
}
342346

347+
// AddNamespacedNameHashAliasLabelOndemand add namesapcedNameHashAlias label on demand
348+
func (r *RuntimeReconciler) AddNamespacedNameHashAliasLabelOndemand(ctx cruntime.ReconcileRequestContext, dataset *datav1alpha1.Dataset) error {
349+
if dataset.GetLabels()[common.LabelAnnotationNamespacedNameHashAlias] != "" {
350+
// hashAlias has been added before
351+
return nil
352+
}
353+
354+
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
355+
dataset, err := r.implement.GetDataset(ctx)
356+
if err != nil {
357+
return err
358+
}
359+
datasetLabels := dataset.GetLabels()
360+
var namespacedNameHashAlias string
361+
362+
// 1. check if the length of namespacedNameHashAlias is longer than 63, which needs to calculate hash alias deterministically
363+
if len(fmt.Sprintf("%s-%s", dataset.Namespace, dataset.Name)) > 63 {
364+
namespacedNameHashAlias = utils.GetNamespacedNameValueWithPrefix("", dataset.Namespace, dataset.Name, "")
365+
} else {
366+
// 2. the length of prefixed namespaced values may longer than 63 and the hash alias has been calculated and stored in cache
367+
found := false
368+
namespacedNameHashAlias, found = utils.GetNamespacedNameHashValueByKey(fmt.Sprintf("%s-%s", dataset.Namespace, dataset.Name))
369+
if !found {
370+
return nil
371+
}
372+
}
373+
374+
if namespacedNameHashAlias == "" {
375+
return nil
376+
}
377+
378+
datasetLabels[common.LabelAnnotationNamespacedNameHashAlias] = namespacedNameHashAlias
379+
datasetToUpdate := dataset.DeepCopy()
380+
datasetToUpdate.Labels = datasetLabels
381+
if err := r.Update(ctx, datasetToUpdate); err != nil {
382+
ctx.Log.Error(err, "Failed to add NamespacedNameHashAliasLabel", "StatusUpdateError", ctx)
383+
return err
384+
}
385+
utils.RemoveNamespacedNameHashValueByKey(fmt.Sprintf("%s-%s", dataset.Namespace, dataset.Name))
386+
return nil
387+
})
388+
}
389+
343390
// GetRuntimeObjectMeta gets runtime object meta
344391
func (r *RuntimeReconciler) GetRuntimeObjectMeta(ctx cruntime.ReconcileRequestContext) (objectMeta metav1.Object, err error) {
345392
objectMetaAccessor, isOM := ctx.Runtime.(metav1.ObjectMetaAccessor)

pkg/csi/plugins/nodeserver.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstag
344344
// 4. remove label on node
345345
// Once the label is removed, fuse pod on corresponding node will be terminated
346346
// since node selector in the fuse daemonSet no longer matches.
347-
fuseLabelKey := utils.GetFuseLabelName(namespace, name)
347+
fuseLabelKey := utils.GetFuseLabelName(namespace, name, runtimeInfo.GetNamespacedNameAlias())
348348
var labelsToModify common.LabelsToModify
349349
labelsToModify.Delete(fuseLabelKey)
350350

@@ -400,7 +400,11 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
400400
}
401401

402402
// 4. Label node to launch FUSE Pod
403-
fuseLabelKey := utils.GetFuseLabelName(namespace, name)
403+
runtimeInfo, err := base.GetRuntimeInfo(ns.client, name, namespace)
404+
if err != nil {
405+
return nil, errors.Wrapf(err, "NodeStageVolume: failed to get runtime info for %s/%s", namespace, name)
406+
}
407+
fuseLabelKey := utils.GetFuseLabelName(namespace, name, runtimeInfo.GetNamespacedNameAlias())
404408
var labelsToModify common.LabelsToModify
405409
labelsToModify.Add(fuseLabelKey, "true")
406410

pkg/ctrl/fuse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (e *Helper) CheckFuseHealthy(recorder record.EventRecorder, runtime base.Ru
109109
func (e *Helper) CleanUpFuse() (count int, err error) {
110110
var (
111111
nodeList = &corev1.NodeList{}
112-
fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName())
112+
fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName(), e.runtimeInfo.GetNamespacedNameAlias())
113113
)
114114

115115
labelNames := []string{fuseLabelKey}
@@ -153,7 +153,7 @@ func (e *Helper) CleanUpFuse() (count int, err error) {
153153
func (e *Helper) GetFuseNodes() (nodes []corev1.Node, err error) {
154154
var (
155155
nodeList = &corev1.NodeList{}
156-
fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName())
156+
fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName(), e.runtimeInfo.GetNamespacedNameAlias())
157157
)
158158

159159
labelNames := []string{fuseLabelKey}

pkg/ddc/alluxio/deprecated_label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (e *AlluxioEngine) HasDeprecatedCommonLabelname() (deprecated bool, err err
5050
nodeSelectors := workers.Spec.Template.Spec.NodeSelector
5151
e.Log.Info("The current node selectors for worker", "workerName", workerName, "nodeSelector", nodeSelectors)
5252

53-
deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name)
53+
deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias())
5454
if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated {
5555
//
5656
e.Log.Info("the deprecated node selector exists", "nodeselector", deprecatedCommonLabelName)

pkg/ddc/alluxio/deprecated_label_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestAlluxioEngine_GetDeprecatedCommonLabelname(t *testing.T) {
6767
},
6868
}
6969
for _, test := range testCases {
70-
out := utils.GetCommonLabelName(true, test.namespace, test.name)
70+
out := utils.GetCommonLabelName(true, test.namespace, test.name, "")
7171
if out != test.out {
7272
t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out)
7373
}

pkg/ddc/alluxio/master_internal_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package alluxio
1818

1919
import (
2020
"fmt"
21+
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
2122
"testing"
2223

2324
"github.com/brahma-adshonor/gohook"
@@ -85,6 +86,11 @@ func TestSetupMasterInternal(t *testing.T) {
8586
}
8687
client := fake.NewFakeClientWithScheme(testScheme, testObjs...)
8788

89+
runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "alluxio")
90+
if err != nil {
91+
t.Errorf("fail to create the runtimeInfo with error %v", err)
92+
}
93+
8894
engine := AlluxioEngine{
8995
name: "hbase",
9096
namespace: "fluid",
@@ -100,8 +106,10 @@ func TestSetupMasterInternal(t *testing.T) {
100106
},
101107
},
102108
},
109+
runtimeInfo: runtimeInfo,
103110
}
104-
err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts)
111+
112+
err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts)
105113
if err != nil {
106114
t.Fatal(err.Error())
107115
}
@@ -195,6 +203,11 @@ func TestGenerateAlluxioValueFile(t *testing.T) {
195203

196204
client := fake.NewFakeClientWithScheme(testScheme, testObjs...)
197205

206+
runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "alluxio")
207+
if err != nil {
208+
t.Errorf("fail to create the runtimeInfo with error %v", err)
209+
}
210+
198211
engine := AlluxioEngine{
199212
name: "hbase",
200213
namespace: "fluid",
@@ -210,9 +223,10 @@ func TestGenerateAlluxioValueFile(t *testing.T) {
210223
},
211224
},
212225
},
226+
runtimeInfo: runtimeInfo,
213227
}
214228

215-
err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts)
229+
err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts)
216230
if err != nil {
217231
t.Fatal(err.Error())
218232
}

pkg/ddc/alluxio/runtime_info.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ func (e *AlluxioEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) {
4343
e.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector)
4444

4545
if !e.UnitTest {
46+
// Setup with Dataset Info
47+
dataset, err := utils.GetDataset(e.Client, e.name, e.namespace)
48+
if err != nil {
49+
if utils.IgnoreNotFound(err) == nil {
50+
e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace)
51+
return e.runtimeInfo, nil
52+
}
53+
54+
e.Log.Info("Failed to get dataset when getRuntimeInfo")
55+
return e.runtimeInfo, err
56+
}
57+
58+
e.runtimeInfo.SetupWithDataset(dataset)
59+
e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive())
60+
61+
e.runtimeInfo.SetNamespacedNameHashAliasWithDataset(dataset)
62+
4663
// Check if the runtime is using deprecated labels
4764
isLabelDeprecated, err := e.HasDeprecatedCommonLabelname()
4865
if err != nil {
@@ -58,22 +75,6 @@ func (e *AlluxioEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) {
5875
e.runtimeInfo.SetDeprecatedPVName(isPVNameDeprecated)
5976

6077
e.Log.Info("Deprecation check finished", "isLabelDeprecated", e.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", e.runtimeInfo.IsDeprecatedPVName())
61-
62-
// Setup with Dataset Info
63-
dataset, err := utils.GetDataset(e.Client, e.name, e.namespace)
64-
if err != nil {
65-
if utils.IgnoreNotFound(err) == nil {
66-
e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace)
67-
return e.runtimeInfo, nil
68-
}
69-
70-
e.Log.Info("Failed to get dataset when getRuntimeInfo")
71-
return e.runtimeInfo, err
72-
}
73-
74-
e.runtimeInfo.SetupWithDataset(dataset)
75-
76-
e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive())
7778
}
7879
}
7980

pkg/ddc/alluxio/shutdown.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (e *AlluxioEngine) destroyWorkers(expectedWorkers int32) (currentWorkers in
292292
labelsToModify.Delete(label)
293293
}
294294

295-
exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name)
295+
exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue()
296296
if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue {
297297
labelsToModify.Delete(labelExclusiveName)
298298
}

pkg/ddc/alluxio/shutdown_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ func TestDestroyWorker(t *testing.T) {
225225
}
226226

227227
if len(newNode.Labels) != len(test.wantedNodeLabels[node.Name]) {
228+
t.Log(newNode.Labels)
229+
t.Log(test.wantedNodeLabels[node.Name])
228230
t.Errorf("fail to decrease the labels")
229231
}
230232
if len(newNode.Labels) != 0 && !reflect.DeepEqual(newNode.Labels, test.wantedNodeLabels[node.Name]) {

pkg/ddc/alluxio/transform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (e *AlluxioEngine) transform(runtime *datav1alpha1.AlluxioRuntime) (value *
5050
}
5151

5252
value.FullnameOverride = e.name
53-
value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name)
53+
value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias())
5454

5555
// 1.transform the common part
5656
err = e.transformCommonPart(runtime, dataset, value)

pkg/ddc/alluxio/transform_fuse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (e *AlluxioEngine) transformFuse(runtime *datav1alpha1.AlluxioRuntime, data
100100
} else {
101101
value.Fuse.NodeSelector = map[string]string{}
102102
}
103-
value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true"
103+
value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true"
104104

105105
// parse fuse container network mode
106106
value.Fuse.HostNetwork = datav1alpha1.IsHostNetwork(runtime.Spec.Fuse.NetworkMode)

pkg/ddc/alluxio/transform_fuse_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package alluxio
1818

1919
import (
20+
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
2021
"reflect"
2122
"testing"
2223

@@ -62,11 +63,17 @@ func TestTransformFuseWithNoArgs(t *testing.T) {
6263
}}, &Alluxio{}, []string{"fuse", "--fuse-opts=kernel_cache,rw,allow_other", "/alluxio/default/test/alluxio-fuse", "/"}, false},
6364
}
6465
for _, test := range tests {
66+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
67+
if err != nil {
68+
t.Errorf("fail to create the runtimeInfo with error %v", err)
69+
}
6570
engine := &AlluxioEngine{
66-
name: "test",
67-
namespace: "default",
68-
Log: fake.NullLogger()}
69-
err := engine.transformFuse(test.runtime, test.dataset, test.alluxioValue)
71+
name: "test",
72+
namespace: "default",
73+
Log: fake.NullLogger(),
74+
runtimeInfo: runtimeInfo,
75+
}
76+
err = engine.transformFuse(test.runtime, test.dataset, test.alluxioValue)
7077
if err != nil {
7178
t.Errorf("Got err %v", err)
7279
}
@@ -153,11 +160,17 @@ func TestTransformFuseWithArgs(t *testing.T) {
153160
}}, &Alluxio{}, []string{"fuse", "--fuse-opts=kernel_cache,allow_other", "/alluxio/default/test/alluxio-fuse", "/"}, false},
154161
}
155162
for _, test := range tests {
163+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
164+
if err != nil {
165+
t.Errorf("fail to create the runtimeInfo with error %v", err)
166+
}
156167
engine := &AlluxioEngine{
157-
name: "test",
158-
namespace: "default",
159-
Log: fake.NullLogger()}
160-
err := engine.transformFuse(test.runtime, test.dataset, test.alluxioValue)
168+
name: "test",
169+
namespace: "default",
170+
Log: fake.NullLogger(),
171+
runtimeInfo: runtimeInfo,
172+
}
173+
err = engine.transformFuse(test.runtime, test.dataset, test.alluxioValue)
161174
if err != nil {
162175
t.Errorf("Got err %v", err)
163176
}

pkg/ddc/base/label.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ limitations under the Licensinfo.
1717
package base
1818

1919
import (
20+
"fmt"
2021
"github.com/fluid-cloudnative/fluid/pkg/common"
2122
"github.com/fluid-cloudnative/fluid/pkg/utils"
23+
"k8s.io/apimachinery/pkg/util/validation"
2224
)
2325

2426
func (info *RuntimeInfo) GetLabelNameForMemory() string {
25-
return utils.GetLabelNameForMemory(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name)
27+
return utils.GetLabelNameForMemory(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.namespacedNameAlias)
2628
}
2729

2830
func (info *RuntimeInfo) GetLabelNameForDisk() string {
29-
return utils.GetLabelNameForDisk(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name)
31+
return utils.GetLabelNameForDisk(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.namespacedNameAlias)
3032
}
3133

3234
func (info *RuntimeInfo) GetLabelNameForTotal() string {
33-
return utils.GetLabelNameForTotal(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name)
35+
return utils.GetLabelNameForTotal(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.namespacedNameAlias)
3436
}
3537

3638
func (info *RuntimeInfo) GetCommonLabelName() string {
37-
return utils.GetCommonLabelName(info.IsDeprecatedNodeLabel(), info.namespace, info.name)
39+
return utils.GetCommonLabelName(info.IsDeprecatedNodeLabel(), info.namespace, info.name, info.namespacedNameAlias)
3840
}
3941

4042
func (info *RuntimeInfo) GetRuntimeLabelName() string {
41-
return utils.GetRuntimeLabelName(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name)
43+
return utils.GetRuntimeLabelName(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.namespacedNameAlias)
4244
}
4345

4446
// GetDatasetNumLabelname get the label to record how much datasets on a node
@@ -48,5 +50,14 @@ func (info *RuntimeInfo) GetDatasetNumLabelName() string {
4850

4951
// GetFuseLabelName gets the label indicating a fuse running on some node.
5052
func (info *RuntimeInfo) GetFuseLabelName() string {
51-
return utils.TransferFullNamespacedNameWithPrefixToLegalValue(common.LabelAnnotationFusePrefix, info.namespace, info.name)
53+
return utils.GetNamespacedNameValueWithPrefix(common.LabelAnnotationFusePrefix, info.namespace, info.name, info.namespacedNameAlias)
54+
}
55+
56+
func (info *RuntimeInfo) GetExclusiveLabelValue() string {
57+
exclusiveLabelValue := fmt.Sprintf("%s_%s", info.namespace, info.name)
58+
if len(exclusiveLabelValue) < validation.DNS1035LabelMaxLength {
59+
return exclusiveLabelValue
60+
}
61+
62+
return utils.GetNamespacedNameValueWithPrefix("", info.namespace, info.name, info.namespacedNameAlias)
5263
}

pkg/ddc/base/label_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestGetStorageLabelName(t *testing.T) {
4949
}
5050

5151
for _, test := range tests {
52-
result := utils.GetStorageLabelName(common.HumanReadType, common.MemoryStorageType, test.info.IsDeprecatedNodeLabel(), test.info.runtimeType, test.info.namespace, test.info.name)
52+
result := utils.GetStorageLabelName(common.HumanReadType, common.MemoryStorageType, test.info.IsDeprecatedNodeLabel(), test.info.runtimeType, test.info.namespace, test.info.name, "")
5353
if test.expectedResult != result {
5454
t.Errorf("check failure, expected %s, get %s", test.expectedResult, result)
5555
}

0 commit comments

Comments
 (0)