-
Notifications
You must be signed in to change notification settings - Fork 14
/
daggen.go
291 lines (268 loc) · 8.7 KB
/
daggen.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
package datasetworker
import (
"bytes"
"context"
"database/sql"
"io"
"github.com/cockroachdb/errors"
"github.com/data-preservation-programs/singularity/database"
"github.com/data-preservation-programs/singularity/model"
"github.com/data-preservation-programs/singularity/pack"
"github.com/data-preservation-programs/singularity/pack/daggen"
"github.com/data-preservation-programs/singularity/pack/packutil"
"github.com/data-preservation-programs/singularity/storagesystem"
"github.com/data-preservation-programs/singularity/util"
commp "github.com/filecoin-project/go-fil-commp-hashhash"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-varint"
"gorm.io/gorm"
)
type DagGenerator struct {
ctx context.Context
db *gorm.DB
attachmentID model.SourceAttachmentID
rows *sql.Rows
root cid.Cid
dirCIDs map[model.DirectoryID]model.CID
buffer io.Reader
done bool
carBlocks []model.CarBlock
offset int64
noInline bool
}
// Read implements the io.Reader interface for the DagGenerator. It generates
// a CAR (Content Addressable Archive) representation of directories from a database,
// which can be read in chunks using the provided byte slice.
//
// Read operation involves several key steps:
// 1. It checks if the context has been canceled or if an error has occurred.
// 2. If there's an existing buffer, it reads from it.
// 3. If reading reaches the end of the current buffer, or if no buffer has been initialized,
// the method fetches the next directory from the database and processes it.
// 4. The directory data is then converted to CAR format, and the resulting bytes are buffered.
// 5. Finally, the buffered data is read into the provided slice.
//
// Parameters:
// - p: A byte slice that will be filled with the generated CAR data.
//
// Returns:
// - The number of bytes read.
// - An error if there's an issue during the operation. If the end of the data is reached,
// it returns io.EOF.
func (d *DagGenerator) Read(p []byte) (int, error) {
if d.ctx.Err() != nil {
return 0, d.ctx.Err()
}
if d.buffer != nil {
n, err := d.buffer.Read(p)
if err == io.EOF {
err = nil
d.buffer = nil
}
return n, err
}
if d.done {
return 0, io.EOF
}
db := d.db
if d.rows == nil {
rows, err := db.
Model(&model.Directory{}).
Where("attachment_id = ? AND exported = ?", d.attachmentID, false).
Order("id asc").Rows()
if err != nil {
return 0, errors.WithStack(err)
}
d.rows = rows
header, err := util.GenerateCarHeader(d.root)
if err != nil {
return 0, errors.WithStack(err)
}
d.buffer = bytes.NewReader(header)
d.offset += int64(len(header))
return 0, nil
}
if !d.rows.Next() {
d.done = true
return 0, nil
}
var dir model.Directory
err := db.ScanRows(d.rows, &dir)
if err != nil {
return 0, errors.WithStack(err)
}
d.dirCIDs[dir.ID] = dir.CID
blks, err := daggen.UnmarshalToBlocks(dir.Data)
if err != nil {
return 0, errors.Wrapf(err, "failed to unmarshall directory %d to blocks", dir.ID)
}
readers := make([]io.Reader, 0, len(blks)*3)
for _, blk := range blks {
if len(blk.RawData()) == 0 && blk.Cid() != packutil.EmptyFileCid {
// This is dummy node. skip putting into car file
continue
}
carBlockSize := len(blk.RawData()) + blk.Cid().ByteLen()
vint := varint.ToUvarint(uint64(carBlockSize))
carBlockSize += len(vint)
readers = append(readers, bytes.NewReader(vint), bytes.NewReader(blk.Cid().Bytes()), bytes.NewReader(blk.RawData()))
if !d.noInline {
d.carBlocks = append(d.carBlocks, model.CarBlock{
CID: model.CID(blk.Cid()),
CarOffset: d.offset,
CarBlockLength: int32(carBlockSize),
Varint: vint,
RawBlock: blk.RawData(),
})
}
d.offset += int64(carBlockSize)
}
d.buffer = io.MultiReader(readers...)
return 0, nil
}
func (d *DagGenerator) Close() error {
if d.rows != nil {
return errors.WithStack(d.rows.Close())
}
return nil
}
func NewDagGenerator(ctx context.Context, db *gorm.DB, attachmentID model.SourceAttachmentID, root cid.Cid, noInline bool) *DagGenerator {
return &DagGenerator{
ctx: ctx,
db: db,
attachmentID: attachmentID,
root: root,
dirCIDs: make(map[model.DirectoryID]model.CID),
noInline: noInline,
}
}
var ErrDagNotReady = errors.New("dag is not ready to be generated")
var ErrDagDisabled = errors.New("dag generation is disabled for this preparation")
// ExportDag exports a Directed Acyclic Graph (DAG) for a given source.
// The function takes a source, iterates through the related directories
// (as rows from the database), and constructs the DAG in the form of a
// CAR (Content Addressable Archive) file. This CAR file represents the
// block structure of the data.
//
// The function:
// - Initializes necessary components like writers and calculators
// - Iterates through the directories linked with the source and fetches blocks
// - Writes the blocks into a CAR file
// - Closes the CAR file and renames it appropriately
// - Saves the CAR meta-information into the database
//
// Parameters:
// - ctx context.Context: The context to control cancellations and timeouts.
// - source model.Source: The source for which the DAG needs to be generated.
//
// The function performs several database and file system operations,
// each of which might result in an error. Errors are wrapped with context
// information and returned.
//
// Returns:
// - error: Standard error interface, returns nil if no error occurred during execution.
func (w *Thread) ExportDag(ctx context.Context, job model.Job) error {
if job.Attachment.Preparation.NoDag {
return errors.WithStack(ErrDagDisabled)
}
rootCID, err := job.Attachment.RootDirectoryCID(ctx, w.dbNoContext)
if err != nil {
return errors.WithStack(err)
}
if rootCID == cid.Undef {
return ErrDagNotReady
}
db := w.dbNoContext.WithContext(ctx)
pieceSize := job.Attachment.Preparation.PieceSize
// storageWriter can be nil for inline preparation
storageID, storageWriter, err := storagesystem.GetRandomOutputWriter(ctx, job.Attachment.Preparation.OutputStorages)
if err != nil {
return errors.WithStack(err)
}
dagGenerator := NewDagGenerator(ctx, db, job.Attachment.ID, rootCID, job.Attachment.Preparation.NoInline)
defer dagGenerator.Close()
var filename string
calc := &commp.Calc{}
var pieceCid cid.Cid
var finalPieceSize uint64
var fileSize int64
if storageWriter != nil {
reader := io.TeeReader(dagGenerator, calc)
filename = uuid.NewString() + ".car"
obj, err := storageWriter.Write(ctx, filename, reader)
if err != nil {
return errors.WithStack(err)
}
fileSize = obj.Size()
if dagGenerator.offset <= 59 {
logger.Info("Nothing to export to dag. Skipping.")
return nil
}
pieceCid, finalPieceSize, err = pack.GetCommp(calc, uint64(pieceSize))
if err != nil {
return errors.WithStack(err)
}
_, err = storageWriter.Move(ctx, obj, pieceCid.String()+".car")
if err != nil && !errors.Is(err, storagesystem.ErrMoveNotSupported) {
logger.Errorf("failed to move car file from %s to %s: %s", filename, pieceCid.String()+".car", err)
}
if err == nil {
filename = pieceCid.String() + ".car"
}
} else {
fileSize, err = io.Copy(calc, dagGenerator)
if err != nil {
return errors.WithStack(err)
}
if dagGenerator.offset <= 59 {
logger.Info("Nothing to export to dag. Skipping.")
return nil
}
pieceCid, finalPieceSize, err = pack.GetCommp(calc, uint64(pieceSize))
if err != nil {
return errors.WithStack(err)
}
}
car := model.Car{
PieceCID: model.CID(pieceCid),
PieceSize: int64(finalPieceSize),
RootCID: model.CID(rootCID),
FileSize: fileSize,
StorageID: storageID,
StoragePath: filename,
AttachmentID: &job.AttachmentID,
PreparationID: job.Attachment.PreparationID,
}
err = database.DoRetry(ctx, func() error {
return db.Transaction(func(db *gorm.DB) error {
err := db.Create(&car).Error
if err != nil {
return errors.WithStack(err)
}
if len(dagGenerator.carBlocks) > 0 {
for i := range dagGenerator.carBlocks {
dagGenerator.carBlocks[i].CarID = car.ID
}
err = db.CreateInBatches(dagGenerator.carBlocks, util.BatchSize).Error
if err != nil {
return errors.WithStack(err)
}
}
for dirID, dirCID := range dagGenerator.dirCIDs {
result := db.Model(&model.Directory{}).Where("id = ? AND cid = ?", dirID, dirCID).Update("exported", true)
if result.Error != nil {
return errors.Wrap(result.Error, "failed to update directory")
}
if result.RowsAffected == 0 {
logger.Warnf("directory %d has changed since we started.", dirID)
}
}
return nil
})
})
if err != nil {
return errors.WithStack(err)
}
return nil
}