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.

274 lines
7.2 KiB

3 years ago
3 years ago
6 years ago
6 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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. "strings"
  8. "sync"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  15. "github.com/chrislusf/seaweedfs/weed/storage/types"
  16. "github.com/chrislusf/seaweedfs/weed/topology"
  17. )
  18. func (ms *MasterServer) ProcessGrowRequest() {
  19. go func() {
  20. filter := sync.Map{}
  21. for {
  22. req, ok := <-ms.vgCh
  23. if !ok {
  24. break
  25. }
  26. if !ms.Topo.IsLeader() {
  27. //discard buffered requests
  28. time.Sleep(time.Second * 1)
  29. continue
  30. }
  31. // filter out identical requests being processed
  32. found := false
  33. filter.Range(func(k, v interface{}) bool {
  34. if reflect.DeepEqual(k, req) {
  35. found = true
  36. }
  37. return !found
  38. })
  39. option := req.Option
  40. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  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. _, 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. vl.DoneGrowRequest()
  51. if req.ErrCh != nil {
  52. req.ErrCh <- err
  53. close(req.ErrCh)
  54. }
  55. filter.Delete(req)
  56. }()
  57. } else {
  58. glog.V(4).Infoln("discard volume grow request")
  59. }
  60. }
  61. }()
  62. }
  63. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  64. resp := &master_pb.LookupVolumeResponse{}
  65. volumeLocations := ms.lookupVolumeId(req.VolumeOrFileIds, req.Collection)
  66. for _, result := range volumeLocations {
  67. var locations []*master_pb.Location
  68. for _, loc := range result.Locations {
  69. locations = append(locations, &master_pb.Location{
  70. Url: loc.Url,
  71. PublicUrl: loc.PublicUrl,
  72. })
  73. }
  74. var auth string
  75. if strings.Contains(result.VolumeOrFileId, ",") { // this is a file id
  76. auth = string(security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, result.VolumeOrFileId))
  77. }
  78. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  79. VolumeOrFileId: result.VolumeOrFileId,
  80. Locations: locations,
  81. Error: result.Error,
  82. Auth: auth,
  83. })
  84. }
  85. return resp, nil
  86. }
  87. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  88. if !ms.Topo.IsLeader() {
  89. return nil, raft.NotLeaderError
  90. }
  91. if req.Count == 0 {
  92. req.Count = 1
  93. }
  94. if req.Replication == "" {
  95. req.Replication = ms.option.DefaultReplicaPlacement
  96. }
  97. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  98. if err != nil {
  99. return nil, err
  100. }
  101. ttl, err := needle.ReadTTL(req.Ttl)
  102. if err != nil {
  103. return nil, err
  104. }
  105. diskType := types.ToDiskType(req.DiskType)
  106. option := &topology.VolumeGrowOption{
  107. Collection: req.Collection,
  108. ReplicaPlacement: replicaPlacement,
  109. Ttl: ttl,
  110. DiskType: diskType,
  111. Preallocate: ms.preallocateSize,
  112. DataCenter: req.DataCenter,
  113. Rack: req.Rack,
  114. DataNode: req.DataNode,
  115. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  116. }
  117. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  118. if !vl.HasGrowRequest() && vl.ShouldGrowVolumes(option) {
  119. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  120. return nil, fmt.Errorf("no free volumes left for " + option.String())
  121. }
  122. vl.AddGrowRequest()
  123. ms.vgCh <- &topology.VolumeGrowRequest{
  124. Option: option,
  125. Count: int(req.WritableVolumeCount),
  126. }
  127. }
  128. var (
  129. lastErr error
  130. maxTimeout = time.Second * 10
  131. startTime = time.Now()
  132. )
  133. for time.Now().Sub(startTime) < maxTimeout {
  134. fid, count, dnList, err := ms.Topo.PickForWrite(req.Count, option)
  135. if err == nil {
  136. dn := dnList.Head()
  137. var replicas []*master_pb.Location
  138. for _, r := range dnList.Rest() {
  139. replicas = append(replicas, &master_pb.Location{
  140. Url: r.Url(),
  141. PublicUrl: r.PublicUrl,
  142. GrpcPort: uint32(r.GrpcPort),
  143. })
  144. }
  145. return &master_pb.AssignResponse{
  146. Fid: fid,
  147. Location: &master_pb.Location{
  148. Url: dn.Url(),
  149. PublicUrl: dn.PublicUrl,
  150. GrpcPort: uint32(dn.GrpcPort),
  151. },
  152. Count: count,
  153. Auth: string(security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  154. Replicas: replicas,
  155. }, nil
  156. }
  157. //glog.V(4).Infoln("waiting for volume growing...")
  158. lastErr = err
  159. time.Sleep(200 * time.Millisecond)
  160. }
  161. return nil, lastErr
  162. }
  163. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  164. if !ms.Topo.IsLeader() {
  165. return nil, raft.NotLeaderError
  166. }
  167. if req.Replication == "" {
  168. req.Replication = ms.option.DefaultReplicaPlacement
  169. }
  170. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  171. if err != nil {
  172. return nil, err
  173. }
  174. ttl, err := needle.ReadTTL(req.Ttl)
  175. if err != nil {
  176. return nil, err
  177. }
  178. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, types.ToDiskType(req.DiskType))
  179. stats := volumeLayout.Stats()
  180. resp := &master_pb.StatisticsResponse{
  181. TotalSize: stats.TotalSize,
  182. UsedSize: stats.UsedSize,
  183. FileCount: stats.FileCount,
  184. }
  185. return resp, nil
  186. }
  187. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  188. if !ms.Topo.IsLeader() {
  189. return nil, raft.NotLeaderError
  190. }
  191. resp := &master_pb.VolumeListResponse{
  192. TopologyInfo: ms.Topo.ToTopologyInfo(),
  193. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  194. }
  195. return resp, nil
  196. }
  197. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  198. if !ms.Topo.IsLeader() {
  199. return nil, raft.NotLeaderError
  200. }
  201. resp := &master_pb.LookupEcVolumeResponse{}
  202. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  203. if !found {
  204. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  205. }
  206. resp.VolumeId = req.VolumeId
  207. for shardId, shardLocations := range ecLocations.Locations {
  208. var locations []*master_pb.Location
  209. for _, dn := range shardLocations {
  210. locations = append(locations, &master_pb.Location{
  211. Url: string(dn.Id()),
  212. PublicUrl: dn.PublicUrl,
  213. })
  214. }
  215. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  216. ShardId: uint32(shardId),
  217. Locations: locations,
  218. })
  219. }
  220. return resp, nil
  221. }
  222. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  223. if !ms.Topo.IsLeader() {
  224. return nil, raft.NotLeaderError
  225. }
  226. resp := &master_pb.VacuumVolumeResponse{}
  227. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), ms.preallocateSize)
  228. return resp, nil
  229. }