@@ -2,6 +2,7 @@ package blob_uploader
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
7
8
"github.com/prometheus/client_golang/prometheus"
@@ -67,7 +68,7 @@ func (b *BlobUploader) UploadBlobToS3() {
67
68
}
68
69
69
70
// get un-uploaded batches from database in ascending order by their index.
70
- dbBatch , err := b .batchOrm . GetFirstUnuploadedBatchByPlatform (b .ctx , b .cfg .StartBatch , types .BlobStoragePlatformS3 )
71
+ dbBatch , err := b .GetFirstUnuploadedBatchByPlatform (b .ctx , b .cfg .StartBatch , types .BlobStoragePlatformS3 )
71
72
if err != nil {
72
73
log .Error ("Failed to fetch unuploaded batch" , "err" , err )
73
74
return
@@ -85,7 +86,7 @@ func (b *BlobUploader) UploadBlobToS3() {
85
86
if err != nil {
86
87
log .Error ("failed to construct constructBlobCodec payload " , "codecVersion" , codecVersion , "batch index" , dbBatch .Index , "err" , err )
87
88
b .metrics .rollupBlobUploaderUploadToS3FailedTotal .Inc ()
88
- if updateErr := b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , types .BlobStoragePlatformS3 , types .BlobUploadStatusFailed ); updateErr != nil {
89
+ if updateErr := b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , dbBatch . Hash , types .BlobStoragePlatformS3 , types .BlobUploadStatusFailed ); updateErr != nil {
89
90
log .Error ("failed to update blob upload status to failed" , "batch index" , dbBatch .Index , "err" , updateErr )
90
91
}
91
92
return
@@ -97,7 +98,7 @@ func (b *BlobUploader) UploadBlobToS3() {
97
98
log .Error ("failed to calculate versioned blob hash" , "batch index" , dbBatch .Index , "err" , err )
98
99
b .metrics .rollupBlobUploaderUploadToS3FailedTotal .Inc ()
99
100
// update status to failed
100
- if updateErr := b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , types .BlobStoragePlatformS3 , types .BlobUploadStatusFailed ); updateErr != nil {
101
+ if updateErr := b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , dbBatch . Hash , types .BlobStoragePlatformS3 , types .BlobUploadStatusFailed ); updateErr != nil {
101
102
log .Error ("failed to update blob upload status to failed" , "batch index" , dbBatch .Index , "err" , updateErr )
102
103
}
103
104
return
@@ -110,14 +111,14 @@ func (b *BlobUploader) UploadBlobToS3() {
110
111
log .Error ("failed to upload blob data to AWS S3" , "batch index" , dbBatch .Index , "versioned blob hash" , key , "err" , err )
111
112
b .metrics .rollupBlobUploaderUploadToS3FailedTotal .Inc ()
112
113
// update status to failed
113
- if updateErr := b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , types .BlobStoragePlatformS3 , types .BlobUploadStatusFailed ); updateErr != nil {
114
+ if updateErr := b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , dbBatch . Hash , types .BlobStoragePlatformS3 , types .BlobUploadStatusFailed ); updateErr != nil {
114
115
log .Error ("failed to update blob upload status to failed" , "batch index" , dbBatch .Index , "err" , updateErr )
115
116
}
116
117
return
117
118
}
118
119
119
120
// update status to uploaded
120
- if err = b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , types .BlobStoragePlatformS3 , types .BlobUploadStatusUploaded ); err != nil {
121
+ if err = b .blobUploadOrm .InsertOrUpdateBlobUpload (b .ctx , dbBatch .Index , dbBatch . Hash , types .BlobStoragePlatformS3 , types .BlobUploadStatusUploaded ); err != nil {
121
122
log .Error ("failed to update blob upload status to uploaded" , "batch index" , dbBatch .Index , "err" , err )
122
123
b .metrics .rollupBlobUploaderUploadToS3FailedTotal .Inc ()
123
124
return
@@ -195,3 +196,56 @@ func (b *BlobUploader) constructBlobCodec(dbBatch *orm.Batch) (*kzg4844.Blob, er
195
196
196
197
return daBatch .Blob (), nil
197
198
}
199
+
200
+ // GetFirstUnuploadedBatchByPlatform retrieves the first batch that either hasn't been uploaded to corresponding blob storage service
201
+ // The batch must have a commit_tx_hash (committed).
202
+ func (b * BlobUploader ) GetFirstUnuploadedBatchByPlatform (ctx context.Context , startBatch uint64 , platform types.BlobStoragePlatform ) (* orm.Batch , error ) {
203
+ batchIndex , err := b .blobUploadOrm .GetNextBatchIndexToUploadByPlatform (ctx , startBatch , platform )
204
+ if err != nil {
205
+ return nil , err
206
+ }
207
+
208
+ var batch * orm.Batch
209
+ for {
210
+ var err error
211
+ batch , err = b .batchOrm .GetBatchByIndex (ctx , batchIndex )
212
+ if err != nil {
213
+ if errors .Is (err , gorm .ErrRecordNotFound ) {
214
+ log .Debug ("got batch not proposed for blob uploading" , "batch_index" , batchIndex , "platform" , platform .String ())
215
+ return nil , nil
216
+ }
217
+ return nil , err
218
+ }
219
+
220
+ // to check if the parent batch uploaded
221
+ // if no, there is a batch revert happened, we need to fallback to upload previous batch
222
+ // skip the check if the parent batch is genesis batch
223
+ if batchIndex <= 1 || batchIndex == startBatch {
224
+ break
225
+ }
226
+ fields := map [string ]interface {}{
227
+ "batch_index = ?" : batchIndex - 1 ,
228
+ "batch_hash = ?" : batch .ParentBatchHash ,
229
+ "platform = ?" : platform ,
230
+ "status = ?" : types .BlobUploadStatusUploaded ,
231
+ }
232
+ blobUpload , err := b .blobUploadOrm .GetBlobUploads (ctx , fields , nil , 1 )
233
+ if err != nil {
234
+ return nil , err
235
+ }
236
+
237
+ if len (blobUpload ) == 0 {
238
+ batchIndex --
239
+ continue
240
+ }
241
+
242
+ break
243
+ }
244
+
245
+ if len (batch .CommitTxHash ) == 0 {
246
+ log .Debug ("got batch not committed for blob uploading" , "batch_index" , batchIndex , "platform" , platform .String ())
247
+ return nil , nil
248
+ }
249
+
250
+ return batch , nil
251
+ }
0 commit comments