From 45d41ed9e53870a061fe53d8c7ae29a44520f80e Mon Sep 17 00:00:00 2001 From: sunxiaofei03 Date: Thu, 29 Aug 2019 19:22:25 +0800 Subject: [PATCH] replace iteration with hashmap in *state_of_world --- .../cache/actual_state_of_world.go | 27 ++++++++---------- .../cache/actual_state_of_world.go | 28 ++++++++----------- .../cache/desired_state_of_world.go | 8 ++---- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/pkg/controller/volume/attachdetach/cache/actual_state_of_world.go b/pkg/controller/volume/attachdetach/cache/actual_state_of_world.go index 039fab7e6b8..b8f50157f78 100644 --- a/pkg/controller/volume/attachdetach/cache/actual_state_of_world.go +++ b/pkg/controller/volume/attachdetach/cache/actual_state_of_world.go @@ -569,13 +569,10 @@ func (asw *actualStateOfWorld) GetAttachedVolumesForNode( attachedVolumes := make( []AttachedVolume, 0 /* len */, len(asw.attachedVolumes) /* cap */) for _, volumeObj := range asw.attachedVolumes { - for actualNodeName, nodeObj := range volumeObj.nodesAttachedTo { - if actualNodeName == nodeName { - attachedVolumes = append( - attachedVolumes, - getAttachedVolume(&volumeObj, &nodeObj)) - break - } + if nodeObj, nodeExists := volumeObj.nodesAttachedTo[nodeName]; nodeExists { + attachedVolumes = append( + attachedVolumes, + getAttachedVolume(&volumeObj, &nodeObj)) } } @@ -610,9 +607,9 @@ func (asw *actualStateOfWorld) GetNodesForAttachedVolume(volumeName v1.UniqueVol } nodes := []types.NodeName{} - for k, nodesAttached := range volumeObj.nodesAttachedTo { + for nodeName, nodesAttached := range volumeObj.nodesAttachedTo { if nodesAttached.attachedConfirmed { - nodes = append(nodes, k) + nodes = append(nodes, nodeName) } } return nodes @@ -627,14 +624,14 @@ func (asw *actualStateOfWorld) GetVolumesToReportAttached() map[types.NodeName][ if nodeToUpdateObj.statusUpdateNeeded { attachedVolumes := make( []v1.AttachedVolume, + 0, len(nodeToUpdateObj.volumesToReportAsAttached) /* len */) - i := 0 for _, volume := range nodeToUpdateObj.volumesToReportAsAttached { - attachedVolumes[i] = v1.AttachedVolume{ - Name: volume, - DevicePath: asw.attachedVolumes[volume].devicePath, - } - i++ + attachedVolumes = append(attachedVolumes, + v1.AttachedVolume{ + Name: volume, + DevicePath: asw.attachedVolumes[volume].devicePath, + }) } volumesToReportAttached[nodeToUpdateObj.nodeName] = attachedVolumes } diff --git a/pkg/kubelet/volumemanager/cache/actual_state_of_world.go b/pkg/kubelet/volumemanager/cache/actual_state_of_world.go index 0cc57aed0c4..08fe393e5a2 100644 --- a/pkg/kubelet/volumemanager/cache/actual_state_of_world.go +++ b/pkg/kubelet/volumemanager/cache/actual_state_of_world.go @@ -492,11 +492,7 @@ func (asw *actualStateOfWorld) MarkRemountRequired( asw.Lock() defer asw.Unlock() for volumeName, volumeObj := range asw.attachedVolumes { - for mountedPodName, podObj := range volumeObj.mountedPods { - if mountedPodName != podName { - continue - } - + if podObj, podExists := volumeObj.mountedPods[podName]; podExists { volumePlugin, err := asw.volumePluginMgr.FindPluginBySpec(podObj.volumeSpec) if err != nil || volumePlugin == nil { @@ -523,14 +519,14 @@ func (asw *actualStateOfWorld) MarkFSResizeRequired( podName volumetypes.UniquePodName) { asw.Lock() defer asw.Unlock() - volumeObj, exist := asw.attachedVolumes[volumeName] - if !exist { + volumeObj, volumeExists := asw.attachedVolumes[volumeName] + if !volumeExists { klog.Warningf("MarkFSResizeRequired for volume %s failed as volume not exist", volumeName) return } - podObj, exist := volumeObj.mountedPods[podName] - if !exist { + podObj, podExists := volumeObj.mountedPods[podName] + if !podExists { klog.Warningf("MarkFSResizeRequired for volume %s failed "+ "as pod(%s) not exist", volumeName, podName) return @@ -648,8 +644,8 @@ func (asw *actualStateOfWorld) VolumeExistsWithSpecName(podName volumetypes.Uniq asw.RLock() defer asw.RUnlock() for _, volumeObj := range asw.attachedVolumes { - for name, podObj := range volumeObj.mountedPods { - if podName == name && podObj.volumeSpec.Name() == volumeSpecName { + if podObj, podExists := volumeObj.mountedPods[podName]; podExists { + if podObj.volumeSpec.Name() == volumeSpecName { return true } } @@ -687,12 +683,10 @@ func (asw *actualStateOfWorld) GetMountedVolumesForPod( defer asw.RUnlock() mountedVolume := make([]MountedVolume, 0 /* len */, len(asw.attachedVolumes) /* cap */) for _, volumeObj := range asw.attachedVolumes { - for mountedPodName, podObj := range volumeObj.mountedPods { - if mountedPodName == podName { - mountedVolume = append( - mountedVolume, - getMountedVolume(&podObj, &volumeObj)) - } + if podObj, podExists := volumeObj.mountedPods[podName]; podExists { + mountedVolume = append( + mountedVolume, + getMountedVolume(&podObj, &volumeObj)) } } diff --git a/pkg/kubelet/volumemanager/cache/desired_state_of_world.go b/pkg/kubelet/volumemanager/cache/desired_state_of_world.go index 3387a2a93b4..7d4ee41be37 100644 --- a/pkg/kubelet/volumemanager/cache/desired_state_of_world.go +++ b/pkg/kubelet/volumemanager/cache/desired_state_of_world.go @@ -362,8 +362,8 @@ func (dsw *desiredStateOfWorld) VolumeExistsWithSpecName(podName types.UniquePod dsw.RLock() defer dsw.RUnlock() for _, volumeObj := range dsw.volumesToMount { - for name, podObj := range volumeObj.podsToMount { - if podName == name && podObj.volumeSpec.Name() == volumeSpecName { + if podObj, podExists := volumeObj.podsToMount[podName]; podExists { + if podObj.volumeSpec.Name() == volumeSpecName { return true } } @@ -378,9 +378,7 @@ func (dsw *desiredStateOfWorld) GetPods() map[types.UniquePodName]bool { podList := make(map[types.UniquePodName]bool) for _, volumeObj := range dsw.volumesToMount { for podName := range volumeObj.podsToMount { - if !podList[podName] { - podList[podName] = true - } + podList[podName] = true } } return podList