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.

285 lines
8.1 KiB

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