master_server_handlers_admin.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/backend/memory_map"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  14. "github.com/chrislusf/seaweedfs/weed/storage/types"
  15. "github.com/chrislusf/seaweedfs/weed/topology"
  16. "github.com/chrislusf/seaweedfs/weed/util"
  17. )
  18. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  19. collectionName := r.FormValue("collection")
  20. collection, ok := ms.Topo.FindCollection(collectionName)
  21. if !ok {
  22. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
  23. return
  24. }
  25. for _, server := range collection.ListVolumeServers() {
  26. err := operation.WithVolumeServerClient(server.Url(), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  27. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  28. Collection: collection.Name,
  29. })
  30. return deleteErr
  31. })
  32. if err != nil {
  33. writeJsonError(w, r, http.StatusInternalServerError, err)
  34. return
  35. }
  36. }
  37. ms.Topo.DeleteCollection(collectionName)
  38. w.WriteHeader(http.StatusNoContent)
  39. return
  40. }
  41. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  42. m := make(map[string]interface{})
  43. m["Version"] = util.Version()
  44. m["Topology"] = ms.Topo.ToMap()
  45. writeJsonQuiet(w, r, http.StatusOK, m)
  46. }
  47. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  48. gcString := r.FormValue("garbageThreshold")
  49. gcThreshold := ms.option.GarbageThreshold
  50. if gcString != "" {
  51. var err error
  52. gcThreshold, err = strconv.ParseFloat(gcString, 32)
  53. if err != nil {
  54. glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
  55. writeJsonError(w, r, http.StatusNotAcceptable, fmt.Errorf("garbageThreshold %s is not a valid float number", gcString))
  56. return
  57. }
  58. }
  59. // glog.Infoln("garbageThreshold =", gcThreshold)
  60. ms.Topo.Vacuum(ms.grpcDialOption, gcThreshold, ms.preallocateSize)
  61. ms.dirStatusHandler(w, r)
  62. }
  63. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  64. count := 0
  65. option, err := ms.getVolumeGrowOption(r)
  66. if err != nil {
  67. writeJsonError(w, r, http.StatusNotAcceptable, err)
  68. return
  69. }
  70. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  71. if ms.Topo.AvailableSpaceFor(option) < int64(count*option.ReplicaPlacement.GetCopyCount()) {
  72. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.AvailableSpaceFor(option), count*option.ReplicaPlacement.GetCopyCount())
  73. } else {
  74. count, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, count, option, ms.Topo)
  75. }
  76. } else {
  77. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  78. }
  79. if err != nil {
  80. writeJsonError(w, r, http.StatusNotAcceptable, err)
  81. } else {
  82. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  83. }
  84. }
  85. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  86. m := make(map[string]interface{})
  87. m["Version"] = util.Version()
  88. m["Volumes"] = ms.Topo.ToVolumeMap()
  89. writeJsonQuiet(w, r, http.StatusOK, m)
  90. }
  91. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  92. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  93. collection := r.FormValue("collection")
  94. location := ms.findVolumeLocation(collection, vid)
  95. if location.Error == "" {
  96. loc := location.Locations[rand.Intn(len(location.Locations))]
  97. var url string
  98. if r.URL.RawQuery != "" {
  99. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
  100. } else {
  101. url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
  102. }
  103. http.Redirect(w, r, url, http.StatusPermanentRedirect)
  104. } else {
  105. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  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.option.Port)
  113. }
  114. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  115. if ms.Topo.IsLeader() {
  116. submitForClientHandler(w, r, func() string { return ms.selfUrl(r) }, ms.grpcDialOption)
  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, func() string { return masterUrl }, ms.grpcDialOption)
  123. }
  124. }
  125. }
  126. func (ms *MasterServer) shouldVolumeGrow(option *topology.VolumeGrowOption) bool {
  127. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  128. active, high := vl.GetActiveVolumeCount(option)
  129. //glog.V(0).Infof("active volume: %d, high usage volume: %d\n", active, high)
  130. return active <= high
  131. }
  132. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  133. replicationString := r.FormValue("replication")
  134. if replicationString == "" {
  135. replicationString = ms.option.DefaultReplicaPlacement
  136. }
  137. replicaPlacement, err := super_block.NewReplicaPlacementFromString(replicationString)
  138. if err != nil {
  139. return nil, err
  140. }
  141. ttl, err := needle.ReadTTL(r.FormValue("ttl"))
  142. if err != nil {
  143. return nil, err
  144. }
  145. memoryMapMaxSizeMb, err := memory_map.ReadMemoryMapMaxSizeMb(r.FormValue("memoryMapMaxSizeMb"))
  146. if err != nil {
  147. return nil, err
  148. }
  149. diskType := types.ToDiskType(r.FormValue("disk"))
  150. preallocate := ms.preallocateSize
  151. if r.FormValue("preallocate") != "" {
  152. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  153. if err != nil {
  154. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  155. }
  156. }
  157. volumeGrowOption := &topology.VolumeGrowOption{
  158. Collection: r.FormValue("collection"),
  159. ReplicaPlacement: replicaPlacement,
  160. Ttl: ttl,
  161. DiskType: diskType,
  162. Preallocate: preallocate,
  163. DataCenter: r.FormValue("dataCenter"),
  164. Rack: r.FormValue("rack"),
  165. DataNode: r.FormValue("dataNode"),
  166. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  167. }
  168. return volumeGrowOption, nil
  169. }