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.

177 lines
5.0 KiB

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