github.com/letterj/go-ethereum@v1.8.22-0.20190204142846-520024dfd689/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 "crypto/md5" 21 crand "crypto/rand" 22 "fmt" 23 "io" 24 "math/rand" 25 "sync" 26 "time" 27 28 "github.com/ethereum/go-ethereum/log" 29 "github.com/ethereum/go-ethereum/metrics" 30 "github.com/pborman/uuid" 31 32 cli "gopkg.in/urfave/cli.v1" 33 ) 34 35 func uploadAndSync(c *cli.Context) error { 36 generateEndpoints(scheme, cluster, appName, from, to) 37 seed := int(time.Now().UnixNano() / 1e6) 38 39 log.Info("uploading to "+endpoints[0]+" and syncing", "seed", seed) 40 41 h := md5.New() 42 r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h) 43 44 t1 := time.Now() 45 hash, err := upload(r, filesize*1000, endpoints[0]) 46 if err != nil { 47 log.Error(err.Error()) 48 return err 49 } 50 metrics.GetOrRegisterResettingTimer("upload-and-sync.upload-time", nil).UpdateSince(t1) 51 52 fhash := h.Sum(nil) 53 54 log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash)) 55 56 time.Sleep(time.Duration(syncDelay) * time.Second) 57 58 wg := sync.WaitGroup{} 59 if single { 60 rand.Seed(time.Now().UTC().UnixNano()) 61 randIndex := 1 + rand.Intn(len(endpoints)-1) 62 ruid := uuid.New()[:8] 63 wg.Add(1) 64 go func(endpoint string, ruid string) { 65 for { 66 start := time.Now() 67 err := fetch(hash, endpoint, fhash, ruid) 68 if err != nil { 69 continue 70 } 71 72 metrics.GetOrRegisterResettingTimer("upload-and-sync.single.fetch-time", nil).UpdateSince(start) 73 wg.Done() 74 return 75 } 76 }(endpoints[randIndex], ruid) 77 } else { 78 for _, endpoint := range endpoints[1:] { 79 ruid := uuid.New()[:8] 80 wg.Add(1) 81 go func(endpoint string, ruid string) { 82 for { 83 start := time.Now() 84 err := fetch(hash, endpoint, fhash, ruid) 85 if err != nil { 86 continue 87 } 88 89 metrics.GetOrRegisterResettingTimer("upload-and-sync.each.fetch-time", nil).UpdateSince(start) 90 wg.Done() 91 return 92 } 93 }(endpoint, ruid) 94 } 95 } 96 wg.Wait() 97 log.Info("all endpoints synced random file successfully") 98 99 return nil 100 }