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.

178 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. FilerGroup: fs.option.FilerGroup,
  90. }
  91. glog.V(4).Infof("GetFilerConfiguration: %v", t)
  92. return t, nil
  93. }
  94. func (fs *FilerServer) KeepConnected(stream filer_pb.SeaweedFiler_KeepConnectedServer) error {
  95. req, err := stream.Recv()
  96. if err != nil {
  97. return err
  98. }
  99. clientName := util.JoinHostPort(req.Name, int(req.GrpcPort))
  100. m := make(map[string]bool)
  101. for _, tp := range req.Resources {
  102. m[tp] = true
  103. }
  104. fs.brokersLock.Lock()
  105. fs.brokers[clientName] = m
  106. glog.V(0).Infof("+ broker %v", clientName)
  107. fs.brokersLock.Unlock()
  108. defer func() {
  109. fs.brokersLock.Lock()
  110. delete(fs.brokers, clientName)
  111. glog.V(0).Infof("- broker %v: %v", clientName, err)
  112. fs.brokersLock.Unlock()
  113. }()
  114. for {
  115. if err := stream.Send(&filer_pb.KeepConnectedResponse{}); err != nil {
  116. glog.V(0).Infof("send broker %v: %+v", clientName, err)
  117. return err
  118. }
  119. // println("replied")
  120. if _, err := stream.Recv(); err != nil {
  121. glog.V(0).Infof("recv broker %v: %v", clientName, err)
  122. return err
  123. }
  124. // println("received")
  125. }
  126. }
  127. func (fs *FilerServer) LocateBroker(ctx context.Context, req *filer_pb.LocateBrokerRequest) (resp *filer_pb.LocateBrokerResponse, err error) {
  128. resp = &filer_pb.LocateBrokerResponse{}
  129. fs.brokersLock.Lock()
  130. defer fs.brokersLock.Unlock()
  131. var localBrokers []*filer_pb.LocateBrokerResponse_Resource
  132. for b, m := range fs.brokers {
  133. if _, found := m[req.Resource]; found {
  134. resp.Found = true
  135. resp.Resources = []*filer_pb.LocateBrokerResponse_Resource{
  136. {
  137. GrpcAddresses: b,
  138. ResourceCount: int32(len(m)),
  139. },
  140. }
  141. return
  142. }
  143. localBrokers = append(localBrokers, &filer_pb.LocateBrokerResponse_Resource{
  144. GrpcAddresses: b,
  145. ResourceCount: int32(len(m)),
  146. })
  147. }
  148. resp.Resources = localBrokers
  149. return resp, nil
  150. }