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.

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