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.

126 lines
3.9 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. forRead := r.FormValue("read")
  67. isRead := forRead == "yes"
  68. ms.maybeAddJwtAuthorization(w, fileId, !isRead)
  69. }
  70. writeJsonQuiet(w, r, httpStatus, location)
  71. }
  72. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  73. stats.AssignRequest()
  74. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  75. if e != nil || requestedCount == 0 {
  76. requestedCount = 1
  77. }
  78. option, err := ms.getVolumeGrowOption(r)
  79. if err != nil {
  80. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  81. return
  82. }
  83. if !ms.Topo.HasWritableVolume(option) {
  84. if ms.Topo.FreeSpace() <= 0 {
  85. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left!"})
  86. return
  87. }
  88. ms.vgLock.Lock()
  89. defer ms.vgLock.Unlock()
  90. if !ms.Topo.HasWritableVolume(option) {
  91. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOpiton, ms.Topo); err != nil {
  92. writeJsonError(w, r, http.StatusInternalServerError,
  93. fmt.Errorf("Cannot grow volume group! %v", err))
  94. return
  95. }
  96. }
  97. }
  98. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  99. if err == nil {
  100. ms.maybeAddJwtAuthorization(w, fid, true)
  101. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  102. } else {
  103. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  104. }
  105. }
  106. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  107. var encodedJwt security.EncodedJwt
  108. if isWrite {
  109. encodedJwt = security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  110. } else {
  111. encodedJwt = security.GenJwt(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  112. }
  113. if encodedJwt == "" {
  114. return
  115. }
  116. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  117. }