-
Notifications
You must be signed in to change notification settings - Fork 332
/
abuse_create_branches.go
117 lines (104 loc) · 3.46 KB
/
abuse_create_branches.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
package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/helpers"
"github.com/treeverse/lakefs/pkg/testutil/stress"
)
var abuseCreateBranchesCmd = &cobra.Command{
Use: "create-branches <source ref URI>",
Short: "Create a lot of branches very quickly.",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source ref URI", args[0])
cleanOnly := Must(cmd.Flags().GetBool("clean-only"))
branchPrefix := Must(cmd.Flags().GetString("branch-prefix"))
amount := Must(cmd.Flags().GetInt("amount"))
parallelism := Must(cmd.Flags().GetInt("parallelism"))
client := getClient()
fmt.Println("Source ref:", u)
deleteGen := stress.NewGenerator("delete branch", parallelism)
const paginationAmount = 1000
deleteGen.Setup(func(add stress.GeneratorAddFn) {
currentOffset := apigen.PaginationAfter(branchPrefix)
amount := apigen.PaginationAmount(paginationAmount)
for {
resp, err := client.ListBranchesWithResponse(cmd.Context(), u.Repository, &apigen.ListBranchesParams{
After: ¤tOffset,
Amount: &amount,
})
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
if resp.JSON200 == nil {
Die("Bad response from server", 1)
}
for _, ref := range resp.JSON200.Results {
if !strings.HasPrefix(ref.Id, branchPrefix) {
return
}
add(ref.Id) // this branch should be deleted!
}
pagination := resp.JSON200.Pagination
if !pagination.HasMore {
return
}
currentOffset = apigen.PaginationAfter(pagination.NextOffset)
}
})
// wait for deletes to end
deleteGen.Run(func(input chan string, output chan stress.Result) {
for branch := range input {
start := time.Now()
_, err := client.DeleteBranchWithResponse(cmd.Context(), u.Repository, branch, &apigen.DeleteBranchParams{})
output <- stress.Result{
Error: err,
Took: time.Since(start),
}
}
})
if cleanOnly {
return // done.
}
// start creating branches
generator := stress.NewGenerator("create branch", parallelism, stress.WithSignalHandlersFor(os.Interrupt, syscall.SIGTERM))
// generate create branch requests
generator.Setup(func(add stress.GeneratorAddFn) {
for i := 0; i < amount; i++ {
add(fmt.Sprintf("%s-%d", branchPrefix, i))
}
})
generator.Run(func(input chan string, output chan stress.Result) {
ctx := cmd.Context()
for branch := range input {
start := time.Now()
resp, err := client.CreateBranchWithResponse(
ctx, u.Repository, apigen.CreateBranchJSONRequestBody{
Name: branch,
Source: u.Ref,
})
if err == nil && resp.StatusCode() != http.StatusCreated {
err = helpers.ResponseAsError(resp)
}
output <- stress.Result{
Error: err,
Took: time.Since(start),
}
}
})
},
}
//nolint:gochecknoinits
func init() {
abuseCmd.AddCommand(abuseCreateBranchesCmd)
abuseCreateBranchesCmd.Flags().String("branch-prefix", "abuse-", "prefix to create branches under")
abuseCreateBranchesCmd.Flags().Bool("clean-only", false, "only clean up past runs")
abuseCreateBranchesCmd.Flags().Int("amount", abuseDefaultAmount, "amount of things to do")
abuseCreateBranchesCmd.Flags().Int("parallelism", abuseDefaultParallelism, "amount of things to do in parallel")
}