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.

245 lines
6.8 KiB

3 years 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.vgCh
  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. if req.ErrCh != nil {
  56. req.ErrCh <- err
  57. close(req.ErrCh)
  58. }
  59. filter.Delete(req)
  60. }()
  61. } else {
  62. glog.V(4).Infoln("discard volume grow request")
  63. time.Sleep(time.Millisecond * 211)
  64. vl.DoneGrowRequest()
  65. }
  66. }
  67. }()
  68. }
  69. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  70. resp := &master_pb.LookupVolumeResponse{}
  71. volumeLocations := ms.lookupVolumeId(req.VolumeOrFileIds, req.Collection)
  72. for _, volumeOrFileId := range req.VolumeOrFileIds {
  73. vid := volumeOrFileId
  74. commaSep := strings.Index(vid, ",")
  75. if commaSep > 0 {
  76. vid = vid[0:commaSep]
  77. }
  78. if result, found := volumeLocations[vid]; found {
  79. var locations []*master_pb.Location
  80. for _, loc := range result.Locations {
  81. locations = append(locations, &master_pb.Location{
  82. Url: loc.Url,
  83. PublicUrl: loc.PublicUrl,
  84. DataCenter: loc.DataCenter,
  85. })
  86. }
  87. var auth string
  88. if commaSep > 0 { // this is a file id
  89. auth = string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, result.VolumeOrFileId))
  90. }
  91. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  92. VolumeOrFileId: result.VolumeOrFileId,
  93. Locations: locations,
  94. Error: result.Error,
  95. Auth: auth,
  96. })
  97. }
  98. }
  99. return resp, nil
  100. }
  101. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  102. if !ms.Topo.IsLeader() {
  103. return nil, raft.NotLeaderError
  104. }
  105. if req.Replication == "" {
  106. req.Replication = ms.option.DefaultReplicaPlacement
  107. }
  108. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  109. if err != nil {
  110. return nil, err
  111. }
  112. ttl, err := needle.ReadTTL(req.Ttl)
  113. if err != nil {
  114. return nil, err
  115. }
  116. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, types.ToDiskType(req.DiskType))
  117. stats := volumeLayout.Stats()
  118. totalSize := ms.Topo.GetDiskUsages().GetMaxVolumeCount() * int64(ms.option.VolumeSizeLimitMB) * 1024 * 1024
  119. resp := &master_pb.StatisticsResponse{
  120. TotalSize: uint64(totalSize),
  121. UsedSize: stats.UsedSize,
  122. FileCount: stats.FileCount,
  123. }
  124. return resp, nil
  125. }
  126. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  127. if !ms.Topo.IsLeader() {
  128. return nil, raft.NotLeaderError
  129. }
  130. resp := &master_pb.VolumeListResponse{
  131. TopologyInfo: ms.Topo.ToTopologyInfo(),
  132. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  133. }
  134. return resp, nil
  135. }
  136. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  137. if !ms.Topo.IsLeader() {
  138. return nil, raft.NotLeaderError
  139. }
  140. resp := &master_pb.LookupEcVolumeResponse{}
  141. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  142. if !found {
  143. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  144. }
  145. resp.VolumeId = req.VolumeId
  146. for shardId, shardLocations := range ecLocations.Locations {
  147. var locations []*master_pb.Location
  148. for _, dn := range shardLocations {
  149. locations = append(locations, &master_pb.Location{
  150. Url: string(dn.Id()),
  151. PublicUrl: dn.PublicUrl,
  152. DataCenter: dn.GetDataCenterId(),
  153. })
  154. }
  155. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  156. ShardId: uint32(shardId),
  157. Locations: locations,
  158. })
  159. }
  160. return resp, nil
  161. }
  162. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  163. if !ms.Topo.IsLeader() {
  164. return nil, raft.NotLeaderError
  165. }
  166. resp := &master_pb.VacuumVolumeResponse{}
  167. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), req.VolumeId, req.Collection, ms.preallocateSize)
  168. return resp, nil
  169. }
  170. func (ms *MasterServer) DisableVacuum(ctx context.Context, req *master_pb.DisableVacuumRequest) (*master_pb.DisableVacuumResponse, error) {
  171. ms.Topo.DisableVacuum()
  172. resp := &master_pb.DisableVacuumResponse{}
  173. return resp, nil
  174. }
  175. func (ms *MasterServer) EnableVacuum(ctx context.Context, req *master_pb.EnableVacuumRequest) (*master_pb.EnableVacuumResponse, error) {
  176. ms.Topo.EnableVacuum()
  177. resp := &master_pb.EnableVacuumResponse{}
  178. return resp, nil
  179. }
  180. func (ms *MasterServer) VolumeMarkReadonly(ctx context.Context, req *master_pb.VolumeMarkReadonlyRequest) (*master_pb.VolumeMarkReadonlyResponse, error) {
  181. if !ms.Topo.IsLeader() {
  182. return nil, raft.NotLeaderError
  183. }
  184. resp := &master_pb.VolumeMarkReadonlyResponse{}
  185. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(req.ReplicaPlacement))
  186. vl := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, needle.LoadTTLFromUint32(req.Ttl), types.ToDiskType(req.DiskType))
  187. dataNodes := ms.Topo.Lookup(req.Collection, needle.VolumeId(req.VolumeId))
  188. for _, dn := range dataNodes {
  189. if dn.Ip == req.Ip && dn.Port == int(req.Port) {
  190. if req.IsReadonly {
  191. vl.SetVolumeReadOnly(dn, needle.VolumeId(req.VolumeId))
  192. } else {
  193. vl.SetVolumeWritable(dn, needle.VolumeId(req.VolumeId))
  194. }
  195. }
  196. }
  197. return resp, nil
  198. }