repeated_vacuum.go 2.1 KB

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