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.

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