-
Notifications
You must be signed in to change notification settings - Fork 29
/
commitworker.go
298 lines (278 loc) · 8.65 KB
/
commitworker.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
package sdk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"strconv"
"sync"
"time"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/zboxcore/allocationchange"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/gosdk/zboxcore/client"
"github.com/0chain/gosdk/zboxcore/fileref"
. "github.com/0chain/gosdk/zboxcore/logger"
"github.com/0chain/gosdk/zboxcore/marker"
"github.com/0chain/gosdk/zboxcore/zboxutil"
)
type ReferencePathResult struct {
*fileref.ReferencePath
LatestWM *marker.WriteMarker `json:"latest_write_marker"`
}
type CommitResult struct {
Success bool `json:"success"`
ErrorMessage string `json:"error_msg,omitempty"`
}
func ErrorCommitResult(errMsg string) *CommitResult {
result := &CommitResult{Success: false, ErrorMessage: errMsg}
return result
}
func SuccessCommitResult() *CommitResult {
result := &CommitResult{Success: true}
return result
}
type CommitRequest struct {
changes []allocationchange.AllocationChange
blobber *blockchain.StorageNode
allocationID string
allocationTx string
connectionID string
wg *sync.WaitGroup
result *CommitResult
}
var commitChan map[string]chan *CommitRequest
var initCommitMutex sync.Mutex
func InitCommitWorker(blobbers []*blockchain.StorageNode) {
// if commitChan != nil {
// for _, v := range commitChan {
// close(v)
// }
// }
// commitChan = make(map[string]chan *CommitRequest)
// for _, blobber := range blobbers {
// Logger.Info("Atempting to start the commit worker for ", blobber.Baseurl)
// commitChan[blobber.ID] = make(chan *CommitRequest, 1)
// go startCommitWorker(blobber)
// }
initCommitMutex.Lock()
defer initCommitMutex.Unlock()
if commitChan == nil {
commitChan = make(map[string]chan *CommitRequest)
}
for _, blobber := range blobbers {
if _, ok := commitChan[blobber.ID]; !ok {
commitChan[blobber.ID] = make(chan *CommitRequest, 1)
blobberChan := commitChan[blobber.ID]
go startCommitWorker(blobberChan, blobber.ID)
}
}
}
func startCommitWorker(blobberChan chan *CommitRequest, blobberID string) {
for true {
commitreq, open := <-blobberChan
if !open {
break
}
commitreq.processCommit()
}
initCommitMutex.Lock()
defer initCommitMutex.Unlock()
delete(commitChan, blobberID)
}
func (commitreq *CommitRequest) processCommit() {
Logger.Info("received a commit request")
paths := make([]string, 0)
for _, change := range commitreq.changes {
paths = append(paths, change.GetAffectedPath())
}
var req *http.Request
var lR ReferencePathResult
req, err := zboxutil.NewReferencePathRequest(commitreq.blobber.Baseurl, commitreq.allocationTx, paths)
if err != nil || len(paths) == 0 {
Logger.Error("Creating ref path req", err)
return
}
ctx, cncl := context.WithTimeout(context.Background(), (time.Second * 30))
err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error {
if err != nil {
Logger.Error("Ref path error:", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
Logger.Error("Ref path response : ", resp.StatusCode)
}
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Logger.Error("Ref path: Resp", err)
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Reference path error response: Status: %d - %s ", resp.StatusCode, string(resp_body))
} else {
//Logger.Info("Reference path:", string(resp_body))
err = json.Unmarshal(resp_body, &lR)
if err != nil {
Logger.Error("Reference path json decode error: ", err)
return err
}
}
return nil
})
//process the commit request for the blobber here
if err != nil {
commitreq.result = ErrorCommitResult(err.Error())
commitreq.wg.Done()
return
}
rootRef, err := lR.GetDirTree(commitreq.allocationID)
if lR.LatestWM != nil {
//TODO: Verify the writemarker
err = lR.LatestWM.VerifySignature(client.GetClientPublicKey())
if err != nil {
commitreq.result = ErrorCommitResult(err.Error())
commitreq.wg.Done()
return
}
rootRef.CalculateHash()
prevAllocationRoot := encryption.Hash(rootRef.Hash + ":" + strconv.FormatInt(lR.LatestWM.Timestamp, 10))
if prevAllocationRoot != lR.LatestWM.AllocationRoot {
// Removing this check for testing purpose as per the convo with Saswata
Logger.Info("Allocation root from latest writemarker mismatch. Expected: " + prevAllocationRoot + " got: " + lR.LatestWM.AllocationRoot)
// err = commitreq.calculateHashRequest(ctx, paths)
// if err != nil {
// commitreq.result = ErrorCommitResult("Failed to call blobber to recalculate the hash. URL: " + commitreq.blobber.Baseurl + ", Err : " + err.Error())
// commitreq.wg.Done()
// return
// }
// Logger.Info("Recalculate hash call to blobber successfull")
// commitreq.result = ErrorCommitResult("Allocation root from latest writemarker mismatch. Expected: " + prevAllocationRoot + " got: " + lR.LatestWM.AllocationRoot)
// commitreq.wg.Done()
// return
}
}
if err != nil {
commitreq.result = ErrorCommitResult(err.Error())
commitreq.wg.Done()
return
}
size := int64(0)
for _, change := range commitreq.changes {
err = change.ProcessChange(rootRef)
if err != nil {
break
}
size += change.GetSize()
}
if err != nil {
commitreq.result = ErrorCommitResult(err.Error())
commitreq.wg.Done()
return
}
err = commitreq.commitBlobber(rootRef, lR.LatestWM, size)
if err != nil {
commitreq.result = ErrorCommitResult(err.Error())
commitreq.wg.Done()
return
}
commitreq.result = SuccessCommitResult()
commitreq.wg.Done()
}
func (req *CommitRequest) commitBlobber(rootRef *fileref.Ref, latestWM *marker.WriteMarker, size int64) error {
wm := &marker.WriteMarker{}
timestamp := int64(common.Now())
wm.AllocationRoot = encryption.Hash(rootRef.Hash + ":" + strconv.FormatInt(timestamp, 10))
if latestWM != nil {
wm.PreviousAllocationRoot = latestWM.AllocationRoot
} else {
wm.PreviousAllocationRoot = ""
}
wm.AllocationID = req.allocationID
wm.Size = size
wm.BlobberID = req.blobber.ID
wm.Timestamp = timestamp
wm.ClientID = client.GetClientID()
err := wm.Sign()
if err != nil {
Logger.Error("Signing writemarker failed: ", err)
return err
}
body := new(bytes.Buffer)
formWriter := multipart.NewWriter(body)
wmData, err := json.Marshal(wm)
if err != nil {
Logger.Error("Creating writemarker failed: ", err)
return err
}
formWriter.WriteField("connection_id", req.connectionID)
formWriter.WriteField("write_marker", string(wmData))
formWriter.Close()
httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationTx, body)
if err != nil {
Logger.Error("Error creating commit req: ", err)
return err
}
httpreq.Header.Add("Content-Type", formWriter.FormDataContentType())
ctx, cncl := context.WithTimeout(context.Background(), (time.Second * 60))
Logger.Info("Committing to blobber." + req.blobber.Baseurl)
err = zboxutil.HttpDo(ctx, cncl, httpreq, func(resp *http.Response, err error) error {
if err != nil {
Logger.Error("Commit: ", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
Logger.Info(req.blobber.Baseurl, req.connectionID, " committed")
} else {
Logger.Error("Commit response: ", resp.StatusCode)
}
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Logger.Error("Response read: ", err)
return err
}
if resp.StatusCode != http.StatusOK {
Logger.Error(req.blobber.Baseurl, " Commit response:", string(resp_body))
return common.NewError("commit_error", string(resp_body))
}
return nil
})
return err
}
func AddCommitRequest(req *CommitRequest) {
commitChan[req.blobber.ID] <- req
}
func (commitreq *CommitRequest) calculateHashRequest(ctx context.Context, paths []string) error {
var req *http.Request
req, err := zboxutil.NewCalculateHashRequest(commitreq.blobber.Baseurl, commitreq.allocationTx, paths)
if err != nil || len(paths) == 0 {
Logger.Error("Creating calculate hash req", err)
return err
}
ctx, cncl := context.WithTimeout(context.Background(), (time.Second * 30))
err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error {
if err != nil {
Logger.Error("Calculate hash error:", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
Logger.Error("Calculate hash response : ", resp.StatusCode)
}
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Logger.Error("Calculate hash: Resp", err)
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Calculate hash error response: Status: %d - %s ", resp.StatusCode, string(resp_body))
}
return nil
})
return err
}