/
upload.go
219 lines (187 loc) · 5.62 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package upload
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"sort"
"time"
"github.com/ethersphere/beekeeper/pkg/bee"
"github.com/ethersphere/beekeeper/pkg/bee/api"
"github.com/ethersphere/beekeeper/pkg/beekeeper"
"github.com/ethersphere/beekeeper/pkg/logging"
"github.com/ethersphere/beekeeper/pkg/orchestration"
"github.com/ethersphere/beekeeper/pkg/random"
"golang.org/x/sync/errgroup"
)
// Options represents simulation options
type Options struct {
FileCount int64
GasPrice string
MaxFileSize int64
MinFileSize int64
PostageAmount int64
PostageDepth uint64
PostageLabel string
Retries int
RetryDelay time.Duration
Seed int64
Timeout time.Duration
UploadNodeName string
UploadNodePercentage int
SyncUpload bool
}
// NewDefaultOptions returns new default options
func NewDefaultOptions() Options {
return Options{
FileCount: 0,
GasPrice: "",
MaxFileSize: 1048576, // 1mb = 1*1024*1024
MinFileSize: 1048576, // 1mb = 1*1024*1024
PostageAmount: 1000,
PostageDepth: 16,
PostageLabel: "test-label",
Retries: 5,
RetryDelay: 1 * time.Second,
Seed: 0,
Timeout: 1 * time.Minute,
UploadNodeName: "",
UploadNodePercentage: 50,
}
}
// compile simulation whether Upload implements interface
var _ beekeeper.Action = (*Simulation)(nil)
// Simulation instance
type Simulation struct {
logger logging.Logger
}
// NewSimulation returns new upload simulation
func NewSimulation(logger logging.Logger) beekeeper.Action {
return &Simulation{
logger: logger,
}
}
// Run executes upload stress
func (s *Simulation) Run(ctx context.Context, cluster orchestration.Cluster, opts interface{}) (err error) {
s.logger.Info("running upload simulation")
o, ok := opts.(Options)
if !ok {
return fmt.Errorf("invalid options type")
}
if o.MinFileSize > o.MaxFileSize {
return fmt.Errorf("file min size must be less or equal than file max size")
}
clients, err := cluster.NodesClients(ctx)
if err != nil {
return fmt.Errorf("node clients: %w", err)
}
rnd := random.PseudoGenerator(o.Seed)
// choose nodes for upload
nodes := []string{}
if o.UploadNodeName != "" {
nodes = append(nodes, o.UploadNodeName)
} else {
if o.UploadNodePercentage < 0 || o.UploadNodePercentage > 100 {
return fmt.Errorf("upload-nodes-percentage must be number between 0 and 100")
}
allNodes := []string{}
for k := range clients {
allNodes = append(allNodes, k)
}
nodeCount := int(math.Round(float64(len(clients)*o.UploadNodePercentage) / 100))
nodes = randomPick(rnd, allNodes, nodeCount)
}
sort.Strings(nodes)
concurrency := 100
rnds := random.PseudoGenerators(rnd.Int63(), len(nodes))
uGroup := new(errgroup.Group)
uSemaphore := make(chan struct{}, concurrency)
for i, n := range nodes {
i := i
n := n
c := clients[n]
uSemaphore <- struct{}{}
uGroup.Go(func() error {
defer func() {
<-uSemaphore
}()
ctx, ctxCancel := context.WithTimeout(ctx, o.Timeout)
defer ctxCancel()
overlay, err := c.Overlay(ctx)
if err != nil {
return fmt.Errorf("node %s: %w", n, err)
}
var fileCount int64
for {
var tag api.TagResponse
if o.SyncUpload {
if tag, err = c.CreateTag(ctx); err != nil {
return fmt.Errorf("create tag on node %s: %w", n, err)
}
}
// set file size
fileSize := rnds[i].Int63n(o.MaxFileSize-o.MinFileSize+1) + o.MinFileSize
file := bee.NewRandomFile(rnds[i], "filename", fileSize)
var batchID string
retryCount := 0
for {
retryCount++
if retryCount > o.Retries {
return fmt.Errorf("file %s upload to node %s exceeded number of retires", file.Address().String(), overlay)
}
select {
case <-time.After(o.RetryDelay):
case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil
}
return ctx.Err()
}
batchID, err = c.GetOrCreateBatch(ctx, o.PostageAmount, o.PostageDepth, o.PostageLabel)
if err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil
}
return fmt.Errorf("node %s: batch id %w", n, err)
}
if err := c.UploadFile(ctx, &file, api.UploadOptions{BatchID: batchID, Tag: tag.Uid}); err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil
}
s.logger.Infof("error: uploading file %s (size %d) to node %s, batch ID %s: %v", file.Address().String(), fileSize, overlay, batchID, err)
continue
}
break
}
s.logger.Infof("File %s (size %d) uploaded to node %s, batch ID %s", file.Address().String(), fileSize, overlay, batchID)
fileCount++
if o.FileCount > 0 && fileCount >= o.FileCount {
s.logger.Infof("Uploaded %d files to node %s", fileCount, n)
return nil
}
if o.SyncUpload {
if err = c.WaitSync(ctx, tag.Uid); err != nil {
s.logger.Infof("sync with node %s: %v", n, err)
continue
}
s.logger.Infof("file %s synced successfully with node %s", file.Address().String(), n)
}
}
})
}
if err := uGroup.Wait(); err != nil {
return err
}
s.logger.Info("upload stress completed successfully")
return
}
// randomPick randomly picks n elements from the list, and returns lists of picked elements
func randomPick(rnd *rand.Rand, list []string, n int) (picked []string) {
for i := 0; i < n; i++ {
index := rnd.Intn(len(list))
picked = append(picked, list[index])
list = append(list[:index], list[index+1:]...)
}
return
}