repeated_vacuum.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "log"
  7. "math/rand"
  8. "time"
  9. "context"
  10. "google.golang.org/grpc"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/security"
  13. "github.com/seaweedfs/seaweedfs/weed/util"
  14. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  15. )
  16. var (
  17. master = flag.String("master", "127.0.0.1:9333", "the master server")
  18. repeat = flag.Int("n", 5, "repeat how many times")
  19. garbageThreshold = flag.Float64("garbageThreshold", 0.3, "garbageThreshold")
  20. replication = flag.String("replication", "", "replication 000, 001, 002, etc")
  21. )
  22. func main() {
  23. flag.Parse()
  24. util_http.InitGlobalHttpClient()
  25. util.LoadSecurityConfiguration()
  26. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  27. genFile(grpcDialOption, 0)
  28. go func() {
  29. for {
  30. println("vacuum threshold", *garbageThreshold)
  31. _, _, err := util_http.Get(fmt.Sprintf("http://%s/vol/vacuum?garbageThreshold=%f", pb.ServerAddress(*master).ToHttpAddress(), *garbageThreshold))
  32. if err != nil {
  33. log.Fatalf("vacuum: %v", err)
  34. }
  35. time.Sleep(time.Second)
  36. }
  37. }()
  38. for i := 0; i < *repeat; i++ {
  39. // create 2 files, and delete one of them
  40. assignResult, targetUrl := genFile(grpcDialOption, i)
  41. util_http.Delete(targetUrl, string(assignResult.Auth))
  42. }
  43. }
  44. func genFile(grpcDialOption grpc.DialOption, i int) (*operation.AssignResult, string) {
  45. assignResult, err := operation.Assign(func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*master) }, grpcDialOption, &operation.VolumeAssignRequest{
  46. Count: 1,
  47. Replication: *replication,
  48. })
  49. if err != nil {
  50. log.Fatalf("assign: %v", err)
  51. }
  52. data := make([]byte, 1024)
  53. rand.Read(data)
  54. targetUrl := fmt.Sprintf("http://%s/%s", assignResult.Url, assignResult.Fid)
  55. uploadOption := &operation.UploadOption{
  56. UploadUrl: targetUrl,
  57. Filename: fmt.Sprintf("test%d", i),
  58. Cipher: false,
  59. IsInputCompressed: true,
  60. MimeType: "bench/test",
  61. PairMap: nil,
  62. Jwt: assignResult.Auth,
  63. }
  64. uploader, err := operation.NewUploader()
  65. if err != nil {
  66. log.Fatalf("upload: %v", err)
  67. }
  68. _, err = uploader.UploadData(data, uploadOption)
  69. if err != nil {
  70. log.Fatalf("upload: %v", err)
  71. }
  72. return assignResult, targetUrl
  73. }