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.

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