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.

158 lines
4.8 KiB

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