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.

172 lines
5.6 KiB

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