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.

331 lines
9.1 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
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. "github.com/seaweedfs/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 _, volumeOrFileId := range req.VolumeOrFileIds {
  72. vid := volumeOrFileId
  73. commaSep := strings.Index(vid, ",")
  74. if commaSep > 0 {
  75. vid = vid[0:commaSep]
  76. }
  77. if result, found := volumeLocations[vid]; found {
  78. var locations []*master_pb.Location
  79. for _, loc := range result.Locations {
  80. locations = append(locations, &master_pb.Location{
  81. Url: loc.Url,
  82. PublicUrl: loc.PublicUrl,
  83. DataCenter: loc.DataCenter,
  84. })
  85. }
  86. var auth string
  87. if commaSep > 0 { // this is a file id
  88. auth = string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, result.VolumeOrFileId))
  89. }
  90. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  91. VolumeOrFileId: result.VolumeOrFileId,
  92. Locations: locations,
  93. Error: result.Error,
  94. Auth: auth,
  95. })
  96. }
  97. }
  98. return resp, nil
  99. }
  100. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  101. if !ms.Topo.IsLeader() {
  102. return nil, raft.NotLeaderError
  103. }
  104. if req.Count == 0 {
  105. req.Count = 1
  106. }
  107. if req.Replication == "" {
  108. req.Replication = ms.option.DefaultReplicaPlacement
  109. }
  110. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  111. if err != nil {
  112. return nil, err
  113. }
  114. ttl, err := needle.ReadTTL(req.Ttl)
  115. if err != nil {
  116. return nil, err
  117. }
  118. diskType := types.ToDiskType(req.DiskType)
  119. option := &topology.VolumeGrowOption{
  120. Collection: req.Collection,
  121. ReplicaPlacement: replicaPlacement,
  122. Ttl: ttl,
  123. DiskType: diskType,
  124. Preallocate: ms.preallocateSize,
  125. DataCenter: req.DataCenter,
  126. Rack: req.Rack,
  127. DataNode: req.DataNode,
  128. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  129. }
  130. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  131. if !vl.HasGrowRequest() && vl.ShouldGrowVolumes(option) {
  132. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  133. return nil, fmt.Errorf("no free volumes left for " + option.String())
  134. }
  135. vl.AddGrowRequest()
  136. ms.vgCh <- &topology.VolumeGrowRequest{
  137. Option: option,
  138. Count: int(req.WritableVolumeCount),
  139. }
  140. }
  141. var (
  142. lastErr error
  143. maxTimeout = time.Second * 10
  144. startTime = time.Now()
  145. )
  146. for time.Now().Sub(startTime) < maxTimeout {
  147. fid, count, dnList, err := ms.Topo.PickForWrite(req.Count, option)
  148. if err == nil {
  149. dn := dnList.Head()
  150. var replicas []*master_pb.Location
  151. for _, r := range dnList.Rest() {
  152. replicas = append(replicas, &master_pb.Location{
  153. Url: r.Url(),
  154. PublicUrl: r.PublicUrl,
  155. GrpcPort: uint32(r.GrpcPort),
  156. DataCenter: r.GetDataCenterId(),
  157. })
  158. }
  159. return &master_pb.AssignResponse{
  160. Fid: fid,
  161. Location: &master_pb.Location{
  162. Url: dn.Url(),
  163. PublicUrl: dn.PublicUrl,
  164. GrpcPort: uint32(dn.GrpcPort),
  165. DataCenter: dn.GetDataCenterId(),
  166. },
  167. Count: count,
  168. Auth: string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  169. Replicas: replicas,
  170. }, nil
  171. }
  172. //glog.V(4).Infoln("waiting for volume growing...")
  173. lastErr = err
  174. time.Sleep(200 * time.Millisecond)
  175. }
  176. return nil, lastErr
  177. }
  178. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  179. if !ms.Topo.IsLeader() {
  180. return nil, raft.NotLeaderError
  181. }
  182. if req.Replication == "" {
  183. req.Replication = ms.option.DefaultReplicaPlacement
  184. }
  185. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  186. if err != nil {
  187. return nil, err
  188. }
  189. ttl, err := needle.ReadTTL(req.Ttl)
  190. if err != nil {
  191. return nil, err
  192. }
  193. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, types.ToDiskType(req.DiskType))
  194. stats := volumeLayout.Stats()
  195. totalSize := ms.Topo.GetDiskUsages().GetMaxVolumeCount() * int64(ms.option.VolumeSizeLimitMB) * 1024 * 1024
  196. resp := &master_pb.StatisticsResponse{
  197. TotalSize: uint64(totalSize),
  198. UsedSize: stats.UsedSize,
  199. FileCount: stats.FileCount,
  200. }
  201. return resp, nil
  202. }
  203. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  204. if !ms.Topo.IsLeader() {
  205. return nil, raft.NotLeaderError
  206. }
  207. resp := &master_pb.VolumeListResponse{
  208. TopologyInfo: ms.Topo.ToTopologyInfo(),
  209. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  210. }
  211. return resp, nil
  212. }
  213. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  214. if !ms.Topo.IsLeader() {
  215. return nil, raft.NotLeaderError
  216. }
  217. resp := &master_pb.LookupEcVolumeResponse{}
  218. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  219. if !found {
  220. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  221. }
  222. resp.VolumeId = req.VolumeId
  223. for shardId, shardLocations := range ecLocations.Locations {
  224. var locations []*master_pb.Location
  225. for _, dn := range shardLocations {
  226. locations = append(locations, &master_pb.Location{
  227. Url: string(dn.Id()),
  228. PublicUrl: dn.PublicUrl,
  229. DataCenter: dn.GetDataCenterId(),
  230. })
  231. }
  232. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  233. ShardId: uint32(shardId),
  234. Locations: locations,
  235. })
  236. }
  237. return resp, nil
  238. }
  239. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  240. if !ms.Topo.IsLeader() {
  241. return nil, raft.NotLeaderError
  242. }
  243. resp := &master_pb.VacuumVolumeResponse{}
  244. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), req.VolumeId, req.Collection, ms.preallocateSize)
  245. return resp, nil
  246. }
  247. func (ms *MasterServer) DisableVacuum(ctx context.Context, req *master_pb.DisableVacuumRequest) (*master_pb.DisableVacuumResponse, error) {
  248. ms.Topo.DisableVacuum()
  249. resp := &master_pb.DisableVacuumResponse{}
  250. return resp, nil
  251. }
  252. func (ms *MasterServer) EnableVacuum(ctx context.Context, req *master_pb.EnableVacuumRequest) (*master_pb.EnableVacuumResponse, error) {
  253. ms.Topo.EnableVacuum()
  254. resp := &master_pb.EnableVacuumResponse{}
  255. return resp, nil
  256. }
  257. func (ms *MasterServer) VolumeMarkReadonly(ctx context.Context, req *master_pb.VolumeMarkReadonlyRequest) (*master_pb.VolumeMarkReadonlyResponse, error) {
  258. if !ms.Topo.IsLeader() {
  259. return nil, raft.NotLeaderError
  260. }
  261. resp := &master_pb.VolumeMarkReadonlyResponse{}
  262. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(req.ReplicaPlacement))
  263. vl := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, needle.LoadTTLFromUint32(req.Ttl), types.ToDiskType(req.DiskType))
  264. dataNodes := ms.Topo.Lookup(req.Collection, needle.VolumeId(req.VolumeId))
  265. for _, dn := range dataNodes {
  266. if dn.Ip == req.Ip && dn.Port == int(req.Port) {
  267. if req.IsReadonly {
  268. vl.SetVolumeReadOnly(dn, needle.VolumeId(req.VolumeId))
  269. } else {
  270. vl.SetVolumeWritable(dn, needle.VolumeId(req.VolumeId))
  271. }
  272. }
  273. }
  274. return resp, nil
  275. }