-
Notifications
You must be signed in to change notification settings - Fork 30
/
repairworker.go
206 lines (177 loc) · 5.14 KB
/
repairworker.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package sdk
import (
"context"
"math/bits"
"os"
"sync"
"github.com/0chain/gosdk/zboxcore/fileref"
. "github.com/0chain/gosdk/zboxcore/logger"
"go.uber.org/zap"
)
type RepairRequest struct {
listDir *ListResult
isRepairCanceled bool
localRootPath string
statusCB StatusCallback
completedCallback func()
filesRepaired int
wg *sync.WaitGroup
}
type RepairStatusCB struct {
wg *sync.WaitGroup
success bool
err error
statusCB StatusCallback
}
func (cb *RepairStatusCB) CommitMetaCompleted(request, response string, err error) {
cb.statusCB.CommitMetaCompleted(request, response, err)
}
func (cb *RepairStatusCB) Started(allocationId, filePath string, op int, totalBytes int) {
cb.statusCB.Started(allocationId, filePath, op, totalBytes)
}
func (cb *RepairStatusCB) InProgress(allocationId, filePath string, op int, completedBytes int) {
cb.statusCB.InProgress(allocationId, filePath, op, completedBytes)
}
func (cb *RepairStatusCB) RepairCompleted(filesRepaired int) {
cb.statusCB.RepairCompleted(filesRepaired)
}
func (cb *RepairStatusCB) Completed(allocationId, filePath string, filename string, mimetype string, size int, op int) {
cb.statusCB.Completed(allocationId, filePath, filename, mimetype, size, op)
cb.success = true
cb.wg.Done()
}
func (cb *RepairStatusCB) Error(allocationID string, filePath string, op int, err error) {
cb.statusCB.Error(allocationID, filePath, op, err)
cb.success = false
cb.err = err
cb.wg.Done()
}
func (r *RepairRequest) processRepair(ctx context.Context, a *Allocation) {
if r.completedCallback != nil {
defer r.completedCallback()
}
if r.checkForCancel(a) {
return
}
r.iterateDir(a, r.listDir)
if r.statusCB != nil {
r.statusCB.RepairCompleted(r.filesRepaired)
}
return
}
func (r *RepairRequest) iterateDir(a *Allocation, dir *ListResult) {
switch dir.Type {
case fileref.DIRECTORY:
if len(dir.Children) == 0 {
var err error
fullconsensus := float32(a.DataShards + a.ParityShards)
consensusThresh := 100 / fullconsensus
dir, err = a.listDir(dir.Path, consensusThresh, fullconsensus)
if err != nil {
Logger.Error("Failed to get listDir for path ", zap.Any("path", dir.Path), zap.Error(err))
return
}
}
for _, childDir := range dir.Children {
if r.checkForCancel(a) {
return
}
r.iterateDir(a, childDir)
}
case fileref.FILE:
r.repairFile(a, dir)
default:
Logger.Info("Invalid directory type", zap.Any("type", dir.Type))
}
return
}
func (r *RepairRequest) repairFile(a *Allocation, file *ListResult) {
if r.checkForCancel(a) {
return
}
Logger.Info("Checking file for the path :", zap.Any("path", file.Path))
found, repairRequired, _, err := a.RepairRequired(file.Path)
if err != nil {
Logger.Error("repair_required_failed", zap.Error(err))
return
}
if repairRequired {
Logger.Info("Repair required for the path :", zap.Any("path", file.Path))
if bits.OnesCount32(found) >= a.DataShards {
Logger.Info("Repair by upload", zap.Any("path", file.Path))
var wg sync.WaitGroup
statusCB := &RepairStatusCB{
wg: &wg,
statusCB: r.statusCB,
}
localPath := r.getLocalPath(file)
if !checkFileExists(localPath) {
if r.checkForCancel(a) {
return
}
Logger.Info("Downloading file for the path :", zap.Any("path", file.Path))
wg.Add(1)
err = a.DownloadFile(localPath, file.Path, statusCB)
if err != nil {
Logger.Error("download_file_failed", zap.Error(err))
return
}
wg.Wait()
if !statusCB.success {
Logger.Error("Failed to download file for repair, Status call back success failed",
zap.Any("localpath", localPath), zap.Any("remotepath", file.Path))
return
}
Logger.Info("Download file success for repair", zap.Any("localpath", localPath), zap.Any("remotepath", file.Path))
statusCB.success = false
}
if r.checkForCancel(a) {
return
}
Logger.Info("Repairing file for the path :", zap.Any("path", file.Path))
wg.Add(1)
err = a.RepairFile(localPath, file.Path, statusCB)
if err != nil {
Logger.Error("repair_file_failed", zap.Error(err))
return
}
wg.Wait()
if !statusCB.success {
Logger.Error("Failed to repair file, Status call back success failed",
zap.Any("localpath", localPath), zap.Any("remotepath", file.Path))
return
}
} else {
Logger.Info("Repair by delete", zap.Any("path", file.Path))
consensus := float32(bits.OnesCount32(found))
err := a.deleteFile(file.Path, consensus, consensus)
if err != nil {
Logger.Error("repair_file_failed", zap.Error(err))
return
}
}
Logger.Info("Repair file success", zap.Any("remotepath", file.Path))
r.filesRepaired++
}
return
}
func (r *RepairRequest) getLocalPath(file *ListResult) string {
return r.localRootPath + file.Path
}
func checkFileExists(localPath string) bool {
info, err := os.Stat(localPath)
if err != nil {
return false
}
return !info.IsDir()
}
func (r *RepairRequest) checkForCancel(a *Allocation) bool {
if r.isRepairCanceled {
Logger.Info("Repair Cancelled by the user")
if r.statusCB != nil {
r.statusCB.RepairCompleted(r.filesRepaired)
}
return true
}
return false
}