master_server_handlers_admin.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "math/rand/v2"
  8. "net/http"
  9. "strconv"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/backend/memory_map"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  17. "github.com/seaweedfs/seaweedfs/weed/topology"
  18. "github.com/seaweedfs/seaweedfs/weed/util"
  19. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  20. )
  21. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  22. collectionName := r.FormValue("collection")
  23. collection, ok := ms.Topo.FindCollection(collectionName)
  24. if !ok {
  25. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
  26. return
  27. }
  28. for _, server := range collection.ListVolumeServers() {
  29. err := operation.WithVolumeServerClient(false, server.ServerAddress(), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  30. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  31. Collection: collection.Name,
  32. })
  33. return deleteErr
  34. })
  35. if err != nil {
  36. writeJsonError(w, r, http.StatusInternalServerError, err)
  37. return
  38. }
  39. }
  40. ms.Topo.DeleteCollection(collectionName)
  41. w.WriteHeader(http.StatusNoContent)
  42. return
  43. }
  44. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  45. m := make(map[string]interface{})
  46. m["Version"] = util.Version()
  47. m["Topology"] = ms.Topo.ToInfo()
  48. writeJsonQuiet(w, r, http.StatusOK, m)
  49. }
  50. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  51. gcString := r.FormValue("garbageThreshold")
  52. gcThreshold := ms.option.GarbageThreshold
  53. if gcString != "" {
  54. var err error
  55. gcThreshold, err = strconv.ParseFloat(gcString, 32)
  56. if err != nil {
  57. glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
  58. writeJsonError(w, r, http.StatusNotAcceptable, fmt.Errorf("garbageThreshold %s is not a valid float number", gcString))
  59. return
  60. }
  61. }
  62. // glog.Infoln("garbageThreshold =", gcThreshold)
  63. ms.Topo.Vacuum(ms.grpcDialOption, gcThreshold, ms.option.MaxParallelVacuumPerServer, 0, "", ms.preallocateSize)
  64. ms.dirStatusHandler(w, r)
  65. }
  66. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  67. count := uint64(0)
  68. option, err := ms.getVolumeGrowOption(r)
  69. if err != nil {
  70. writeJsonError(w, r, http.StatusNotAcceptable, err)
  71. return
  72. }
  73. glog.V(0).Infof("volumeGrowHandler received %v from %v", option.String(), r.RemoteAddr)
  74. if count, err = strconv.ParseUint(r.FormValue("count"), 10, 32); err == nil {
  75. replicaCount := int64(count * uint64(option.ReplicaPlacement.GetCopyCount()))
  76. if ms.Topo.AvailableSpaceFor(option) < replicaCount {
  77. err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.AvailableSpaceFor(option), replicaCount)
  78. } else if !ms.Topo.DataCenterExists(option.DataCenter) {
  79. err = fmt.Errorf("data center %v not found in topology", option.DataCenter)
  80. } else {
  81. var newVidLocations []*master_pb.VolumeLocation
  82. newVidLocations, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, uint32(count), option, ms.Topo)
  83. count = uint64(len(newVidLocations))
  84. }
  85. } else {
  86. err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
  87. }
  88. if err != nil {
  89. writeJsonError(w, r, http.StatusNotAcceptable, err)
  90. } else {
  91. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  92. }
  93. }
  94. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  95. m := make(map[string]interface{})
  96. m["Version"] = util.Version()
  97. m["Volumes"] = ms.Topo.ToVolumeMap()
  98. writeJsonQuiet(w, r, http.StatusOK, m)
  99. }
  100. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  101. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  102. collection := r.FormValue("collection")
  103. location := ms.findVolumeLocation(collection, vid)
  104. if location.Error == "" {
  105. loc := location.Locations[rand.IntN(len(location.Locations))]
  106. url, _ := util_http.NormalizeUrl(loc.PublicUrl)
  107. if r.URL.RawQuery != "" {
  108. url = url + r.URL.Path + "?" + r.URL.RawQuery
  109. } else {
  110. url = url + r.URL.Path
  111. }
  112. http.Redirect(w, r, url, http.StatusPermanentRedirect)
  113. } else {
  114. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
  115. }
  116. }
  117. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  118. if ms.Topo.IsLeader() {
  119. submitForClientHandler(w, r, func(ctx context.Context) pb.ServerAddress { return ms.option.Master }, ms.grpcDialOption)
  120. } else {
  121. masterUrl, err := ms.Topo.Leader()
  122. if err != nil {
  123. writeJsonError(w, r, http.StatusInternalServerError, err)
  124. } else {
  125. submitForClientHandler(w, r, func(ctx context.Context) pb.ServerAddress { return masterUrl }, ms.grpcDialOption)
  126. }
  127. }
  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 := super_block.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. diskType := types.ToDiskType(r.FormValue("disk"))
  147. preallocate := ms.preallocateSize
  148. if r.FormValue("preallocate") != "" {
  149. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  150. if err != nil {
  151. return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
  152. }
  153. }
  154. volumeGrowOption := &topology.VolumeGrowOption{
  155. Collection: r.FormValue("collection"),
  156. ReplicaPlacement: replicaPlacement,
  157. Ttl: ttl,
  158. DiskType: diskType,
  159. Preallocate: preallocate,
  160. DataCenter: r.FormValue("dataCenter"),
  161. Rack: r.FormValue("rack"),
  162. DataNode: r.FormValue("dataNode"),
  163. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  164. }
  165. return volumeGrowOption, nil
  166. }
  167. func (ms *MasterServer) collectionInfoHandler(w http.ResponseWriter, r *http.Request) {
  168. //get collection from request
  169. collectionName := r.FormValue("collection")
  170. if collectionName == "" {
  171. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection is required"))
  172. return
  173. }
  174. //output details of the volumes?
  175. detail := r.FormValue("detail") == "true"
  176. //collect collection info
  177. collection, ok := ms.Topo.FindCollection(collectionName)
  178. if !ok {
  179. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
  180. return
  181. }
  182. volumeLayouts := collection.GetAllVolumeLayouts()
  183. if detail {
  184. //prepare the json response
  185. all_stats := make([]map[string]interface{}, len(volumeLayouts))
  186. for i, volumeLayout := range volumeLayouts {
  187. volumeLayoutStats := volumeLayout.Stats()
  188. m := make(map[string]interface{})
  189. m["Version"] = util.Version()
  190. m["Collection"] = collectionName
  191. m["TotalSize"] = volumeLayoutStats.TotalSize
  192. m["FileCount"] = volumeLayoutStats.FileCount
  193. m["UsedSize"] = volumeLayoutStats.UsedSize
  194. all_stats[i] = m
  195. }
  196. //write it
  197. writeJsonQuiet(w, r, http.StatusOK, all_stats)
  198. } else {
  199. //prepare the json response
  200. collectionStats := map[string]interface{}{
  201. "Version": util.Version(),
  202. "Collection": collectionName,
  203. "TotalSize": uint64(0),
  204. "FileCount": uint64(0),
  205. "UsedSize": uint64(0),
  206. "VolumeCount": uint64(0),
  207. }
  208. for _, volumeLayout := range volumeLayouts {
  209. volumeLayoutStats := volumeLayout.Stats()
  210. collectionStats["TotalSize"] = collectionStats["TotalSize"].(uint64) + volumeLayoutStats.TotalSize
  211. collectionStats["FileCount"] = collectionStats["FileCount"].(uint64) + volumeLayoutStats.FileCount
  212. collectionStats["UsedSize"] = collectionStats["UsedSize"].(uint64) + volumeLayoutStats.UsedSize
  213. collectionStats["VolumeCount"] = collectionStats["VolumeCount"].(uint64) + 1
  214. }
  215. //write it
  216. writeJsonQuiet(w, r, http.StatusOK, collectionStats)
  217. }
  218. }