-
Notifications
You must be signed in to change notification settings - Fork 29
/
attributesworker.go
189 lines (157 loc) · 4.64 KB
/
attributesworker.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
package sdk
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/bits"
"mime/multipart"
"net/http"
"sync"
"time"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/allocationchange"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/gosdk/zboxcore/zboxutil"
. "github.com/0chain/gosdk/zboxcore/logger"
)
type AttributesRequest struct {
allocationID string //
allocationTx string //
blobbers []*blockchain.StorageNode //
remotefilepath string // path (not hash)
Attributes fileref.Attributes // new attributes
attributes string // new attributes (JSON)
attributesMask uint32 //
connectionID string //
Consensus //
ctx context.Context //
wg *sync.WaitGroup //
}
func (ar *AttributesRequest) getObjectTreeFromBlobber(
blobber *blockchain.StorageNode) (fileref.RefEntity, error) {
return getObjectTreeFromBlobber(ar.ctx, ar.allocationID, ar.allocationTx,
ar.remotefilepath, blobber)
}
func (ar *AttributesRequest) updateBlobberObjectAttributes(
blobber *blockchain.StorageNode, blobberIdx int) (
re fileref.RefEntity, err error) {
re, err = ar.getObjectTreeFromBlobber(ar.blobbers[blobberIdx])
if err != nil {
return
}
var (
body bytes.Buffer
form = multipart.NewWriter(&body)
)
form.WriteField("connection_id", ar.connectionID)
form.WriteField("path", ar.remotefilepath)
form.WriteField("attributes", ar.attributes)
form.Close()
var httpreq *http.Request
httpreq, err = zboxutil.NewAttributesRequest(blobber.Baseurl,
ar.allocationTx, &body)
if err != nil {
Logger.Error(blobber.Baseurl,
"Error creating update attributes request", err)
return
}
httpreq.Header.Add("Content-Type", form.FormDataContentType())
var ctx, cncl = context.WithTimeout(ar.ctx, (time.Second * 30))
defer cncl()
err = zboxutil.HttpDo(ctx, cncl, httpreq,
func(resp *http.Response, err error) error {
if err != nil {
Logger.Error("Request error: ", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
ar.consensus++
ar.attributesMask |= (1 << uint32(blobberIdx))
Logger.Info(blobber.Baseurl, " "+ar.remotefilepath,
" attributes updated.")
return nil
}
var respBody []byte
if respBody, err = ioutil.ReadAll(resp.Body); err != nil {
Logger.Error(blobber.Baseurl, "Reading response: ", err)
return nil
}
Logger.Error(blobber.Baseurl, "Response error: ",
string(respBody))
return nil
})
return
}
func (ar *AttributesRequest) ProcessAttributes() (err error) {
var (
numList = len(ar.blobbers)
objectTreeRefs = make([]fileref.RefEntity, numList)
)
ar.wg = &sync.WaitGroup{}
ar.wg.Add(numList)
for i := 0; i < numList; i++ {
go func(bidx int) {
defer ar.wg.Done()
var re, err = ar.updateBlobberObjectAttributes(ar.blobbers[bidx],
bidx)
if err != nil {
Logger.Error(err.Error())
return
}
objectTreeRefs[bidx] = re
}(i)
}
ar.wg.Wait()
if !ar.isConsensusOk() {
return fmt.Errorf("Update attributes failed: " +
"request failed, operation failed")
}
ar.consensus = 0
var wg sync.WaitGroup
wg.Add(bits.OnesCount32(ar.attributesMask))
var (
commitReqs = make([]*CommitRequest, bits.OnesCount32(ar.attributesMask))
c, pos = 0, 0
)
for i := ar.attributesMask; i != 0; i &= ^(1 << uint32(pos)) {
pos = bits.TrailingZeros32(i)
var commitReq CommitRequest
commitReq.allocationID = ar.allocationID
commitReq.allocationTx = ar.allocationTx
commitReq.blobber = ar.blobbers[pos]
var change = new(allocationchange.AttributesChange)
change.AllocationID = ar.allocationID
change.ConnectionID = ar.connectionID
change.Path = ar.remotefilepath
change.Attributes = ar.Attributes
change.NumBlocks = 0
change.Size = 0
change.Operation = allocationchange.UPDATE_ATTRS_OPERATION
commitReq.changes = append(commitReq.changes, change)
commitReq.connectionID = ar.connectionID
commitReq.wg = &wg
commitReqs[c] = &commitReq
go AddCommitRequest(&commitReq)
c++
}
wg.Wait()
for _, commitReq := range commitReqs {
if commitReq.result != nil {
if commitReq.result.Success {
Logger.Info("Commit success", commitReq.blobber.Baseurl)
ar.consensus++
} else {
Logger.Info("Commit failed", commitReq.blobber.Baseurl,
commitReq.result.ErrorMessage)
}
} else {
Logger.Info("Commit result not set", commitReq.blobber.Baseurl)
}
}
if !ar.isConsensusOk() {
return fmt.Errorf("Delete failed: Commit consensus failed")
}
return nil
}