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.

171 lines
5.5 KiB

10 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/weed-fs/go/glog"
  11. "github.com/chrislusf/weed-fs/go/operation"
  12. "github.com/chrislusf/weed-fs/go/storage"
  13. "github.com/chrislusf/weed-fs/go/topology"
  14. "github.com/chrislusf/weed-fs/go/util"
  15. "github.com/golang/protobuf/proto"
  16. )
  17. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  18. collection, ok := ms.Topo.GetCollection(r.FormValue("collection"))
  19. if !ok {
  20. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist!", r.FormValue("collection")))
  21. return
  22. }
  23. for _, server := range collection.ListVolumeServers() {
  24. _, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
  25. if err != nil {
  26. writeJsonError(w, r, http.StatusInternalServerError, err)
  27. return
  28. }
  29. }
  30. ms.Topo.DeleteCollection(r.FormValue("collection"))
  31. }
  32. func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
  33. body, err := ioutil.ReadAll(r.Body)
  34. if err != nil {
  35. writeJsonError(w, r, http.StatusBadRequest, err)
  36. return
  37. }
  38. joinMessage := &operation.JoinMessage{}
  39. if err = proto.Unmarshal(body, joinMessage); err != nil {
  40. writeJsonError(w, r, http.StatusBadRequest, err)
  41. return
  42. }
  43. if *joinMessage.Ip == "" {
  44. *joinMessage.Ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
  45. }
  46. if glog.V(4) {
  47. if jsonData, jsonError := json.Marshal(joinMessage); jsonError != nil {
  48. glog.V(0).Infoln("json marshaling error: ", jsonError)
  49. writeJsonError(w, r, http.StatusBadRequest, jsonError)
  50. return
  51. } else {
  52. glog.V(4).Infoln("Proto size", len(body), "json size", len(jsonData), string(jsonData))
  53. }
  54. }
  55. ms.Topo.ProcessJoinMessage(joinMessage)
  56. writeJsonQuiet(w, r, http.StatusOK, operation.JoinResult{VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024})
  57. }
  58. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  59. m := make(map[string]interface{})
  60. m["Version"] = util.VERSION
  61. m["Topology"] = ms.Topo.ToMap()
  62. writeJsonQuiet(w, r, http.StatusOK, m)
  63. }
  64. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  65. gcThreshold := r.FormValue("garbageThreshold")
  66. if gcThreshold == "" {
  67. gcThreshold = ms.garbageThreshold
  68. }
  69. debug("garbageThreshold =", gcThreshold)
  70. ms.Topo.Vacuum(gcThreshold)
  71. ms.dirStatusHandler(w, r)
  72. }
  73. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  74. count := 0
  75. option, err := ms.getVolumeGrowOption(r)
  76. if err != nil {
  77. writeJsonError(w, r, http.StatusNotAcceptable, err)
  78. return
  79. }
  80. if err == nil {
  81. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  82. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  83. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  84. } else {
  85. count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
  86. }
  87. } else {
  88. err = errors.New("parameter count is not found")
  89. }
  90. }
  91. if err != nil {
  92. writeJsonError(w, r, http.StatusNotAcceptable, err)
  93. } else {
  94. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  95. }
  96. }
  97. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  98. m := make(map[string]interface{})
  99. m["Version"] = util.VERSION
  100. m["Volumes"] = ms.Topo.ToVolumeMap()
  101. writeJsonQuiet(w, r, http.StatusOK, m)
  102. }
  103. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  104. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  105. volumeId, err := storage.NewVolumeId(vid)
  106. if err != nil {
  107. debug("parsing error:", err, r.URL.Path)
  108. return
  109. }
  110. machines := ms.Topo.Lookup("", volumeId)
  111. if machines != nil && len(machines) > 0 {
  112. http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  113. } else {
  114. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d not found.", volumeId))
  115. }
  116. }
  117. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  118. if ms.Topo.IsLeader() {
  119. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  120. } else {
  121. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  122. }
  123. }
  124. func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  125. if ms.Topo.IsLeader() {
  126. deleteForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  127. } else {
  128. deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  129. }
  130. }
  131. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  132. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  133. return vl.GetActiveVolumeCount(option) > 0
  134. }
  135. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  136. replicationString := r.FormValue("replication")
  137. if replicationString == "" {
  138. replicationString = ms.defaultReplicaPlacement
  139. }
  140. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  141. if err != nil {
  142. return nil, err
  143. }
  144. ttl, err := storage.ReadTTL(r.FormValue("ttl"))
  145. if err != nil {
  146. return nil, err
  147. }
  148. volumeGrowOption := &topology.VolumeGrowOption{
  149. Collection: r.FormValue("collection"),
  150. ReplicaPlacement: replicaPlacement,
  151. Ttl: ttl,
  152. DataCenter: r.FormValue("dataCenter"),
  153. Rack: r.FormValue("rack"),
  154. DataNode: r.FormValue("dataNode"),
  155. }
  156. return volumeGrowOption, nil
  157. }