-
Notifications
You must be signed in to change notification settings - Fork 26
/
updatemirror.go
195 lines (170 loc) · 4.91 KB
/
updatemirror.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
package command
import (
"context"
"errors"
"fmt"
"io/fs"
"reflect"
"runtime"
"strings"
"time"
"github.com/ipni/storetheindex/carstore"
"github.com/ipni/storetheindex/config"
"github.com/ipni/storetheindex/filestore"
"github.com/urfave/cli/v2"
)
var UpdateMirrorCmd = &cli.Command{
Name: "update-mirror",
Usage: "Update CAR files from read mirror to write mirror",
Flags: updateMirrorFlags,
Action: updateMirrorAction,
}
var updateMirrorFlags = []cli.Flag{
&cli.IntFlag{
Name: "concurrency",
Usage: "Number of concurrent operations. Setting 0 uses number of cores.",
Aliases: []string{"c"},
Value: 0,
},
}
type stats struct {
totalSize int64
checked, updates, rdErrs, wrErrs int
}
func updateMirrorAction(cctx *cli.Context) error {
cfgMirror, err := getMirrorConfig()
if err != nil {
return err
}
readStore, writeStore, err := getMirrorStores(cfgMirror)
if err != nil {
return err
}
carSuffix, err := getCARSuffix(cfgMirror)
if err != nil {
return err
}
ctx := cctx.Context
concurrency := cctx.Int("concurrency")
if concurrency < 0 {
return errors.New("concurrency value must be greater than 0")
}
if concurrency == 0 {
concurrency = runtime.NumCPU()
}
fmt.Println("Updating existing CARs in", writeStore.Type(), "write mirror from CARs in", readStore.Type(), "read mirror. concurrency =", concurrency)
statsOut := make(chan stats)
start := time.Now()
filesChan, errs := writeStore.List(ctx, "/", false)
for g := 0; g < concurrency; g++ {
go func(files <-chan *filestore.File) {
var st stats
for wrFile := range files {
if !strings.HasSuffix(wrFile.Path, carSuffix) {
continue
}
st.checked++
rdFile, err := readStore.Head(ctx, wrFile.Path)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
fmt.Println("Cannot get info for file in read mirror:", err)
st.rdErrs++
}
// else CAR file not found in read store.
continue
}
if rdFile.Size != wrFile.Size {
fmt.Println("Updating CAR", wrFile.Path, "to size", rdFile.Size)
_, rc, err := readStore.Get(ctx, rdFile.Path)
if err != nil {
fmt.Println("Cannot read from read mirror:", err)
st.rdErrs++
continue
}
_, err = writeStore.Put(ctx, wrFile.Path, rc)
rc.Close()
if err != nil {
fmt.Println("Failed to update CAR in write mirror:", err)
st.wrErrs++
continue
}
st.updates++
st.totalSize += rdFile.Size
}
}
statsOut <- st
}(filesChan)
}
var statsSum stats
for g := 0; g < concurrency; g++ {
st := <-statsOut
statsSum.checked += st.checked
statsSum.updates += st.updates
statsSum.rdErrs += st.rdErrs
statsSum.wrErrs += st.wrErrs
statsSum.totalSize += st.totalSize
}
elapsed := time.Since(start)
err = <-errs
if err != nil {
if errors.Is(err, context.Canceled) {
fmt.Println("Mirror update canceled")
err = nil
} else {
fmt.Println("Mirror update ended due to error")
err = fmt.Errorf("error listing files in read mirror: %w", err)
}
} else {
fmt.Println("Mirror update complete")
}
fmt.Println(" Elapsed: ", elapsed.String())
fmt.Println(" Checked: ", statsSum.checked)
fmt.Println(" Read errors: ", statsSum.rdErrs)
fmt.Println(" Write errors:", statsSum.wrErrs)
fmt.Println(" Bytes copied:", statsSum.totalSize)
fmt.Println(" Updated CARs:", statsSum.updates)
return err
}
func getMirrorStores(cfgMirror config.Mirror) (filestore.Interface, filestore.Interface, error) {
readStore, err := filestore.MakeFilestore(cfgMirror.Retrieval)
if err != nil {
return nil, nil, fmt.Errorf("cannot create car file storage for read mirror: %w", err)
}
if readStore == nil {
return nil, nil, errors.New("read mirror is enabled with no storage backend")
}
writeStore, err := filestore.MakeFilestore(cfgMirror.Storage)
if err != nil {
return nil, nil, fmt.Errorf("cannot create car file storage for write mirror: %w", err)
}
if writeStore == nil {
return nil, nil, errors.New("write mirror is enabled with no storage backend")
}
return readStore, writeStore, nil
}
func getMirrorConfig() (config.Mirror, error) {
cfg, err := loadConfig("")
if err != nil {
return config.Mirror{}, err
}
cfgMirror := cfg.Ingest.AdvertisementMirror
if !cfgMirror.Write {
return config.Mirror{}, errors.New("write mirror not enabled")
}
if !cfgMirror.Read {
return config.Mirror{}, errors.New("read mirror not enabled")
}
if reflect.DeepEqual(cfgMirror.Storage, cfgMirror.Retrieval) {
return config.Mirror{}, errors.New("read and write mirrors have the same storage")
}
return cfgMirror, nil
}
func getCARSuffix(cfgMirror config.Mirror) (string, error) {
switch cfgMirror.Compress {
case carstore.Gzip, "gz":
return carstore.CarFileSuffix + carstore.GzipFileSuffix, nil
case "", "none", "nil", "null":
return carstore.CarFileSuffix, nil
}
return "", fmt.Errorf("unsupported compression: %s", cfgMirror.Compress)
}