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.

155 lines
4.6 KiB

6 years ago
4 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. "github.com/chrislusf/seaweedfs/weed/topology"
  12. )
  13. func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
  14. volumeLocations = make(map[string]operation.LookupResult)
  15. for _, vid := range vids {
  16. commaSep := strings.Index(vid, ",")
  17. if commaSep > 0 {
  18. vid = vid[0:commaSep]
  19. }
  20. if _, ok := volumeLocations[vid]; ok {
  21. continue
  22. }
  23. volumeLocations[vid] = ms.findVolumeLocation(collection, vid)
  24. }
  25. return
  26. }
  27. // If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
  28. // If "volumeId" is provided, this only returns the volumeId location
  29. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  30. vid := r.FormValue("volumeId")
  31. if vid != "" {
  32. // backward compatible
  33. commaSep := strings.Index(vid, ",")
  34. if commaSep > 0 {
  35. vid = vid[0:commaSep]
  36. }
  37. }
  38. fileId := r.FormValue("fileId")
  39. if fileId != "" {
  40. commaSep := strings.Index(fileId, ",")
  41. if commaSep > 0 {
  42. vid = fileId[0:commaSep]
  43. }
  44. }
  45. collection := r.FormValue("collection") // optional, but can be faster if too many collections
  46. location := ms.findVolumeLocation(collection, vid)
  47. httpStatus := http.StatusOK
  48. if location.Error != "" || location.Locations == nil {
  49. httpStatus = http.StatusNotFound
  50. } else {
  51. forRead := r.FormValue("read")
  52. isRead := forRead == "yes"
  53. ms.maybeAddJwtAuthorization(w, fileId, !isRead)
  54. }
  55. writeJsonQuiet(w, r, httpStatus, location)
  56. }
  57. // findVolumeLocation finds the volume location from master topo if it is leader,
  58. // or from master client if not leader
  59. func (ms *MasterServer) findVolumeLocation(collection, vid string) operation.LookupResult {
  60. var locations []operation.Location
  61. var err error
  62. if ms.Topo.IsLeader() {
  63. volumeId, newVolumeIdErr := needle.NewVolumeId(vid)
  64. if newVolumeIdErr != nil {
  65. err = fmt.Errorf("Unknown volume id %s", vid)
  66. } else {
  67. machines := ms.Topo.Lookup(collection, volumeId)
  68. for _, loc := range machines {
  69. locations = append(locations, operation.Location{Url: loc.Url(), PublicUrl: loc.PublicUrl})
  70. }
  71. }
  72. } else {
  73. machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
  74. for _, loc := range machines {
  75. locations = append(locations, operation.Location{Url: loc.Url, PublicUrl: loc.PublicUrl})
  76. }
  77. err = getVidLocationsErr
  78. }
  79. if len(locations) == 0 && err == nil {
  80. err = fmt.Errorf("volume id %s not found", vid)
  81. }
  82. ret := operation.LookupResult{
  83. VolumeId: vid,
  84. Locations: locations,
  85. }
  86. if err != nil {
  87. ret.Error = err.Error()
  88. }
  89. return ret
  90. }
  91. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  92. stats.AssignRequest()
  93. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  94. if e != nil || requestedCount == 0 {
  95. requestedCount = 1
  96. }
  97. writableVolumeCount, e := strconv.Atoi(r.FormValue("writableVolumeCount"))
  98. if e != nil {
  99. writableVolumeCount = 0
  100. }
  101. option, err := ms.getVolumeGrowOption(r)
  102. if err != nil {
  103. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  104. return
  105. }
  106. if ms.shouldVolumeGrow(option) {
  107. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  108. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left for " + option.String()})
  109. return
  110. }
  111. errCh := make(chan error, 1)
  112. ms.vgCh <- &topology.VolumeGrowRequest{
  113. Option: option,
  114. Count: writableVolumeCount,
  115. ErrCh: errCh,
  116. }
  117. if err := <-errCh; err != nil {
  118. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("cannot grow volume group! %v", err))
  119. return
  120. }
  121. }
  122. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  123. if err == nil {
  124. ms.maybeAddJwtAuthorization(w, fid, true)
  125. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  126. } else {
  127. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  128. }
  129. }
  130. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  131. if fileId == "" {
  132. return
  133. }
  134. var encodedJwt security.EncodedJwt
  135. if isWrite {
  136. encodedJwt = security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  137. } else {
  138. encodedJwt = security.GenJwt(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  139. }
  140. if encodedJwt == "" {
  141. return
  142. }
  143. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  144. }