master_server_handlers.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. if newVolumeIdErr != nil {
  64. err = fmt.Errorf("Unknown volume id %s", vid)
  65. } else {
  66. machines := ms.Topo.Lookup(collection, volumeId)
  67. for _, loc := range machines {
  68. locations = append(locations, operation.Location{Url: loc.Url(), PublicUrl: loc.PublicUrl})
  69. }
  70. }
  71. } else {
  72. machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
  73. for _, loc := range machines {
  74. locations = append(locations, operation.Location{Url: loc.Url, PublicUrl: loc.PublicUrl})
  75. }
  76. err = getVidLocationsErr
  77. }
  78. if len(locations) == 0 && err == nil {
  79. err = fmt.Errorf("volume id %s not found", vid)
  80. }
  81. ret := operation.LookupResult{
  82. VolumeId: vid,
  83. Locations: locations,
  84. }
  85. if err != nil {
  86. ret.Error = err.Error()
  87. }
  88. return ret
  89. }
  90. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  91. stats.AssignRequest()
  92. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  93. if e != nil || requestedCount == 0 {
  94. requestedCount = 1
  95. }
  96. writableVolumeCount, e := strconv.Atoi(r.FormValue("writableVolumeCount"))
  97. if e != nil {
  98. writableVolumeCount = 0
  99. }
  100. option, err := ms.getVolumeGrowOption(r)
  101. if err != nil {
  102. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  103. return
  104. }
  105. if !ms.Topo.HasWritableVolume(option) {
  106. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  107. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left for " + option.String()})
  108. return
  109. }
  110. ms.vgLock.Lock()
  111. defer ms.vgLock.Unlock()
  112. if !ms.Topo.HasWritableVolume(option) {
  113. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOption, ms.Topo, writableVolumeCount); err != nil {
  114. writeJsonError(w, r, http.StatusInternalServerError,
  115. fmt.Errorf("Cannot grow volume group! %v", err))
  116. return
  117. }
  118. }
  119. }
  120. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  121. if err == nil {
  122. ms.maybeAddJwtAuthorization(w, fid, true)
  123. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  124. } else {
  125. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  126. }
  127. }
  128. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  129. if fileId == "" {
  130. return
  131. }
  132. var encodedJwt security.EncodedJwt
  133. if isWrite {
  134. encodedJwt = security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  135. } else {
  136. encodedJwt = security.GenJwt(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  137. }
  138. if encodedJwt == "" {
  139. return
  140. }
  141. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  142. }