/
dirmetadataupdate.go
575 lines (540 loc) · 18.3 KB
/
dirmetadataupdate.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright 2019 DxChain, All rights reserved.
// Use of this source code is governed by an Apache
// License 2.0 that can be found in the LICENSE file.
package filesystem
import (
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
"unsafe"
"github.com/DxChainNetwork/godx/common"
"github.com/DxChainNetwork/godx/common/writeaheadlog"
"github.com/DxChainNetwork/godx/rlp"
"github.com/DxChainNetwork/godx/storage"
"github.com/DxChainNetwork/godx/storage/storageclient/filesystem/dxdir"
"github.com/DxChainNetwork/godx/storage/storageclient/filesystem/dxfile"
)
var (
// errUpdateAlreadyInProgress is the error when creating a new update, found
// there is already an update goroutine in progress.
errUpdateAlreadyInProgress = errors.New("another goroutine is working on metadata update")
// errStopped is the error that happens when the system stops during the update
errStopped = errors.New("file system stopped during update")
// errInterrupted is the error that happens during an update, another update interrupt
// and redo the update
errInterrupted = errors.New("file update is interrupted")
)
type (
// dirMetadataUpdate is a single dirMetadataUpdate for updating a dxdir metadata
dirMetadataUpdate struct {
// dxPath is the path relates to the root path of the directory currently being repaired
dxPath storage.DxPath
// updateInProgress is the atomic field of whether the current threadedUpdate is on going.
// There should be at most one threadedUpdate on going
updateInProgress uint32
// stop is a channel indicating whether a stop request is received for the update.
// The source of stop comes from two conditions:
// 1. the program is shutting down
// 2. A new thread is trying to update the current DxPath
stop chan struct{}
// consecutiveFails is the atomic field for counting the consecutive failed times.
// When consecutiveFails reaches a certain number, the dirMetadata update is released.
consecutiveFails uint32
// walTxn is the wal transaction associated with dirMetadataUpdate.
// It is stored when first time the goroutine is established.
walTxn unsafe.Pointer
}
// metadataForUpdate contains the least information for dxdir update
metadataForUpdate struct {
numFiles uint64
totalSize uint64
health uint32
stuckHealth uint32
minRedundancy uint32
numStuckSegments uint32
timeLastHealthCheck time.Time
}
)
// InitAndUpdateDirMetadata create the update intent, and then apply the intent.
// The actual metadata update is executed in a thread updateDirMetadata goroutine
func (fs *fileSystem) InitAndUpdateDirMetadata(path storage.DxPath) error {
// Initialize the dirMetadataUpdate, that is, recordDirMetadataUpdate
txn, err := fs.recordDirMetadataIntent(path)
if err != nil {
return fmt.Errorf("cannot update metadata at %v", path.Path)
}
// Apply the update
if err = fs.updateDirMetadata(path, txn); err != nil {
// tm already closed or update thread already in progress
if err == errUpdateAlreadyInProgress {
err = txn.Release()
}
return err
}
return nil
}
// recordDirMetadataIntent record and commit the dirMetadata intent to the wal
func (fs *fileSystem) recordDirMetadataIntent(path storage.DxPath) (*writeaheadlog.Transaction, error) {
op, err := createWalOp(path)
if err != nil {
return nil, err
}
txn, err := fs.updateWal.NewTransaction([]writeaheadlog.Operation{op})
if err != nil {
return nil, err
}
if <-txn.InitComplete; txn.InitErr != nil {
return nil, txn.InitErr
}
if err = <-txn.Commit(); err != nil {
return nil, err
}
return txn, nil
}
// createWalOp creates a dir metadata update based on the give path
func createWalOp(path storage.DxPath) (writeaheadlog.Operation, error) {
b, err := rlp.EncodeToBytes(path.Path)
if err != nil {
return writeaheadlog.Operation{}, err
}
return writeaheadlog.Operation{
Name: dirMetadataUpdateName,
Data: b,
}, nil
}
// decodeWalOp decode the wal.Operation to DxPath
func decodeWalOp(operation writeaheadlog.Operation) (storage.DxPath, error) {
if operation.Name != dirMetadataUpdateName {
return storage.DxPath{}, fmt.Errorf("unknown operation name for updateWal [%s]", operation.Name)
}
var s string
if err := rlp.DecodeBytes(operation.Data, &s); err != nil {
return storage.DxPath{}, err
}
if len(s) == 0 {
return storage.RootDxPath(), nil
}
return storage.NewDxPath(s)
}
// applyDirMetadataUpdate creates a new dirMetadataUpdate and initialize a goroutine of
// calculateMetadataAndApply as necessary. The function holds a key of fs.lock in order
// to be executed exclusively from cleanUp.
// 1. If the update already exists in fs.unfinishedUpdates, signal the current update thread
// signalStop
// 2. Else if there is already an update thread in progress (signaled by
// update.updateInProgress == 1), return
// 3. Else add to fs.tm, update fs.unfinishedUpdates, and start a goroutine to
// calculateMetadataAndApply
//
// The returned error type is could be two cases:
// 1. threadmanager already stopped. It would be safe to throw the error during error handling
// 2. there is already a thread updating the metadata. return errUpdateAlreadyInProgress
//
// The function is called in three places:
// 1. A dxfile is updated and the update is bubble to the root
// 2. A MaintenanceLoop loops over the fs.unfinishedUpdates field for previously failed update.
// In this case, walTxn should be nil
// 3. During initialization, unfinished updates are decoded from wal and this function is called.
func (fs *fileSystem) updateDirMetadata(path storage.DxPath, walTxn *writeaheadlog.Transaction) (err error) {
fs.lock.Lock()
defer fs.lock.Unlock()
// If the update already exists in the unfinishedUpdates, signalStop
update, exist := fs.unfinishedUpdates[path]
if exist {
update.signalStop()
} else {
update = &dirMetadataUpdate{
dxPath: path,
updateInProgress: 0,
stop: make(chan struct{}, 1),
}
}
// Check whether the update has an update thread in progress.
// Create an update thread only if the updateInProgress is false
swapped := atomic.CompareAndSwapUint32(&update.updateInProgress, 0, 1)
if !swapped {
// Already an update thread in progress, return
err = errUpdateAlreadyInProgress
return
}
defer func() {
// If error happened swap back the updateInProgress value
if err != nil {
atomic.StoreUint32(&update.updateInProgress, 0)
}
}()
if err = fs.tm.Add(); err != nil {
err = errStopped
return
}
fs.unfinishedUpdates[path] = update
// If the input walTxn is not nil, update the walTxn field
if walTxn != nil {
atomic.StorePointer(&update.walTxn, unsafe.Pointer(walTxn))
}
go fs.calculateMetadataAndApply(update)
return
}
// signalStop is a helper function that update the redo field and try to fill the stop channel.
// It is triggered when a new update is created while currently a dirMetadataUpdate is in progress.
func (update *dirMetadataUpdate) signalStop() {
// Try to insert into the channel. If cannot, there is already struct in stop channel.
select {
case update.stop <- struct{}{}:
default:
}
}
// calculateMetadataAndApply is the threaded function that calculate the metadata
// and save to dxdir. This is the core function of dirMetadataUpdate
func (fs *fileSystem) calculateMetadataAndApply(update *dirMetadataUpdate) {
var err error
// Call cleanUp to clean up the update
defer func() {
update.cleanUp(fs, err)
}()
// Before the update, clear the stop channel
select {
case <-update.stop:
default:
}
for {
if fs.disrupt("cmaa1") {
err = errDisrupted
return
}
// Check whether a new update is called, and check whether the program is stopped
select {
case <-update.stop:
continue
case <-fs.tm.StopChan():
err = errStopped
return
default:
}
if fs.disrupt("cmaa2") {
err = errDisrupted
return
}
// Calculate the metadata
var md *dxdir.Metadata
md, err = fs.loopDirAndCalculateDirMetadata(update)
// If stop signal is found, continue to next loop
if err == errInterrupted {
continue
}
// If error happened, return
if err != nil {
return
}
// Apply and write the metadata to the dxdir
err = fs.applyDxDirMetadata(update.dxPath, md)
if err != nil {
return
}
if fs.disrupt("cmaa3") {
err = errDisrupted
return
}
// If stop signal received, continue to next loop;
// If StopChan received, continue to cleanUp
// If no error happened, hold fs.lock and continue to cleanUp process
// Note: this is not a beautiful coding style, but it is necessary to
// lock the process prior to cleanUp execution. Else a new update might
// be initiated and block cleanUp, which finally cause stop signal is
// ignored.
fs.lock.Lock()
select {
case <-update.stop:
fs.lock.Unlock()
continue
case <-fs.tm.StopChan():
err = errStopped
fs.lock.Unlock()
return
default:
}
break
}
return
}
// cleanUp is the defer function that is called for the goroutine of calculateMetadataAndApply.
// currently fs.lock is locked when error is nil; unlocked when error is not nil
// It do the following:
// 1. Determine whether release is necessary
// 2. If release is needed, delete the entry in fs.unfinishedUpdates.
// 3. If release is needed, Release the transaction
// 4. Set the updateInProgress value to 0, notifying this goroutine is over
// 5. Unlock fs.lock if necessary
// 6. Notify thread manager this goroutine is done.
func (update *dirMetadataUpdate) cleanUp(fs *fileSystem, origErr error) {
// Set the updateInProgress value to 0, notifying this goroutine is over
atomic.StoreUint32(&update.updateInProgress, 0)
defer func() {
// Unlock fs.lock if fs.lock is locked previously
if origErr == nil {
fs.lock.Unlock()
}
// notify the thread manager the work is done
fs.tm.Done()
}()
// If the error is errStopped, do nothing and simply return
if origErr == errStopped {
return
}
// Calculate consecutive failes
if origErr != nil {
atomic.AddUint32(&update.consecutiveFails, 1)
}
fails := atomic.LoadUint32(&update.consecutiveFails)
//Determine whether to release the update
err := origErr
release := fails >= numConsecutiveFailRelease || origErr == nil
if release {
// release the wal transaction
txn := (*writeaheadlog.Transaction)(atomic.LoadPointer(&update.walTxn))
if txn != nil {
err = common.ErrCompose(origErr, txn.Release())
}
// delete the update from unfinishedUpdates
if origErr != nil {
fs.lock.Lock()
delete(fs.unfinishedUpdates, update.dxPath)
fs.lock.Unlock()
} else {
delete(fs.unfinishedUpdates, update.dxPath)
}
}
if err == nil {
// If the update is to update the root, do root checks
if update.dxPath.IsRoot() {
d, err := fs.dirSet.Open(update.dxPath)
if err != nil {
fs.logger.Warn("cannot open root directory")
return
}
md := d.Metadata()
// Signal repairNeeded as necessary
if md.Health < dxfile.RepairHealthThreshold {
select {
case fs.repairNeeded <- struct{}{}:
default:
}
}
// Signal stuckFound as necessary
if md.NumStuckSegments > 0 {
select {
case fs.stuckFound <- struct{}{}:
default:
}
}
if err := d.Close(); err != nil {
fs.logger.Warn("cannot close root directory", "origErr", err)
}
return
}
// no error happened. Continue to update parent
parent, err := update.dxPath.Parent()
if err != nil {
fs.logger.Warn("cannot create parent directory", "path", update.dxPath.Path, "origErr", err)
return
}
// InitAndUpdateDirMetadata will hold the fs.lock. Thus call it with a goroutine to avoid
// deadlock
go func() {
if err = fs.InitAndUpdateDirMetadata(parent); err != nil {
fs.logger.Warn("cannot update parent directory", "path", update.dxPath.Path, "origErr", err)
}
}()
} else {
if release {
// released updates failed more than numConsecutiveFailRelease times
fs.logger.Error("cannot update the metadata.", "consecutive fails", fails, "origErr", origErr)
} else {
// unreleased updates
fs.logger.Warn("cannot update the metadata. Try later", "origErr", origErr)
}
}
}
// loopDirAndCalculateDirMetadata loops over all files under the DxPath and calculate the updated
// metadata of the update
func (fs *fileSystem) loopDirAndCalculateDirMetadata(update *dirMetadataUpdate) (*dxdir.Metadata, error) {
// Set default metadata value
metadata := &dxdir.Metadata{
NumFiles: 0,
TotalSize: 0,
Health: dxdir.DefaultHealth,
StuckHealth: dxdir.DefaultHealth,
MinRedundancy: math.MaxUint32,
TimeLastHealthCheck: uint64(time.Now().Unix()),
TimeModify: uint64(time.Now().Unix()),
NumStuckSegments: 0,
DxPath: update.dxPath,
RootPath: fs.fileRootDir,
}
// Read all files and directories under the path
fileInfos, err := ioutil.ReadDir(string(fs.fileRootDir.Join(update.dxPath)))
if err != nil {
return nil, err
}
// Iterate over all files under the directory
for _, file := range fileInfos {
// If there is a stop signal, return the error of errStopped
select {
case <-update.stop:
return nil, errInterrupted
case <-fs.tm.StopChan():
return nil, errStopped
default:
}
ext := filepath.Ext(file.Name())
var md *metadataForUpdate
if ext == storage.DxFileExt {
// File type DxFile
md, err = fs.calculateDxFileMetadata(update.dxPath, file.Name())
if err != nil {
fs.logger.Warn("cannot calculate the file metadata", "path", update.dxPath.Path, "err", err)
continue
}
} else if file.IsDir() {
// File type DxDir
md, err = fs.calculateDxDirMetadata(update.dxPath, file.Name())
if err == os.ErrExist {
continue
}
if err != nil {
fs.logger.Warn("cannot calculate the file metadata", "path", update.dxPath.Path, "err", err)
continue
}
} else {
// Ignore all files other than DxFile and DxDir
continue
}
metadata = applyMetadataForUpdateToMetadata(metadata, md)
}
return metadata, nil
}
// calculateDxFileMetadata update, calculate and apply the health related field of a dxfile.
func (fs *fileSystem) calculateDxFileMetadata(path storage.DxPath, filename string) (*metadataForUpdate, error) {
// Deal with the file names. Input path is the DxPath of the target directory.
// filename is the system filename of the dxfile.
filenameNoSuffix := strings.TrimSuffix(filename, storage.DxFileExt)
fileDxPath, err := path.Join(filenameNoSuffix)
if err != nil {
return nil, err
}
// Open the DxPath
file, err := fs.fileSet.Open(fileDxPath)
if err != nil {
return nil, fmt.Errorf("cannot open DxPath %v: %v", fileDxPath.Path, err)
}
defer file.Close()
// Get the healthInfoMap, mark all healthy as unstuck, and then calculate the health
healthInfoTable := fs.contractManager.HostHealthMapByID(file.HostIDs())
if err = file.MarkAllUnhealthySegmentsAsStuck(healthInfoTable); err != nil {
return nil, fmt.Errorf("cannot mark stuck segments for file %v: %v", fileDxPath.Path, err)
}
if err = file.MarkAllHealthySegmentsAsUnstuck(healthInfoTable); err != nil {
return nil, fmt.Errorf("cannot mark unstuck segments for file %v: %v", fileDxPath.Path, err)
}
health, stuckHealth, numStuckSegments := file.Health(healthInfoTable)
redundancy := file.Redundancy(healthInfoTable)
// Update TimeLastHealthCheck
if err := file.SetTimeLastHealthCheck(time.Now()); err != nil {
return nil, fmt.Errorf("cannot SetTimeLastHealthCheck for file %v: %v", fileDxPath.Path, err)
}
cachedMetadata := dxfile.CachedHealthMetadata{
Health: health,
StuckHealth: stuckHealth,
Redundancy: redundancy,
}
// apply cached metadata and return
return &metadataForUpdate{
numFiles: 1,
totalSize: file.FileSize(),
health: health,
stuckHealth: stuckHealth,
minRedundancy: redundancy,
numStuckSegments: numStuckSegments,
timeLastHealthCheck: time.Now(),
}, file.ApplyCachedHealthMetadata(cachedMetadata)
}
// calculateDxDirMetadata calculate and return the metadata from the .dxdir file
func (fs *fileSystem) calculateDxDirMetadata(path storage.DxPath, filename string) (*metadataForUpdate, error) {
path, err := path.Join(filename)
if err != nil {
return nil, err
}
d, err := fs.dirSet.Open(path)
if os.IsNotExist(err) {
// The .dxdir not exist. Create a new one
d, err = fs.dirSet.NewDxDir(path)
if os.IsExist(err) {
return nil, os.ErrExist
}
if err != nil {
return nil, fmt.Errorf("cannot create the .dxdir file for file %v: %v", path.Path, err)
}
} else if err != nil {
return nil, err
}
defer d.Close()
// No error, or the dxdir is created.
rawMetadata := d.Metadata()
return &metadataForUpdate{
numFiles: rawMetadata.NumFiles,
totalSize: rawMetadata.TotalSize,
health: rawMetadata.Health,
stuckHealth: rawMetadata.StuckHealth,
minRedundancy: rawMetadata.MinRedundancy,
numStuckSegments: rawMetadata.NumStuckSegments,
timeLastHealthCheck: time.Unix(int64(d.Metadata().TimeLastHealthCheck), 0),
}, nil
}
// applyDxDirMetadata apply the calculated metadata to the dxdir path
func (fs *fileSystem) applyDxDirMetadata(path storage.DxPath, md *dxdir.Metadata) error {
var d *dxdir.DirSetEntryWithID
var err error
d, err = fs.dirSet.NewDxDir(path)
if err == os.ErrExist {
d, err = fs.dirSet.Open(path)
if err != nil {
return err
}
} else if err != nil {
return err
}
defer d.Close()
err = d.UpdateMetadata(*md)
if err != nil {
return err
}
return nil
}
// applyMetadataForUpdateToMetadata apply a metadataForUpdate to dxdir.Metadata
func applyMetadataForUpdateToMetadata(md *dxdir.Metadata, update *metadataForUpdate) *dxdir.Metadata {
md.NumFiles += update.numFiles
md.TotalSize += update.totalSize
// If the update health or stuckHealth has higher priority than md.Health, update
if dxfile.CmpRepairPriority(update.health, md.Health) > 0 {
md.Health = update.health
}
if dxfile.CmpRepairPriority(update.stuckHealth, md.StuckHealth) > 0 {
md.StuckHealth = update.stuckHealth
}
// Update minRedundancy
if update.minRedundancy < md.MinRedundancy {
md.MinRedundancy = update.minRedundancy
}
md.NumStuckSegments += update.numStuckSegments
// update timeLastHealthCheck. TimeLastHealthCheck is the oldest time for health check
if uint64(update.timeLastHealthCheck.Unix()) < md.TimeLastHealthCheck {
md.TimeLastHealthCheck = uint64(update.timeLastHealthCheck.Unix())
}
return md
}