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.

170 lines
5.6 KiB

10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage"
  10. "github.com/chrislusf/seaweedfs/weed/topology"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "math/rand"
  13. "net/http"
  14. "strconv"
  15. )
  16. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  17. collection, ok := ms.Topo.FindCollection(r.FormValue("collection"))
  18. if !ok {
  19. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", r.FormValue("collection")))
  20. return
  21. }
  22. for _, server := range collection.ListVolumeServers() {
  23. err := operation.WithVolumeServerClient(server.Url(), ms.grpcDialOpiton, func(client volume_server_pb.VolumeServerClient) error {
  24. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  25. Collection: collection.Name,
  26. })
  27. return deleteErr
  28. })
  29. if err != nil {
  30. writeJsonError(w, r, http.StatusInternalServerError, err)
  31. return
  32. }
  33. }
  34. ms.Topo.DeleteCollection(r.FormValue("collection"))
  35. }
  36. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  37. m := make(map[string]interface{})
  38. m["Version"] = util.VERSION
  39. m["Topology"] = ms.Topo.ToMap()
  40. writeJsonQuiet(w, r, http.StatusOK, m)
  41. }
  42. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  43. gcString := r.FormValue("garbageThreshold")
  44. gcThreshold := ms.garbageThreshold
  45. if gcString != "" {
  46. var err error
  47. gcThreshold, err = strconv.ParseFloat(gcString, 32)
  48. if err != nil {
  49. glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
  50. return
  51. }
  52. }
  53. glog.Infoln("garbageThreshold =", gcThreshold)
  54. ms.Topo.Vacuum(ms.grpcDialOpiton, gcThreshold, ms.preallocate)
  55. ms.dirStatusHandler(w, r)
  56. }
  57. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  58. count := 0
  59. option, err := ms.getVolumeGrowOption(r)
  60. if err != nil {
  61. writeJsonError(w, r, http.StatusNotAcceptable, err)
  62. return
  63. }
  64. if err == nil {
  65. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  66. if ms.Topo.FreeSpace() < int64(count*option.ReplicaPlacement.GetCopyCount()) {
  67. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.FreeSpace(), count*option.ReplicaPlacement.GetCopyCount())
  68. } else {
  69. count, err = ms.vg.GrowByCountAndType(ms.grpcDialOpiton, count, option, ms.Topo)
  70. }
  71. } else {
  72. err = errors.New("parameter count is not found")
  73. }
  74. }
  75. if err != nil {
  76. writeJsonError(w, r, http.StatusNotAcceptable, err)
  77. } else {
  78. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  79. }
  80. }
  81. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  82. m := make(map[string]interface{})
  83. m["Version"] = util.VERSION
  84. m["Volumes"] = ms.Topo.ToVolumeMap()
  85. writeJsonQuiet(w, r, http.StatusOK, m)
  86. }
  87. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  88. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  89. volumeId, err := storage.NewVolumeId(vid)
  90. if err != nil {
  91. debug("parsing error:", err, r.URL.Path)
  92. return
  93. }
  94. collection := r.FormValue("collection")
  95. machines := ms.Topo.Lookup(collection, volumeId)
  96. if machines != nil && len(machines) > 0 {
  97. var url string
  98. if r.URL.RawQuery != "" {
  99. url = util.NormalizeUrl(machines[rand.Intn(len(machines))].PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  100. } else {
  101. url = util.NormalizeUrl(machines[rand.Intn(len(machines))].PublicUrl) + r.URL.Path
  102. }
  103. http.Redirect(w, r, url, http.StatusMovedPermanently)
  104. } else {
  105. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d or collection %s not found", volumeId, collection))
  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.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.grpcDialOpiton)
  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.grpcDialOpiton)
  123. }
  124. }
  125. }
  126. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  127. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  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.defaultReplicaPlacement
  134. }
  135. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  136. if err != nil {
  137. return nil, err
  138. }
  139. ttl, err := storage.ReadTTL(r.FormValue("ttl"))
  140. if err != nil {
  141. return nil, err
  142. }
  143. preallocate := ms.preallocate
  144. if r.FormValue("preallocate") != "" {
  145. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  146. if err != nil {
  147. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  148. }
  149. }
  150. volumeGrowOption := &topology.VolumeGrowOption{
  151. Collection: r.FormValue("collection"),
  152. ReplicaPlacement: replicaPlacement,
  153. Ttl: ttl,
  154. Prealloacte: preallocate,
  155. DataCenter: r.FormValue("dataCenter"),
  156. Rack: r.FormValue("rack"),
  157. DataNode: r.FormValue("dataNode"),
  158. }
  159. return volumeGrowOption, nil
  160. }