-
Notifications
You must be signed in to change notification settings - Fork 12
/
storage.go
1010 lines (891 loc) · 27.1 KB
/
storage.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package storage
import (
"context"
"errors"
"fmt"
"os"
"sync"
"sync/atomic"
"time"
lru "github.com/hashicorp/golang-lru/v2"
"go.sia.tech/core/consensus"
rhp2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
"go.sia.tech/hostd/alerts"
"go.sia.tech/hostd/internal/threadgroup"
"go.sia.tech/siad/modules"
"go.uber.org/zap"
"lukechampine.com/frand"
)
const (
// MaxTempSectorBlocks is the maximum number of blocks that a temp sector
// can be stored for.
MaxTempSectorBlocks = 144 * 7 // 7 days
)
// VolumeStatus is the status of a volume.
const (
VolumeStatusUnavailable = "unavailable"
VolumeStatusCreating = "creating"
VolumeStatusResizing = "resizing"
VolumeStatusRemoving = "removing"
VolumeStatusReady = "ready"
)
type (
// Alerts can be used to register alerts.
Alerts interface {
Register(alerts.Alert)
Dismiss(...types.Hash256)
}
// A ChainManager is used to get the current consensus state.
ChainManager interface {
TipState() consensus.State
Subscribe(s modules.ConsensusSetSubscriber, ccID modules.ConsensusChangeID, cancel <-chan struct{}) error
}
// A SectorLocation is a location of a sector within a volume.
SectorLocation struct {
ID int64
Volume int64
Index uint64
Root types.Hash256
}
// A TempSector is a stored sector that is not attached to a contract. It
// will be deleted after the expiration height unless it is appended to a
// contract.
TempSector struct {
Root types.Hash256
Expiration uint64
}
// A SectorReference contains the references to a sector.
SectorReference struct {
Contracts []types.FileContractID `json:"contracts"`
TempStorage int `json:"tempStorage"`
Locks int `json:"locks"`
}
// A VolumeManager manages storage using local volumes.
VolumeManager struct {
cacheHits uint64 // ensure 64-bit alignment on 32-bit systems
cacheMisses uint64
a Alerts
vs VolumeStore
cm ChainManager
log *zap.Logger
recorder *sectorAccessRecorder
tg *threadgroup.ThreadGroup
mu sync.Mutex // protects the following fields
lastCleanup time.Time
volumes map[int64]*volume
// changedVolumes tracks volumes that need to be fsynced
changedVolumes map[int64]bool
cache *lru.Cache[types.Hash256, *[rhp2.SectorSize]byte] // Added cache
}
)
// loadVolumes opens all volumes. Volumes that are already loaded are skipped.
func (vm *VolumeManager) loadVolumes() error {
done, err := vm.tg.Add()
if err != nil {
return fmt.Errorf("failed to add to threadgroup: %w", err)
}
defer done()
volumes, err := vm.vs.Volumes()
if err != nil {
return fmt.Errorf("failed to load volumes: %w", err)
}
vm.mu.Lock()
defer vm.mu.Unlock()
// load the volumes into memory
for _, vol := range volumes {
// if the volume has not been loaded yet, create a new volume
v := vm.volumes[vol.ID]
if v == nil {
v = &volume{
stats: VolumeStats{
Status: VolumeStatusUnavailable,
},
}
vm.volumes[vol.ID] = v
}
if err := v.OpenVolume(vol.LocalPath, false); err != nil {
v.appendError(fmt.Errorf("failed to open volume: %w", err))
vm.log.Error("unable to open volume", zap.Error(err), zap.Int64("id", vol.ID), zap.String("path", vol.LocalPath))
// mark the volume as unavailable
if err := vm.vs.SetAvailable(vol.ID, false); err != nil {
return fmt.Errorf("failed to mark volume '%v' as unavailable: %w", vol.LocalPath, err)
}
// register an alert
vm.a.Register(alerts.Alert{
ID: frand.Entropy256(),
Severity: alerts.SeverityError,
Message: "Failed to open volume",
Data: map[string]any{
"volume": vol.LocalPath,
"error": err.Error(),
},
Timestamp: time.Now(),
})
continue
}
// mark the volume as available
if err := vm.vs.SetAvailable(vol.ID, true); err != nil {
return fmt.Errorf("failed to mark volume '%v' as available: %w", vol.LocalPath, err)
} else if err := v.SetStatus(VolumeStatusReady); err != nil {
return fmt.Errorf("failed to set volume status: %w", err)
}
vm.log.Debug("loaded volume", zap.Int64("id", vol.ID), zap.String("path", vol.LocalPath))
}
return nil
}
// migrateSector migrates a sector to a new location. The sector is read from
// its current location and written to its new location. The volume is
// immediately synced after the sector is written.
func (vm *VolumeManager) migrateSector(loc SectorLocation) error {
// read the sector from the old location
sector, err := vm.Read(loc.Root)
if err != nil {
return fmt.Errorf("failed to read sector: %w", err)
}
// calculate the returned root
root := rhp2.SectorRoot(sector)
// verify the the sector is not corrupt
if root != loc.Root {
return fmt.Errorf("sector corrupt: %v != %v", loc.Root, root)
}
vm.mu.Lock()
vol, ok := vm.volumes[loc.Volume]
vm.mu.Unlock()
if !ok {
return fmt.Errorf("volume %v not found", loc.Volume)
}
// write the sector to the new location and sync the volume
if err := vol.WriteSector(sector, loc.Index); err != nil {
return fmt.Errorf("failed to write sector: %w", err)
} else if err := vol.Sync(); err != nil {
return fmt.Errorf("failed to sync volume: %w", err)
}
return nil
}
// growVolume grows a volume by adding sectors to the end of the volume.
func (vm *VolumeManager) growVolume(ctx context.Context, id int64, volume *volume, oldMaxSectors, newMaxSectors uint64) error {
log := vm.log.Named("grow").With(zap.Int64("volumeID", id), zap.Uint64("start", oldMaxSectors), zap.Uint64("end", newMaxSectors))
if oldMaxSectors > newMaxSectors { // sanity check
log.Panic("old sectors must be less than new sectors")
}
// register an alert
alert := alerts.Alert{
ID: frand.Entropy256(),
Message: "Growing volume",
Severity: alerts.SeverityInfo,
Data: map[string]any{
"volumeID": id,
"oldSectors": oldMaxSectors,
"currentSectors": oldMaxSectors,
"targetSectors": newMaxSectors,
},
Timestamp: time.Now(),
}
vm.a.Register(alert)
// dismiss the alert when the function returns. It is the caller's
// responsibility to register a completion alert
defer vm.a.Dismiss(alert.ID)
for current := oldMaxSectors; current < newMaxSectors; current += resizeBatchSize {
// stop early if the context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
target := current + resizeBatchSize
if target > newMaxSectors {
target = newMaxSectors
}
// truncate the file and add the indices to the volume store. resize is
// done in chunks to prevent holding a lock for too long and to allow
// progress tracking.
if err := volume.Resize(current, target); err != nil {
return fmt.Errorf("failed to expand volume data: %w", err)
} else if err := vm.vs.GrowVolume(id, target); err != nil {
return fmt.Errorf("failed to expand volume metadata: %w", err)
}
log.Debug("expanded volume", zap.Uint64("current", current))
// update the alert
alert.Data["currentSectors"] = target
vm.a.Register(alert)
// sleep to allow other operations to run
time.Sleep(time.Millisecond)
}
return nil
}
// shrinkVolume shrinks a volume by removing sectors from the end of the volume.
func (vm *VolumeManager) shrinkVolume(ctx context.Context, id int64, volume *volume, oldMaxSectors, newMaxSectors uint64) error {
log := vm.log.Named("shrink").With(zap.Int64("volumeID", id), zap.Uint64("start", oldMaxSectors), zap.Uint64("end", newMaxSectors))
if oldMaxSectors <= newMaxSectors {
return errors.New("old sectors must be greater than new sectors")
}
// register the alert
a := alerts.Alert{
ID: frand.Entropy256(),
Message: "Shrinking volume",
Severity: alerts.SeverityInfo,
Data: map[string]any{
"volumeID": id,
"oldSectors": oldMaxSectors,
"currentSectors": oldMaxSectors,
"targetSectors": newMaxSectors,
"migratedSectors": 0,
},
Timestamp: time.Now(),
}
vm.a.Register(a)
// dismiss the alert when the function returns. It is the caller's
// responsibility to register a completion alert
defer vm.a.Dismiss(a.ID)
// migrate any sectors outside of the target range.
var migrated int
migrated, failed, err := vm.vs.MigrateSectors(ctx, id, newMaxSectors, func(newLoc SectorLocation) error {
if err := vm.migrateSector(newLoc); err != nil {
return err
}
migrated++
// update the alert
a.Data["migratedSectors"] = migrated
vm.a.Register(a)
return nil
})
log.Info("migrated sectors", zap.Int("migrated", migrated), zap.Int("failed", failed))
if err != nil {
return err
} else if failed > 0 {
return ErrMigrationFailed
}
for current := oldMaxSectors; current > newMaxSectors; {
// stop early if the context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
var target uint64
if current > resizeBatchSize {
target = current - resizeBatchSize
if target < newMaxSectors {
target = newMaxSectors
}
} else {
target = newMaxSectors
}
if err := vm.vs.ShrinkVolume(id, target); err != nil {
return fmt.Errorf("failed to shrink volume metadata: %w", err)
} else if err := volume.Resize(current, target); err != nil {
return fmt.Errorf("failed to shrink volume data to %v sectors: %w", current, err)
}
log.Debug("shrunk volume", zap.Uint64("current", current))
current = target
// update the alert
a.Data["currentSectors"] = current
vm.a.Register(a)
// sleep to allow other operations to run
time.Sleep(time.Millisecond)
}
return nil
}
// volumeStats returns the stats for a volume. A lock must be held on the volume
// manager before this function is called.
func (vm *VolumeManager) volumeStats(id int64) VolumeStats {
v, ok := vm.volumes[id]
if !ok {
return VolumeStats{
Status: VolumeStatusUnavailable,
}
}
return v.Stats()
}
// Close gracefully shutsdown the volume manager.
func (vm *VolumeManager) Close() error {
// wait for all operations to stop
vm.tg.Stop()
vm.mu.Lock()
defer vm.mu.Unlock()
// flush any pending metrics
vm.recorder.Flush()
// sync and close all open volumes
for id, vol := range vm.volumes {
if err := vol.Sync(); err != nil {
vm.log.Error("failed to sync volume", zap.Int64("id", id), zap.Error(err))
} else if err := vol.Close(); err != nil {
vm.log.Error("failed to close volume", zap.Int64("id", id), zap.Error(err))
}
delete(vm.volumes, id)
}
return nil
}
// SectorReferences returns the references to a sector.
func (vm *VolumeManager) SectorReferences(root types.Hash256) (SectorReference, error) {
return vm.vs.SectorReferences(root)
}
// Usage returns the total and used storage space, in sectors, from the storage manager.
func (vm *VolumeManager) Usage() (usedSectors uint64, totalSectors uint64, err error) {
done, err := vm.tg.Add()
if err != nil {
return 0, 0, err
}
defer done()
return vm.vs.StorageUsage()
}
// Volumes returns a list of all volumes in the storage manager.
func (vm *VolumeManager) Volumes() ([]VolumeMeta, error) {
done, err := vm.tg.Add()
if err != nil {
return nil, err
}
defer done()
volumes, err := vm.vs.Volumes()
if err != nil {
return nil, fmt.Errorf("failed to get volumes: %w", err)
}
vm.mu.Lock()
defer vm.mu.Unlock()
var results []VolumeMeta
for _, vol := range volumes {
meta := VolumeMeta{
Volume: vol,
VolumeStats: vm.volumeStats(vol.ID),
}
results = append(results, meta)
}
return results, nil
}
// Volume returns a volume by its ID.
func (vm *VolumeManager) Volume(id int64) (VolumeMeta, error) {
done, err := vm.tg.Add()
if err != nil {
return VolumeMeta{}, err
}
defer done()
vol, err := vm.vs.Volume(id)
if err != nil {
return VolumeMeta{}, fmt.Errorf("failed to get volume: %w", err)
}
vm.mu.Lock()
defer vm.mu.Unlock()
return VolumeMeta{
Volume: vol,
VolumeStats: vm.volumeStats(vol.ID),
}, nil
}
// AddVolume adds a new volume to the storage manager
func (vm *VolumeManager) AddVolume(ctx context.Context, localPath string, maxSectors uint64, result chan<- error) (Volume, error) {
if maxSectors == 0 {
return Volume{}, errors.New("max sectors must be greater than 0")
}
done, err := vm.tg.Add()
if err != nil {
return Volume{}, err
}
defer done()
// check that the volume file does not already exist
if _, err := os.Stat(localPath); err == nil {
return Volume{}, fmt.Errorf("volume file already exists: %s", localPath)
} else if !errors.Is(err, os.ErrNotExist) {
return Volume{}, fmt.Errorf("failed to stat volume file: %w", err)
}
f, err := os.Create(localPath)
if err != nil {
return Volume{}, fmt.Errorf("failed to create volume file: %w", err)
}
volumeID, err := vm.vs.AddVolume(localPath, false)
if err != nil {
return Volume{}, fmt.Errorf("failed to add volume to store: %w", err)
}
// add the new volume to the volume map
vm.mu.Lock()
vol := &volume{
data: f,
stats: VolumeStats{
Status: VolumeStatusCreating,
},
}
vm.volumes[volumeID] = vol
vm.mu.Unlock()
vm.vs.SetAvailable(volumeID, true)
go func() {
defer vol.SetStatus(VolumeStatusReady)
log := vm.log.Named("initialize").With(zap.Int64("volumeID", volumeID), zap.Uint64("maxSectors", maxSectors))
start := time.Now()
err := vm.growVolume(ctx, volumeID, vol, 0, maxSectors)
alert := alerts.Alert{
ID: frand.Entropy256(),
Data: map[string]interface{}{
"volumeID": volumeID,
"elapsed": time.Since(start),
"target": maxSectors,
},
Timestamp: time.Now(),
}
if err != nil {
log.Error("failed to initialize volume", zap.Error(err))
alert.Message = "Failed to initialize volume"
alert.Severity = alerts.SeverityError
alert.Data["error"] = err.Error()
} else {
alert.Message = "Volume initialized"
alert.Severity = alerts.SeverityInfo
}
vm.a.Register(alert)
select {
case result <- err:
default:
}
}()
return vm.vs.Volume(volumeID)
}
// SetReadOnly sets the read-only status of a volume.
func (vm *VolumeManager) SetReadOnly(id int64, readOnly bool) error {
done, err := vm.tg.Add()
if err != nil {
return err
}
defer done()
// check that the volume is available and not busy
vm.mu.Lock()
vol, ok := vm.volumes[id]
vm.mu.Unlock()
if !ok {
return fmt.Errorf("volume %v not found", id)
} else if vol.Status() != VolumeStatusReady {
return fmt.Errorf("volume is %v", vol.Status())
}
if err := vm.vs.SetReadOnly(id, readOnly); err != nil {
return fmt.Errorf("failed to set volume %v to read-only: %w", id, err)
}
return nil
}
// RemoveVolume removes a volume from the manager.
func (vm *VolumeManager) RemoveVolume(ctx context.Context, id int64, force bool, result chan<- error) error {
log := vm.log.Named("remove").With(zap.Int64("volumeID", id), zap.Bool("force", force))
done, err := vm.tg.Add()
if err != nil {
return err
}
defer done()
vm.mu.Lock()
vol, ok := vm.volumes[id]
vm.mu.Unlock()
if !ok {
return fmt.Errorf("volume %v not found", id)
}
oldStatus := vol.Status()
if err := vol.SetStatus(VolumeStatusRemoving); err != nil {
return fmt.Errorf("failed to set volume status: %w", err)
}
stat, err := vm.vs.Volume(id)
if err != nil {
return fmt.Errorf("failed to get volume: %w", err)
}
// set the volume to read-only to prevent new sectors from being added
if err := vm.vs.SetReadOnly(id, true); err != nil {
return fmt.Errorf("failed to set volume %v to read-only: %w", id, err)
}
alert := alerts.Alert{
ID: frand.Entropy256(),
Message: "Removing volume",
Severity: alerts.SeverityInfo,
Data: map[string]interface{}{
"volumeID": id,
"sectors": stat.TotalSectors,
"used": stat.UsedSectors,
"migrated": 0,
"failed": 0,
},
Timestamp: time.Now(),
}
go func() {
defer vol.SetStatus(oldStatus)
var migrated, failed int
updateRemovalAlert := func(message string, severity alerts.Severity, err error) {
alert.Message = message
alert.Severity = severity
alert.Data["migrated"] = migrated
alert.Data["failed"] = failed
if err != nil {
alert.Data["error"] = err.Error()
}
vm.a.Register(alert)
}
migrated, failed, err := vm.vs.MigrateSectors(ctx, id, 0, func(newLoc SectorLocation) error {
err := vm.migrateSector(newLoc)
if err != nil {
failed++
} else {
migrated++
}
updateRemovalAlert("Removing volume", alerts.SeverityInfo, nil) // error is ignored during migration
return err
})
if err != nil {
log.Error("failed to migrate sectors", zap.Error(err))
// update the alert
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
result <- err
return
} else if !force && failed > 0 {
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, ErrMigrationFailed)
result <- ErrMigrationFailed
return
}
// close the volume and remove it from memory
if err := vol.Close(); err != nil {
log.Error("failed to close volume", zap.Error(err))
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
result <- err
return
} else if err := os.Remove(stat.LocalPath); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Error("failed to remove volume file", zap.Error(err))
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
result <- err
return
}
delete(vm.volumes, id)
// remove the volume from the volume store
if err := vm.vs.RemoveVolume(id, force); err != nil {
log.Error("failed to remove volume", zap.Error(err))
// update the alert
updateRemovalAlert("Failed to remove volume", alerts.SeverityError, err)
result <- err
return
}
updateRemovalAlert("Volume removed", alerts.SeverityInfo, nil)
result <- nil
}()
return nil
}
// ResizeVolume resizes a volume to the specified size.
func (vm *VolumeManager) ResizeVolume(ctx context.Context, id int64, maxSectors uint64, result chan<- error) error {
done, err := vm.tg.Add()
if err != nil {
return err
}
defer done()
stat, err := vm.vs.Volume(id)
if err != nil {
return fmt.Errorf("failed to get volume: %w", err)
}
vm.mu.Lock()
defer vm.mu.Unlock()
vol, ok := vm.volumes[id]
if !ok {
return fmt.Errorf("volume %v not found", id)
}
// check that the volume is not already being resized
if err := vol.SetStatus(VolumeStatusResizing); err != nil {
return fmt.Errorf("failed to set volume status: %w", err)
}
var resetReadOnly bool
if stat.TotalSectors > maxSectors && !stat.ReadOnly {
// set the volume to read-only to prevent new sectors from being added
// while the volume is being shrunk
if err := vm.vs.SetReadOnly(id, true); err != nil {
return fmt.Errorf("failed to set volume %v to read-only: %w", id, err)
}
resetReadOnly = true
}
go func() {
log := vm.log.Named("resize").With(zap.Int64("volumeID", id))
defer func() {
if resetReadOnly {
// reset the volume to read-write
if err := vm.vs.SetReadOnly(id, false); err != nil {
vm.log.Error("failed to set volume to read-write", zap.Error(err))
}
}
vol.SetStatus(VolumeStatusReady)
}()
ctx, cancel, err := vm.tg.AddContext(ctx)
if err != nil {
result <- err
return
}
defer cancel()
start := time.Now()
current := stat.TotalSectors
target := maxSectors
switch {
case current > target:
// volume is shrinking
err = vm.shrinkVolume(ctx, id, vol, stat.TotalSectors, maxSectors)
case current < target:
// volume is growing
err = vm.growVolume(ctx, id, vol, stat.TotalSectors, maxSectors)
}
alert := alerts.Alert{
ID: frand.Entropy256(),
Data: map[string]interface{}{
"volumeID": id,
"elapsed": time.Since(start),
"targetSectors": maxSectors,
},
Timestamp: time.Now(),
}
if err != nil {
log.Error("failed to resize volume", zap.Error(err))
alert.Message = "Volume resize failed"
alert.Severity = alerts.SeverityError
alert.Data["error"] = err.Error()
} else {
alert.Message = "Volume resized"
alert.Severity = alerts.SeverityInfo
}
vm.a.Register(alert)
select {
case result <- err:
default:
}
}()
return nil
}
// RemoveSector deletes a sector's metadata and zeroes its data.
func (vm *VolumeManager) RemoveSector(root types.Hash256) error {
done, err := vm.tg.Add()
if err != nil {
return err
}
defer done()
// get and lock the sector's current location
loc, release, err := vm.vs.SectorLocation(root)
if err != nil {
return fmt.Errorf("failed to locate sector %v: %w", root, err)
}
defer release()
// remove the sector from the volume store
if err := vm.vs.RemoveSector(root); err != nil {
return fmt.Errorf("failed to remove sector %v: %w", root, err)
}
vm.mu.Lock()
defer vm.mu.Unlock()
// get the volume from memory
vol, ok := vm.volumes[loc.Volume]
if !ok {
return fmt.Errorf("volume %v not found", loc.Volume)
}
// zero the sector and immediately sync the volume
var zeroes [rhp2.SectorSize]byte
if err := vol.WriteSector(&zeroes, loc.Index); err != nil {
return fmt.Errorf("failed to zero sector %v: %w", root, err)
} else if err := vol.Sync(); err != nil {
return fmt.Errorf("failed to sync volume %v: %w", loc.Volume, err)
}
// eject the sector from the cache
vm.cache.Remove(root)
return nil
}
// LockSector prevents the sector with the given root from being pruned. If the
// sector does not exist, an error is returned. Release must be called when the
// sector is no longer needed.
func (vm *VolumeManager) LockSector(root types.Hash256) (func() error, error) {
done, err := vm.tg.Add()
if err != nil {
return nil, err
}
defer done()
_, release, err := vm.vs.SectorLocation(root)
return release, err
}
// CacheStats returns the number of cache hits and misses.
func (vm *VolumeManager) CacheStats() (hits, misses uint64) {
return atomic.LoadUint64(&vm.cacheHits), atomic.LoadUint64(&vm.cacheMisses)
}
// Read reads the sector with the given root
func (vm *VolumeManager) Read(root types.Hash256) (*[rhp2.SectorSize]byte, error) {
done, err := vm.tg.Add()
if err != nil {
return nil, err
}
defer done()
// Check the cache first
if sector, ok := vm.cache.Get(root); ok {
vm.recorder.AddCacheHit()
atomic.AddUint64(&vm.cacheHits, 1)
return sector, nil
}
// Cache miss, read from disk
loc, release, err := vm.vs.SectorLocation(root)
if err != nil {
return nil, fmt.Errorf("failed to locate sector: %w", err)
}
defer release()
vm.mu.Lock()
v, ok := vm.volumes[loc.Volume]
if !ok {
vm.mu.Unlock()
return nil, fmt.Errorf("volume %v not found", loc.Volume)
}
vm.mu.Unlock()
sector, err := v.ReadSector(loc.Index)
if err != nil {
stats := v.Stats()
vm.a.Register(alerts.Alert{
ID: v.alertID("read"),
Severity: alerts.SeverityError,
Message: "Failed to read sector",
Data: map[string]interface{}{
"volume": v.Location(),
"failedReads": stats.FailedReads,
"failedWrites": stats.FailedWrites,
"sector": root,
"error": err.Error(),
},
Timestamp: time.Now(),
})
return nil, fmt.Errorf("failed to read sector data: %w", err)
}
// Add sector to cache
vm.cache.Add(root, sector)
vm.recorder.AddCacheMiss()
atomic.AddUint64(&vm.cacheMisses, 1)
vm.recorder.AddRead()
return sector, nil
}
// Sync syncs the data files of changed volumes.
func (vm *VolumeManager) Sync() error {
done, err := vm.tg.Add()
if err != nil {
return err
}
defer done()
vm.mu.Lock()
var toSync []int64
for id := range vm.changedVolumes {
toSync = append(toSync, id)
}
vm.mu.Unlock()
for _, id := range toSync {
vm.mu.Lock()
vol, ok := vm.volumes[id]
vm.mu.Unlock()
if !ok {
continue
}
if err := vol.Sync(); err != nil {
return fmt.Errorf("failed to sync volume %v: %w", id, err)
}
vm.mu.Lock()
delete(vm.changedVolumes, id)
vm.mu.Unlock()
}
return nil
}
// Write writes a sector to a volume. release should only be called after the
// contract roots have been committed to prevent the sector from being deleted.
func (vm *VolumeManager) Write(root types.Hash256, data *[rhp2.SectorSize]byte) (func() error, error) {
done, err := vm.tg.Add()
if err != nil {
return nil, err
}
defer done()
release, err := vm.vs.StoreSector(root, func(loc SectorLocation, exists bool) error {
if exists {
return nil
}
start := time.Now()
vm.mu.Lock()
vol, ok := vm.volumes[loc.Volume]
vm.mu.Unlock()
if !ok {
return fmt.Errorf("volume %v not found", loc.Volume)
}
// write the sector to the volume
if err := vol.WriteSector(data, loc.Index); err != nil {
stats := vol.Stats()
vm.a.Register(alerts.Alert{
ID: vol.alertID("write"),
Severity: alerts.SeverityError,
Message: "Failed to write sector",
Data: map[string]interface{}{
"volume": vol.Location(),
"failedReads": stats.FailedReads,
"failedWrites": stats.FailedWrites,
"sector": root,
"error": err.Error(),
},
Timestamp: time.Now(),
})
return fmt.Errorf("failed to write sector data: %w", err)
}
vm.log.Debug("wrote sector", zap.String("root", root.String()), zap.Int64("volume", loc.Volume), zap.Uint64("index", loc.Index), zap.Duration("elapsed", time.Since(start)))
// Add newly written sector to cache
vm.cache.Add(root, data)
// mark the volume as changed
vm.mu.Lock()
vm.changedVolumes[loc.Volume] = true
vm.mu.Unlock()
return nil
})
if err == nil {
vm.recorder.AddWrite()
}
return release, err
}
// AddTemporarySectors adds sectors to the temporary store. The sectors are not
// referenced by a contract and will be removed at the expiration height.
func (vm *VolumeManager) AddTemporarySectors(sectors []TempSector) error {
if len(sectors) == 0 {
return nil
}
done, err := vm.tg.Add()
if err != nil {
return err
}
defer done()
return vm.vs.AddTemporarySectors(sectors)
}
// ResizeCache resizes the cache to the given size.
func (vm *VolumeManager) ResizeCache(size uint32) {
// Resize the underlying cache data structure
vm.cache.Resize(int(size))
}
// ProcessConsensusChange is called when the consensus set changes.
func (vm *VolumeManager) ProcessConsensusChange(cc modules.ConsensusChange) {
vm.mu.Lock()
defer vm.mu.Unlock()
delta := time.Since(vm.lastCleanup)
if delta < cleanupInterval {
return
}
vm.lastCleanup = time.Now()
go func() {
log := vm.log.Named("cleanup").With(zap.Uint64("height", uint64(cc.BlockHeight)))
if err := vm.vs.ExpireTempSectors(uint64(cc.BlockHeight)); err != nil {
log.Error("failed to expire temp sectors", zap.Error(err))
}
}()
}
// NewVolumeManager creates a new VolumeManager.
func NewVolumeManager(vs VolumeStore, a Alerts, cm ChainManager, log *zap.Logger, sectorCacheSize uint32) (*VolumeManager, error) {
// Initialize cache with LRU eviction and a max capacity of 64
cache, err := lru.New[types.Hash256, *[rhp2.SectorSize]byte](64)
if err != nil {
return nil, fmt.Errorf("failed to initialize cache: %w", err)
}
// resize the cache, prevents an error in lru.New when initializing the
// cache to 0
cache.Resize(int(sectorCacheSize))
vm := &VolumeManager{
vs: vs,
a: a,
cm: cm,
log: log,
recorder: §orAccessRecorder{
store: vs,
log: log.Named("recorder"),
},
volumes: make(map[int64]*volume),
changedVolumes: make(map[int64]bool),
cache: cache,