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.

191 lines
6.7 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/stats"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "code.google.com/p/weed-fs/go/util"
  6. "encoding/json"
  7. "errors"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. )
  12. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  13. vid := r.FormValue("volumeId")
  14. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  15. commaSep := strings.Index(vid, ",")
  16. if commaSep > 0 {
  17. vid = vid[0:commaSep]
  18. }
  19. volumeId, err := storage.NewVolumeId(vid)
  20. if err == nil {
  21. machines := ms.Topo.Lookup(collection, volumeId)
  22. if machines != nil {
  23. ret := []map[string]string{}
  24. for _, dn := range machines {
  25. ret = append(ret, map[string]string{"url": dn.Url(), "publicUrl": dn.PublicUrl})
  26. }
  27. writeJsonQuiet(w, r, map[string]interface{}{"locations": ret})
  28. } else {
  29. w.WriteHeader(http.StatusNotFound)
  30. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  31. }
  32. } else {
  33. w.WriteHeader(http.StatusNotAcceptable)
  34. writeJsonQuiet(w, r, map[string]string{"error": "unknown volumeId format " + vid})
  35. }
  36. }
  37. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  38. stats.AssignRequest()
  39. c, e := strconv.Atoi(r.FormValue("count"))
  40. if e != nil {
  41. c = 1
  42. }
  43. replication := r.FormValue("replication")
  44. if replication == "" {
  45. replication = ms.defaultReplicaPlacement
  46. }
  47. collection := r.FormValue("collection")
  48. dataCenter := r.FormValue("dataCenter")
  49. replicaPlacement, err := storage.NewReplicaPlacementFromString(replication)
  50. if err != nil {
  51. w.WriteHeader(http.StatusNotAcceptable)
  52. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  53. return
  54. }
  55. if ms.Topo.GetVolumeLayout(collection, replicaPlacement).GetActiveVolumeCount(dataCenter) <= 0 {
  56. if ms.Topo.FreeSpace() <= 0 {
  57. w.WriteHeader(http.StatusNotFound)
  58. writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
  59. return
  60. } else {
  61. ms.vgLock.Lock()
  62. defer ms.vgLock.Unlock()
  63. if ms.Topo.GetVolumeLayout(collection, replicaPlacement).GetActiveVolumeCount(dataCenter) <= 0 {
  64. if _, err = ms.vg.AutomaticGrowByType(collection, replicaPlacement, dataCenter, ms.Topo); err != nil {
  65. writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
  66. return
  67. }
  68. }
  69. }
  70. }
  71. fid, count, dn, err := ms.Topo.PickForWrite(collection, replicaPlacement, c, dataCenter)
  72. if err == nil {
  73. writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
  74. } else {
  75. w.WriteHeader(http.StatusNotAcceptable)
  76. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  77. }
  78. }
  79. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  80. collection, ok := ms.Topo.GetCollection(r.FormValue("collection"))
  81. if !ok {
  82. writeJsonQuiet(w, r, map[string]interface{}{"error": "collection " + r.FormValue("collection") + "does not exist!"})
  83. return
  84. }
  85. for _, server := range collection.ListVolumeServers() {
  86. _, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
  87. if err != nil {
  88. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  89. return
  90. }
  91. }
  92. ms.Topo.DeleteCollection(r.FormValue("collection"))
  93. }
  94. func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
  95. init := r.FormValue("init") == "true"
  96. ip := r.FormValue("ip")
  97. if ip == "" {
  98. ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
  99. }
  100. port, _ := strconv.Atoi(r.FormValue("port"))
  101. maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
  102. s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
  103. publicUrl := r.FormValue("publicUrl")
  104. volumes := new([]storage.VolumeInfo)
  105. if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
  106. writeJsonQuiet(w, r, map[string]string{"error": "Cannot unmarshal \"volumes\": " + err.Error()})
  107. return
  108. }
  109. debug(s, "volumes", r.FormValue("volumes"))
  110. ms.Topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
  111. m := make(map[string]interface{})
  112. m["VolumeSizeLimit"] = uint64(ms.volumeSizeLimitMB) * 1024 * 1024
  113. writeJsonQuiet(w, r, m)
  114. }
  115. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  116. m := make(map[string]interface{})
  117. m["Version"] = util.VERSION
  118. m["Topology"] = ms.Topo.ToMap()
  119. writeJsonQuiet(w, r, m)
  120. }
  121. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  122. gcThreshold := r.FormValue("garbageThreshold")
  123. if gcThreshold == "" {
  124. gcThreshold = ms.garbageThreshold
  125. }
  126. debug("garbageThreshold =", gcThreshold)
  127. ms.Topo.Vacuum(gcThreshold)
  128. ms.dirStatusHandler(w, r)
  129. }
  130. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  131. count := 0
  132. replicaPlacement, err := storage.NewReplicaPlacementFromString(r.FormValue("replication"))
  133. if err == nil {
  134. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  135. if ms.Topo.FreeSpace() < count*replicaPlacement.GetCopyCount() {
  136. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*replicaPlacement.GetCopyCount()))
  137. } else {
  138. count, err = ms.vg.GrowByCountAndType(count, r.FormValue("collection"), replicaPlacement, r.FormValue("dataCenter"), ms.Topo)
  139. }
  140. } else {
  141. err = errors.New("parameter count is not found")
  142. }
  143. }
  144. if err != nil {
  145. w.WriteHeader(http.StatusNotAcceptable)
  146. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  147. } else {
  148. w.WriteHeader(http.StatusNotAcceptable)
  149. writeJsonQuiet(w, r, map[string]interface{}{"count": count})
  150. }
  151. }
  152. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  153. m := make(map[string]interface{})
  154. m["Version"] = util.VERSION
  155. m["Volumes"] = ms.Topo.ToVolumeMap()
  156. writeJsonQuiet(w, r, m)
  157. }
  158. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  159. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  160. volumeId, err := storage.NewVolumeId(vid)
  161. if err != nil {
  162. debug("parsing error:", err, r.URL.Path)
  163. return
  164. }
  165. machines := ms.Topo.Lookup("", volumeId)
  166. if machines != nil && len(machines) > 0 {
  167. http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  168. } else {
  169. w.WriteHeader(http.StatusNotFound)
  170. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  171. }
  172. }
  173. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  174. if ms.Topo.IsLeader() {
  175. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  176. } else {
  177. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  178. }
  179. }