upload_and_sync.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "fmt"
  20. "math/rand"
  21. "sync"
  22. "time"
  23. "github.com/ethereum/go-ethereum/log"
  24. "github.com/ethereum/go-ethereum/metrics"
  25. "github.com/ethereum/go-ethereum/swarm/testutil"
  26. "github.com/pborman/uuid"
  27. cli "gopkg.in/urfave/cli.v1"
  28. )
  29. func uploadAndSyncCmd(ctx *cli.Context, tuid string) error {
  30. randomBytes := testutil.RandomBytes(seed, filesize*1000)
  31. errc := make(chan error)
  32. go func() {
  33. errc <- uplaodAndSync(ctx, randomBytes, tuid)
  34. }()
  35. select {
  36. case err := <-errc:
  37. if err != nil {
  38. metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1)
  39. }
  40. return err
  41. case <-time.After(time.Duration(timeout) * time.Second):
  42. metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1)
  43. // trigger debug functionality on randomBytes
  44. return fmt.Errorf("timeout after %v sec", timeout)
  45. }
  46. }
  47. func uplaodAndSync(c *cli.Context, randomBytes []byte, tuid string) error {
  48. log.Info("uploading to "+httpEndpoint(hosts[0])+" and syncing", "tuid", tuid, "seed", seed)
  49. t1 := time.Now()
  50. hash, err := upload(randomBytes, httpEndpoint(hosts[0]))
  51. if err != nil {
  52. log.Error(err.Error())
  53. return err
  54. }
  55. t2 := time.Since(t1)
  56. metrics.GetOrRegisterResettingTimer("upload-and-sync.upload-time", nil).Update(t2)
  57. fhash, err := digest(bytes.NewReader(randomBytes))
  58. if err != nil {
  59. log.Error(err.Error())
  60. return err
  61. }
  62. log.Info("uploaded successfully", "tuid", tuid, "hash", hash, "took", t2, "digest", fmt.Sprintf("%x", fhash))
  63. time.Sleep(time.Duration(syncDelay) * time.Second)
  64. wg := sync.WaitGroup{}
  65. if single {
  66. randIndex := 1 + rand.Intn(len(hosts)-1)
  67. ruid := uuid.New()[:8]
  68. wg.Add(1)
  69. go func(endpoint string, ruid string) {
  70. for {
  71. start := time.Now()
  72. err := fetch(hash, endpoint, fhash, ruid, tuid)
  73. if err != nil {
  74. continue
  75. }
  76. ended := time.Since(start)
  77. metrics.GetOrRegisterResettingTimer("upload-and-sync.single.fetch-time", nil).Update(ended)
  78. log.Info("fetch successful", "tuid", tuid, "ruid", ruid, "took", ended, "endpoint", endpoint)
  79. wg.Done()
  80. return
  81. }
  82. }(httpEndpoint(hosts[randIndex]), ruid)
  83. } else {
  84. for _, endpoint := range hosts[1:] {
  85. ruid := uuid.New()[:8]
  86. wg.Add(1)
  87. go func(endpoint string, ruid string) {
  88. for {
  89. start := time.Now()
  90. err := fetch(hash, endpoint, fhash, ruid, tuid)
  91. if err != nil {
  92. continue
  93. }
  94. ended := time.Since(start)
  95. metrics.GetOrRegisterResettingTimer("upload-and-sync.each.fetch-time", nil).Update(ended)
  96. log.Info("fetch successful", "tuid", tuid, "ruid", ruid, "took", ended, "endpoint", endpoint)
  97. wg.Done()
  98. return
  99. }
  100. }(httpEndpoint(endpoint), ruid)
  101. }
  102. }
  103. wg.Wait()
  104. log.Info("all hosts synced random file successfully")
  105. return nil
  106. }