24
24
migrationMetaDB = "data001-migration-meta.sqlite3"
25
25
accessUpdateBufferSize = 100000
26
26
commitInsertsInterval = 60 * time .Second
27
- metaSyncBatchSize = 10000
27
+ metaSyncBatchSize = 5000
28
28
lowSpaceThresholdGB = 50 // in GB
29
29
minKeysToMigrate = 100
30
30
@@ -39,7 +39,7 @@ func init() {
39
39
40
40
type UpdateMessages []UpdateMessage
41
41
42
- // AccessUpdate holds the key and the last accessed time.
42
+ // UpdateMessage holds the key and the last accessed time.
43
43
type UpdateMessage struct {
44
44
Key string
45
45
LastAccessTime time.Time
@@ -104,12 +104,14 @@ func NewMigrationMetaStore(ctx context.Context, dataDir string, cloud cloud.Stor
104
104
log .P2P ().WithContext (ctx ).Errorf ("cannot create migration table in sqlite database: %s" , err .Error ())
105
105
}
106
106
107
- if handler .isMetaSyncRequired () {
108
- err := handler .syncMetaWithData (ctx )
109
- if err != nil {
110
- log .WithContext (ctx ).WithError (err ).Error ("error syncing meta with p2p data" )
107
+ go func () {
108
+ if handler .isMetaSyncRequired () {
109
+ err := handler .syncMetaWithData (ctx )
110
+ if err != nil {
111
+ log .WithContext (ctx ).WithError (err ).Error ("error syncing meta with p2p data" )
112
+ }
111
113
}
112
- }
114
+ }()
113
115
114
116
go handler .startLastAccessedUpdateWorker (ctx )
115
117
go handler .startInsertWorker (ctx )
@@ -184,21 +186,36 @@ func (d *MigrationMetaStore) isMetaSyncRequired() bool {
184
186
}
185
187
186
188
func (d * MigrationMetaStore ) syncMetaWithData (ctx context.Context ) error {
187
- query := `SELECT key, data, updatedAt FROM data LIMIT ? OFFSET ?`
188
189
var offset int
189
190
191
+ query := `SELECT key, data, updatedAt FROM data LIMIT ? OFFSET ?`
192
+ insertQuery := `
193
+ INSERT INTO meta (key, last_accessed, access_count, data_size)
194
+ VALUES (?, ?, 1, ?)
195
+ ON CONFLICT(key) DO
196
+ UPDATE SET
197
+ last_accessed = EXCLUDED.last_accessed,
198
+ data_size = EXCLUDED.data_size,
199
+ access_count = access_count + 1`
200
+
190
201
for {
191
202
rows , err := d .p2pDataStore .Queryx (query , metaSyncBatchSize , offset )
192
203
if err != nil {
193
204
log .WithContext (ctx ).WithError (err ).Error ("Error querying p2p data store" )
194
205
return err
195
206
}
196
207
197
- log .WithContext (ctx ).WithField ("offset" , offset ).Info ("Syncing meta with p2p data store" )
198
- var batchUpdates []UpdateMessage
199
- found := false
208
+ var batchProcessed bool
209
+
210
+ tx , err := d .db .Beginx ()
211
+ if err != nil {
212
+ rows .Close ()
213
+ log .WithContext (ctx ).WithError (err ).Error ("Failed to start transaction" )
214
+ return err
215
+ }
216
+
200
217
for rows .Next () {
201
- found = true
218
+ batchProcessed = true
202
219
var r Record
203
220
var t * time.Time
204
221
@@ -210,23 +227,35 @@ func (d *MigrationMetaStore) syncMetaWithData(ctx context.Context) error {
210
227
r .UpdatedAt = * t
211
228
}
212
229
213
- dataSize := len (r .Data )
214
- batchUpdates = append (batchUpdates , UpdateMessage {
215
- Key : r .Key ,
216
- LastAccessTime : r .UpdatedAt ,
217
- Size : dataSize ,
218
- })
230
+ if _ , err := tx .Exec (insertQuery , r .Key , r .UpdatedAt , len (r .Data )); err != nil {
231
+ tx .Rollback ()
232
+ rows .Close ()
233
+ log .WithContext (ctx ).WithError (err ).Error ("Failed to execute batch insert" )
234
+ return err
235
+ }
236
+ }
237
+
238
+ if err := rows .Err (); err != nil {
239
+ tx .Rollback ()
240
+ rows .Close ()
241
+ log .WithContext (ctx ).WithError (err ).Error ("Error iterating rows" )
242
+ return err
219
243
}
220
- rows .Close ()
221
244
222
- if ! found {
245
+ if batchProcessed {
246
+ if err := tx .Commit (); err != nil {
247
+ rows .Close ()
248
+ log .WithContext (ctx ).WithError (err ).Error ("Failed to commit transaction" )
249
+ return err
250
+ }
251
+ } else {
252
+ tx .Rollback ()
253
+ rows .Close ()
223
254
break
224
255
}
225
256
226
- // Send batch for insertion using the existing channel-based mechanism.
227
- PostKeysInsert (batchUpdates )
228
-
229
- offset += len (batchUpdates ) // Move the offset forward by the number of items processed.
257
+ rows .Close ()
258
+ offset += metaSyncBatchSize
230
259
}
231
260
232
261
return nil
0 commit comments