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.

189 lines
6.7 KiB

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