-
Notifications
You must be signed in to change notification settings - Fork 40
/
upload_object.go
209 lines (197 loc) · 8.58 KB
/
upload_object.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
package sqldb
import (
"fmt"
"gorm.io/gorm"
corespdb "github.com/bnb-chain/greenfield-storage-provider/core/spdb"
storetypes "github.com/bnb-chain/greenfield-storage-provider/store/types"
"github.com/bnb-chain/greenfield-storage-provider/util"
)
func (s *SpDBImpl) InsertUploadProgress(objectID uint64, isAgentUpload bool) error {
taskState := storetypes.TaskState_TASK_STATE_INIT_UNSPECIFIED
if result := s.db.Create(&UploadObjectProgressTable{
ObjectID: objectID,
TaskState: int32(taskState),
IsAgentUpload: isAgentUpload,
TaskStateDescription: taskState.String(),
CreateTimestampSecond: GetCurrentUnixTime(),
UpdateTimestampSecond: GetCurrentUnixTime(),
}); result.Error != nil || result.RowsAffected != 1 {
return fmt.Errorf("failed to insert upload record: %s", result.Error)
}
return nil
}
func (s *SpDBImpl) DeleteUploadProgress(objectID uint64) error {
return s.db.Delete(&UploadObjectProgressTable{
ObjectID: objectID, // should be the primary key
}).Error
}
func (s *SpDBImpl) UpdateUploadProgress(uploadMeta *corespdb.UploadObjectMeta) error {
if len(uploadMeta.SecondaryEndpoints) != 0 {
if result := s.db.Model(&UploadObjectProgressTable{}).Where("object_id = ?", uploadMeta.ObjectID).
Updates(&UploadObjectProgressTable{
TaskState: int32(uploadMeta.TaskState),
TaskStateDescription: uploadMeta.TaskState.String(),
GlobalVirtualGroupID: uploadMeta.GlobalVirtualGroupID,
ErrorDescription: uploadMeta.ErrorDescription,
SecondaryEndpoints: util.JoinWithComma(uploadMeta.SecondaryEndpoints),
SecondarySignatures: util.BytesSliceToString(uploadMeta.SecondarySignatures),
UpdateTimestampSecond: GetCurrentUnixTime(),
}); result.Error != nil || result.RowsAffected != 1 {
return fmt.Errorf("failed to update upload task record: %s", result.Error)
}
} else {
if result := s.db.Model(&UploadObjectProgressTable{}).Where("object_id = ?", uploadMeta.ObjectID).
Updates(&UploadObjectProgressTable{
TaskState: int32(uploadMeta.TaskState),
TaskStateDescription: uploadMeta.TaskState.String(),
ErrorDescription: uploadMeta.ErrorDescription,
UpdateTimestampSecond: GetCurrentUnixTime(),
}); result.Error != nil || result.RowsAffected != 1 {
return fmt.Errorf("failed to update upload task record: %s", result.Error)
}
}
return nil
}
func (s *SpDBImpl) GetUploadState(objectID uint64) (storetypes.TaskState, string, error) {
queryReturn := &UploadObjectProgressTable{}
result := s.db.First(queryReturn, "object_id = ?", objectID)
if result.Error != nil {
return storetypes.TaskState_TASK_STATE_INIT_UNSPECIFIED, "failed to query upload table", fmt.Errorf(
"failed to query upload table: %s", result.Error)
}
return storetypes.TaskState(queryReturn.TaskState), queryReturn.ErrorDescription, nil
}
func (s *SpDBImpl) GetUploadMetasToReplicate(limit int, timeoutSecond int64) ([]*corespdb.UploadObjectMeta, error) {
var (
result *gorm.DB
uploadObjectProgresses []UploadObjectProgressTable
returnUploadObjectMetas []*corespdb.UploadObjectMeta
)
result = s.db.Where("task_state IN ?", []string{
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_UPLOAD_OBJECT_DONE)),
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_REPLICATE_OBJECT_DOING)),
}).Order("update_timestamp_second DESC").Limit(limit).Find(&uploadObjectProgresses)
if result.Error != nil {
return nil, fmt.Errorf("failed to query upload table: %s", result.Error)
}
expired := GetCurrentUnixTime() - timeoutSecond
for _, u := range uploadObjectProgresses {
if expired > u.UpdateTimestampSecond {
break
}
returnUploadObjectMetas = append(returnUploadObjectMetas, &corespdb.UploadObjectMeta{
ObjectID: u.ObjectID,
})
}
return returnUploadObjectMetas, nil
}
func (s *SpDBImpl) GetUploadMetasToSeal(limit int, timeoutSecond int64) ([]*corespdb.UploadObjectMeta, error) {
var (
result *gorm.DB
uploadObjectProgresses []UploadObjectProgressTable
returnUploadObjectMetas []*corespdb.UploadObjectMeta
)
result = s.db.Where("task_state IN ?", []string{
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_REPLICATE_OBJECT_DONE)),
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_SEAL_OBJECT_DOING)),
}).Order("update_timestamp_second DESC").Limit(limit).Find(&uploadObjectProgresses)
if result.Error != nil {
return nil, fmt.Errorf("failed to query upload table: %s", result.Error)
}
expired := GetCurrentUnixTime() - timeoutSecond
for _, u := range uploadObjectProgresses {
if expired > u.UpdateTimestampSecond {
break
}
secondarySignatures, err := util.StringToBytesSlice(u.SecondarySignatures)
if err != nil {
return nil, err
}
returnUploadObjectMetas = append(returnUploadObjectMetas, &corespdb.UploadObjectMeta{
ObjectID: u.ObjectID,
GlobalVirtualGroupID: u.GlobalVirtualGroupID,
SecondaryEndpoints: util.SplitByComma(u.SecondaryEndpoints),
SecondarySignatures: secondarySignatures,
IsAgentUpload: u.IsAgentUpload,
})
}
return returnUploadObjectMetas, nil
}
func (s *SpDBImpl) GetUploadMetasToReplicateByStartTS(limit int, startTS int64) ([]*corespdb.UploadObjectMeta, error) {
var (
result *gorm.DB
uploadObjectProgresses []UploadObjectProgressTable
returnUploadObjectMetas []*corespdb.UploadObjectMeta
)
result = s.db.Where("task_state IN ? and create_timestamp_second > ?", []string{
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_UPLOAD_OBJECT_DONE)),
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_REPLICATE_OBJECT_DOING)),
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_REPLICATE_OBJECT_ERROR)),
}, startTS).Order("create_timestamp_second ASC").Limit(limit).Find(&uploadObjectProgresses)
if result.Error != nil {
return nil, fmt.Errorf("failed to query upload table: %s", result.Error)
}
for _, u := range uploadObjectProgresses {
secondarySignatures, err := util.StringToBytesSlice(u.SecondarySignatures)
if err != nil {
return nil, err
}
returnUploadObjectMetas = append(returnUploadObjectMetas, &corespdb.UploadObjectMeta{
ObjectID: u.ObjectID,
GlobalVirtualGroupID: u.GlobalVirtualGroupID,
SecondaryEndpoints: util.SplitByComma(u.SecondaryEndpoints),
SecondarySignatures: secondarySignatures,
CreateTimeStampSecond: u.CreateTimestampSecond,
IsAgentUpload: u.IsAgentUpload,
})
}
return returnUploadObjectMetas, nil
}
func (s *SpDBImpl) GetUploadMetasToSealByStartTS(limit int, startTS int64) ([]*corespdb.UploadObjectMeta, error) {
var (
result *gorm.DB
uploadObjectProgresses []UploadObjectProgressTable
returnUploadObjectMetas []*corespdb.UploadObjectMeta
)
result = s.db.Where("task_state IN ? and create_timestamp_second > ?", []string{
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_REPLICATE_OBJECT_DONE)),
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_SEAL_OBJECT_DOING)),
util.Uint32ToString(uint32(storetypes.TaskState_TASK_STATE_SEAL_OBJECT_ERROR)),
}, startTS).Order("create_timestamp_second ASC").Limit(limit).Find(&uploadObjectProgresses)
if result.Error != nil {
return nil, fmt.Errorf("failed to query upload table: %s", result.Error)
}
for _, u := range uploadObjectProgresses {
secondarySignatures, err := util.StringToBytesSlice(u.SecondarySignatures)
if err != nil {
return nil, err
}
returnUploadObjectMetas = append(returnUploadObjectMetas, &corespdb.UploadObjectMeta{
ObjectID: u.ObjectID,
GlobalVirtualGroupID: u.GlobalVirtualGroupID,
SecondaryEndpoints: util.SplitByComma(u.SecondaryEndpoints),
SecondarySignatures: secondarySignatures,
CreateTimeStampSecond: u.CreateTimestampSecond,
})
}
return returnUploadObjectMetas, nil
}
func (s *SpDBImpl) GetUploadMetasToRejectUnsealByRangeTS(limit int, startTS int64, endTS int64) ([]*corespdb.UploadObjectMeta, error) {
var (
result *gorm.DB
uploadObjectProgresses []UploadObjectProgressTable
returnUploadObjectMetas []*corespdb.UploadObjectMeta
)
result = s.db.Where("create_timestamp_second > ? and create_timestamp_second <= ?",
startTS, endTS).Order("create_timestamp_second ASC").Limit(limit).Find(&uploadObjectProgresses)
if result.Error != nil {
return nil, fmt.Errorf("failed to query upload table: %s", result.Error)
}
for _, u := range uploadObjectProgresses {
returnUploadObjectMetas = append(returnUploadObjectMetas, &corespdb.UploadObjectMeta{
ObjectID: u.ObjectID,
CreateTimeStampSecond: u.CreateTimestampSecond,
})
}
return returnUploadObjectMetas, nil
}