master_server_handlers_admin.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand/v2"
  6. "net/http"
  7. "strconv"
  8. "github.com/seaweedfs/seaweedfs/weed/util/version"
  9. "github.com/seaweedfs/seaweedfs/weed/pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/operation"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/backend/memory_map"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  17. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  18. "github.com/seaweedfs/seaweedfs/weed/topology"
  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"] = version.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, false)
  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"] = version.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. ver := needle.GetCurrentVersion()
  155. volumeGrowOption := &topology.VolumeGrowOption{
  156. Collection: r.FormValue("collection"),
  157. ReplicaPlacement: replicaPlacement,
  158. Ttl: ttl,
  159. DiskType: diskType,
  160. Preallocate: preallocate,
  161. DataCenter: r.FormValue("dataCenter"),
  162. Rack: r.FormValue("rack"),
  163. DataNode: r.FormValue("dataNode"),
  164. MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
  165. Version: uint32(ver),
  166. }
  167. return volumeGrowOption, nil
  168. }
  169. func (ms *MasterServer) collectionInfoHandler(w http.ResponseWriter, r *http.Request) {
  170. //get collection from request
  171. collectionName := r.FormValue("collection")
  172. if collectionName == "" {
  173. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection is required"))
  174. return
  175. }
  176. //output details of the volumes?
  177. detail := r.FormValue("detail") == "true"
  178. //collect collection info
  179. collection, ok := ms.Topo.FindCollection(collectionName)
  180. if !ok {
  181. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
  182. return
  183. }
  184. volumeLayouts := collection.GetAllVolumeLayouts()
  185. if detail {
  186. //prepare the json response
  187. all_stats := make([]map[string]interface{}, len(volumeLayouts))
  188. for i, volumeLayout := range volumeLayouts {
  189. volumeLayoutStats := volumeLayout.Stats()
  190. m := make(map[string]interface{})
  191. m["Version"] = version.Version()
  192. m["Collection"] = collectionName
  193. m["TotalSize"] = volumeLayoutStats.TotalSize
  194. m["FileCount"] = volumeLayoutStats.FileCount
  195. m["UsedSize"] = volumeLayoutStats.UsedSize
  196. all_stats[i] = m
  197. }
  198. //write it
  199. writeJsonQuiet(w, r, http.StatusOK, all_stats)
  200. } else {
  201. //prepare the json response
  202. collectionStats := map[string]interface{}{
  203. "Version": version.Version(),
  204. "Collection": collectionName,
  205. "TotalSize": uint64(0),
  206. "FileCount": uint64(0),
  207. "UsedSize": uint64(0),
  208. "VolumeCount": uint64(0),
  209. }
  210. for _, volumeLayout := range volumeLayouts {
  211. volumeLayoutStats := volumeLayout.Stats()
  212. collectionStats["TotalSize"] = collectionStats["TotalSize"].(uint64) + volumeLayoutStats.TotalSize
  213. collectionStats["FileCount"] = collectionStats["FileCount"].(uint64) + volumeLayoutStats.FileCount
  214. collectionStats["UsedSize"] = collectionStats["UsedSize"].(uint64) + volumeLayoutStats.UsedSize
  215. collectionStats["VolumeCount"] = collectionStats["VolumeCount"].(uint64) + 1
  216. }
  217. //write it
  218. writeJsonQuiet(w, r, http.StatusOK, collectionStats)
  219. }
  220. }