master_server_handlers_admin.go 5.9 KB

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