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.

185 lines
6.4 KiB

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