master_grpc_server_volume.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/seaweedfs/raft"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/security"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  16. )
  17. func (ms *MasterServer) ProcessGrowRequest() {
  18. go func() {
  19. filter := sync.Map{}
  20. for {
  21. req, ok := <-ms.volumeGrowthRequestChan
  22. if !ok {
  23. break
  24. }
  25. if !ms.Topo.IsLeader() {
  26. //discard buffered requests
  27. time.Sleep(time.Second * 1)
  28. continue
  29. }
  30. // filter out identical requests being processed
  31. found := false
  32. filter.Range(func(k, v interface{}) bool {
  33. if reflect.DeepEqual(k, req) {
  34. found = true
  35. }
  36. return !found
  37. })
  38. option := req.Option
  39. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  40. // not atomic but it's okay
  41. if !found && vl.ShouldGrowVolumes(option) {
  42. filter.Store(req, nil)
  43. // we have lock called inside vg
  44. go func() {
  45. glog.V(1).Infoln("starting automatic volume grow")
  46. start := time.Now()
  47. newVidLocations, err := ms.vg.AutomaticGrowByType(req.Option, ms.grpcDialOption, ms.Topo, req.Count)
  48. glog.V(1).Infoln("finished automatic volume grow, cost ", time.Now().Sub(start))
  49. if err == nil {
  50. for _, newVidLocation := range newVidLocations {
  51. ms.broadcastToClients(&master_pb.KeepConnectedResponse{VolumeLocation: newVidLocation})
  52. }
  53. }
  54. vl.DoneGrowRequest()
  55. filter.Delete(req)
  56. }()
  57. } else {
  58. glog.V(4).Infoln("discard volume grow request")
  59. time.Sleep(time.Millisecond * 211)
  60. vl.DoneGrowRequest()
  61. }
  62. }
  63. }()
  64. }
  65. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  66. resp := &master_pb.LookupVolumeResponse{}
  67. volumeLocations := ms.lookupVolumeId(req.VolumeOrFileIds, req.Collection)
  68. for _, volumeOrFileId := range req.VolumeOrFileIds {
  69. vid := volumeOrFileId
  70. commaSep := strings.Index(vid, ",")
  71. if commaSep > 0 {
  72. vid = vid[0:commaSep]
  73. }
  74. if result, found := volumeLocations[vid]; found {
  75. var locations []*master_pb.Location
  76. for _, loc := range result.Locations {
  77. locations = append(locations, &master_pb.Location{
  78. Url: loc.Url,
  79. PublicUrl: loc.PublicUrl,
  80. DataCenter: loc.DataCenter,
  81. })
  82. }
  83. var auth string
  84. if commaSep > 0 { // this is a file id
  85. auth = string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, result.VolumeOrFileId))
  86. }
  87. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  88. VolumeOrFileId: result.VolumeOrFileId,
  89. Locations: locations,
  90. Error: result.Error,
  91. Auth: auth,
  92. })
  93. }
  94. }
  95. return resp, nil
  96. }
  97. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  98. if !ms.Topo.IsLeader() {
  99. return nil, raft.NotLeaderError
  100. }
  101. if req.Replication == "" {
  102. req.Replication = ms.option.DefaultReplicaPlacement
  103. }
  104. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  105. if err != nil {
  106. return nil, err
  107. }
  108. ttl, err := needle.ReadTTL(req.Ttl)
  109. if err != nil {
  110. return nil, err
  111. }
  112. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, types.ToDiskType(req.DiskType))
  113. stats := volumeLayout.Stats()
  114. totalSize := ms.Topo.GetDiskUsages().GetMaxVolumeCount() * int64(ms.option.VolumeSizeLimitMB) * 1024 * 1024
  115. resp := &master_pb.StatisticsResponse{
  116. TotalSize: uint64(totalSize),
  117. UsedSize: stats.UsedSize,
  118. FileCount: stats.FileCount,
  119. }
  120. return resp, nil
  121. }
  122. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  123. if !ms.Topo.IsLeader() {
  124. return nil, raft.NotLeaderError
  125. }
  126. resp := &master_pb.VolumeListResponse{
  127. TopologyInfo: ms.Topo.ToTopologyInfo(),
  128. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  129. }
  130. return resp, nil
  131. }
  132. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  133. if !ms.Topo.IsLeader() {
  134. return nil, raft.NotLeaderError
  135. }
  136. resp := &master_pb.LookupEcVolumeResponse{}
  137. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  138. if !found {
  139. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  140. }
  141. resp.VolumeId = req.VolumeId
  142. for shardId, shardLocations := range ecLocations.Locations {
  143. var locations []*master_pb.Location
  144. for _, dn := range shardLocations {
  145. locations = append(locations, &master_pb.Location{
  146. Url: string(dn.Id()),
  147. PublicUrl: dn.PublicUrl,
  148. DataCenter: dn.GetDataCenterId(),
  149. })
  150. }
  151. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  152. ShardId: uint32(shardId),
  153. Locations: locations,
  154. })
  155. }
  156. return resp, nil
  157. }
  158. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  159. if !ms.Topo.IsLeader() {
  160. return nil, raft.NotLeaderError
  161. }
  162. resp := &master_pb.VacuumVolumeResponse{}
  163. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), req.VolumeId, req.Collection, ms.preallocateSize)
  164. return resp, nil
  165. }
  166. func (ms *MasterServer) DisableVacuum(ctx context.Context, req *master_pb.DisableVacuumRequest) (*master_pb.DisableVacuumResponse, error) {
  167. ms.Topo.DisableVacuum()
  168. resp := &master_pb.DisableVacuumResponse{}
  169. return resp, nil
  170. }
  171. func (ms *MasterServer) EnableVacuum(ctx context.Context, req *master_pb.EnableVacuumRequest) (*master_pb.EnableVacuumResponse, error) {
  172. ms.Topo.EnableVacuum()
  173. resp := &master_pb.EnableVacuumResponse{}
  174. return resp, nil
  175. }
  176. func (ms *MasterServer) VolumeMarkReadonly(ctx context.Context, req *master_pb.VolumeMarkReadonlyRequest) (*master_pb.VolumeMarkReadonlyResponse, error) {
  177. if !ms.Topo.IsLeader() {
  178. return nil, raft.NotLeaderError
  179. }
  180. resp := &master_pb.VolumeMarkReadonlyResponse{}
  181. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(req.ReplicaPlacement))
  182. vl := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, needle.LoadTTLFromUint32(req.Ttl), types.ToDiskType(req.DiskType))
  183. dataNodes := ms.Topo.Lookup(req.Collection, needle.VolumeId(req.VolumeId))
  184. for _, dn := range dataNodes {
  185. if dn.Ip == req.Ip && dn.Port == int(req.Port) {
  186. if req.IsReadonly {
  187. vl.SetVolumeReadOnly(dn, needle.VolumeId(req.VolumeId))
  188. } else {
  189. vl.SetVolumeWritable(dn, needle.VolumeId(req.VolumeId))
  190. }
  191. }
  192. }
  193. return resp, nil
  194. }