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.

179 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 := uint64(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.ParseUint(r.FormValue("count"), 10, 32); err == nil {
  74. replicaCount := int64(count * uint64(option.ReplicaPlacement.GetCopyCount()))
  75. if ms.Topo.AvailableSpaceFor(option) < replicaCount {
  76. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.AvailableSpaceFor(option), replicaCount)
  77. } else if !ms.Topo.DataCenterExists(option.DataCenter) {
  78. err = fmt.Errorf("data center %v not found in topology", option.DataCenter)
  79. } else {
  80. var newVidLocations []*master_pb.VolumeLocation
  81. newVidLocations, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, uint32(count), option, ms.Topo)
  82. count = uint64(len(newVidLocations))
  83. }
  84. } else {
  85. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  86. }
  87. if err != nil {
  88. writeJsonError(w, r, http.StatusNotAcceptable, err)
  89. } else {
  90. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  91. }
  92. }
  93. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  94. m := make(map[string]interface{})
  95. m["Version"] = util.Version()
  96. m["Volumes"] = ms.Topo.ToVolumeMap()
  97. writeJsonQuiet(w, r, http.StatusOK, m)
  98. }
  99. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  100. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  101. collection := r.FormValue("collection")
  102. location := ms.findVolumeLocation(collection, vid)
  103. if location.Error == "" {
  104. loc := location.Locations[rand.Intn(len(location.Locations))]
  105. var url string
  106. if r.URL.RawQuery != "" {
  107. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  108. } else {
  109. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  110. }
  111. http.Redirect(w, r, url, http.StatusPermanentRedirect)
  112. } else {
  113. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  114. }
  115. }
  116. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  117. if ms.Topo.IsLeader() {
  118. submitForClientHandler(w, r, func(ctx context.Context) pb.ServerAddress { return ms.option.Master }, ms.grpcDialOption)
  119. } else {
  120. masterUrl, err := ms.Topo.Leader()
  121. if err != nil {
  122. writeJsonError(w, r, http.StatusInternalServerError, err)
  123. } else {
  124. submitForClientHandler(w, r, func(ctx context.Context) pb.ServerAddress { return masterUrl }, ms.grpcDialOption)
  125. }
  126. }
  127. }
  128. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  129. replicationString := r.FormValue("replication")
  130. if replicationString == "" {
  131. replicationString = ms.option.DefaultReplicaPlacement
  132. }
  133. replicaPlacement, err := super_block.NewReplicaPlacementFromString(replicationString)
  134. if err != nil {
  135. return nil, err
  136. }
  137. ttl, err := needle.ReadTTL(r.FormValue("ttl"))
  138. if err != nil {
  139. return nil, err
  140. }
  141. memoryMapMaxSizeMb, err := memory_map.ReadMemoryMapMaxSizeMb(r.FormValue("memoryMapMaxSizeMb"))
  142. if err != nil {
  143. return nil, err
  144. }
  145. diskType := types.ToDiskType(r.FormValue("disk"))
  146. preallocate := ms.preallocateSize
  147. if r.FormValue("preallocate") != "" {
  148. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  149. if err != nil {
  150. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  151. }
  152. }
  153. volumeGrowOption := &topology.VolumeGrowOption{
  154. Collection: r.FormValue("collection"),
  155. ReplicaPlacement: replicaPlacement,
  156. Ttl: ttl,
  157. DiskType: diskType,
  158. Preallocate: preallocate,
  159. DataCenter: r.FormValue("dataCenter"),
  160. Rack: r.FormValue("rack"),
  161. DataNode: r.FormValue("dataNode"),
  162. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  163. }
  164. return volumeGrowOption, nil
  165. }