Skip to content

Commit ffd07e1

Browse files
authored
porep: No-overhead unsealed creation in Filalize (#174)
* porep: No-overhead unsealed creation in Filalize * Fix unsealed path in finalize
1 parent 09dd6ee commit ffd07e1

File tree

2 files changed

+131
-7
lines changed

2 files changed

+131
-7
lines changed

lib/ffi/sdr_funcs.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11+
"strings"
1112

1213
"github.com/KarpelesLab/reflink"
1314
"github.com/ipfs/go-cid"
@@ -477,14 +478,32 @@ func (sb *SealCalls) LocalStorage(ctx context.Context) ([]storiface.StoragePath,
477478
return sb.sectors.localStore.Local(ctx)
478479
}
479480

480-
func (sb *SealCalls) FinalizeSector(ctx context.Context, sector storiface.SectorRef, keepUnsealed bool) error {
481-
alloc := storiface.FTNone
482-
if keepUnsealed {
483-
// note: In Curio we don't write the unsealed file in any of the previous stages, it's only written here from tree-d
484-
alloc = storiface.FTUnsealed
481+
func changePathType(path string, newType storiface.SectorFileType) (string, error) {
482+
// /some/parent/[type]/filename -> /some/parent/[newType]/filename
483+
484+
dir, file := filepath.Split(path)
485+
if dir == "" {
486+
return "", xerrors.Errorf("path has too few components: %s", path)
487+
}
488+
489+
components := strings.Split(strings.TrimRight(dir, string(filepath.Separator)), string(filepath.Separator))
490+
if len(components) < 1 {
491+
return "", xerrors.Errorf("path has too few components: %s", path)
485492
}
486493

487-
sectorPaths, pathIDs, releaseSector, err := sb.sectors.AcquireSector(ctx, nil, sector, storiface.FTCache, alloc, storiface.PathSealing)
494+
newTypeName := newType.String()
495+
496+
components[len(components)-1] = newTypeName
497+
newPath := filepath.Join(append(components, file)...)
498+
499+
if filepath.IsAbs(path) {
500+
newPath = filepath.Join(string(filepath.Separator), newPath)
501+
}
502+
503+
return newPath, nil
504+
}
505+
func (sb *SealCalls) FinalizeSector(ctx context.Context, sector storiface.SectorRef, keepUnsealed bool) error {
506+
sectorPaths, pathIDs, releaseSector, err := sb.sectors.AcquireSector(ctx, nil, sector, storiface.FTCache, storiface.FTNone, storiface.PathSealing)
488507
if err != nil {
489508
return xerrors.Errorf("acquiring sector paths: %w", err)
490509
}
@@ -496,6 +515,21 @@ func (sb *SealCalls) FinalizeSector(ctx context.Context, sector storiface.Sector
496515
}
497516

498517
if keepUnsealed {
518+
// We are going to be moving the unsealed file, no need to allocate storage specifically for it
519+
sectorPaths.Unsealed, err = changePathType(sectorPaths.Cache, storiface.FTUnsealed)
520+
if err != nil {
521+
return xerrors.Errorf("changing path type: %w", err)
522+
}
523+
524+
pathIDs.Unsealed = pathIDs.Cache // this is just an uuid string
525+
526+
defer func() {
527+
// We don't pass FTUnsealed to Acquire, so releaseSector won't declare it. Do it here.
528+
if err := sb.sectors.sindex.StorageDeclareSector(ctx, storiface.ID(pathIDs.Unsealed), sector.ID, storiface.FTUnsealed, true); err != nil {
529+
log.Errorf("declare unsealed sector error: %+v", err)
530+
}
531+
}()
532+
499533
// tree-d contains exactly unsealed data in the prefix, so
500534
// * we move it to a temp file
501535
// * we truncate the temp file to the sector size
@@ -579,7 +613,12 @@ afterUnsealedMove:
579613
return xerrors.Errorf("clearing cache: %w", err)
580614
}
581615

582-
if err := sb.ensureOneCopy(ctx, sector.ID, pathIDs, storiface.FTCache|alloc); err != nil {
616+
maybeUns := storiface.FTUnsealed
617+
if !keepUnsealed {
618+
maybeUns = storiface.FTNone
619+
}
620+
621+
if err := sb.ensureOneCopy(ctx, sector.ID, pathIDs, storiface.FTCache|maybeUns); err != nil {
583622
return xerrors.Errorf("ensure one copy: %w", err)
584623
}
585624

lib/ffi/sdr_funcs_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package ffi
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/filecoin-project/lotus/storage/sealer/storiface"
8+
)
9+
10+
func TestChangePathType(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
path string
14+
newType storiface.SectorFileType
15+
expected string
16+
expectError bool
17+
}{
18+
{
19+
name: "Valid relative path change",
20+
path: filepath.Join("some", "parent", "sealed", "filename"),
21+
newType: storiface.FTCache,
22+
expected: filepath.Join("some", "parent", "cache", "filename"),
23+
expectError: false,
24+
},
25+
{
26+
name: "Valid absolute path change",
27+
path: filepath.Join("/", "some", "parent", "sealed", "filename"),
28+
newType: storiface.FTCache,
29+
expected: filepath.Join("/", "some", "parent", "cache", "filename"),
30+
expectError: false,
31+
},
32+
{
33+
name: "Same type, no change (relative)",
34+
path: filepath.Join("some", "parent", "sealed", "filename"),
35+
newType: storiface.FTSealed,
36+
expected: filepath.Join("some", "parent", "sealed", "filename"),
37+
expectError: false,
38+
},
39+
{
40+
name: "Same type, no change (absolute)",
41+
path: filepath.Join("/", "some", "parent", "sealed", "filename"),
42+
newType: storiface.FTSealed,
43+
expected: filepath.Join("/", "some", "parent", "sealed", "filename"),
44+
expectError: false,
45+
},
46+
{
47+
name: "Too few components",
48+
path: "filename",
49+
newType: storiface.FTCache,
50+
expected: "",
51+
expectError: true,
52+
},
53+
{
54+
name: "Empty path",
55+
path: "",
56+
newType: storiface.FTCache,
57+
expected: "",
58+
expectError: true,
59+
},
60+
}
61+
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
result, err := changePathType(tt.path, tt.newType)
65+
66+
if tt.expectError {
67+
if err == nil {
68+
t.Errorf("Expected an error, but got nil")
69+
}
70+
} else {
71+
if err != nil {
72+
t.Errorf("Unexpected error: %v", err)
73+
}
74+
if result != tt.expected {
75+
t.Errorf("Expected %s, but got %s", tt.expected, result)
76+
}
77+
78+
// Check if the absolute/relative nature of the path is preserved
79+
if filepath.IsAbs(tt.path) != filepath.IsAbs(result) {
80+
t.Errorf("Absolute/relative nature of path not preserved. Original: %s, Result: %s", tt.path, result)
81+
}
82+
}
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)