upload_and_sync.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. package main
  17. import (
  18. "bytes"
  19. "context"
  20. "fmt"
  21. "io/ioutil"
  22. "math/rand"
  23. "os"
  24. "strings"
  25. "sync"
  26. "time"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/metrics"
  29. "github.com/ethereum/go-ethereum/rpc"
  30. "github.com/ethereum/go-ethereum/swarm/api"
  31. "github.com/ethereum/go-ethereum/swarm/storage"
  32. "github.com/ethereum/go-ethereum/swarm/testutil"
  33. "github.com/pborman/uuid"
  34. cli "gopkg.in/urfave/cli.v1"
  35. )
  36. func uploadAndSyncCmd(ctx *cli.Context, tuid string) error {
  37. randomBytes := testutil.RandomBytes(seed, filesize*1000)
  38. errc := make(chan error)
  39. go func() {
  40. errc <- uplaodAndSync(ctx, randomBytes, tuid)
  41. }()
  42. select {
  43. case err := <-errc:
  44. if err != nil {
  45. metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1)
  46. }
  47. return err
  48. case <-time.After(time.Duration(timeout) * time.Second):
  49. metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1)
  50. e := fmt.Errorf("timeout after %v sec", timeout)
  51. // trigger debug functionality on randomBytes
  52. err := trackChunks(randomBytes[:])
  53. if err != nil {
  54. e = fmt.Errorf("%v; triggerChunkDebug failed: %v", e, err)
  55. }
  56. return e
  57. }
  58. }
  59. func trackChunks(testData []byte) error {
  60. log.Warn("Test timed out; running chunk debug sequence")
  61. addrs, err := getAllRefs(testData)
  62. if err != nil {
  63. return err
  64. }
  65. log.Trace("All references retrieved")
  66. for i, ref := range addrs {
  67. log.Trace(fmt.Sprintf("ref %d", i), "ref", ref)
  68. }
  69. // has-chunks
  70. for _, host := range hosts {
  71. httpHost := fmt.Sprintf("ws://%s:%d", host, 8546)
  72. log.Trace("Calling `Has` on host", "httpHost", httpHost)
  73. hostChunks := []string{}
  74. rpcClient, err := rpc.Dial(httpHost)
  75. if err != nil {
  76. log.Trace("Error dialing host", "err", err)
  77. return err
  78. }
  79. log.Trace("rpc dial ok")
  80. var hasInfo []api.HasInfo
  81. err = rpcClient.Call(&hasInfo, "bzz_has", addrs)
  82. if err != nil {
  83. log.Trace("Error calling host", "err", err)
  84. return err
  85. }
  86. log.Trace("rpc call ok")
  87. count := 0
  88. for i, info := range hasInfo {
  89. if i == 0 {
  90. log.Trace("first hasInfo", "addr", info.Addr, "host", host, "i", i)
  91. }
  92. if i == len(hasInfo)-1 {
  93. log.Trace("last hasInfo", "addr", info.Addr, "host", host, "i", i)
  94. }
  95. if info.Has {
  96. hostChunks = append(hostChunks, "1")
  97. } else {
  98. hostChunks = append(hostChunks, "0")
  99. count++
  100. }
  101. }
  102. if count == 0 {
  103. log.Info("host reported to have all chunks", "host", host)
  104. }
  105. log.Trace("chunks", "chunks", strings.Join(hostChunks, ""), "host", host)
  106. }
  107. return nil
  108. }
  109. func getAllRefs(testData []byte) (storage.AddressCollection, error) {
  110. log.Trace("Getting all references for given root hash")
  111. datadir, err := ioutil.TempDir("", "chunk-debug")
  112. if err != nil {
  113. return nil, fmt.Errorf("unable to create temp dir: %v", err)
  114. }
  115. defer os.RemoveAll(datadir)
  116. fileStore, err := storage.NewLocalFileStore(datadir, make([]byte, 32))
  117. if err != nil {
  118. return nil, err
  119. }
  120. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(trackTimeout)*time.Second)
  121. defer cancel()
  122. reader := bytes.NewReader(testData)
  123. return fileStore.GetAllReferences(ctx, reader, false)
  124. }
  125. func uplaodAndSync(c *cli.Context, randomBytes []byte, tuid string) error {
  126. log.Info("uploading to "+httpEndpoint(hosts[0])+" and syncing", "tuid", tuid, "seed", seed)
  127. t1 := time.Now()
  128. hash, err := upload(randomBytes, httpEndpoint(hosts[0]))
  129. if err != nil {
  130. log.Error(err.Error())
  131. return err
  132. }
  133. t2 := time.Since(t1)
  134. metrics.GetOrRegisterResettingTimer("upload-and-sync.upload-time", nil).Update(t2)
  135. fhash, err := digest(bytes.NewReader(randomBytes))
  136. if err != nil {
  137. log.Error(err.Error())
  138. return err
  139. }
  140. log.Info("uploaded successfully", "tuid", tuid, "hash", hash, "took", t2, "digest", fmt.Sprintf("%x", fhash))
  141. time.Sleep(time.Duration(syncDelay) * time.Second)
  142. wg := sync.WaitGroup{}
  143. if single {
  144. randIndex := 1 + rand.Intn(len(hosts)-1)
  145. ruid := uuid.New()[:8]
  146. wg.Add(1)
  147. go func(endpoint string, ruid string) {
  148. for {
  149. start := time.Now()
  150. err := fetch(hash, endpoint, fhash, ruid, tuid)
  151. if err != nil {
  152. continue
  153. }
  154. ended := time.Since(start)
  155. metrics.GetOrRegisterResettingTimer("upload-and-sync.single.fetch-time", nil).Update(ended)
  156. log.Info("fetch successful", "tuid", tuid, "ruid", ruid, "took", ended, "endpoint", endpoint)
  157. wg.Done()
  158. return
  159. }
  160. }(httpEndpoint(hosts[randIndex]), ruid)
  161. } else {
  162. for _, endpoint := range hosts[1:] {
  163. ruid := uuid.New()[:8]
  164. wg.Add(1)
  165. go func(endpoint string, ruid string) {
  166. for {
  167. start := time.Now()
  168. err := fetch(hash, endpoint, fhash, ruid, tuid)
  169. if err != nil {
  170. continue
  171. }
  172. ended := time.Since(start)
  173. metrics.GetOrRegisterResettingTimer("upload-and-sync.each.fetch-time", nil).Update(ended)
  174. log.Info("fetch successful", "tuid", tuid, "ruid", ruid, "took", ended, "endpoint", endpoint)
  175. wg.Done()
  176. return
  177. }
  178. }(httpEndpoint(endpoint), ruid)
  179. }
  180. }
  181. wg.Wait()
  182. log.Info("all hosts synced random file successfully")
  183. return nil
  184. }