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.

178 lines
5.6 KiB

10 years ago
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{
  57. VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024,
  58. SecretKey: string(ms.guard.SecretKey),
  59. })
  60. }
  61. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  62. m := make(map[string]interface{})
  63. m["Version"] = util.VERSION
  64. m["Topology"] = ms.Topo.ToMap()
  65. writeJsonQuiet(w, r, http.StatusOK, m)
  66. }
  67. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  68. gcThreshold := r.FormValue("garbageThreshold")
  69. if gcThreshold == "" {
  70. gcThreshold = ms.garbageThreshold
  71. }
  72. debug("garbageThreshold =", gcThreshold)
  73. ms.Topo.Vacuum(gcThreshold)
  74. ms.dirStatusHandler(w, r)
  75. }
  76. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  77. count := 0
  78. option, err := ms.getVolumeGrowOption(r)
  79. if err != nil {
  80. writeJsonError(w, r, http.StatusNotAcceptable, err)
  81. return
  82. }
  83. if err == nil {
  84. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  85. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  86. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  87. } else {
  88. count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
  89. }
  90. } else {
  91. err = errors.New("parameter count is not found")
  92. }
  93. }
  94. if err != nil {
  95. writeJsonError(w, r, http.StatusNotAcceptable, err)
  96. } else {
  97. writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
  98. }
  99. }
  100. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  101. m := make(map[string]interface{})
  102. m["Version"] = util.VERSION
  103. m["Volumes"] = ms.Topo.ToVolumeMap()
  104. writeJsonQuiet(w, r, http.StatusOK, m)
  105. }
  106. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  107. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  108. volumeId, err := storage.NewVolumeId(vid)
  109. if err != nil {
  110. debug("parsing error:", err, r.URL.Path)
  111. return
  112. }
  113. machines := ms.Topo.Lookup("", volumeId)
  114. if machines != nil && len(machines) > 0 {
  115. url := machines[0].PublicUrl
  116. if url == "" {
  117. url = machines[0].Url()
  118. }
  119. http.Redirect(w, r, "http://"+url+r.URL.Path, http.StatusMovedPermanently)
  120. } else {
  121. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d not found.", volumeId))
  122. }
  123. }
  124. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  125. if ms.Topo.IsLeader() {
  126. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  127. } else {
  128. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  129. }
  130. }
  131. func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  132. if ms.Topo.IsLeader() {
  133. deleteForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  134. } else {
  135. deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  136. }
  137. }
  138. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  139. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  140. return vl.GetActiveVolumeCount(option) > 0
  141. }
  142. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  143. replicationString := r.FormValue("replication")
  144. if replicationString == "" {
  145. replicationString = ms.defaultReplicaPlacement
  146. }
  147. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  148. if err != nil {
  149. return nil, err
  150. }
  151. ttl, err := storage.ReadTTL(r.FormValue("ttl"))
  152. if err != nil {
  153. return nil, err
  154. }
  155. volumeGrowOption := &topology.VolumeGrowOption{
  156. Collection: r.FormValue("collection"),
  157. ReplicaPlacement: replicaPlacement,
  158. Ttl: ttl,
  159. DataCenter: r.FormValue("dataCenter"),
  160. Rack: r.FormValue("rack"),
  161. DataNode: r.FormValue("dataNode"),
  162. }
  163. return volumeGrowOption, nil
  164. }