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.

158 lines
5.5 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. maxFileKey, _ := strconv.ParseUint(r.FormValue("maxFileKey"), 10, 64)
  37. s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
  38. publicUrl := r.FormValue("publicUrl")
  39. volumes := new([]storage.VolumeInfo)
  40. if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
  41. writeJsonQuiet(w, r, operation.JoinResult{Error: "Cannot unmarshal \"volumes\": " + err.Error()})
  42. return
  43. }
  44. debug(s, "volumes", r.FormValue("volumes"))
  45. ms.Topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, maxFileKey, r.FormValue("dataCenter"), r.FormValue("rack"))
  46. writeJsonQuiet(w, r, operation.JoinResult{VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024})
  47. }
  48. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  49. m := make(map[string]interface{})
  50. m["Version"] = util.VERSION
  51. m["Topology"] = ms.Topo.ToMap()
  52. writeJsonQuiet(w, r, m)
  53. }
  54. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  55. gcThreshold := r.FormValue("garbageThreshold")
  56. if gcThreshold == "" {
  57. gcThreshold = ms.garbageThreshold
  58. }
  59. debug("garbageThreshold =", gcThreshold)
  60. ms.Topo.Vacuum(gcThreshold)
  61. ms.dirStatusHandler(w, r)
  62. }
  63. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  64. count := 0
  65. option, err := ms.getVolumeGrowOption(r)
  66. if err != nil {
  67. w.WriteHeader(http.StatusNotAcceptable)
  68. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  69. return
  70. }
  71. if err == nil {
  72. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  73. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  74. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  75. } else {
  76. count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
  77. }
  78. } else {
  79. err = errors.New("parameter count is not found")
  80. }
  81. }
  82. if err != nil {
  83. w.WriteHeader(http.StatusNotAcceptable)
  84. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  85. } else {
  86. w.WriteHeader(http.StatusNotAcceptable)
  87. writeJsonQuiet(w, r, map[string]interface{}{"count": count})
  88. }
  89. }
  90. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  91. m := make(map[string]interface{})
  92. m["Version"] = util.VERSION
  93. m["Volumes"] = ms.Topo.ToVolumeMap()
  94. writeJsonQuiet(w, r, m)
  95. }
  96. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  97. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  98. volumeId, err := storage.NewVolumeId(vid)
  99. if err != nil {
  100. debug("parsing error:", err, r.URL.Path)
  101. return
  102. }
  103. machines := ms.Topo.Lookup("", volumeId)
  104. if machines != nil && len(machines) > 0 {
  105. http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  106. } else {
  107. w.WriteHeader(http.StatusNotFound)
  108. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  109. }
  110. }
  111. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  112. if ms.Topo.IsLeader() {
  113. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  114. } else {
  115. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  116. }
  117. }
  118. func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  119. if ms.Topo.IsLeader() {
  120. deleteForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  121. } else {
  122. deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  123. }
  124. }
  125. func (ms *MasterServer) hasWriableVolume(option *topology.VolumeGrowOption) bool {
  126. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement)
  127. return vl.GetActiveVolumeCount(option) > 0
  128. }
  129. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  130. replicationString := r.FormValue("replication")
  131. if replicationString == "" {
  132. replicationString = ms.defaultReplicaPlacement
  133. }
  134. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  135. if err != nil {
  136. return nil, err
  137. }
  138. volumeGrowOption := &topology.VolumeGrowOption{
  139. Collection: r.FormValue("collection"),
  140. ReplicaPlacement: replicaPlacement,
  141. DataCenter: r.FormValue("dataCenter"),
  142. Rack: r.FormValue("rack"),
  143. DataNode: r.FormValue("dataNode"),
  144. }
  145. return volumeGrowOption, nil
  146. }