You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

6 years ago
6 years ago
  1. package operation
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func WithVolumeServerClient(volumeServer string, fn func(volume_server_pb.VolumeServerClient) error) error {
  12. grpcAddress, err := toVolumeServerGrpcAddress(volumeServer)
  13. if err != nil {
  14. return err
  15. }
  16. grpcConnection, err := util.GrpcDial(grpcAddress)
  17. if err != nil {
  18. return fmt.Errorf("fail to dial %s: %v", grpcAddress, err)
  19. }
  20. defer grpcConnection.Close()
  21. client := volume_server_pb.NewVolumeServerClient(grpcConnection)
  22. return fn(client)
  23. }
  24. func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err error) {
  25. sepIndex := strings.LastIndex(volumeServer, ":")
  26. port, err := strconv.Atoi(volumeServer[sepIndex+1:])
  27. if err != nil {
  28. glog.Errorf("failed to parse volume server address: %v", volumeServer)
  29. return "", err
  30. }
  31. return fmt.Sprintf("%s:%d", volumeServer[0:sepIndex], port+10000), nil
  32. }
  33. func withMasterServerClient(masterServer string, fn func(masterClient master_pb.SeaweedClient) error) error {
  34. grpcConnection, err := util.GrpcDial(masterServer)
  35. if err != nil {
  36. return fmt.Errorf("fail to dial %s: %v", masterServer, err)
  37. }
  38. defer grpcConnection.Close()
  39. client := master_pb.NewSeaweedClient(grpcConnection)
  40. return fn(client)
  41. }