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.

108 lines
3.3 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/operation"
  4. "code.google.com/p/weed-fs/go/stats"
  5. "code.google.com/p/weed-fs/go/storage"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. )
  10. func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
  11. volumeLocations = make(map[string]operation.LookupResult)
  12. for _, vid := range vids {
  13. commaSep := strings.Index(vid, ",")
  14. if commaSep > 0 {
  15. vid = vid[0:commaSep]
  16. }
  17. if _, ok := volumeLocations[vid]; ok {
  18. continue
  19. }
  20. volumeId, err := storage.NewVolumeId(vid)
  21. if err == nil {
  22. machines := ms.Topo.Lookup(collection, volumeId)
  23. if machines != nil {
  24. var ret []operation.Location
  25. for _, dn := range machines {
  26. ret = append(ret, operation.Location{Url: dn.Url(), PublicUrl: dn.PublicUrl})
  27. }
  28. volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Locations: ret}
  29. } else {
  30. volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: "volumeId not found."}
  31. }
  32. } else {
  33. volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: "Unknown volumeId format."}
  34. }
  35. }
  36. return
  37. }
  38. // Takes one volumeId only, can not do batch lookup
  39. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  40. vid := r.FormValue("volumeId")
  41. commaSep := strings.Index(vid, ",")
  42. if commaSep > 0 {
  43. vid = vid[0:commaSep]
  44. }
  45. vids := []string{vid}
  46. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  47. volumeLocations := ms.lookupVolumeId(vids, collection)
  48. location := volumeLocations[vid]
  49. if location.Error != "" {
  50. w.WriteHeader(http.StatusNotFound)
  51. }
  52. writeJsonQuiet(w, r, location)
  53. }
  54. // This can take batched volumeIds, &volumeId=x&volumeId=y&volumeId=z
  55. func (ms *MasterServer) volumeLookupHandler(w http.ResponseWriter, r *http.Request) {
  56. r.ParseForm()
  57. vids := r.Form["volumeId"]
  58. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  59. volumeLocations := ms.lookupVolumeId(vids, collection)
  60. var ret []operation.LookupResult
  61. for _, volumeLocation := range volumeLocations {
  62. ret = append(ret, volumeLocation)
  63. }
  64. writeJsonQuiet(w, r, ret)
  65. }
  66. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  67. stats.AssignRequest()
  68. requestedCount, e := strconv.Atoi(r.FormValue("count"))
  69. if e != nil {
  70. requestedCount = 1
  71. }
  72. option, err := ms.getVolumeGrowOption(r)
  73. if err != nil {
  74. w.WriteHeader(http.StatusNotAcceptable)
  75. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  76. return
  77. }
  78. if !ms.Topo.HasWriableVolume(option) {
  79. if ms.Topo.FreeSpace() <= 0 {
  80. w.WriteHeader(http.StatusNotFound)
  81. writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
  82. return
  83. } else {
  84. ms.vgLock.Lock()
  85. defer ms.vgLock.Unlock()
  86. if !ms.Topo.HasWriableVolume(option) {
  87. if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
  88. writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
  89. return
  90. }
  91. }
  92. }
  93. }
  94. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  95. if err == nil {
  96. writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
  97. } else {
  98. w.WriteHeader(http.StatusNotAcceptable)
  99. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  100. }
  101. }