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.

157 lines
5.4 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/operation"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "code.google.com/p/weed-fs/go/topology"
  6. "code.google.com/p/weed-fs/go/util"
  7. "encoding/json"
  8. "errors"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. )
  13. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  14. collection, ok := ms.Topo.GetCollection(r.FormValue("collection"))
  15. if !ok {
  16. writeJsonQuiet(w, r, map[string]interface{}{"error": "collection " + r.FormValue("collection") + "does not exist!"})
  17. return
  18. }
  19. for _, server := range collection.ListVolumeServers() {
  20. _, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
  21. if err != nil {
  22. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  23. return
  24. }
  25. }
  26. ms.Topo.DeleteCollection(r.FormValue("collection"))
  27. }
  28. func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
  29. init := r.FormValue("init") == "true"
  30. ip := r.FormValue("ip")
  31. if ip == "" {
  32. ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
  33. }
  34. port, _ := strconv.Atoi(r.FormValue("port"))
  35. maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
  36. s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
  37. publicUrl := r.FormValue("publicUrl")
  38. volumes := new([]storage.VolumeInfo)
  39. if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
  40. writeJsonQuiet(w, r, operation.JoinResult{Error: "Cannot unmarshal \"volumes\": " + err.Error()})
  41. return
  42. }
  43. debug(s, "volumes", r.FormValue("volumes"))
  44. ms.Topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
  45. writeJsonQuiet(w, r, operation.JoinResult{VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024})
  46. }
  47. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  48. m := make(map[string]interface{})
  49. m["Version"] = util.VERSION
  50. m["Topology"] = ms.Topo.ToMap()
  51. writeJsonQuiet(w, r, m)
  52. }
  53. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  54. gcThreshold := r.FormValue("garbageThreshold")
  55. if gcThreshold == "" {
  56. gcThreshold = ms.garbageThreshold
  57. }
  58. debug("garbageThreshold =", gcThreshold)
  59. ms.Topo.Vacuum(gcThreshold)
  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. w.WriteHeader(http.StatusNotAcceptable)
  67. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  68. return
  69. }
  70. if err == nil {
  71. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  72. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  73. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  74. } else {
  75. count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
  76. }
  77. } else {
  78. err = errors.New("parameter count is not found")
  79. }
  80. }
  81. if err != nil {
  82. w.WriteHeader(http.StatusNotAcceptable)
  83. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  84. } else {
  85. w.WriteHeader(http.StatusNotAcceptable)
  86. writeJsonQuiet(w, r, map[string]interface{}{"count": count})
  87. }
  88. }
  89. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  90. m := make(map[string]interface{})
  91. m["Version"] = util.VERSION
  92. m["Volumes"] = ms.Topo.ToVolumeMap()
  93. writeJsonQuiet(w, r, m)
  94. }
  95. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  96. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  97. volumeId, err := storage.NewVolumeId(vid)
  98. if err != nil {
  99. debug("parsing error:", err, r.URL.Path)
  100. return
  101. }
  102. machines := ms.Topo.Lookup("", volumeId)
  103. if machines != nil && len(machines) > 0 {
  104. http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  105. } else {
  106. w.WriteHeader(http.StatusNotFound)
  107. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  108. }
  109. }
  110. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  111. if ms.Topo.IsLeader() {
  112. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  113. } else {
  114. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  115. }
  116. }
  117. func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  118. if ms.Topo.IsLeader() {
  119. deleteForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  120. } else {
  121. deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  122. }
  123. }
  124. func (ms *MasterServer) hasWriableVolume(option *topology.VolumeGrowOption) bool {
  125. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement)
  126. return vl.GetActiveVolumeCount(option) > 0
  127. }
  128. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  129. replicationString := r.FormValue("replication")
  130. if replicationString == "" {
  131. replicationString = ms.defaultReplicaPlacement
  132. }
  133. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  134. if err != nil {
  135. return nil, err
  136. }
  137. volumeGrowOption := &topology.VolumeGrowOption{
  138. Collection: r.FormValue("collection"),
  139. ReplicaPlacement: replicaPlacement,
  140. DataCenter: r.FormValue("dataCenter"),
  141. Rack: r.FormValue("rack"),
  142. DataNode: r.FormValue("dataNode"),
  143. }
  144. return volumeGrowOption, nil
  145. }