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.

168 lines
5.5 KiB

6 years ago
6 years ago
6 years ago
10 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. collection := r.FormValue("collection")
  91. location := ms.findVolumeLocation(collection, vid)
  92. if location.Error == "" {
  93. loc := location.Locations[rand.Intn(len(location.Locations))]
  94. var url string
  95. if r.URL.RawQuery != "" {
  96. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  97. } else {
  98. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  99. }
  100. http.Redirect(w, r, url, http.StatusMovedPermanently)
  101. } else {
  102. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  103. }
  104. }
  105. func (ms *MasterServer) selfUrl(r *http.Request) string {
  106. if r.Host != "" {
  107. return r.Host
  108. }
  109. return "localhost:" + strconv.Itoa(ms.option.Port)
  110. }
  111. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  112. if ms.Topo.IsLeader() {
  113. submitForClientHandler(w, r, ms.selfUrl(r), ms.grpcDialOpiton)
  114. } else {
  115. masterUrl, err := ms.Topo.Leader()
  116. if err != nil {
  117. writeJsonError(w, r, http.StatusInternalServerError, err)
  118. } else {
  119. submitForClientHandler(w, r, masterUrl, ms.grpcDialOpiton)
  120. }
  121. }
  122. }
  123. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  124. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  125. return vl.GetActiveVolumeCount(option) > 0
  126. }
  127. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  128. replicationString := r.FormValue("replication")
  129. if replicationString == "" {
  130. replicationString = ms.option.DefaultReplicaPlacement
  131. }
  132. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  133. if err != nil {
  134. return nil, err
  135. }
  136. ttl, err := needle.ReadTTL(r.FormValue("ttl"))
  137. if err != nil {
  138. return nil, err
  139. }
  140. preallocate := ms.preallocateSize
  141. if r.FormValue("preallocate") != "" {
  142. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  143. if err != nil {
  144. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  145. }
  146. }
  147. volumeGrowOption := &topology.VolumeGrowOption{
  148. Collection: r.FormValue("collection"),
  149. ReplicaPlacement: replicaPlacement,
  150. Ttl: ttl,
  151. Prealloacte: preallocate,
  152. DataCenter: r.FormValue("dataCenter"),
  153. Rack: r.FormValue("rack"),
  154. DataNode: r.FormValue("dataNode"),
  155. }
  156. return volumeGrowOption, nil
  157. }