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
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. proto "code.google.com/p/goprotobuf/proto"
  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. )
  16. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  17. collection, ok := ms.Topo.GetCollection(r.FormValue("collection"))
  18. if !ok {
  19. writeJsonQuiet(w, r, map[string]interface{}{"error": "collection " + r.FormValue("collection") + "does not exist!"})
  20. return
  21. }
  22. for _, server := range collection.ListVolumeServers() {
  23. _, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
  24. if err != nil {
  25. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  26. return
  27. }
  28. }
  29. ms.Topo.DeleteCollection(r.FormValue("collection"))
  30. }
  31. func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
  32. body, err := ioutil.ReadAll(r.Body)
  33. if err != nil {
  34. writeJsonError(w, r, err)
  35. return
  36. }
  37. joinMessage := &operation.JoinMessage{}
  38. if err = proto.Unmarshal(body, joinMessage); err != nil {
  39. writeJsonError(w, r, err)
  40. return
  41. }
  42. if *joinMessage.Ip == "" {
  43. *joinMessage.Ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
  44. }
  45. if glog.V(4) {
  46. if jsonData, jsonError := json.Marshal(joinMessage); jsonError != nil {
  47. glog.V(0).Infoln("json marshaling error: ", jsonError)
  48. writeJsonError(w, r, jsonError)
  49. return
  50. } else {
  51. glog.V(4).Infoln("Proto size", len(body), "json size", len(jsonData), string(jsonData))
  52. }
  53. }
  54. ms.Topo.ProcessJoinMessage(joinMessage)
  55. writeJsonQuiet(w, r, operation.JoinResult{VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024})
  56. }
  57. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  58. m := make(map[string]interface{})
  59. m["Version"] = util.VERSION
  60. m["Topology"] = ms.Topo.ToMap()
  61. writeJsonQuiet(w, r, m)
  62. }
  63. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  64. gcThreshold := r.FormValue("garbageThreshold")
  65. if gcThreshold == "" {
  66. gcThreshold = ms.garbageThreshold
  67. }
  68. debug("garbageThreshold =", gcThreshold)
  69. ms.Topo.Vacuum(gcThreshold)
  70. ms.dirStatusHandler(w, r)
  71. }
  72. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  73. count := 0
  74. option, err := ms.getVolumeGrowOption(r)
  75. if err != nil {
  76. w.WriteHeader(http.StatusNotAcceptable)
  77. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  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. w.WriteHeader(http.StatusNotAcceptable)
  93. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  94. } else {
  95. w.WriteHeader(http.StatusOK)
  96. writeJsonQuiet(w, r, map[string]interface{}{"count": count})
  97. }
  98. }
  99. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  100. m := make(map[string]interface{})
  101. m["Version"] = util.VERSION
  102. m["Volumes"] = ms.Topo.ToVolumeMap()
  103. writeJsonQuiet(w, r, m)
  104. }
  105. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  106. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  107. volumeId, err := storage.NewVolumeId(vid)
  108. if err != nil {
  109. debug("parsing error:", err, r.URL.Path)
  110. return
  111. }
  112. machines := ms.Topo.Lookup("", volumeId)
  113. if machines != nil && len(machines) > 0 {
  114. http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  115. } else {
  116. w.WriteHeader(http.StatusNotFound)
  117. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  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. }