/
multi_operation_worker.go
359 lines (325 loc) · 10.2 KB
/
multi_operation_worker.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
package sdk
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"sync"
"time"
"github.com/0chain/errors"
"github.com/remeh/sizedwaitgroup"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/zboxcore/allocationchange"
"github.com/0chain/gosdk/zboxcore/client"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/logger"
l "github.com/0chain/gosdk/zboxcore/logger"
"github.com/0chain/gosdk/zboxcore/zboxutil"
"github.com/google/uuid"
)
const (
DefaultCreateConnectionTimeOut = 10 * time.Second
)
var BatchSize = 6
type MultiOperationOption func(mo *MultiOperation)
func WithRepair() MultiOperationOption {
return func(mo *MultiOperation) {
mo.Consensus.consensusThresh = 0
mo.isRepair = true
}
}
type Operationer interface {
Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error)
buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange
Verify(allocObj *Allocation) error
Completed(allocObj *Allocation)
Error(allocObj *Allocation, consensus int, err error)
}
type MultiOperation struct {
connectionID string
operations []Operationer
allocationObj *Allocation
ctx context.Context
ctxCncl context.CancelCauseFunc
operationMask zboxutil.Uint128
maskMU *sync.Mutex
Consensus
changes [][]allocationchange.AllocationChange
isRepair bool
}
func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) {
defer func() {
if err == nil {
mo.maskMU.Lock()
mo.operationMask = mo.operationMask.Or(zboxutil.NewUint128(1).Lsh(uint64(blobberIdx)))
mo.maskMU.Unlock()
}
}()
var (
resp *http.Response
shouldContinue bool
latestRespMsg string
latestStatusCode int
)
blobber := mo.allocationObj.Blobbers[blobberIdx]
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", mo.connectionID)
if err != nil {
return err, false
}
formWriter.Close()
var httpreq *http.Request
httpreq, err = zboxutil.NewConnectionRequest(blobber.Baseurl, mo.allocationObj.ID, mo.allocationObj.Tx, body)
if err != nil {
l.Logger.Error(blobber.Baseurl, "Error creating new connection request", err)
return
}
httpreq.Header.Add("Content-Type", formWriter.FormDataContentType())
ctx, cncl := context.WithTimeout(mo.ctx, DefaultCreateConnectionTimeOut)
defer cncl()
err = zboxutil.HttpDo(ctx, cncl, httpreq, func(r *http.Response, err error) error {
resp = r
return err
})
if err != nil {
logger.Logger.Error("Create Connection: ", 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 {
l.Logger.Info(blobber.Baseurl, " connection obj created.")
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 (mo *MultiOperation) Process() error {
l.Logger.Info("MultiOperation Process start")
wg := &sync.WaitGroup{}
mo.changes = make([][]allocationchange.AllocationChange, len(mo.operations))
ctx := mo.ctx
ctxCncl := mo.ctxCncl
defer ctxCncl(nil)
swg := sizedwaitgroup.New(BatchSize)
errsSlice := make([]error, len(mo.operations))
mo.operationMask = zboxutil.NewUint128(0)
for idx, op := range mo.operations {
uid := util.GetNewUUID()
swg.Add()
go func(op Operationer, idx int) {
defer swg.Done()
// Check for other goroutines signal
select {
case <-ctx.Done():
return
default:
}
refs, mask, err := op.Process(mo.allocationObj, mo.connectionID) // Process with each blobber
if err != nil {
l.Logger.Error(err)
errsSlice[idx] = errors.New("", err.Error())
ctxCncl(err)
return
}
mo.maskMU.Lock()
mo.operationMask = mo.operationMask.Or(mask)
mo.maskMU.Unlock()
changes := op.buildChange(refs, uid)
mo.changes[idx] = changes
}(op, idx)
}
swg.Wait()
// Check consensus
if mo.operationMask.CountOnes() < mo.consensusThresh || ctx.Err() != nil {
majorErr := zboxutil.MajorError(errsSlice)
if majorErr != nil {
return errors.New("consensus_not_met",
fmt.Sprintf("Multioperation failed. Required consensus %d got %d. Major error: %s",
mo.consensusThresh, mo.operationMask.CountOnes(), majorErr.Error()))
}
return errors.New("consensus_not_met",
fmt.Sprintf("Multioperation failed. Required consensus %d got %d",
mo.consensusThresh, mo.operationMask.CountOnes()))
}
// Take transpose of mo.change because it will be easier to iterate mo if it contains blobber changes
// in row instead of column. Currently mo.change[0] contains allocationChange for operation 1 and so on.
// But we want mo.changes[0] to have allocationChange for blobber 1 and mo.changes[1] to have allocationChange for
// blobber 2 and so on.
start := time.Now()
mo.changes = zboxutil.Transpose(mo.changes)
writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), mo.allocationObj)
if err != nil {
return fmt.Errorf("Operation failed: %s", err.Error())
}
l.Logger.Info("Trying to lock write marker.....")
if singleClientMode {
mo.allocationObj.commitMutex.Lock()
} else {
err = writeMarkerMutex.Lock(mo.ctx, &mo.operationMask, mo.maskMU,
mo.allocationObj.Blobbers, &mo.Consensus, 0, time.Minute, mo.connectionID)
if err != nil {
return fmt.Errorf("Operation failed: %s", err.Error())
}
}
logger.Logger.Info("[writemarkerLocked]", time.Since(start).Milliseconds())
start = time.Now()
status := Commit
if !mo.isRepair && !mo.allocationObj.checkStatus {
status, err = mo.allocationObj.CheckAllocStatus()
if err != nil {
logger.Logger.Error("Error checking allocation status", err)
if singleClientMode {
mo.allocationObj.commitMutex.Unlock()
} else {
writeMarkerMutex.Unlock(mo.ctx, mo.operationMask, mo.allocationObj.Blobbers, time.Minute, mo.connectionID) //nolint: errcheck
}
return fmt.Errorf("Check allocation status failed: %s", err.Error())
}
if status == Repair {
logger.Logger.Info("Repairing allocation")
if singleClientMode {
mo.allocationObj.commitMutex.Unlock()
} else {
writeMarkerMutex.Unlock(mo.ctx, mo.operationMask, mo.allocationObj.Blobbers, time.Minute, mo.connectionID) //nolint: errcheck
}
statusBar := NewRepairBar(mo.allocationObj.ID)
if statusBar == nil {
for _, op := range mo.operations {
op.Error(mo.allocationObj, 0, ErrRetryOperation)
}
return ErrRetryOperation
}
statusBar.wg.Add(1)
err = mo.allocationObj.RepairAlloc(statusBar)
if err != nil {
return err
}
statusBar.wg.Wait()
if statusBar.success {
l.Logger.Info("Repair success")
} else {
l.Logger.Error("Repair failed")
}
for _, op := range mo.operations {
op.Error(mo.allocationObj, 0, ErrRetryOperation)
}
return ErrRetryOperation
}
}
if singleClientMode {
mo.allocationObj.checkStatus = true
defer mo.allocationObj.commitMutex.Unlock()
} else {
defer writeMarkerMutex.Unlock(mo.ctx, mo.operationMask, mo.allocationObj.Blobbers, time.Minute, mo.connectionID) //nolint: errcheck
}
if status != Commit {
for _, op := range mo.operations {
op.Error(mo.allocationObj, 0, ErrRetryOperation)
}
return ErrRetryOperation
}
logger.Logger.Info("[checkAllocStatus]", time.Since(start).Milliseconds())
mo.Consensus.Reset()
activeBlobbers := mo.operationMask.CountOnes()
commitReqs := make([]*CommitRequest, activeBlobbers)
start = time.Now()
wg.Add(activeBlobbers)
var pos uint64
var counter = 0
timestamp := int64(common.Now())
for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) {
pos = uint64(i.TrailingZeros())
commitReq := &CommitRequest{
allocationID: mo.allocationObj.ID,
allocationTx: mo.allocationObj.Tx,
blobber: mo.allocationObj.Blobbers[pos],
connectionID: mo.connectionID,
wg: wg,
timestamp: timestamp,
blobberInd: pos,
}
commitReq.changes = append(commitReq.changes, mo.changes[pos]...)
commitReqs[counter] = commitReq
l.Logger.Info("Commit request sending to blobber ", commitReq.blobber.Baseurl)
go AddCommitRequest(commitReq)
counter++
}
wg.Wait()
logger.Logger.Info("[commitRequests]", time.Since(start).Milliseconds())
rollbackMask := zboxutil.NewUint128(0)
errSlice := make([]error, len(commitReqs))
for idx, commitReq := range commitReqs {
if commitReq.result != nil {
if commitReq.result.Success {
l.Logger.Info("Commit success", commitReq.blobber.Baseurl)
if !mo.isRepair {
rollbackMask = rollbackMask.Or(zboxutil.NewUint128(1).Lsh(commitReq.blobberInd))
}
mo.consensus++
} else {
errSlice[idx] = errors.New("commit_failed", commitReq.result.ErrorMessage)
l.Logger.Info("Commit failed", commitReq.blobber.Baseurl, commitReq.result.ErrorMessage)
}
} else {
l.Logger.Info("Commit result not set", commitReq.blobber.Baseurl)
}
}
if !mo.isConsensusOk() {
mo.allocationObj.checkStatus = false
err = zboxutil.MajorError(errSlice)
if mo.getConsensus() != 0 {
l.Logger.Info("Rolling back changes on minority blobbers")
mo.allocationObj.RollbackWithMask(rollbackMask)
}
for _, op := range mo.operations {
op.Error(mo.allocationObj, mo.getConsensus(), err)
}
return err
} else {
for _, op := range mo.operations {
op.Completed(mo.allocationObj)
}
}
return nil
}