-
Notifications
You must be signed in to change notification settings - Fork 402
/
fetchpieces.go
210 lines (186 loc) · 5.99 KB
/
fetchpieces.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path"
"strconv"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/context2"
"storj.io/common/pb"
"storj.io/common/uuid"
"storj.io/private/process"
"storj.io/private/version"
"storj.io/storj/private/revocation"
"storj.io/storj/satellite"
"storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/orders"
"storj.io/storj/satellite/satellitedb"
)
func cmdFetchPieces(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
log := zap.L()
streamID, err := uuid.FromString(args[0])
if err != nil {
return errs.New("invalid stream-id (should be in UUID form): %w", err)
}
streamPosition, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return errs.New("stream position must be a number: %w", err)
}
saveDir := args[2]
destDirStat, err := os.Stat(saveDir)
if !destDirStat.IsDir() {
return errs.New("destination dir %q is not a directory", saveDir)
}
identity, err := runCfg.Identity.Load()
if err != nil {
log.Error("Failed to load identity.", zap.Error(err))
return errs.New("Failed to load identity: %+v", err)
}
db, err := satellitedb.Open(ctx, log.Named("db"), runCfg.Database, satellitedb.Options{ApplicationName: "satellite-pieces-fetcher"})
if err != nil {
return errs.New("Error starting master database: %+v", err)
}
defer func() {
err = errs.Combine(err, db.Close())
}()
metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Metainfo.DatabaseURL,
runCfg.Config.Metainfo.Metabase("satellite-pieces-fetcher"))
if err != nil {
return errs.New("Error creating metabase connection: %+v", err)
}
defer func() {
err = errs.Combine(err, metabaseDB.Close())
}()
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config)
if err != nil {
return errs.New("Error creating revocation database: %+v", err)
}
defer func() {
err = errs.Combine(err, revocationDB.Close())
}()
rollupsWriteCache := orders.NewRollupsWriteCache(log.Named("orders-write-cache"), db.Orders(), runCfg.Orders.FlushBatchSize)
defer func() {
err = errs.Combine(err, rollupsWriteCache.CloseAndFlush(context2.WithoutCancellation(ctx)))
}()
peer, err := satellite.NewRepairer(
log,
identity,
metabaseDB,
revocationDB,
db.RepairQueue(),
db.Buckets(),
db.OverlayCache(),
db.NodeEvents(),
db.Reputation(),
db.Containment(),
rollupsWriteCache,
version.Build,
&runCfg.Config,
process.AtomicLevel(cmd),
)
if err != nil {
return err
}
segmentInfo, err := metabaseDB.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{
StreamID: streamID,
Position: metabase.SegmentPositionFromEncoded(streamPosition),
})
if err != nil {
return err
}
pieceInfos, err := peer.SegmentRepairer.AdminFetchPieces(ctx, &segmentInfo, saveDir)
if err != nil {
return err
}
for pieceIndex, pieceInfo := range pieceInfos {
if pieceInfo.GetLimit == nil {
continue
}
log := log.With(zap.Int("piece-index", pieceIndex))
if err := pieceInfo.FetchError; err != nil {
writeErrorMessageToFile(log, err, saveDir, pieceIndex)
}
writeMetaInfoToFile(log, pieceInfo.GetLimit, pieceInfo.OriginalLimit, pieceInfo.Hash, saveDir, pieceIndex)
if pieceInfo.Reader != nil {
writePieceToFile(log, pieceInfo.Reader, saveDir, pieceIndex)
if err := pieceInfo.Reader.Close(); err != nil {
log.Error("could not close piece reader", zap.Error(err))
}
}
}
return nil
}
func writeErrorMessageToFile(log *zap.Logger, err error, saveDir string, pieceIndex int) {
errorMessage := err.Error()
filename := path.Join(saveDir, fmt.Sprintf("piece.%d.error.txt", pieceIndex))
log = log.With(zap.String("file-path", filename))
errorFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0o700)
if err != nil {
log.Error("could not open error file", zap.Error(err))
return
}
defer func() {
if err := errorFile.Close(); err != nil {
log.Error("could not close error file", zap.Error(err))
}
}()
if _, err := errorFile.WriteString(errorMessage); err != nil {
log.Error("could not write to error file", zap.Error(err))
}
}
type pieceMetaInfo struct {
Hash *pb.PieceHash `json:"hash"`
GetLimit *pb.AddressedOrderLimit `json:"get_limit"`
OriginalLimit *pb.OrderLimit `json:"original_limit"`
}
func writeMetaInfoToFile(log *zap.Logger, getLimit *pb.AddressedOrderLimit, originalLimit *pb.OrderLimit, hash *pb.PieceHash, saveDir string, pieceIndex int) {
filename := path.Join(saveDir, fmt.Sprintf("piece.%d.metainfo.json", pieceIndex))
log = log.With(zap.String("file-path", filename))
metaInfoFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o700)
if err != nil {
log.Error("could not open metainfo file", zap.Error(err))
return
}
defer func() {
if err := metaInfoFile.Close(); err != nil {
log.Error("could not close metainfo file", zap.Error(err))
}
}()
metaInfo := pieceMetaInfo{
Hash: hash,
GetLimit: getLimit,
OriginalLimit: originalLimit,
}
metaInfoJSON, err := json.Marshal(&metaInfo)
if err != nil {
log.Error("could not marshal metainfo JSON object", zap.Error(err))
return
}
if _, err := metaInfoFile.Write(metaInfoJSON); err != nil {
log.Error("could not write JSON to metainfo file", zap.Error(err))
}
}
func writePieceToFile(log *zap.Logger, pieceReader io.ReadCloser, saveDir string, pieceIndex int) {
filename := path.Join(saveDir, fmt.Sprintf("piece.%d.contents", pieceIndex))
log = log.With(zap.String("file-path", filename))
contentFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o700)
if err != nil {
log.Error("could not open content file", zap.Error(err))
return
}
defer func() {
if err := contentFile.Close(); err != nil {
log.Error("could not close content file", zap.Error(err))
}
}()
if _, err := io.Copy(contentFile, pieceReader); err != nil {
log.Error("could not copy from piece reader to contents file", zap.Error(err))
}
}