-
Notifications
You must be signed in to change notification settings - Fork 38
/
receive_task.go
220 lines (204 loc) · 10.7 KB
/
receive_task.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
package receiver
import (
"bytes"
"context"
"net/http"
"time"
"github.com/bnb-chain/greenfield-common/go/hash"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfsperrors"
"github.com/bnb-chain/greenfield-storage-provider/core/module"
corespdb "github.com/bnb-chain/greenfield-storage-provider/core/spdb"
"github.com/bnb-chain/greenfield-storage-provider/core/task"
"github.com/bnb-chain/greenfield-storage-provider/core/taskqueue"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)
var (
ErrDanglingTask = gfsperrors.Register(module.ReceiveModularName, http.StatusBadRequest, 80001, "OoooH... request lost, try again later")
ErrRepeatedTask = gfsperrors.Register(module.ReceiveModularName, http.StatusNotAcceptable, 80002, "request repeated")
ErrUnfinishedTask = gfsperrors.Register(module.ReceiveModularName, http.StatusForbidden, 80003, "replicate piece unfinished")
ErrInvalidDataChecksum = gfsperrors.Register(module.ReceiveModularName, http.StatusNotAcceptable, 80004, "verify data checksum failed")
)
func ErrPieceStoreWithDetail(detail string) *gfsperrors.GfSpError {
return gfsperrors.Register(module.ReceiveModularName, http.StatusInternalServerError, 85101, detail)
}
func ErrGfSpDBWithDetail(detail string) *gfsperrors.GfSpError {
return gfsperrors.Register(module.ReceiveModularName, http.StatusInternalServerError, 85201, detail)
}
func (r *ReceiveModular) HandleReceivePieceTask(ctx context.Context, task task.ReceivePieceTask, data []byte) error {
var (
err error
)
defer func() {
task.SetError(err)
log.CtxDebugw(ctx, task.Info())
}()
checkHasTime := time.Now()
if r.receiveQueue.Has(task.Key()) {
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_check_has_time").Observe(time.Since(checkHasTime).Seconds())
log.CtxErrorw(ctx, "has repeat receive task", "task", task)
err = ErrRepeatedTask
return ErrRepeatedTask
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_check_has_time").Observe(time.Since(checkHasTime).Seconds())
pushTime := time.Now()
err = r.receiveQueue.Push(task)
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_push_time").Observe(time.Since(pushTime).Seconds())
if err != nil {
log.CtxErrorw(ctx, "failed to push receive task to queue", "task", task, "error", err)
return err
}
defer r.receiveQueue.PopByKey(task.Key())
checksum := hash.GenerateChecksum(data)
if !bytes.Equal(checksum, task.GetPieceChecksum()) {
log.CtxErrorw(ctx, "failed to compare checksum", "task", task, "actual_checksum", checksum, "expected_checksum", task.GetPieceChecksum())
err = ErrInvalidDataChecksum
return ErrInvalidDataChecksum
}
var pieceKey string
if task.GetObjectInfo().GetRedundancyType() == storagetypes.REDUNDANCY_EC_TYPE {
pieceKey = r.baseApp.PieceOp().ECPieceKey(task.GetObjectInfo().Id.Uint64(), task.GetSegmentIdx(), uint32(task.GetRedundancyIdx()), task.GetObjectInfo().GetVersion())
} else {
pieceKey = r.baseApp.PieceOp().SegmentPieceKey(task.GetObjectInfo().Id.Uint64(), task.GetSegmentIdx(), task.GetObjectInfo().GetVersion())
}
setDBTime := time.Now()
if err = r.baseApp.GfSpDB().SetReplicatePieceChecksum(task.GetObjectInfo().Id.Uint64(), task.GetSegmentIdx(), task.GetRedundancyIdx(), task.GetPieceChecksum(), task.GetObjectInfo().GetVersion()); err != nil {
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_set_mysql_time").Observe(time.Since(setDBTime).Seconds())
log.CtxErrorw(ctx, "failed to set checksum to db", "task", task, "error", err)
return ErrGfSpDBWithDetail("failed to set checksum to db, error: " + err.Error())
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_set_mysql_time").Observe(time.Since(setDBTime).Seconds())
setPieceTime := time.Now()
if err = r.baseApp.PieceStore().PutPiece(ctx, pieceKey, data); err != nil {
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_set_piece_time").Observe(time.Since(setPieceTime).Seconds())
log.CtxErrorw(ctx, "failed to put piece into piece store", "task", task, "error", err)
return ErrPieceStoreWithDetail("failed to put piece into piece store, error: " + err.Error())
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_set_piece_time").Observe(time.Since(setPieceTime).Seconds())
log.CtxDebugw(ctx, "succeed to receive piece data")
return nil
}
func (r *ReceiveModular) HandleDoneReceivePieceTask(ctx context.Context, task task.ReceivePieceTask) ([]byte, error) {
var err error
defer func() {
task.SetError(err)
log.CtxDebugw(ctx, task.Info())
}()
pushTime := time.Now()
if err = r.receiveQueue.Push(task); err != nil {
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_push_time").Observe(time.Since(pushTime).Seconds())
log.CtxErrorw(ctx, "failed to push receive task", "task", task, "error", err)
return nil, err
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_push_time").Observe(time.Since(pushTime).Seconds())
defer r.receiveQueue.PopByKey(task.Key())
segmentCount := r.baseApp.PieceOp().SegmentPieceCount(task.GetObjectInfo().GetPayloadSize(),
task.GetStorageParams().VersionedParams.GetMaxSegmentSize())
getChecksumsTime := time.Now()
pieceChecksums, err := r.baseApp.GfSpDB().GetAllReplicatePieceChecksumOptimized(task.GetObjectInfo().Id.Uint64(),
task.GetRedundancyIdx(), segmentCount)
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_get_checksums_time").Observe(time.Since(getChecksumsTime).Seconds())
if err != nil {
log.CtxErrorw(ctx, "failed to get checksum from db", "task", task, "error", err)
return nil, ErrGfSpDBWithDetail("failed to get checksum from db, error: " + err.Error())
}
if len(pieceChecksums) != int(segmentCount) {
log.CtxErrorw(ctx, "replicate piece unfinished", "task", task)
err = ErrUnfinishedTask
return nil, ErrUnfinishedTask
}
expectedIntegrityHash := task.GetObjectInfo().GetChecksums()[task.GetRedundancyIdx()+1]
integrityChecksum := hash.GenerateIntegrityHash(pieceChecksums)
if !bytes.Equal(expectedIntegrityHash, integrityChecksum) {
log.CtxErrorw(ctx, "failed to compare checksum", "task", task, "actual_checksum", integrityChecksum, "expected_checksum", expectedIntegrityHash)
err = ErrInvalidDataChecksum
return nil, ErrInvalidDataChecksum
}
signTime := time.Now()
signature, err := r.baseApp.GfSpClient().SignSecondarySealBls(ctx, task.GetObjectInfo().Id.Uint64(),
task.GetGlobalVirtualGroupId(), task.GetObjectInfo().GetChecksums())
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_sign_time").Observe(time.Since(signTime).Seconds())
if err != nil {
log.CtxErrorw(ctx, "failed to sign the integrity hash", "task", task, "error", err)
return nil, err
}
setIntegrityTime := time.Now()
if task.GetObjectInfo().GetIsUpdating() {
integrityMeta := &corespdb.ShadowIntegrityMeta{
ObjectID: task.GetObjectInfo().Id.Uint64(),
RedundancyIndex: task.GetRedundancyIdx(),
IntegrityChecksum: integrityChecksum,
PieceChecksumList: pieceChecksums,
Version: task.GetObjectInfo().GetVersion(),
ObjectSize: task.GetObjectInfo().GetPayloadSize(),
}
err = r.baseApp.GfSpDB().SetShadowObjectIntegrity(integrityMeta)
} else {
integrityMeta := &corespdb.IntegrityMeta{
ObjectID: task.GetObjectInfo().Id.Uint64(),
RedundancyIndex: task.GetRedundancyIdx(),
IntegrityChecksum: integrityChecksum,
PieceChecksumList: pieceChecksums,
ObjectSize: task.GetObjectInfo().GetPayloadSize(),
}
err = r.baseApp.GfSpDB().SetObjectIntegrity(integrityMeta)
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_set_integrity_time").Observe(time.Since(setIntegrityTime).Seconds())
if err != nil {
log.CtxErrorw(ctx, "failed to write integrity meta to db", "task", task, "error", err)
return nil, ErrGfSpDBWithDetail("failed to write integrity meta to db, error: " + err.Error())
}
// only in the case when the secondary sp is the primary SP in a GVG and it is delegated upload, it should not delete the piece hash at this moment.(will be deleted when the sealing is success)
// in all other cases, piece hash should be deleted from DB
var skipDeleteChecksum bool
if task.GetIsAgentUploadTask() {
gvg, queryErr := r.baseApp.Consensus().QueryGlobalVirtualGroup(ctx, task.GetGlobalVirtualGroupId())
if queryErr != nil {
log.CtxErrorw(ctx, "failed to QueryGlobalVirtualGroup", "error", queryErr)
return nil, ErrGfSpDBWithDetail("failed to QueryGlobalVirtualGroup, error: " + queryErr.Error())
}
spID, idErr := r.getSPID()
if idErr != nil {
log.CtxErrorw(ctx, "failed to getSPID", "error", idErr)
return nil, ErrGfSpDBWithDetail("failed to getSPID, error: " + idErr.Error())
}
if spID == gvg.PrimarySpId {
skipDeleteChecksum = true
}
}
deletePieceHashTime := time.Now()
if !skipDeleteChecksum {
if err = r.baseApp.GfSpDB().DeleteAllReplicatePieceChecksumOptimized(
task.GetObjectInfo().Id.Uint64(), task.GetRedundancyIdx()); err != nil {
log.CtxErrorw(ctx, "failed to delete all replicate piece checksum", "task", task, "error", err)
// ignore the error,let the request go, the background task will gc the meta again later
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_delete_piece_hash_time").
Observe(time.Since(deletePieceHashTime).Seconds())
}
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_delete_piece_hash_time").
Observe(time.Since(deletePieceHashTime).Seconds())
// the manager dispatch the task to confirm whether seal on chain as secondary sp.
task.SetError(nil)
if task.GetBucketMigration() {
return signature, nil
}
go func() {
reportTime := time.Now()
if reportErr := r.baseApp.GfSpClient().ReportTask(context.Background(), task); reportErr != nil {
log.CtxErrorw(ctx, "failed to report receive task for confirming seal", "error", reportErr)
// ignore the error,let the request go, the background task will gc the unsealed data later
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_report_time").
Observe(time.Since(reportTime).Seconds())
}
metrics.PerfReceivePieceTimeHistogram.WithLabelValues("receive_piece_server_done_report_time").
Observe(time.Since(reportTime).Seconds())
}()
log.CtxDebugw(ctx, "succeed to done receive piece")
return signature, nil
}
func (r *ReceiveModular) QueryTasks(ctx context.Context, subKey task.TKey) ([]task.Task, error) {
receiveTasks, _ := taskqueue.ScanTQueueBySubKey(r.receiveQueue, subKey)
return receiveTasks, nil
}