Skip to content

Commit 9986d5d

Browse files
authored
Merge pull request #414 from yussufsh/test
Improve hosts scan time and do not skip if any read error
2 parents 7b9cb06 + f2b3183 commit 9986d5d

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

adhoc-controllers/controllers/nodeupdate_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
2223
"github.com/pkg/errors"
2324
corev1 "k8s.io/api/core/v1"
2425
apierrors "k8s.io/apimachinery/pkg/api/errors"
2526
"k8s.io/apimachinery/pkg/runtime"
26-
"k8s.io/klog"
27+
"k8s.io/klog/v2"
2728
ctrl "sigs.k8s.io/controller-runtime"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930
"sigs.k8s.io/ibm-powervs-block-csi-driver/pkg/cloud"

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
k8s.io/api v0.26.1
2020
k8s.io/apimachinery v0.26.1
2121
k8s.io/client-go v1.26.1
22-
k8s.io/klog v1.0.0
2322
k8s.io/klog/v2 v2.100.1
2423
k8s.io/kubernetes v1.26.1
2524
k8s.io/mount-utils v0.26.1

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
139139
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
140140
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
141141
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
142-
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
143142
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
144143
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
145144
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
@@ -1016,8 +1015,6 @@ k8s.io/component-base v0.26.1 h1:4ahudpeQXHZL5kko+iDHqLj/FSGAEUnSVO0EBbgDd+4=
10161015
k8s.io/component-base v0.26.1/go.mod h1:VHrLR0b58oC035w6YQiBSbtsf0ThuSwXP+p5dD/kAWU=
10171016
k8s.io/component-helpers v0.26.1 h1:Y5h1OYUJTGyHZlSAsc7mcfNsWF08S/MlrQyF/vn93mU=
10181017
k8s.io/component-helpers v0.26.1/go.mod h1:jxNTnHb1axLe93MyVuvKj9T/+f4nxBVrj/xf01/UNFk=
1019-
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
1020-
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
10211018
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
10221019
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
10231020
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E=

pkg/device/device.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"regexp"
2525
"strings"
26+
"sync"
2627
"time"
2728

2829
"k8s.io/klog/v2"
@@ -31,6 +32,7 @@ import (
3132
var (
3233
lastCleanExecuted time.Time
3334
lastStaleCleanExecuted time.Time
35+
scanLock = &sync.Mutex{}
3436
)
3537

3638
type LinuxDevice interface {
@@ -81,7 +83,8 @@ func (d *Device) Populate(needActivePath bool) error {
8183
tmpPathname := "dm-" + result["Minor"]
8284
uuid, err := getUUID(tmpPathname)
8385
if err != nil {
84-
return err
86+
klog.Warning(err)
87+
continue
8588
}
8689
tmpWWID := strings.TrimPrefix(uuid, "mpath-")
8790
tmpWWN := tmpWWID[1:] // truncate scsi-id prefix
@@ -122,11 +125,6 @@ func (d *Device) DeleteDevice() (err error) {
122125

123126
// CreateDevice: attach and create linux devices to host
124127
func (d *Device) CreateDevice() (err error) {
125-
if err = scsiHostRescan(); err != nil {
126-
return err
127-
}
128-
// wait for device to appear after rescan
129-
time.Sleep(time.Second * 1)
130128

131129
if err = d.createLinuxDevice(); err != nil {
132130
klog.Errorf("unable to create device for wwn %v", d.WWN)
@@ -139,6 +137,35 @@ func (d *Device) CreateDevice() (err error) {
139137
return nil
140138
}
141139

140+
// scsiHostRescanWithLock: scans all scsi hosts with locks
141+
// This is calling scsiHostRescan
142+
// which works in a way that only 1 scan will run at a time
143+
// and other requests will wait till the scan is complete
144+
// but will not scan again as it is already scanned.
145+
func scsiHostRescanWithLock() (err error) {
146+
start := time.Now()
147+
var scan bool = true
148+
defer scanLock.Unlock()
149+
150+
for {
151+
if scanLock.TryLock() {
152+
if scan {
153+
err = scsiHostRescan()
154+
}
155+
return err
156+
} else {
157+
if time.Since(start) > time.Minute {
158+
// Scanning usually takes < 30s. If wait is more than a min then return.
159+
return
160+
}
161+
162+
// Already locked, wait for it to complete and don't scan again.
163+
scan = false
164+
time.Sleep(5 * time.Second)
165+
}
166+
}
167+
}
168+
142169
// createLinuxDevice: attaches and creates a new linux device
143170
// Try device discovery; retry every 5 sec if no device found or have 0 slaves
144171
// In between checks we will try to cleanup:
@@ -151,6 +178,12 @@ func (d *Device) CreateDevice() (err error) {
151178
func (d *Device) createLinuxDevice() (err error) {
152179
// Start a Countdown ticker
153180
for i := 0; i <= 10; i++ {
181+
if err = scsiHostRescanWithLock(); err != nil {
182+
return err
183+
}
184+
// wait for device to appear after rescan
185+
time.Sleep(time.Second * 1)
186+
154187
err := d.Populate(true)
155188
if err != nil {
156189
return err

pkg/device/multipath.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28-
"k8s.io/klog"
28+
"k8s.io/klog/v2"
2929
)
3030

3131
const (
@@ -375,7 +375,8 @@ OUTER:
375375
wwidPath := scsiPath + f.Name() + "/device/wwid"
376376
wwid, err := readFirstLine(wwidPath)
377377
if err != nil {
378-
return err
378+
klog.Warning(err)
379+
continue
379380
}
380381
entries := strings.Split(wwid, ".")
381382
if len(entries) <= 1 {

pkg/driver/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (d *nodeService) deleteDevice(deviceName string) error {
302302
}
303303
dev := NewDevice(wwn)
304304
if err := dev.Populate(false); err != nil {
305-
return fmt.Errorf("failed to delete device %s: %v", deviceName, err)
305+
return fmt.Errorf("failed to populate device %s: %v", deviceName, err)
306306
}
307307
if dev.GetMapper() == "" {
308308
return fmt.Errorf("failed to find device %s mapper for wwn %s", deviceName, wwn)

0 commit comments

Comments
 (0)