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.

118 lines
3.7 KiB

6 years ago
6 years ago
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/security"
  9. "github.com/chrislusf/seaweedfs/weed/stats"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. )
  12. func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
  13. volumeLocations = make(map[string]operation.LookupResult)
  14. for _, vid := range vids {
  15. commaSep := strings.Index(vid, ",")
  16. if commaSep > 0 {
  17. vid = vid[0:commaSep]
  18. }
  19. if _, ok := volumeLocations[vid]; ok {
  20. continue
  21. }
  22. volumeId, err := needle.NewVolumeId(vid)
  23. if err == nil {
  24. machines := ms.Topo.Lookup(collection, volumeId)
  25. if machines != nil {
  26. var ret []operation.Location
  27. for _, dn := range machines {
  28. ret = append(ret, operation.Location{Url: dn.Url(), PublicUrl: dn.PublicUrl})
  29. }
  30. volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Locations: ret}
  31. } else {
  32. volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: fmt.Sprintf("volumeId %s not found.", vid)}
  33. }
  34. } else {
  35. volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: fmt.Sprintf("Unknown volumeId format: %s", vid)}
  36. }
  37. }
  38. return
  39. }
  40. // If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
  41. // If "volumeId" is provided, this only returns the volumeId location
  42. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  43. vid := r.FormValue("volumeId")
  44. if vid != "" {
  45. // backward compatible
  46. commaSep := strings.Index(vid, ",")
  47. if commaSep > 0 {
  48. vid = vid[0:commaSep]
  49. }
  50. }
  51. fileId := r.FormValue("fileId")
  52. if fileId != "" {
  53. commaSep := strings.Index(fileId, ",")
  54. if commaSep > 0 {
  55. vid = fileId[0:commaSep]
  56. }
  57. }
  58. vids := []string{vid}
  59. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  60. volumeLocations := ms.lookupVolumeId(vids, collection)
  61. location := volumeLocations[vid]
  62. httpStatus := http.StatusOK
  63. if location.Error != "" {
  64. httpStatus = http.StatusNotFound
  65. } else {
  66. ms.maybeAddJwtAuthorization(w, fileId)
  67. }
  68. writeJsonQuiet(w, r, httpStatus, location)
  69. }
  70. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  71. stats.AssignRequest()
  72. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  73. if e != nil || requestedCount == 0 {
  74. requestedCount = 1
  75. }
  76. option, err := ms.getVolumeGrowOption(r)
  77. if err != nil {
  78. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  79. return
  80. }
  81. if !ms.Topo.HasWritableVolume(option) {
  82. if ms.Topo.FreeSpace() <= 0 {
  83. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left!"})
  84. return
  85. }
  86. ms.vgLock.Lock()
  87. defer ms.vgLock.Unlock()
  88. if !ms.Topo.HasWritableVolume(option) {
  89. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOpiton, ms.Topo); err != nil {
  90. writeJsonError(w, r, http.StatusInternalServerError,
  91. fmt.Errorf("Cannot grow volume group! %v", err))
  92. return
  93. }
  94. }
  95. }
  96. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  97. if err == nil {
  98. ms.maybeAddJwtAuthorization(w, fid)
  99. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  100. } else {
  101. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  102. }
  103. }
  104. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string) {
  105. encodedJwt := security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  106. if encodedJwt == "" {
  107. return
  108. }
  109. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  110. }