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.

174 lines
5.5 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. http.Redirect(w, r, "http://"+machines[0].Url()+r.URL.Path, http.StatusMovedPermanently)
  116. } else {
  117. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d not found.", volumeId))
  118. }
  119. }
  120. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  121. if ms.Topo.IsLeader() {
  122. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  123. } else {
  124. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  125. }
  126. }
  127. func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  128. if ms.Topo.IsLeader() {
  129. deleteForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  130. } else {
  131. deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  132. }
  133. }
  134. func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
  135. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  136. return vl.GetActiveVolumeCount(option) > 0
  137. }
  138. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  139. replicationString := r.FormValue("replication")
  140. if replicationString == "" {
  141. replicationString = ms.defaultReplicaPlacement
  142. }
  143. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  144. if err != nil {
  145. return nil, err
  146. }
  147. ttl, err := storage.ReadTTL(r.FormValue("ttl"))
  148. if err != nil {
  149. return nil, err
  150. }
  151. volumeGrowOption := &topology.VolumeGrowOption{
  152. Collection: r.FormValue("collection"),
  153. ReplicaPlacement: replicaPlacement,
  154. Ttl: ttl,
  155. DataCenter: r.FormValue("dataCenter"),
  156. Rack: r.FormValue("rack"),
  157. DataNode: r.FormValue("dataNode"),
  158. }
  159. return volumeGrowOption, nil
  160. }