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.

182 lines
6.1 KiB

6 years ago
5 years ago
6 years ago
6 years ago
10 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/chrislusf/seaweedfs/weed/storage"
  6. "math/rand"
  7. "net/http"
  8. "strconv"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  12. "github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  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. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  71. if ms.Topo.AvailableSpaceFor(option) < int64(count*option.ReplicaPlacement.GetCopyCount()) {
  72. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.AvailableSpaceFor(option), count*option.ReplicaPlacement.GetCopyCount())
  73. } else {
  74. count, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, count, option, ms.Topo)
  75. }
  76. } else {
  77. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  78. }
  79. if err != nil {
  80. writeJsonError(w, r, http.StatusNotAcceptable, err)
  81. } else {
  82. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  83. }
  84. }
  85. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  86. m := make(map[string]interface{})
  87. m["Version"] = util.Version()
  88. m["Volumes"] = ms.Topo.ToVolumeMap()
  89. writeJsonQuiet(w, r, http.StatusOK, m)
  90. }
  91. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  92. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  93. collection := r.FormValue("collection")
  94. location := ms.findVolumeLocation(collection, vid)
  95. if location.Error == "" {
  96. loc := location.Locations[rand.Intn(len(location.Locations))]
  97. var url string
  98. if r.URL.RawQuery != "" {
  99. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  100. } else {
  101. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  102. }
  103. http.Redirect(w, r, url, http.StatusPermanentRedirect)
  104. } else {
  105. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  106. }
  107. }
  108. func (ms *MasterServer) selfUrl(r *http.Request) string {
  109. if r.Host != "" {
  110. return r.Host
  111. }
  112. return "localhost:" + strconv.Itoa(ms.option.Port)
  113. }
  114. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  115. if ms.Topo.IsLeader() {
  116. submitForClientHandler(w, r, ms.selfUrl(r), ms.grpcDialOption)
  117. } else {
  118. masterUrl, err := ms.Topo.Leader()
  119. if err != nil {
  120. writeJsonError(w, r, http.StatusInternalServerError, err)
  121. } else {
  122. submitForClientHandler(w, r, masterUrl, ms.grpcDialOption)
  123. }
  124. }
  125. }
  126. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  127. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  128. return vl.GetActiveVolumeCount(option) > 0
  129. }
  130. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  131. replicationString := r.FormValue("replication")
  132. if replicationString == "" {
  133. replicationString = ms.option.DefaultReplicaPlacement
  134. }
  135. replicaPlacement, err := super_block.NewReplicaPlacementFromString(replicationString)
  136. if err != nil {
  137. return nil, err
  138. }
  139. ttl, err := needle.ReadTTL(r.FormValue("ttl"))
  140. if err != nil {
  141. return nil, err
  142. }
  143. memoryMapMaxSizeMb, err := memory_map.ReadMemoryMapMaxSizeMb(r.FormValue("memoryMapMaxSizeMb"))
  144. if err != nil {
  145. return nil, err
  146. }
  147. diskType := storage.ToDiskType(r.FormValue("disk"))
  148. preallocate := ms.preallocateSize
  149. if r.FormValue("preallocate") != "" {
  150. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  151. if err != nil {
  152. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  153. }
  154. }
  155. volumeGrowOption := &topology.VolumeGrowOption{
  156. Collection: r.FormValue("collection"),
  157. ReplicaPlacement: replicaPlacement,
  158. Ttl: ttl,
  159. DiskType: diskType,
  160. Prealloacte: preallocate,
  161. DataCenter: r.FormValue("dataCenter"),
  162. Rack: r.FormValue("rack"),
  163. DataNode: r.FormValue("dataNode"),
  164. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  165. }
  166. return volumeGrowOption, nil
  167. }