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.

248 lines
6.3 KiB

6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/raft"
  6. "reflect"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  14. "github.com/chrislusf/seaweedfs/weed/storage/types"
  15. "github.com/chrislusf/seaweedfs/weed/topology"
  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. // not atomic but it's okay
  39. if !found && ms.shouldVolumeGrow(req.Option) {
  40. filter.Store(req, nil)
  41. // we have lock called inside vg
  42. go func() {
  43. glog.V(1).Infoln("starting automatic volume grow")
  44. start := time.Now()
  45. _, err := ms.vg.AutomaticGrowByType(req.Option, ms.grpcDialOption, ms.Topo, req.Count)
  46. glog.V(1).Infoln("finished automatic volume grow, cost ", time.Now().Sub(start))
  47. if req.ErrCh != nil {
  48. req.ErrCh <- err
  49. close(req.ErrCh)
  50. }
  51. filter.Delete(req)
  52. }()
  53. } else {
  54. glog.V(4).Infoln("discard volume grow request")
  55. }
  56. }
  57. }()
  58. }
  59. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  60. resp := &master_pb.LookupVolumeResponse{}
  61. volumeLocations := ms.lookupVolumeId(req.VolumeIds, req.Collection)
  62. for _, result := range volumeLocations {
  63. var locations []*master_pb.Location
  64. for _, loc := range result.Locations {
  65. locations = append(locations, &master_pb.Location{
  66. Url: loc.Url,
  67. PublicUrl: loc.PublicUrl,
  68. })
  69. }
  70. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  71. VolumeId: result.VolumeId,
  72. Locations: locations,
  73. Error: result.Error,
  74. })
  75. }
  76. return resp, nil
  77. }
  78. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  79. if !ms.Topo.IsLeader() {
  80. return nil, raft.NotLeaderError
  81. }
  82. if req.Count == 0 {
  83. req.Count = 1
  84. }
  85. if req.Replication == "" {
  86. req.Replication = ms.option.DefaultReplicaPlacement
  87. }
  88. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  89. if err != nil {
  90. return nil, err
  91. }
  92. ttl, err := needle.ReadTTL(req.Ttl)
  93. if err != nil {
  94. return nil, err
  95. }
  96. diskType := types.ToDiskType(req.DiskType)
  97. option := &topology.VolumeGrowOption{
  98. Collection: req.Collection,
  99. ReplicaPlacement: replicaPlacement,
  100. Ttl: ttl,
  101. DiskType: diskType,
  102. Preallocate: ms.preallocateSize,
  103. DataCenter: req.DataCenter,
  104. Rack: req.Rack,
  105. DataNode: req.DataNode,
  106. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  107. }
  108. if ms.shouldVolumeGrow(option) {
  109. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  110. return nil, fmt.Errorf("no free volumes left for " + option.String())
  111. }
  112. ms.vgCh <- &topology.VolumeGrowRequest{
  113. Option: option,
  114. Count: int(req.WritableVolumeCount),
  115. }
  116. }
  117. var (
  118. lastErr error
  119. maxTimeout = time.Second * 10
  120. startTime = time.Now()
  121. )
  122. for time.Now().Sub(startTime) < maxTimeout {
  123. fid, count, dn, err := ms.Topo.PickForWrite(req.Count, option)
  124. if err == nil {
  125. return &master_pb.AssignResponse{
  126. Fid: fid,
  127. Url: dn.Url(),
  128. PublicUrl: dn.PublicUrl,
  129. Count: count,
  130. Auth: string(security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  131. }, nil
  132. }
  133. //glog.V(4).Infoln("waiting for volume growing...")
  134. lastErr = err
  135. time.Sleep(200 * time.Millisecond)
  136. }
  137. return nil, lastErr
  138. }
  139. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  140. if !ms.Topo.IsLeader() {
  141. return nil, raft.NotLeaderError
  142. }
  143. if req.Replication == "" {
  144. req.Replication = ms.option.DefaultReplicaPlacement
  145. }
  146. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  147. if err != nil {
  148. return nil, err
  149. }
  150. ttl, err := needle.ReadTTL(req.Ttl)
  151. if err != nil {
  152. return nil, err
  153. }
  154. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, types.ToDiskType(req.DiskType))
  155. stats := volumeLayout.Stats()
  156. resp := &master_pb.StatisticsResponse{
  157. TotalSize: stats.TotalSize,
  158. UsedSize: stats.UsedSize,
  159. FileCount: stats.FileCount,
  160. }
  161. return resp, nil
  162. }
  163. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  164. if !ms.Topo.IsLeader() {
  165. return nil, raft.NotLeaderError
  166. }
  167. resp := &master_pb.VolumeListResponse{
  168. TopologyInfo: ms.Topo.ToTopologyInfo(),
  169. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  170. }
  171. return resp, nil
  172. }
  173. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  174. if !ms.Topo.IsLeader() {
  175. return nil, raft.NotLeaderError
  176. }
  177. resp := &master_pb.LookupEcVolumeResponse{}
  178. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  179. if !found {
  180. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  181. }
  182. resp.VolumeId = req.VolumeId
  183. for shardId, shardLocations := range ecLocations.Locations {
  184. var locations []*master_pb.Location
  185. for _, dn := range shardLocations {
  186. locations = append(locations, &master_pb.Location{
  187. Url: string(dn.Id()),
  188. PublicUrl: dn.PublicUrl,
  189. })
  190. }
  191. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  192. ShardId: uint32(shardId),
  193. Locations: locations,
  194. })
  195. }
  196. return resp, nil
  197. }
  198. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  199. if !ms.Topo.IsLeader() {
  200. return nil, raft.NotLeaderError
  201. }
  202. resp := &master_pb.VacuumVolumeResponse{}
  203. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), ms.preallocateSize)
  204. return resp, nil
  205. }