-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathboot_disk.go
98 lines (80 loc) · 2.06 KB
/
boot_disk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package health
import (
"context"
"io"
"os"
"sync/atomic"
"syscall"
"time"
"github.com/ydb-platform/nbs/cloud/tasks/logging"
)
////////////////////////////////////////////////////////////////////////////////
const bootDiskProbePeriod = time.Minute
const bootDiskProbeAgeThreshold = 2 * time.Minute
////////////////////////////////////////////////////////////////////////////////
type bootDiskCheck struct {
filePath string
buffer []byte
lastCheck atomic.Pointer[checkResult]
}
type checkResult struct {
timestamp time.Time
success bool
}
func newBootDiskCheck() *bootDiskCheck {
check := bootDiskCheck{
// Reading binary itself to check if disk is available.
filePath: os.Args[0],
buffer: make([]byte, 4096),
}
check.updateResult(true)
return &check
}
func (d *bootDiskCheck) Start(ctx context.Context) {
go func() {
ticker := time.NewTicker(bootDiskProbePeriod)
defer ticker.Stop()
for {
select {
case <-ticker.C:
d.updateResult(d.probe(ctx))
case <-ctx.Done():
return
}
}
}()
}
func (d *bootDiskCheck) Check(ctx context.Context) bool {
lastCheckResult := *d.lastCheck.Load()
if time.Since(lastCheckResult.timestamp) > bootDiskProbeAgeThreshold {
logging.Warn(ctx,
"Boot disk health check failed because probe is hanging. Last check was at %v",
lastCheckResult.timestamp.Format(time.RFC3339),
)
return false
}
return lastCheckResult.success
}
////////////////////////////////////////////////////////////////////////////////
func (d *bootDiskCheck) probe(ctx context.Context) bool {
f, err := os.OpenFile(d.filePath, os.O_RDONLY|syscall.O_DIRECT, 0600)
if err != nil {
logging.Warn(ctx, "Boot disk health check failed to open file: %v", err)
return false
}
defer f.Close()
_, err = f.Read(d.buffer)
if err != nil && err != io.EOF {
logging.Warn(ctx, "Boot disk health check failed to read file: %v", err)
return false
}
return true
}
func (d *bootDiskCheck) updateResult(success bool) {
now := time.Now()
result := checkResult{
timestamp: now,
success: true,
}
d.lastCheck.Store(&result)
}