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.

104 lines
3.4 KiB

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