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.

95 lines
3.0 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. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  57. stats.AssignRequest()
  58. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  59. if e != nil || requestedCount == 0 {
  60. requestedCount = 1
  61. }
  62. option, err := ms.getVolumeGrowOption(r)
  63. if err != nil {
  64. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  65. return
  66. }
  67. if !ms.Topo.HasWritableVolume(option) {
  68. if ms.Topo.FreeSpace() <= 0 {
  69. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left!"})
  70. return
  71. }
  72. ms.vgLock.Lock()
  73. defer ms.vgLock.Unlock()
  74. if !ms.Topo.HasWritableVolume(option) {
  75. if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
  76. writeJsonError(w, r, http.StatusInternalServerError,
  77. fmt.Errorf("Cannot grow volume group! %v", err))
  78. return
  79. }
  80. }
  81. }
  82. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  83. if err == nil {
  84. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  85. } else {
  86. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  87. }
  88. }