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.

105 lines
3.3 KiB

10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "github.com/chrislusf/weed-fs/go/operation"
  7. "github.com/chrislusf/weed-fs/go/stats"
  8. "github.com/chrislusf/weed-fs/go/storage"
  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. writeJsonQuiet(w, r, volumeLocations)
  61. }
  62. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  63. stats.AssignRequest()
  64. requestedCount, e := strconv.Atoi(r.FormValue("count"))
  65. if e != nil {
  66. requestedCount = 1
  67. }
  68. option, err := ms.getVolumeGrowOption(r)
  69. if err != nil {
  70. w.WriteHeader(http.StatusNotAcceptable)
  71. writeJsonQuiet(w, r, operation.AssignResult{Error: err.Error()})
  72. return
  73. }
  74. if !ms.Topo.HasWritableVolume(option) {
  75. if ms.Topo.FreeSpace() <= 0 {
  76. w.WriteHeader(http.StatusNotFound)
  77. writeJsonQuiet(w, r, operation.AssignResult{Error: "No free volumes left!"})
  78. return
  79. } else {
  80. ms.vgLock.Lock()
  81. defer ms.vgLock.Unlock()
  82. if !ms.Topo.HasWritableVolume(option) {
  83. if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
  84. writeJsonQuiet(w, r, operation.AssignResult{Error: "Cannot grow volume group! " + err.Error()})
  85. return
  86. }
  87. }
  88. }
  89. }
  90. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  91. if err == nil {
  92. writeJsonQuiet(w, r, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  93. } else {
  94. w.WriteHeader(http.StatusNotAcceptable)
  95. writeJsonQuiet(w, r, operation.AssignResult{Error: err.Error()})
  96. }
  97. }