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.

175 lines
5.6 KiB

10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "math/rand"
  7. "net/http"
  8. "strconv"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/chrislusf/seaweedfs/weed/storage"
  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. collection, ok := ms.Topo.FindCollection(r.FormValue("collection"))
  19. if !ok {
  20. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", r.FormValue("collection")))
  21. return
  22. }
  23. for _, server := range collection.ListVolumeServers() {
  24. err := operation.WithVolumeServerClient(server.Url(), func(client volume_server_pb.VolumeServerClient) error {
  25. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  26. defer cancel()
  27. _, deleteErr := client.DeleteCollection(ctx, &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(r.FormValue("collection"))
  38. }
  39. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  40. m := make(map[string]interface{})
  41. m["Version"] = util.VERSION
  42. m["Topology"] = ms.Topo.ToMap()
  43. writeJsonQuiet(w, r, http.StatusOK, m)
  44. }
  45. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  46. gcString := r.FormValue("garbageThreshold")
  47. gcThreshold := ms.garbageThreshold
  48. if gcString != "" {
  49. var err error
  50. gcThreshold, err = strconv.ParseFloat(gcString, 32)
  51. if err != nil {
  52. glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
  53. return
  54. }
  55. }
  56. glog.Infoln("garbageThreshold =", gcThreshold)
  57. ms.Topo.Vacuum(gcThreshold, ms.preallocate)
  58. ms.dirStatusHandler(w, r)
  59. }
  60. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  61. count := 0
  62. option, err := ms.getVolumeGrowOption(r)
  63. if err != nil {
  64. writeJsonError(w, r, http.StatusNotAcceptable, err)
  65. return
  66. }
  67. if err == nil {
  68. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  69. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  70. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  71. } else {
  72. count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
  73. }
  74. } else {
  75. err = errors.New("parameter count is not found")
  76. }
  77. }
  78. if err != nil {
  79. writeJsonError(w, r, http.StatusNotAcceptable, err)
  80. } else {
  81. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  82. }
  83. }
  84. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  85. m := make(map[string]interface{})
  86. m["Version"] = util.VERSION
  87. m["Volumes"] = ms.Topo.ToVolumeMap()
  88. writeJsonQuiet(w, r, http.StatusOK, m)
  89. }
  90. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  91. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  92. volumeId, err := storage.NewVolumeId(vid)
  93. if err != nil {
  94. debug("parsing error:", err, r.URL.Path)
  95. return
  96. }
  97. collection := r.FormValue("collection")
  98. machines := ms.Topo.Lookup(collection, volumeId)
  99. if machines != nil && len(machines) > 0 {
  100. var url string
  101. if r.URL.RawQuery != "" {
  102. url = util.NormalizeUrl(machines[rand.Intn(len(machines))].PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  103. } else {
  104. url = util.NormalizeUrl(machines[rand.Intn(len(machines))].PublicUrl) + r.URL.Path
  105. }
  106. http.Redirect(w, r, url, http.StatusMovedPermanently)
  107. } else {
  108. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d or collection %s not found", volumeId, collection))
  109. }
  110. }
  111. func (ms *MasterServer) selfUrl(r *http.Request) string {
  112. if r.Host != "" {
  113. return r.Host
  114. }
  115. return "localhost:" + strconv.Itoa(ms.port)
  116. }
  117. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  118. if ms.Topo.IsLeader() {
  119. submitForClientHandler(w, r, ms.selfUrl(r))
  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, masterUrl)
  126. }
  127. }
  128. }
  129. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  130. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  131. return vl.GetActiveVolumeCount(option) > 0
  132. }
  133. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  134. replicationString := r.FormValue("replication")
  135. if replicationString == "" {
  136. replicationString = ms.defaultReplicaPlacement
  137. }
  138. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  139. if err != nil {
  140. return nil, err
  141. }
  142. ttl, err := storage.ReadTTL(r.FormValue("ttl"))
  143. if err != nil {
  144. return nil, err
  145. }
  146. preallocate := ms.preallocate
  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. Prealloacte: preallocate,
  158. DataCenter: r.FormValue("dataCenter"),
  159. Rack: r.FormValue("rack"),
  160. DataNode: r.FormValue("dataNode"),
  161. }
  162. return volumeGrowOption, nil
  163. }