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.

153 lines
4.2 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  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 (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  12. var output *master_pb.StatisticsResponse
  13. err = fs.filer.MasterClient.WithClient(false, func(masterClient master_pb.SeaweedClient) error {
  14. grpcResponse, grpcErr := masterClient.Statistics(context.Background(), &master_pb.StatisticsRequest{
  15. Replication: req.Replication,
  16. Collection: req.Collection,
  17. Ttl: req.Ttl,
  18. DiskType: req.DiskType,
  19. })
  20. if grpcErr != nil {
  21. return grpcErr
  22. }
  23. output = grpcResponse
  24. return nil
  25. })
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &filer_pb.StatisticsResponse{
  30. TotalSize: output.TotalSize,
  31. UsedSize: output.UsedSize,
  32. FileCount: output.FileCount,
  33. }, nil
  34. }
  35. func (fs *FilerServer) Ping(ctx context.Context, req *filer_pb.PingRequest) (resp *filer_pb.PingResponse, pingErr error) {
  36. resp = &filer_pb.PingResponse{}
  37. if req.TargetType == "Filer" {
  38. pingErr = pb.WithFilerClient(false, pb.ServerAddress(req.Target), fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  39. _, err := client.Ping(ctx, &filer_pb.PingRequest{})
  40. return err
  41. })
  42. }
  43. if req.TargetType == "VolumeServer" {
  44. pingErr = pb.WithVolumeServerClient(false, pb.ServerAddress(req.Target), fs.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  45. _, err := client.Ping(ctx, &volume_server_pb.PingRequest{})
  46. return err
  47. })
  48. }
  49. return
  50. }
  51. func (fs *FilerServer) GetFilerConfiguration(ctx context.Context, req *filer_pb.GetFilerConfigurationRequest) (resp *filer_pb.GetFilerConfigurationResponse, err error) {
  52. clusterId, _ := fs.filer.Store.KvGet(context.Background(), []byte("clusterId"))
  53. t := &filer_pb.GetFilerConfigurationResponse{
  54. Masters: pb.ToAddressStringsFromMap(fs.option.Masters),
  55. Collection: fs.option.Collection,
  56. Replication: fs.option.DefaultReplication,
  57. MaxMb: uint32(fs.option.MaxMB),
  58. DirBuckets: fs.filer.DirBucketsPath,
  59. Cipher: fs.filer.Cipher,
  60. Signature: fs.filer.Signature,
  61. MetricsAddress: fs.metricsAddress,
  62. MetricsIntervalSec: int32(fs.metricsIntervalSec),
  63. Version: util.Version(),
  64. ClusterId: string(clusterId),
  65. }
  66. glog.V(4).Infof("GetFilerConfiguration: %v", t)
  67. return t, nil
  68. }
  69. func (fs *FilerServer) KeepConnected(stream filer_pb.SeaweedFiler_KeepConnectedServer) error {
  70. req, err := stream.Recv()
  71. if err != nil {
  72. return err
  73. }
  74. clientName := util.JoinHostPort(req.Name, int(req.GrpcPort))
  75. m := make(map[string]bool)
  76. for _, tp := range req.Resources {
  77. m[tp] = true
  78. }
  79. fs.brokersLock.Lock()
  80. fs.brokers[clientName] = m
  81. glog.V(0).Infof("+ broker %v", clientName)
  82. fs.brokersLock.Unlock()
  83. defer func() {
  84. fs.brokersLock.Lock()
  85. delete(fs.brokers, clientName)
  86. glog.V(0).Infof("- broker %v: %v", clientName, err)
  87. fs.brokersLock.Unlock()
  88. }()
  89. for {
  90. if err := stream.Send(&filer_pb.KeepConnectedResponse{}); err != nil {
  91. glog.V(0).Infof("send broker %v: %+v", clientName, err)
  92. return err
  93. }
  94. // println("replied")
  95. if _, err := stream.Recv(); err != nil {
  96. glog.V(0).Infof("recv broker %v: %v", clientName, err)
  97. return err
  98. }
  99. // println("received")
  100. }
  101. }
  102. func (fs *FilerServer) LocateBroker(ctx context.Context, req *filer_pb.LocateBrokerRequest) (resp *filer_pb.LocateBrokerResponse, err error) {
  103. resp = &filer_pb.LocateBrokerResponse{}
  104. fs.brokersLock.Lock()
  105. defer fs.brokersLock.Unlock()
  106. var localBrokers []*filer_pb.LocateBrokerResponse_Resource
  107. for b, m := range fs.brokers {
  108. if _, found := m[req.Resource]; found {
  109. resp.Found = true
  110. resp.Resources = []*filer_pb.LocateBrokerResponse_Resource{
  111. {
  112. GrpcAddresses: b,
  113. ResourceCount: int32(len(m)),
  114. },
  115. }
  116. return
  117. }
  118. localBrokers = append(localBrokers, &filer_pb.LocateBrokerResponse_Resource{
  119. GrpcAddresses: b,
  120. ResourceCount: int32(len(m)),
  121. })
  122. }
  123. resp.Resources = localBrokers
  124. return resp, nil
  125. }