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.

216 lines
7.3 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/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) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  14. vid := r.FormValue("volumeId")
  15. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  16. commaSep := strings.Index(vid, ",")
  17. if commaSep > 0 {
  18. vid = vid[0:commaSep]
  19. }
  20. volumeId, err := storage.NewVolumeId(vid)
  21. if err == nil {
  22. machines := ms.Topo.Lookup(collection, volumeId)
  23. if machines != nil {
  24. ret := []map[string]string{}
  25. for _, dn := range machines {
  26. ret = append(ret, map[string]string{"url": dn.Url(), "publicUrl": dn.PublicUrl})
  27. }
  28. writeJsonQuiet(w, r, map[string]interface{}{"locations": ret})
  29. } else {
  30. w.WriteHeader(http.StatusNotFound)
  31. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  32. }
  33. } else {
  34. w.WriteHeader(http.StatusNotAcceptable)
  35. writeJsonQuiet(w, r, map[string]string{"error": "unknown volumeId format " + vid})
  36. }
  37. }
  38. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  39. stats.AssignRequest()
  40. requestedCount, e := strconv.Atoi(r.FormValue("count"))
  41. if e != nil {
  42. requestedCount = 1
  43. }
  44. option, err := ms.getVolumeGrowOption(r)
  45. if err != nil {
  46. w.WriteHeader(http.StatusNotAcceptable)
  47. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  48. return
  49. }
  50. if !ms.Topo.HasWriableVolume(option) {
  51. if ms.Topo.FreeSpace() <= 0 {
  52. w.WriteHeader(http.StatusNotFound)
  53. writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
  54. return
  55. } else {
  56. ms.vgLock.Lock()
  57. defer ms.vgLock.Unlock()
  58. if !ms.Topo.HasWriableVolume(option) {
  59. if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
  60. writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
  61. return
  62. }
  63. }
  64. }
  65. }
  66. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  67. if err == nil {
  68. writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
  69. } else {
  70. w.WriteHeader(http.StatusNotAcceptable)
  71. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  72. }
  73. }
  74. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  75. collection, ok := ms.Topo.GetCollection(r.FormValue("collection"))
  76. if !ok {
  77. writeJsonQuiet(w, r, map[string]interface{}{"error": "collection " + r.FormValue("collection") + "does not exist!"})
  78. return
  79. }
  80. for _, server := range collection.ListVolumeServers() {
  81. _, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
  82. if err != nil {
  83. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  84. return
  85. }
  86. }
  87. ms.Topo.DeleteCollection(r.FormValue("collection"))
  88. }
  89. func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
  90. init := r.FormValue("init") == "true"
  91. ip := r.FormValue("ip")
  92. if ip == "" {
  93. ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
  94. }
  95. port, _ := strconv.Atoi(r.FormValue("port"))
  96. maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
  97. s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
  98. publicUrl := r.FormValue("publicUrl")
  99. volumes := new([]storage.VolumeInfo)
  100. if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
  101. writeJsonQuiet(w, r, map[string]string{"error": "Cannot unmarshal \"volumes\": " + err.Error()})
  102. return
  103. }
  104. debug(s, "volumes", r.FormValue("volumes"))
  105. ms.Topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
  106. m := make(map[string]interface{})
  107. m["VolumeSizeLimit"] = uint64(ms.volumeSizeLimitMB) * 1024 * 1024
  108. writeJsonQuiet(w, r, m)
  109. }
  110. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  111. m := make(map[string]interface{})
  112. m["Version"] = util.VERSION
  113. m["Topology"] = ms.Topo.ToMap()
  114. writeJsonQuiet(w, r, m)
  115. }
  116. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  117. gcThreshold := r.FormValue("garbageThreshold")
  118. if gcThreshold == "" {
  119. gcThreshold = ms.garbageThreshold
  120. }
  121. debug("garbageThreshold =", gcThreshold)
  122. ms.Topo.Vacuum(gcThreshold)
  123. ms.dirStatusHandler(w, r)
  124. }
  125. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  126. count := 0
  127. option, err := ms.getVolumeGrowOption(r)
  128. if err != nil {
  129. w.WriteHeader(http.StatusNotAcceptable)
  130. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  131. return
  132. }
  133. if err == nil {
  134. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  135. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  136. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  137. } else {
  138. count, err = ms.vg.GrowByCountAndType(count, option, 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. }
  180. func (ms *MasterServer) hasWriableVolume(option *topology.VolumeGrowOption) bool {
  181. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement)
  182. return vl.GetActiveVolumeCount(option) > 0
  183. }
  184. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  185. replicationString := r.FormValue("replication")
  186. if replicationString == "" {
  187. replicationString = ms.defaultReplicaPlacement
  188. }
  189. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  190. if err != nil {
  191. return nil, err
  192. }
  193. volumeGrowOption := &topology.VolumeGrowOption{
  194. Collection: r.FormValue("collection"),
  195. ReplicaPlacement: replicaPlacement,
  196. DataCenter: r.FormValue("dataCenter"),
  197. Rack: r.FormValue("rack"),
  198. DataNode: r.FormValue("dataNode"),
  199. }
  200. return volumeGrowOption, nil
  201. }