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.

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