-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd_run.go
294 lines (268 loc) Β· 8 KB
/
cmd_run.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
package main
import (
"context"
"errors"
"fmt"
"math/rand"
"time"
shell "github.com/ipfs/go-ipfs-api"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/dennis-tra/nebula-crawler/pkg/maxmind"
"github.com/dennis-tra/nebula-crawler/pkg/udger"
"github.com/probe-lab/tiros/models"
)
var RunCommand = &cli.Command{
Name: "run",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "websites",
Usage: "Websites to test against. Example: 'ipfs.io' or 'filecoin.io",
EnvVars: []string{"TIROS_RUN_WEBSITES"},
Required: true,
},
&cli.StringFlag{
Name: "region",
Usage: "In which region does this tiros task run in",
EnvVars: []string{"TIROS_RUN_REGION"},
Required: true,
},
&cli.IntSliceFlag{
Name: "settle-times",
Usage: "a list of times to settle in seconds",
EnvVars: []string{"TIROS_RUN_SETTLE_TIMES"},
Value: cli.NewIntSlice(10, 1200),
},
&cli.IntFlag{
Name: "times",
Usage: "number of times to test each URL",
EnvVars: []string{"TIROS_RUN_TIMES"},
Value: 3,
},
&cli.BoolFlag{
Name: "lookup-providers",
Usage: "Whether to lookup website providers",
EnvVars: []string{"TIROS_RUN_LOOKUP_PROVIDERS"},
Value: true,
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "Whether to skip DB interactions",
EnvVars: []string{"TIROS_RUN_DRY_RUN"},
},
&cli.StringFlag{
Name: "db-host",
Usage: "On which host address can this clustertest reach the database",
EnvVars: []string{"TIROS_RUN_DATABASE_HOST"},
},
&cli.IntFlag{
Name: "db-port",
Usage: "On which port can this clustertest reach the database",
EnvVars: []string{"TIROS_RUN_DATABASE_PORT"},
},
&cli.StringFlag{
Name: "db-name",
Usage: "The name of the database to use",
EnvVars: []string{"TIROS_RUN_DATABASE_NAME"},
},
&cli.StringFlag{
Name: "db-password",
Usage: "The password for the database to use",
EnvVars: []string{"TIROS_RUN_DATABASE_PASSWORD"},
},
&cli.StringFlag{
Name: "db-user",
Usage: "The user with which to access the database to use",
EnvVars: []string{"TIROS_RUN_DATABASE_USER"},
},
&cli.StringFlag{
Name: "db-sslmode",
Usage: "The sslmode to use when connecting the the database",
EnvVars: []string{"TIROS_RUN_DATABASE_SSL_MODE"},
},
&cli.StringFlag{
Name: "ipfs-host",
Usage: "host at which to reach the IPFS Gateway",
EnvVars: []string{"TIROS_RUN_IPFS_HOST", "TIROS_RUN_KUBO_HOST" /* <- legacy */},
Value: "localhost",
},
&cli.IntFlag{
Name: "ipfs-api-port",
Usage: "port to reach a Kubo-compatible RPC API",
EnvVars: []string{"TIROS_RUN_IPFS_API_PORT", "TIROS_RUN_KUBO_API_PORT" /* <- legacy */},
Value: 5001,
},
&cli.IntFlag{
Name: "ipfs-gateway-port",
Usage: "port to reach the IPFS Gateway",
EnvVars: []string{"TIROS_RUN_IPFS_GATEWAY_PORT", "TIROS_RUN_KUBO_GATEWAY_PORT" /* <- legacy */},
Value: 8080,
},
&cli.IntFlag{
Name: "chrome-cdp-port",
Usage: "port to reach the Chrome DevTools Protocol port",
EnvVars: []string{"TIROS_RUN_CHROME_CDP_PORT"},
Value: 3000,
},
&cli.IntFlag{
Name: "cpu",
Usage: "CPU resources for this measurement run",
EnvVars: []string{"TIROS_RUN_CPU"},
Value: 2,
},
&cli.IntFlag{
Name: "memory",
Usage: "Memory resources for this measurement run",
EnvVars: []string{"TIROS_RUN_MEMORY"},
Value: 4096,
},
&cli.StringFlag{
Name: "udger-db",
Usage: "Path to the Udger DB",
EnvVars: []string{"TIROS_UDGER_DB_PATH"},
},
&cli.StringFlag{
Name: "ipfs-implementation",
Usage: "Which implementation are we testing (KUBO, HELIA)",
EnvVars: []string{"TIROS_IPFS_IMPLEMENTATION"},
Value: "KUBO",
},
&cli.DurationFlag{
Name: "timeout",
Usage: "The maximum allowed time for this experiment to run (0 no timeout)",
EnvVars: []string{"TIROS_RUN_TIMEOUT"},
Value: time.Duration(0),
},
},
Action: RunAction,
}
type tiros struct {
dbClient IDBClient
ipfs *shell.Shell
dbRun *models.Run
mmClient *maxmind.Client
uClient *udger.Client
}
func RunAction(c *cli.Context) error {
log.Infoln("Starting Tiros run...")
defer log.Infoln("Stopped Tiros run.")
// create global timeout context
timeout := c.Duration("timeout")
if timeout > 0 {
ctx, cancel := context.WithTimeout(c.Context, timeout)
defer cancel()
c.Context = ctx
}
// Initialize database client
var err error
var dbClient IDBClient = DBDummyClient{}
if !c.Bool("dry-run") {
dbClient, err = InitClient(c.Context, c.String("db-host"), c.Int("db-port"), c.String("db-name"), c.String("db-user"), c.String("db-password"), c.String("db-sslmode"))
if err != nil {
return fmt.Errorf("init db connection: %w", err)
}
}
// Initialize ipfs client
var ipfsClient *shell.Shell
if c.Int("ipfs-api-port") != 0 {
ipfsClient = shell.NewShell(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", c.Int("ipfs-api-port")))
}
// Initialize maxmind client
mmClient, err := maxmind.NewClient()
if err != nil {
return fmt.Errorf("new maxmind client: %w", err)
}
// Initialize udger db client
uClient, err := udger.NewClient(c.String("udger-db"))
if err != nil {
return fmt.Errorf("new udger db client: %w", err)
}
// configure tiros struct
t := tiros{
dbClient: dbClient,
ipfs: ipfsClient,
mmClient: mmClient,
uClient: uClient,
}
// Create a measurement run entry in the database. This entry will
// contain information about the measurement configuration.
if _, err := t.InitRun(c); err != nil {
return fmt.Errorf("init run: %w", err)
}
// Before we're completely exiting we "seal" the run entry. Right now, this only means we're setting the
// finished_at timestamp.
defer func() {
if _, err = t.dbClient.SealRun(context.Background(), t.dbRun); err != nil {
log.WithError(err).Warnln("Couldn't seal run")
}
}()
// shuffle websites, so that we have a different order in which we request the websites.
// If we didn't do this a single website would always be requested with a comparatively "cold" ipfs node.
websites := c.StringSlice("websites")
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(websites), func(i, j int) {
websites[i], websites[j] = websites[j], websites[i]
})
log.WithFields(log.Fields{
"websites": websites,
"settleTimes": c.IntSlice("settle-times"),
"times": c.Int("times"),
}).Infoln("Starting run!")
providerResults := make(chan *provider)
probeResults := make(chan *probeResult)
go t.measureWebsites(c, websites, probeResults)
if c.Bool("lookup-providers") {
go t.findAllProviders(c, websites, providerResults)
} else {
close(providerResults)
}
for {
log.Infoln("Awaiting Provider or Probe result...")
select {
case pr, more := <-probeResults:
if !more {
log.Infoln("Probing websites done!")
probeResults = nil
} else {
log.WithField("url", pr.url).Infoln("Handling probe result")
if _, err := t.dbClient.SaveMeasurement(c, t.dbRun, pr); err != nil {
return fmt.Errorf("save measurement: %w", err)
}
}
case pr, more := <-providerResults:
if !more {
log.Infoln("Searching for providers done!")
providerResults = nil
} else {
if errors.Is(pr.err, context.DeadlineExceeded) {
pr.err = context.DeadlineExceeded
}
log.WithError(pr.err).
WithField("peerID", pr.id.String()[:16]).
WithField("website", pr.website).
Infoln("Handling provider result")
_, err := t.dbClient.SaveProvider(c, t.dbRun, pr)
if err != nil {
return fmt.Errorf("save provider: %w", err)
}
}
}
if probeResults == nil && providerResults == nil {
break
}
}
return nil
}
func (t *tiros) InitRun(c *cli.Context) (*models.Run, error) {
version, sha, err := t.ipfs.Version()
if err != nil {
return nil, fmt.Errorf("ipfs api offline: %w", err)
}
ipfsImpl := c.String("ipfs-implementation")
dbRun, err := t.dbClient.InsertRun(c, ipfsImpl, fmt.Sprintf("%s-%s", version, sha))
if err != nil {
return nil, fmt.Errorf("insert run: %w", err)
}
t.dbRun = dbRun
return t.dbRun, nil
}