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
5.8 KiB

6 years ago
5 years ago
6 years ago
10 years ago
6 years ago
10 years ago
6 years ago
6 years ago
6 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"
  12. "github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/topology"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  18. collectionName := r.FormValue("collection")
  19. collection, ok := ms.Topo.FindCollection(collectionName)
  20. if !ok {
  21. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
  22. return
  23. }
  24. for _, server := range collection.ListVolumeServers() {
  25. err := operation.WithVolumeServerClient(server.Url(), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  26. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  27. Collection: collection.Name,
  28. })
  29. return deleteErr
  30. })
  31. if err != nil {
  32. writeJsonError(w, r, http.StatusInternalServerError, err)
  33. return
  34. }
  35. }
  36. ms.Topo.DeleteCollection(collectionName)
  37. w.WriteHeader(http.StatusNoContent)
  38. return
  39. }
  40. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  41. m := make(map[string]interface{})
  42. m["Version"] = util.VERSION
  43. m["Topology"] = ms.Topo.ToMap()
  44. writeJsonQuiet(w, r, http.StatusOK, m)
  45. }
  46. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  47. gcString := r.FormValue("garbageThreshold")
  48. gcThreshold := ms.option.GarbageThreshold
  49. if gcString != "" {
  50. var err error
  51. gcThreshold, err = strconv.ParseFloat(gcString, 32)
  52. if err != nil {
  53. glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
  54. return
  55. }
  56. }
  57. glog.Infoln("garbageThreshold =", gcThreshold)
  58. ms.Topo.Vacuum(ms.grpcDialOption, gcThreshold, ms.preallocateSize)
  59. ms.dirStatusHandler(w, r)
  60. }
  61. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  62. count := 0
  63. option, err := ms.getVolumeGrowOption(r)
  64. if err != nil {
  65. writeJsonError(w, r, http.StatusNotAcceptable, err)
  66. return
  67. }
  68. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  69. if ms.Topo.FreeSpace() < int64(count*option.ReplicaPlacement.GetCopyCount()) {
  70. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.FreeSpace(), count*option.ReplicaPlacement.GetCopyCount())
  71. } else {
  72. count, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, count, option, ms.Topo)
  73. }
  74. } else {
  75. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  76. }
  77. if err != nil {
  78. writeJsonError(w, r, http.StatusNotAcceptable, err)
  79. } else {
  80. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  81. }
  82. }
  83. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  84. m := make(map[string]interface{})
  85. m["Version"] = util.VERSION
  86. m["Volumes"] = ms.Topo.ToVolumeMap()
  87. writeJsonQuiet(w, r, http.StatusOK, m)
  88. }
  89. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  90. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  91. collection := r.FormValue("collection")
  92. location := ms.findVolumeLocation(collection, vid)
  93. if location.Error == "" {
  94. loc := location.Locations[rand.Intn(len(location.Locations))]
  95. var url string
  96. if r.URL.RawQuery != "" {
  97. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  98. } else {
  99. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  100. }
  101. http.Redirect(w, r, url, http.StatusMovedPermanently)
  102. } else {
  103. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  104. }
  105. }
  106. func (ms *MasterServer) selfUrl(r *http.Request) string {
  107. if r.Host != "" {
  108. return r.Host
  109. }
  110. return "localhost:" + strconv.Itoa(ms.option.Port)
  111. }
  112. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  113. if ms.Topo.IsLeader() {
  114. submitForClientHandler(w, r, ms.selfUrl(r), ms.grpcDialOption)
  115. } else {
  116. masterUrl, err := ms.Topo.Leader()
  117. if err != nil {
  118. writeJsonError(w, r, http.StatusInternalServerError, err)
  119. } else {
  120. submitForClientHandler(w, r, masterUrl, ms.grpcDialOption)
  121. }
  122. }
  123. }
  124. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  125. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  126. return vl.GetActiveVolumeCount(option) > 0
  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 := storage.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. 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. Prealloacte: preallocate,
  157. DataCenter: r.FormValue("dataCenter"),
  158. Rack: r.FormValue("rack"),
  159. DataNode: r.FormValue("dataNode"),
  160. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  161. }
  162. return volumeGrowOption, nil
  163. }