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.

68 lines
2.2 KiB

5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
  1. package operation
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "strconv"
  6. "strings"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. )
  14. func WithVolumeServerClient(volumeServer string, grpcDialOption grpc.DialOption, fn func(volume_server_pb.VolumeServerClient) error) error {
  15. grpcAddress, err := toVolumeServerGrpcAddress(volumeServer)
  16. if err != nil {
  17. return fmt.Errorf("failed to parse volume server %v: %v", volumeServer, err)
  18. }
  19. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  20. client := volume_server_pb.NewVolumeServerClient(grpcConnection)
  21. return fn(client)
  22. }, grpcAddress, grpcDialOption)
  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 util.JoinHostPort(volumeServer[0:sepIndex], port+10000), nil
  32. }
  33. func WithMasterServerClient(masterServer string, grpcDialOption grpc.DialOption, fn func(masterClient master_pb.SeaweedClient) error) error {
  34. masterGrpcAddress, parseErr := pb.ParseServerToGrpcAddress(masterServer)
  35. if parseErr != nil {
  36. return fmt.Errorf("failed to parse master %v: %v", masterServer, parseErr)
  37. }
  38. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  39. client := master_pb.NewSeaweedClient(grpcConnection)
  40. return fn(client)
  41. }, masterGrpcAddress, grpcDialOption)
  42. }
  43. func WithFilerServerClient(filerServer string, grpcDialOption grpc.DialOption, fn func(masterClient filer_pb.SeaweedFilerClient) error) error {
  44. filerGrpcAddress, parseErr := pb.ParseServerToGrpcAddress(filerServer)
  45. if parseErr != nil {
  46. return fmt.Errorf("failed to parse filer %v: %v", filerGrpcAddress, parseErr)
  47. }
  48. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  49. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  50. return fn(client)
  51. }, filerGrpcAddress, grpcDialOption)
  52. }