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.

241 lines
6.8 KiB

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