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.

139 lines
4.3 KiB

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. volumeLocations[vid] = ms.findVolumeLocation(collection, vid)
  23. }
  24. return
  25. }
  26. // If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
  27. // If "volumeId" is provided, this only returns the volumeId location
  28. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  29. vid := r.FormValue("volumeId")
  30. if vid != "" {
  31. // backward compatible
  32. commaSep := strings.Index(vid, ",")
  33. if commaSep > 0 {
  34. vid = vid[0:commaSep]
  35. }
  36. }
  37. fileId := r.FormValue("fileId")
  38. if fileId != "" {
  39. commaSep := strings.Index(fileId, ",")
  40. if commaSep > 0 {
  41. vid = fileId[0:commaSep]
  42. }
  43. }
  44. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  45. location := ms.findVolumeLocation(collection, vid)
  46. httpStatus := http.StatusOK
  47. if location.Error != "" || location.Locations == nil {
  48. httpStatus = http.StatusNotFound
  49. } else {
  50. forRead := r.FormValue("read")
  51. isRead := forRead == "yes"
  52. ms.maybeAddJwtAuthorization(w, fileId, !isRead)
  53. }
  54. writeJsonQuiet(w, r, httpStatus, location)
  55. }
  56. // findVolumeLocation finds the volume location from master topo if it is leader,
  57. // or from master client if not leader
  58. func (ms *MasterServer) findVolumeLocation(collection, vid string) operation.LookupResult {
  59. var locations []operation.Location
  60. var err error
  61. if ms.Topo.IsLeader() {
  62. volumeId, newVolumeIdErr := needle.NewVolumeId(vid)
  63. machines := ms.Topo.Lookup(collection, volumeId)
  64. for _, loc := range machines {
  65. locations = append(locations, operation.Location{Url: loc.Url(), PublicUrl: loc.PublicUrl})
  66. }
  67. err = newVolumeIdErr
  68. } else {
  69. machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
  70. for _, loc := range machines {
  71. locations = append(locations, operation.Location{Url: loc.Url, PublicUrl: loc.PublicUrl})
  72. }
  73. err = getVidLocationsErr
  74. }
  75. ret := operation.LookupResult{
  76. VolumeId: vid,
  77. Locations: locations,
  78. }
  79. if err != nil {
  80. ret.Error = err.Error()
  81. }
  82. return ret
  83. }
  84. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  85. stats.AssignRequest()
  86. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  87. if e != nil || requestedCount == 0 {
  88. requestedCount = 1
  89. }
  90. option, err := ms.getVolumeGrowOption(r)
  91. if err != nil {
  92. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  93. return
  94. }
  95. if !ms.Topo.HasWritableVolume(option) {
  96. if ms.Topo.FreeSpace() <= 0 {
  97. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left!"})
  98. return
  99. }
  100. ms.vgLock.Lock()
  101. defer ms.vgLock.Unlock()
  102. if !ms.Topo.HasWritableVolume(option) {
  103. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOpiton, ms.Topo); err != nil {
  104. writeJsonError(w, r, http.StatusInternalServerError,
  105. fmt.Errorf("Cannot grow volume group! %v", err))
  106. return
  107. }
  108. }
  109. }
  110. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  111. if err == nil {
  112. ms.maybeAddJwtAuthorization(w, fid, true)
  113. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  114. } else {
  115. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  116. }
  117. }
  118. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  119. var encodedJwt security.EncodedJwt
  120. if isWrite {
  121. encodedJwt = security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  122. } else {
  123. encodedJwt = security.GenJwt(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  124. }
  125. if encodedJwt == "" {
  126. return
  127. }
  128. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  129. }