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.

178 lines
6.3 KiB

6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "math/rand"
  8. "net/http"
  9. "strconv"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/backend/memory_map"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  17. "github.com/seaweedfs/seaweedfs/weed/topology"
  18. "github.com/seaweedfs/seaweedfs/weed/util"
  19. )
  20. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  21. collectionName := r.FormValue("collection")
  22. collection, ok := ms.Topo.FindCollection(collectionName)
  23. if !ok {
  24. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
  25. return
  26. }
  27. for _, server := range collection.ListVolumeServers() {
  28. err := operation.WithVolumeServerClient(false, server.ServerAddress(), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  29. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  30. Collection: collection.Name,
  31. })
  32. return deleteErr
  33. })
  34. if err != nil {
  35. writeJsonError(w, r, http.StatusInternalServerError, err)
  36. return
  37. }
  38. }
  39. ms.Topo.DeleteCollection(collectionName)
  40. w.WriteHeader(http.StatusNoContent)
  41. return
  42. }
  43. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  44. m := make(map[string]interface{})
  45. m["Version"] = util.Version()
  46. m["Topology"] = ms.Topo.ToInfo()
  47. writeJsonQuiet(w, r, http.StatusOK, m)
  48. }
  49. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  50. gcString := r.FormValue("garbageThreshold")
  51. gcThreshold := ms.option.GarbageThreshold
  52. if gcString != "" {
  53. var err error
  54. gcThreshold, err = strconv.ParseFloat(gcString, 32)
  55. if err != nil {
  56. glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
  57. writeJsonError(w, r, http.StatusNotAcceptable, fmt.Errorf("garbageThreshold %s is not a valid float number", gcString))
  58. return
  59. }
  60. }
  61. // glog.Infoln("garbageThreshold =", gcThreshold)
  62. ms.Topo.Vacuum(ms.grpcDialOption, gcThreshold, 0, "", ms.preallocateSize)
  63. ms.dirStatusHandler(w, r)
  64. }
  65. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  66. count := 0
  67. option, err := ms.getVolumeGrowOption(r)
  68. if err != nil {
  69. writeJsonError(w, r, http.StatusNotAcceptable, err)
  70. return
  71. }
  72. glog.V(0).Infof("volumeGrowHandler received %v from %v", option.String(), r.RemoteAddr)
  73. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  74. if ms.Topo.AvailableSpaceFor(option) < int64(count*option.ReplicaPlacement.GetCopyCount()) {
  75. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.AvailableSpaceFor(option), count*option.ReplicaPlacement.GetCopyCount())
  76. } else if !ms.Topo.DataCenterExists(option.DataCenter) {
  77. err = fmt.Errorf("data center %v not found in topology", option.DataCenter)
  78. } else {
  79. var newVidLocations []*master_pb.VolumeLocation
  80. newVidLocations, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, count, option, ms.Topo)
  81. count = len(newVidLocations)
  82. }
  83. } else {
  84. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  85. }
  86. if err != nil {
  87. writeJsonError(w, r, http.StatusNotAcceptable, err)
  88. } else {
  89. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  90. }
  91. }
  92. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  93. m := make(map[string]interface{})
  94. m["Version"] = util.Version()
  95. m["Volumes"] = ms.Topo.ToVolumeMap()
  96. writeJsonQuiet(w, r, http.StatusOK, m)
  97. }
  98. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  99. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  100. collection := r.FormValue("collection")
  101. location := ms.findVolumeLocation(collection, vid)
  102. if location.Error == "" {
  103. loc := location.Locations[rand.Intn(len(location.Locations))]
  104. var url string
  105. if r.URL.RawQuery != "" {
  106. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  107. } else {
  108. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  109. }
  110. http.Redirect(w, r, url, http.StatusPermanentRedirect)
  111. } else {
  112. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  113. }
  114. }
  115. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  116. if ms.Topo.IsLeader() {
  117. submitForClientHandler(w, r, func(ctx context.Context) pb.ServerAddress { return ms.option.Master }, ms.grpcDialOption)
  118. } else {
  119. masterUrl, err := ms.Topo.Leader()
  120. if err != nil {
  121. writeJsonError(w, r, http.StatusInternalServerError, err)
  122. } else {
  123. submitForClientHandler(w, r, func(ctx context.Context) pb.ServerAddress { return masterUrl }, ms.grpcDialOption)
  124. }
  125. }
  126. }
  127. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  128. replicationString := r.FormValue("replication")
  129. if replicationString == "" {
  130. replicationString = ms.option.DefaultReplicaPlacement
  131. }
  132. replicaPlacement, err := super_block.NewReplicaPlacementFromString(replicationString)
  133. if err != nil {
  134. return nil, err
  135. }
  136. ttl, err := needle.ReadTTL(r.FormValue("ttl"))
  137. if err != nil {
  138. return nil, err
  139. }
  140. memoryMapMaxSizeMb, err := memory_map.ReadMemoryMapMaxSizeMb(r.FormValue("memoryMapMaxSizeMb"))
  141. if err != nil {
  142. return nil, err
  143. }
  144. diskType := types.ToDiskType(r.FormValue("disk"))
  145. preallocate := ms.preallocateSize
  146. if r.FormValue("preallocate") != "" {
  147. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  148. if err != nil {
  149. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  150. }
  151. }
  152. volumeGrowOption := &topology.VolumeGrowOption{
  153. Collection: r.FormValue("collection"),
  154. ReplicaPlacement: replicaPlacement,
  155. Ttl: ttl,
  156. DiskType: diskType,
  157. Preallocate: preallocate,
  158. DataCenter: r.FormValue("dataCenter"),
  159. Rack: r.FormValue("rack"),
  160. DataNode: r.FormValue("dataNode"),
  161. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  162. }
  163. return volumeGrowOption, nil
  164. }