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.

168 lines
5.8 KiB

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