github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/cmd/swarm/swarm-smoke/upload_and_sync.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bytes" 21 "context" 22 "crypto/md5" 23 crand "crypto/rand" 24 "errors" 25 "fmt" 26 "io" 27 "io/ioutil" 28 "math/rand" 29 "net/http" 30 "net/http/httptrace" 31 "os" 32 "sync" 33 "time" 34 35 "github.com/ebceco/ebc/log" 36 "github.com/ebceco/ebc/metrics" 37 "github.com/ebceco/ebc/swarm/api" 38 "github.com/ebceco/ebc/swarm/api/client" 39 "github.com/ebceco/ebc/swarm/spancontext" 40 "github.com/ebceco/ebc/swarm/testutil" 41 opentracing "github.com/opentracing/opentracing-go" 42 "github.com/pborman/uuid" 43 44 cli "gopkg.in/urfave/cli.v1" 45 ) 46 47 func generateEndpoints(scheme string, cluster string, app string, from int, to int) { 48 if cluster == "prod" { 49 for port := from; port < to; port++ { 50 endpoints = append(endpoints, fmt.Sprintf("%s://%v.swarm-gateways.net", scheme, port)) 51 } 52 } else { 53 for port := from; port < to; port++ { 54 endpoints = append(endpoints, fmt.Sprintf("%s://%s-%v-%s.stg.swarm-gateways.net", scheme, app, port, cluster)) 55 } 56 } 57 58 if includeLocalhost { 59 endpoints = append(endpoints, "http://localhost:8500") 60 } 61 } 62 63 func cliUploadAndSync(c *cli.Context) error { 64 log.PrintOrigins(true) 65 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(verbosity), log.StreamHandler(os.Stdout, log.TerminalFormat(true)))) 66 67 metrics.GetOrRegisterCounter("upload-and-sync", nil).Inc(1) 68 69 errc := make(chan error) 70 go func() { 71 errc <- uploadAndSync(c) 72 }() 73 74 select { 75 case err := <-errc: 76 if err != nil { 77 metrics.GetOrRegisterCounter("upload-and-sync.fail", nil).Inc(1) 78 } 79 return err 80 case <-time.After(time.Duration(timeout) * time.Second): 81 metrics.GetOrRegisterCounter("upload-and-sync.timeout", nil).Inc(1) 82 return fmt.Errorf("timeout after %v sec", timeout) 83 } 84 } 85 86 func uploadAndSync(c *cli.Context) error { 87 defer func(now time.Time) { 88 totalTime := time.Since(now) 89 90 log.Info("total time", "time", totalTime, "kb", filesize) 91 metrics.GetOrRegisterCounter("upload-and-sync.total-time", nil).Inc(int64(totalTime)) 92 }(time.Now()) 93 94 generateEndpoints(scheme, cluster, appName, from, to) 95 seed := int(time.Now().UnixNano() / 1e6) 96 log.Info("uploading to "+endpoints[0]+" and syncing", "seed", seed) 97 98 randomBytes := testutil.RandomBytes(seed, filesize*1000) 99 100 t1 := time.Now() 101 hash, err := upload(&randomBytes, endpoints[0]) 102 if err != nil { 103 log.Error(err.Error()) 104 return err 105 } 106 metrics.GetOrRegisterCounter("upload-and-sync.upload-time", nil).Inc(int64(time.Since(t1))) 107 108 fhash, err := digest(bytes.NewReader(randomBytes)) 109 if err != nil { 110 log.Error(err.Error()) 111 return err 112 } 113 114 log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash)) 115 116 time.Sleep(time.Duration(syncDelay) * time.Second) 117 118 wg := sync.WaitGroup{} 119 if single { 120 rand.Seed(time.Now().UTC().UnixNano()) 121 randIndex := 1 + rand.Intn(len(endpoints)-1) 122 ruid := uuid.New()[:8] 123 wg.Add(1) 124 go func(endpoint string, ruid string) { 125 for { 126 start := time.Now() 127 err := fetch(hash, endpoint, fhash, ruid) 128 fetchTime := time.Since(start) 129 if err != nil { 130 continue 131 } 132 133 metrics.GetOrRegisterMeter("upload-and-sync.single.fetch-time", nil).Mark(int64(fetchTime)) 134 wg.Done() 135 return 136 } 137 }(endpoints[randIndex], ruid) 138 } else { 139 for _, endpoint := range endpoints { 140 ruid := uuid.New()[:8] 141 wg.Add(1) 142 go func(endpoint string, ruid string) { 143 for { 144 start := time.Now() 145 err := fetch(hash, endpoint, fhash, ruid) 146 fetchTime := time.Since(start) 147 if err != nil { 148 continue 149 } 150 151 metrics.GetOrRegisterMeter("upload-and-sync.each.fetch-time", nil).Mark(int64(fetchTime)) 152 wg.Done() 153 return 154 } 155 }(endpoint, ruid) 156 } 157 } 158 wg.Wait() 159 log.Info("all endpoints synced random file successfully") 160 161 return nil 162 } 163 164 // fetch is getting the requested `hash` from the `endpoint` and compares it with the `original` file 165 func fetch(hash string, endpoint string, original []byte, ruid string) error { 166 ctx, sp := spancontext.StartSpan(context.Background(), "upload-and-sync.fetch") 167 defer sp.Finish() 168 169 log.Trace("sleeping", "ruid", ruid) 170 time.Sleep(3 * time.Second) 171 log.Trace("http get request", "ruid", ruid, "api", endpoint, "hash", hash) 172 173 var tn time.Time 174 reqUri := endpoint + "/bzz:/" + hash + "/" 175 req, _ := http.NewRequest("GET", reqUri, nil) 176 177 opentracing.GlobalTracer().Inject( 178 sp.Context(), 179 opentracing.HTTPHeaders, 180 opentracing.HTTPHeadersCarrier(req.Header)) 181 182 trace := client.GetClientTrace("upload-and-sync - http get", "upload-and-sync", ruid, &tn) 183 184 req = req.WithContext(httptrace.WithClientTrace(ctx, trace)) 185 transport := http.DefaultTransport 186 187 //transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} 188 189 tn = time.Now() 190 res, err := transport.RoundTrip(req) 191 if err != nil { 192 log.Error(err.Error(), "ruid", ruid) 193 return err 194 } 195 log.Trace("http get response", "ruid", ruid, "api", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength) 196 197 if res.StatusCode != 200 { 198 err := fmt.Errorf("expected status code %d, got %v", 200, res.StatusCode) 199 log.Warn(err.Error(), "ruid", ruid) 200 return err 201 } 202 203 defer res.Body.Close() 204 205 rdigest, err := digest(res.Body) 206 if err != nil { 207 log.Warn(err.Error(), "ruid", ruid) 208 return err 209 } 210 211 if !bytes.Equal(rdigest, original) { 212 err := fmt.Errorf("downloaded imported file md5=%x is not the same as the generated one=%x", rdigest, original) 213 log.Warn(err.Error(), "ruid", ruid) 214 return err 215 } 216 217 log.Trace("downloaded file matches random file", "ruid", ruid, "len", res.ContentLength) 218 219 return nil 220 } 221 222 // upload is uploading a file `f` to `endpoint` via the `swarm up` cmd 223 func upload(dataBytes *[]byte, endpoint string) (string, error) { 224 swarm := client.NewClient(endpoint) 225 f := &client.File{ 226 ReadCloser: ioutil.NopCloser(bytes.NewReader(*dataBytes)), 227 ManifestEntry: api.ManifestEntry{ 228 ContentType: "text/plain", 229 Mode: 0660, 230 Size: int64(len(*dataBytes)), 231 }, 232 } 233 234 // upload data to bzz:// and retrieve the content-addressed manifest hash, hex-encoded. 235 return swarm.Upload(f, "", false) 236 } 237 238 func digest(r io.Reader) ([]byte, error) { 239 h := md5.New() 240 _, err := io.Copy(h, r) 241 if err != nil { 242 return nil, err 243 } 244 return h.Sum(nil), nil 245 } 246 247 // generates random data in heap buffer 248 func generateRandomData(datasize int) ([]byte, error) { 249 b := make([]byte, datasize) 250 c, err := crand.Read(b) 251 if err != nil { 252 return nil, err 253 } else if c != datasize { 254 return nil, errors.New("short read") 255 } 256 return b, nil 257 }