|
| 1 | +package substrait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "opti-sql-go/config" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/minio/minio-go" |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +// garbage collection for removing files from s3 storage after expiration |
| 15 | +var dontTouchTestFiles = []string{"country_full.csv", "userdata.parquet", "example.txt", "random_test"} |
| 16 | + |
| 17 | +const ignoreFolder = "result-file-cache" |
| 18 | +const loggerPrefix = "Garbage-Collection" |
| 19 | +const waitTime = time.Second * 5 |
| 20 | + |
| 21 | +func garbageCollection() { |
| 22 | + logger := config.GetLogger() |
| 23 | + logger.Info(fmt.Sprintf("[%v]starting garbage collection, won't touch these files %v", loggerPrefix, dontTouchTestFiles)) |
| 24 | + config := config.GetConfig() |
| 25 | + redisInstance := redis.NewClient(&redis.Options{ |
| 26 | + Addr: config.Server.RedisAddr + ":6379", |
| 27 | + Password: "", // no password |
| 28 | + DB: 0, // use default DB |
| 29 | + Protocol: 2, |
| 30 | + }) |
| 31 | + secretes := config.Secretes |
| 32 | + accessKey := secretes.AccessKey |
| 33 | + secretKey := secretes.SecretKey |
| 34 | + endpoint := secretes.EndpointURL |
| 35 | + bucket := secretes.BucketName |
| 36 | + useSSL := true |
| 37 | + |
| 38 | + client, err := minio.New(endpoint, accessKey, secretKey, useSSL) |
| 39 | + if err != nil { |
| 40 | + logger.Fatal("failed to construct s3 client to delete old files", zap.String("error message", fmt.Sprintf("%v", err))) |
| 41 | + } |
| 42 | + var failedAttempts = 0 |
| 43 | + for { |
| 44 | + start: |
| 45 | + if failedAttempts > 5 { |
| 46 | + logger.Warn("removing files has failed over 5 times, check redis and s3 for issues !!!") |
| 47 | + } |
| 48 | + fmt.Printf("waiting %v minutes before check for files to clear from s3", waitTime.Minutes()) |
| 49 | + time.Sleep(waitTime) |
| 50 | + start := time.Now() |
| 51 | + entries, err := redisInstance.LRange(context.TODO(), ignoreFolder, 0, -1).Result() |
| 52 | + if err != nil { |
| 53 | + logger.Error(fmt.Sprintf("failed to read in files from %v", ignoreFolder), zap.Int("fail counter", failedAttempts)) |
| 54 | + failedAttempts++ |
| 55 | + goto start // try again |
| 56 | + } |
| 57 | + // read all the files in s3 |
| 58 | + doneChan := make(chan struct{}) |
| 59 | + readCount := 0 |
| 60 | + var nonValidFiles []string |
| 61 | + validMap := buildMap(dontTouchTestFiles, entries) |
| 62 | + for fileName := range client.ListObjects(bucket, "", true, doneChan) { |
| 63 | + if !validMap[fileName.Key] { |
| 64 | + nonValidFiles = append(nonValidFiles, fileName.Key) |
| 65 | + } |
| 66 | + readCount++ |
| 67 | + } |
| 68 | + var removedFiles = 0 |
| 69 | + for _, invalidFile := range nonValidFiles { |
| 70 | + err := client.RemoveObject(bucket, invalidFile) |
| 71 | + if err != nil { |
| 72 | + logger.Warn(fmt.Sprintf("error removing %v from s3: %v", invalidFile, err)) |
| 73 | + // log and move on |
| 74 | + } else { |
| 75 | + removedFiles++ |
| 76 | + } |
| 77 | + } |
| 78 | + failedAttempts = 0 // reset failed attempts back to zero |
| 79 | + logger.Info("Garbage Collection metrics", zap.Any("to-keep map", validMap), zap.Int("total-files count", readCount), zap.Int("removed-files count", removedFiles), zap.Any("time-taken", time.Since(start))) |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | +func buildMap(source1 []string, source2 []string) map[string]bool { |
| 85 | + result := make(map[string]bool) |
| 86 | + for _, k := range source1 { |
| 87 | + result[k] = true |
| 88 | + } |
| 89 | + for _, k := range source2 { |
| 90 | + result[k] = true |
| 91 | + } |
| 92 | + return result |
| 93 | +} |
0 commit comments