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.

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