@@ -2,11 +2,13 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"crypto/md5"
6
7
"fmt"
7
8
"io"
8
9
"io/ioutil"
9
10
"net/http"
11
+ "net/http/httptrace"
10
12
"os"
11
13
"os/exec"
12
14
"strings"
@@ -16,8 +18,13 @@ import (
16
18
"github.com/ethereum/go-ethereum/common/hexutil"
17
19
"github.com/ethereum/go-ethereum/crypto"
18
20
"github.com/ethereum/go-ethereum/log"
21
+ "github.com/ethereum/go-ethereum/metrics"
22
+ "github.com/ethereum/go-ethereum/swarm/api/client"
23
+ "github.com/ethereum/go-ethereum/swarm/spancontext"
19
24
"github.com/ethereum/go-ethereum/swarm/storage/feed"
25
+ "github.com/ethereum/go-ethereum/swarm/testutil"
20
26
colorable "github.com/mattn/go-colorable"
27
+ opentracing "github.com/opentracing/opentracing-go"
21
28
"github.com/pborman/uuid"
22
29
cli "gopkg.in/urfave/cli.v1"
23
30
)
@@ -26,11 +33,29 @@ const (
26
33
feedRandomDataLength = 8
27
34
)
28
35
29
- // TODO: retrieve with manifest + extract repeating code
30
36
func cliFeedUploadAndSync (c * cli.Context ) error {
31
-
37
+ metrics . GetOrRegisterCounter ( "feed-and-sync" , nil ). Inc ( 1 )
32
38
log .Root ().SetHandler (log .CallerFileHandler (log .LvlFilterHandler (log .Lvl (verbosity ), log .StreamHandler (colorable .NewColorableStderr (), log .TerminalFormat (true )))))
33
39
40
+ errc := make (chan error )
41
+ go func () {
42
+ errc <- feedUploadAndSync (c )
43
+ }()
44
+
45
+ select {
46
+ case err := <- errc :
47
+ if err != nil {
48
+ metrics .GetOrRegisterCounter ("feed-and-sync.fail" , nil ).Inc (1 )
49
+ }
50
+ return err
51
+ case <- time .After (time .Duration (timeout ) * time .Second ):
52
+ metrics .GetOrRegisterCounter ("feed-and-sync.timeout" , nil ).Inc (1 )
53
+ return fmt .Errorf ("timeout after %v sec" , timeout )
54
+ }
55
+ }
56
+
57
+ // TODO: retrieve with manifest + extract repeating code
58
+ func feedUploadAndSync (c * cli.Context ) error {
34
59
defer func (now time.Time ) { log .Info ("total time" , "time" , time .Since (now ), "size (kb)" , filesize ) }(time .Now ())
35
60
36
61
generateEndpoints (scheme , cluster , appName , from , to )
@@ -204,12 +229,12 @@ func cliFeedUploadAndSync(c *cli.Context) error {
204
229
log .Info ("all endpoints synced random data successfully" )
205
230
206
231
// upload test file
207
- log .Info ("uploading to " + endpoints [0 ] + " and syncing" )
232
+ seed := int (time .Now ().UnixNano () / 1e6 )
233
+ log .Info ("feed uploading to " + endpoints [0 ]+ " and syncing" , "seed" , seed )
208
234
209
- f , cleanup := generateRandomFile (filesize * 1000 )
210
- defer cleanup ()
235
+ randomBytes := testutil .RandomBytes (seed , filesize * 1000 )
211
236
212
- hash , err := upload (f , endpoints [0 ])
237
+ hash , err := upload (& randomBytes , endpoints [0 ])
213
238
if err != nil {
214
239
return err
215
240
}
@@ -218,7 +243,7 @@ func cliFeedUploadAndSync(c *cli.Context) error {
218
243
return err
219
244
}
220
245
multihashHex := hexutil .Encode (hashBytes )
221
- fileHash , err := digest (f )
246
+ fileHash , err := digest (bytes . NewReader ( randomBytes ) )
222
247
if err != nil {
223
248
return err
224
249
}
@@ -284,14 +309,37 @@ func cliFeedUploadAndSync(c *cli.Context) error {
284
309
}
285
310
286
311
func fetchFeed (topic string , user string , endpoint string , original []byte , ruid string ) error {
312
+ ctx , sp := spancontext .StartSpan (context .Background (), "feed-and-sync.fetch" )
313
+ defer sp .Finish ()
314
+
287
315
log .Trace ("sleeping" , "ruid" , ruid )
288
316
time .Sleep (3 * time .Second )
289
317
290
318
log .Trace ("http get request (feed)" , "ruid" , ruid , "api" , endpoint , "topic" , topic , "user" , user )
291
- res , err := http .Get (endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user )
319
+
320
+ var tn time.Time
321
+ reqUri := endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user
322
+ req , _ := http .NewRequest ("GET" , reqUri , nil )
323
+
324
+ opentracing .GlobalTracer ().Inject (
325
+ sp .Context (),
326
+ opentracing .HTTPHeaders ,
327
+ opentracing .HTTPHeadersCarrier (req .Header ))
328
+
329
+ trace := client .GetClientTrace ("feed-and-sync - http get" , "feed-and-sync" , ruid , & tn )
330
+
331
+ req = req .WithContext (httptrace .WithClientTrace (ctx , trace ))
332
+ transport := http .DefaultTransport
333
+
334
+ //transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
335
+
336
+ tn = time .Now ()
337
+ res , err := transport .RoundTrip (req )
292
338
if err != nil {
339
+ log .Error (err .Error (), "ruid" , ruid )
293
340
return err
294
341
}
342
+
295
343
log .Trace ("http get response (feed)" , "ruid" , ruid , "api" , endpoint , "topic" , topic , "user" , user , "code" , res .StatusCode , "len" , res .ContentLength )
296
344
297
345
if res .StatusCode != 200 {
0 commit comments