/
renameworker.go
408 lines (344 loc) · 10.9 KB
/
renameworker.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package sdk
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"sync"
"time"
"github.com/0chain/errors"
"github.com/google/uuid"
"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/client"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/logger"
"github.com/0chain/gosdk/zboxcore/allocationchange"
"github.com/0chain/gosdk/zboxcore/blockchain"
l "github.com/0chain/gosdk/zboxcore/logger"
"github.com/0chain/gosdk/zboxcore/zboxutil"
)
type RenameRequest struct {
allocationObj *Allocation
allocationID string
allocationTx string
blobbers []*blockchain.StorageNode
remotefilepath string
newName string
ctx context.Context
ctxCncl context.CancelFunc
wg *sync.WaitGroup
renameMask zboxutil.Uint128
maskMU *sync.Mutex
connectionID string
consensus Consensus
timestamp int64
}
func (req *RenameRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) {
return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.remotefilepath, blobber)
}
func (req *RenameRequest) renameBlobberObject(
blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) {
defer func() {
if err != nil {
req.maskMU.Lock()
req.renameMask = req.renameMask.And(zboxutil.NewUint128(1).Lsh(uint64(blobberIdx)).Not())
req.maskMU.Unlock()
}
}()
refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx])
if err != nil {
return nil, err
}
var (
resp *http.Response
shouldContinue bool
latestRespMsg string
latestStatusCode int
)
for i := 0; i < 3; i++ {
err, shouldContinue = func() (err error, shouldContinue bool) {
body := new(bytes.Buffer)
formWriter := multipart.NewWriter(body)
err = formWriter.WriteField("connection_id", req.connectionID)
if err != nil {
return err, false
}
err = formWriter.WriteField("path", req.remotefilepath)
if err != nil {
return err, false
}
err = formWriter.WriteField("new_name", req.newName)
if err != nil {
return err, false
}
formWriter.Close()
var httpreq *http.Request
httpreq, err = zboxutil.NewRenameRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body)
if err != nil {
l.Logger.Error(blobber.Baseurl, "Error creating rename request", err)
return
}
httpreq.Header.Add("Content-Type", formWriter.FormDataContentType())
ctx, cncl := context.WithTimeout(req.ctx, DefaultUploadTimeOut)
resp, err = zboxutil.Client.Do(httpreq.WithContext(ctx))
defer cncl()
if err != nil {
logger.Logger.Error("Rename: ", err)
return
}
if resp.Body != nil {
defer resp.Body.Close()
}
var respBody []byte
respBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
logger.Logger.Error("Error: Resp ", err)
return
}
latestRespMsg = string(respBody)
latestStatusCode = resp.StatusCode
if resp.StatusCode == http.StatusOK {
req.consensus.Done()
l.Logger.Info(blobber.Baseurl, " "+req.remotefilepath, " renamed.")
return
}
if resp.StatusCode == http.StatusTooManyRequests {
logger.Logger.Error("Got too many request error")
var r int
r, err = zboxutil.GetRateLimitValue(resp)
if err != nil {
logger.Logger.Error(err)
return
}
time.Sleep(time.Duration(r) * time.Second)
shouldContinue = true
return
}
l.Logger.Error(blobber.Baseurl, "Response: ", string(respBody))
err = errors.New("response_error", string(respBody))
return
}()
if err != nil {
return
}
if shouldContinue {
continue
}
return
}
err = errors.New("unknown_issue",
fmt.Sprintf("last status code: %d, last response message: %s", latestStatusCode, latestRespMsg))
return
}
func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) {
var pos uint64
numList := len(req.blobbers)
objectTreeRefs := make([]fileref.RefEntity, numList)
blobberErrors := make([]error, numList)
req.wg = &sync.WaitGroup{}
for i := req.renameMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) {
pos = uint64(i.TrailingZeros())
req.wg.Add(1)
go func(blobberIdx int) {
defer req.wg.Done()
refEntity, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx)
if err != nil {
blobberErrors[blobberIdx] = err
l.Logger.Error(err.Error())
return
}
objectTreeRefs[blobberIdx] = refEntity
}(int(pos))
}
req.wg.Wait()
return objectTreeRefs, blobberErrors
}
func (req *RenameRequest) ProcessRename() error {
defer req.ctxCncl()
objectTreeRefs, blobberErrors := req.ProcessWithBlobbers()
if !req.consensus.isConsensusOk() {
err := zboxutil.MajorError(blobberErrors)
if err != nil {
return errors.New("rename_failed",
fmt.Sprintf("Rename failed. %s", err.Error()))
}
return errors.New("consensus_not_met",
fmt.Sprintf("Rename failed. Required consensus %d got %d",
req.consensus.consensusThresh, req.consensus.getConsensus()))
}
writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), req.allocationObj)
if err != nil {
return fmt.Errorf("rename failed: %s", err.Error())
}
err = writeMarkerMutex.Lock(req.ctx, &req.renameMask,
req.maskMU, req.blobbers, &req.consensus, 0, time.Minute, req.connectionID)
if err != nil {
return fmt.Errorf("rename failed: %s", err.Error())
}
defer writeMarkerMutex.Unlock(req.ctx, req.renameMask, req.blobbers, time.Minute, req.connectionID) //nolint: errcheck
//Check if the allocation is to be repaired or rolled back
status, err := req.allocationObj.CheckAllocStatus()
if err != nil {
logger.Logger.Error("Error checking allocation status: ", err)
return fmt.Errorf("rename failed: %s", err.Error())
}
if status == Repair {
logger.Logger.Info("Repairing allocation")
//TODO: Need status callback to call repair allocation
// err = req.allocationObj.RepairAlloc()
// if err != nil {
// return err
// }
}
if status != Commit {
return ErrRetryOperation
}
req.consensus.Reset()
req.timestamp = int64(common.Now())
activeBlobbers := req.renameMask.CountOnes()
wg := &sync.WaitGroup{}
wg.Add(activeBlobbers)
commitReqs := make([]*CommitRequest, activeBlobbers)
var pos uint64
var counter int
for i := req.renameMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) {
pos = uint64(i.TrailingZeros())
newChange := &allocationchange.RenameFileChange{
NewName: req.newName,
ObjectTree: objectTreeRefs[pos],
}
newChange.Operation = constants.FileOperationRename
newChange.Size = 0
commitReq := &CommitRequest{
allocationID: req.allocationID,
allocationTx: req.allocationTx,
blobber: req.blobbers[pos],
connectionID: req.connectionID,
wg: wg,
timestamp: req.timestamp,
}
commitReq.changes = append(commitReq.changes, newChange)
commitReqs[counter] = commitReq
go AddCommitRequest(commitReq)
counter++
}
wg.Wait()
var errMessages string
for _, commitReq := range commitReqs {
if commitReq.result != nil {
if commitReq.result.Success {
l.Logger.Info("Commit success", commitReq.blobber.Baseurl)
req.consensus.Done()
} else {
errMessages += commitReq.result.ErrorMessage + "\t"
l.Logger.Info("Commit failed", commitReq.blobber.Baseurl, commitReq.result.ErrorMessage)
}
} else {
l.Logger.Info("Commit result not set", commitReq.blobber.Baseurl)
}
}
if !req.consensus.isConsensusOk() {
return errors.New("consensus_not_met",
fmt.Sprintf("Required consensus %d got %d. Error: %s",
req.consensus.consensusThresh, req.consensus.consensus, errMessages))
}
return nil
}
type RenameOperation struct {
remotefilepath string
ctx context.Context
ctxCncl context.CancelFunc
renameMask zboxutil.Uint128
newName string
maskMU *sync.Mutex
consensus Consensus
}
func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) {
l.Logger.Info("Started Rename Process with Connection Id", connectionID)
// make renameRequest object
rR := &RenameRequest{
allocationObj: allocObj,
allocationID: allocObj.ID,
allocationTx: allocObj.Tx,
connectionID: connectionID,
blobbers: allocObj.Blobbers,
remotefilepath: ro.remotefilepath,
newName: ro.newName,
ctx: ro.ctx,
ctxCncl: ro.ctxCncl,
renameMask: ro.renameMask,
maskMU: ro.maskMU,
wg: &sync.WaitGroup{},
consensus: Consensus{RWMutex: &sync.RWMutex{}},
}
rR.consensus.fullconsensus = ro.consensus.fullconsensus
rR.consensus.consensusThresh = ro.consensus.consensusThresh
objectTreeRefs, blobberErrors := rR.ProcessWithBlobbers()
if !rR.consensus.isConsensusOk() {
err := zboxutil.MajorError(blobberErrors)
if err != nil {
return nil, rR.renameMask, errors.New("rename_failed", fmt.Sprintf("Renamed failed. %s", err.Error()))
}
return nil, rR.renameMask, errors.New("consensus_not_met",
fmt.Sprintf("Rename failed. Required consensus %d, got %d",
rR.consensus.consensusThresh, rR.consensus.consensus))
}
l.Logger.Info("Rename Processs Ended ")
return objectTreeRefs, rR.renameMask, nil
}
func (ro *RenameOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange {
changes := make([]allocationchange.AllocationChange, len(refs))
for idx, ref := range refs {
if ref == nil {
change := &allocationchange.EmptyFileChange{}
changes[idx] = change
continue
}
newChange := &allocationchange.RenameFileChange{
NewName: ro.newName,
ObjectTree: ref,
}
newChange.Operation = constants.FileOperationRename
newChange.Size = 0
changes[idx] = newChange
}
return changes
}
func (ro *RenameOperation) Verify(a *Allocation) error {
if !a.CanRename() {
return constants.ErrFileOptionNotPermitted
}
if ro.remotefilepath == "" {
return errors.New("invalid_path", "Invalid path for the list")
}
if ro.remotefilepath == "/" {
return errors.New("invalid_operation", "cannot rename root path")
}
isabs := zboxutil.IsRemoteAbs(ro.remotefilepath)
if !isabs {
return errors.New("invalid_path", "Path should be valid and absolute")
}
err := ValidateRemoteFileName(ro.newName)
if err != nil {
return err
}
return nil
}
func (ro *RenameOperation) Completed(allocObj *Allocation) {
}
func (ro *RenameOperation) Error(allocObj *Allocation, consensus int, err error) {
}
func NewRenameOperation(remotePath string, destName string, renameMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *RenameOperation {
ro := &RenameOperation{}
ro.remotefilepath = zboxutil.RemoteClean(remotePath)
ro.newName = destName
ro.renameMask = renameMask
ro.maskMU = maskMU
ro.consensus.consensusThresh = consensusTh
ro.consensus.fullconsensus = fullConsensus
ro.ctx, ro.ctxCncl = context.WithCancel(ctx)
return ro
}