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.

173 lines
5.7 KiB

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. "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/needle"
  13. "github.com/chrislusf/seaweedfs/weed/topology"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  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.option.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.preallocateSize)
  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 count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  65. if ms.Topo.FreeSpace() < int64(count*option.ReplicaPlacement.GetCopyCount()) {
  66. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.FreeSpace(), count*option.ReplicaPlacement.GetCopyCount())
  67. } else {
  68. count, err = ms.vg.GrowByCountAndType(ms.grpcDialOpiton, count, option, ms.Topo)
  69. }
  70. } else {
  71. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  72. }
  73. if err != nil {
  74. writeJsonError(w, r, http.StatusNotAcceptable, err)
  75. } else {
  76. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  77. }
  78. }
  79. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  80. m := make(map[string]interface{})
  81. m["Version"] = util.VERSION
  82. m["Volumes"] = ms.Topo.ToVolumeMap()
  83. writeJsonQuiet(w, r, http.StatusOK, m)
  84. }
  85. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  86. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  87. collection := r.FormValue("collection")
  88. location := ms.findVolumeLocation(collection, vid)
  89. if location.Error == "" {
  90. loc := location.Locations[rand.Intn(len(location.Locations))]
  91. var url string
  92. if r.URL.RawQuery != "" {
  93. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  94. } else {
  95. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  96. }
  97. http.Redirect(w, r, url, http.StatusMovedPermanently)
  98. } else {
  99. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  100. }
  101. }
  102. func (ms *MasterServer) selfUrl(r *http.Request) string {
  103. if r.Host != "" {
  104. return r.Host
  105. }
  106. return "localhost:" + strconv.Itoa(ms.option.Port)
  107. }
  108. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  109. if ms.Topo.IsLeader() {
  110. submitForClientHandler(w, r, ms.selfUrl(r), ms.grpcDialOpiton)
  111. } else {
  112. masterUrl, err := ms.Topo.Leader()
  113. if err != nil {
  114. writeJsonError(w, r, http.StatusInternalServerError, err)
  115. } else {
  116. submitForClientHandler(w, r, masterUrl, ms.grpcDialOpiton)
  117. }
  118. }
  119. }
  120. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  121. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  122. return vl.GetActiveVolumeCount(option) > 0
  123. }
  124. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  125. replicationString := r.FormValue("replication")
  126. if replicationString == "" {
  127. replicationString = ms.option.DefaultReplicaPlacement
  128. }
  129. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  130. if err != nil {
  131. return nil, err
  132. }
  133. ttl, err := needle.ReadTTL(r.FormValue("ttl"))
  134. if err != nil {
  135. return nil, err
  136. }
  137. memoryMapMaxSizeMb, err := needle.ReadMemoryMapMaxSizeMb(r.FormValue("memoryMapMaxSizeMb"))
  138. if err != nil {
  139. return nil, err
  140. }
  141. preallocate := ms.preallocateSize
  142. if r.FormValue("preallocate") != "" {
  143. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  144. if err != nil {
  145. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  146. }
  147. }
  148. volumeGrowOption := &topology.VolumeGrowOption{
  149. Collection: r.FormValue("collection"),
  150. ReplicaPlacement: replicaPlacement,
  151. Ttl: ttl,
  152. Prealloacte: preallocate,
  153. DataCenter: r.FormValue("dataCenter"),
  154. Rack: r.FormValue("rack"),
  155. DataNode: r.FormValue("dataNode"),
  156. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  157. }
  158. return volumeGrowOption, nil
  159. }