|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "os" |
| 8 | + "sync/atomic" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 12 | + "github.com/aws/aws-sdk-go-v2/config" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 14 | + "golang.org/x/sync/errgroup" |
| 15 | +) |
| 16 | + |
| 17 | +/* |
| 18 | +Dirty script to generate ghost buckets. It performs a lot of CreateBucket and DeleteBucket operations in parallel, for a |
| 19 | +large number of buckets. After a while, it checks if the buckets are ghost buckets and prints a list. It then tries to |
| 20 | +recreate the ghost buckets and checks again. It prints a list of deep ghost buckets which are the buckets that remain in |
| 21 | +the ghost state after trying to recreate them. |
| 22 | +
|
| 23 | +Exanple usage: |
| 24 | +AWS_ACCESS_KEY_ID=accessKey1 AWS_SECRET_ACCESS_KEY=verySecretKey1 S3_ENDPOINT_URL=127.0.0.1:8000 go run main.go |
| 25 | +
|
| 26 | +This should work both locally and remotely. |
| 27 | +
|
| 28 | +*/ |
| 29 | + |
| 30 | +func main() { |
| 31 | + |
| 32 | + nBucket := 500 |
| 33 | + |
| 34 | + // Create an s3 bucket with the name "my-bucket" |
| 35 | + cfg, err := config.LoadDefaultConfig(context.TODO(), |
| 36 | + config.WithRegion("us-east-1"), |
| 37 | + config.WithBaseEndpoint("http://"+os.Getenv("S3_ENDPOINT_URL")+"/"), |
| 38 | + config.WithRetryer(func() aws.Retryer { |
| 39 | + return aws.NopRetryer{} |
| 40 | + }), |
| 41 | + ) |
| 42 | + if err != nil { |
| 43 | + panic("configuration error, " + err.Error()) |
| 44 | + } |
| 45 | + |
| 46 | + client := s3.NewFromConfig(cfg) |
| 47 | + group, _ := errgroup.WithContext(context.Background()) |
| 48 | + group.SetLimit(400) |
| 49 | + |
| 50 | + bucketNumber := atomic.Int32{} |
| 51 | + |
| 52 | + // Generate random prefix |
| 53 | + base := rand.Intn(4e9) |
| 54 | + |
| 55 | + for i := 0; i < nBucket; i++ { |
| 56 | + time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 57 | + group.Go(func() error { |
| 58 | + bucketName := fmt.Sprintf("test-%010d-%010d", base, bucketNumber.Add(1)) |
| 59 | + hammerWithRequests(client, bucketName) |
| 60 | + return nil |
| 61 | + |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + if err := group.Wait(); err != nil { |
| 66 | + fmt.Println("error: ", err) |
| 67 | + } |
| 68 | + |
| 69 | + fmt.Println("waiting for ghost buckets to appear") |
| 70 | + time.Sleep(30 * time.Second) |
| 71 | + fmt.Println("checking for ghost buckets") |
| 72 | + ghostBuckets := make([]bool, nBucket) |
| 73 | + |
| 74 | + for i := 0; i < nBucket; i++ { |
| 75 | + i := i |
| 76 | + group.Go(func() error { |
| 77 | + bucketName := fmt.Sprintf("test-%010d-%010d", base, i) |
| 78 | + fmt.Println("checking bucket for ghostness: ", bucketName) |
| 79 | + ghost, err := isGhostBucket(client, bucketName) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + ghostBuckets[i] = ghost |
| 84 | + return nil |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + if err := group.Wait(); err != nil { |
| 89 | + fmt.Println("error: ", err) |
| 90 | + } |
| 91 | + |
| 92 | + for i, ghost := range ghostBuckets { |
| 93 | + if ghost { |
| 94 | + fmt.Printf("bucketName: %s isGhostBucket: %t\n", fmt.Sprintf("test-%010d-%010d", base, i), ghost) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + bucketNumber.Store(0) |
| 99 | + |
| 100 | + group.SetLimit(100) |
| 101 | + for i := 0; i < nBucket; i++ { |
| 102 | + group.Go(func() error { |
| 103 | + bucketN := bucketNumber.Add(1) - 1 |
| 104 | + if !ghostBuckets[bucketN] { |
| 105 | + return nil |
| 106 | + } |
| 107 | + bucketName := fmt.Sprintf("test-%010d-%010d", base, bucketN) |
| 108 | + fmt.Println("attempting to recreate bucket: ", bucketName) |
| 109 | + for range make([]struct{}, 50) { |
| 110 | + client.CreateBucket(context.TODO(), &s3.CreateBucketInput{ |
| 111 | + Bucket: &bucketName, |
| 112 | + }) |
| 113 | + time.Sleep(100 * time.Millisecond) |
| 114 | + } |
| 115 | + return nil |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + group.Wait() |
| 120 | + |
| 121 | + deepGhostBuckets := make([]bool, nBucket) |
| 122 | + |
| 123 | + for i := 0; i < nBucket; i++ { |
| 124 | + i := i |
| 125 | + group.Go(func() error { |
| 126 | + bucketName := fmt.Sprintf("test-%010d-%010d", base, i) |
| 127 | + ghost, err := isGhostBucket(client, bucketName) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + deepGhostBuckets[i] = ghost |
| 132 | + return nil |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + if err := group.Wait(); err != nil { |
| 137 | + fmt.Println("error: ", err) |
| 138 | + } |
| 139 | + |
| 140 | + for i, ghost := range deepGhostBuckets { |
| 141 | + if ghost { |
| 142 | + fmt.Printf("bucketName: %s isDeepGhostBucket: %t\n", fmt.Sprintf("test-%010d-%010d", base, i), ghost) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func isGhostBucket(client *s3.Client, bucketName string) (bool, error) { |
| 148 | + |
| 149 | + var existsInList, existsInHead bool |
| 150 | + |
| 151 | + out, err := client.ListBuckets(context.Background(), &s3.ListBucketsInput{}) |
| 152 | + if err != nil { |
| 153 | + return false, err |
| 154 | + } |
| 155 | + for _, b := range out.Buckets { |
| 156 | + if *b.Name == bucketName { |
| 157 | + existsInList = true |
| 158 | + } |
| 159 | + } |
| 160 | + _, err = client.HeadBucket(context.Background(), &s3.HeadBucketInput{ |
| 161 | + Bucket: &bucketName, |
| 162 | + }) |
| 163 | + existsInHead = err == nil |
| 164 | + |
| 165 | + return existsInList && !existsInHead, nil |
| 166 | +} |
| 167 | + |
| 168 | +func hammerWithRequests(client *s3.Client, bucketName string) error { |
| 169 | + client.CreateBucket(context.TODO(), &s3.CreateBucketInput{ |
| 170 | + Bucket: &bucketName, |
| 171 | + }) |
| 172 | + group, _ := errgroup.WithContext(context.Background()) |
| 173 | + for i := 0; i < 100; i++ { |
| 174 | + group.Go(func() error { |
| 175 | + time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond) |
| 176 | + ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(1000*time.Millisecond)) |
| 177 | + _, err := client.CreateBucket(ctx, &s3.CreateBucketInput{ |
| 178 | + Bucket: &bucketName, |
| 179 | + }) |
| 180 | + if err != nil { |
| 181 | + fmt.Println("error creating bucket", bucketName, ": ", err) |
| 182 | + } |
| 183 | + return nil |
| 184 | + }) |
| 185 | + group.Go(func() error { |
| 186 | + time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond) |
| 187 | + ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(1000*time.Millisecond)) |
| 188 | + _, err := client.DeleteBucket(ctx, &s3.DeleteBucketInput{ |
| 189 | + Bucket: &bucketName, |
| 190 | + }) |
| 191 | + if err != nil { |
| 192 | + fmt.Println("error deleting bucket", bucketName, ": ", err) |
| 193 | + } |
| 194 | + return nil |
| 195 | + }) |
| 196 | + } |
| 197 | + group.Wait() |
| 198 | + return nil |
| 199 | +} |
0 commit comments