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.

202 lines
5.9 KiB

3 years ago
9 months ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/operation"
  10. "github.com/seaweedfs/seaweedfs/weed/security"
  11. "github.com/seaweedfs/seaweedfs/weed/stats"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  13. "github.com/seaweedfs/seaweedfs/weed/topology"
  14. )
  15. func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
  16. volumeLocations = make(map[string]operation.LookupResult)
  17. for _, vid := range vids {
  18. commaSep := strings.Index(vid, ",")
  19. if commaSep > 0 {
  20. vid = vid[0:commaSep]
  21. }
  22. if _, ok := volumeLocations[vid]; ok {
  23. continue
  24. }
  25. volumeLocations[vid] = ms.findVolumeLocation(collection, vid)
  26. }
  27. return
  28. }
  29. // If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
  30. // If "volumeId" is provided, this only returns the volumeId location
  31. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  32. vid := r.FormValue("volumeId")
  33. if vid != "" {
  34. // backward compatible
  35. commaSep := strings.Index(vid, ",")
  36. if commaSep > 0 {
  37. vid = vid[0:commaSep]
  38. }
  39. }
  40. fileId := r.FormValue("fileId")
  41. if fileId != "" {
  42. commaSep := strings.Index(fileId, ",")
  43. if commaSep > 0 {
  44. vid = fileId[0:commaSep]
  45. }
  46. }
  47. collection := r.FormValue("collection") // optional, but can be faster if too many collections
  48. location := ms.findVolumeLocation(collection, vid)
  49. httpStatus := http.StatusOK
  50. if location.Error != "" || location.Locations == nil {
  51. httpStatus = http.StatusNotFound
  52. } else {
  53. forRead := r.FormValue("read")
  54. isRead := forRead == "yes"
  55. ms.maybeAddJwtAuthorization(w, fileId, !isRead)
  56. }
  57. writeJsonQuiet(w, r, httpStatus, location)
  58. }
  59. // findVolumeLocation finds the volume location from master topo if it is leader,
  60. // or from master client if not leader
  61. func (ms *MasterServer) findVolumeLocation(collection, vid string) operation.LookupResult {
  62. var locations []operation.Location
  63. var err error
  64. if ms.Topo.IsLeader() {
  65. volumeId, newVolumeIdErr := needle.NewVolumeId(vid)
  66. if newVolumeIdErr != nil {
  67. err = fmt.Errorf("unknown volume id %s", vid)
  68. } else {
  69. machines := ms.Topo.Lookup(collection, volumeId)
  70. for _, loc := range machines {
  71. volInfo, err := loc.GetVolumesById(volumeId)
  72. if err != nil {
  73. glog.V(0).Infof("failed to get volume info from %s: %v", loc.Url(), err)
  74. continue
  75. }
  76. locations = append(locations, operation.Location{
  77. Url: loc.Url(),
  78. PublicUrl: loc.PublicUrl,
  79. DataCenter: loc.GetDataCenterId(),
  80. GrpcPort: loc.GrpcPort,
  81. DataInRemote: volInfo.DataInRemote,
  82. })
  83. }
  84. }
  85. } else {
  86. machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
  87. for _, loc := range machines {
  88. locations = append(locations, operation.Location{
  89. Url: loc.Url,
  90. PublicUrl: loc.PublicUrl,
  91. DataCenter: loc.DataCenter,
  92. GrpcPort: loc.GrpcPort,
  93. DataInRemote: loc.DataInRemote,
  94. })
  95. }
  96. err = getVidLocationsErr
  97. }
  98. if len(locations) == 0 && err == nil {
  99. err = fmt.Errorf("volume id %s not found", vid)
  100. }
  101. ret := operation.LookupResult{
  102. VolumeOrFileId: vid,
  103. Locations: locations,
  104. }
  105. if err != nil {
  106. ret.Error = err.Error()
  107. }
  108. return ret
  109. }
  110. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  111. stats.AssignRequest()
  112. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  113. if e != nil || requestedCount == 0 {
  114. requestedCount = 1
  115. }
  116. writableVolumeCount, e := strconv.ParseUint(r.FormValue("writableVolumeCount"), 10, 32)
  117. if e != nil {
  118. writableVolumeCount = 0
  119. }
  120. option, err := ms.getVolumeGrowOption(r)
  121. if err != nil {
  122. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  123. return
  124. }
  125. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  126. var (
  127. lastErr error
  128. maxTimeout = time.Second * 10
  129. startTime = time.Now()
  130. )
  131. if !ms.Topo.DataCenterExists(option.DataCenter) {
  132. writeJsonQuiet(w, r, http.StatusBadRequest, operation.AssignResult{
  133. Error: fmt.Sprintf("data center %v not found in topology", option.DataCenter),
  134. })
  135. return
  136. }
  137. for time.Now().Sub(startTime) < maxTimeout {
  138. fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(requestedCount, option, vl)
  139. if shouldGrow && !vl.HasGrowRequest() {
  140. glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr)
  141. if err != nil && ms.Topo.AvailableSpaceFor(option) <= 0 {
  142. err = fmt.Errorf("%s and no free volumes left for %s", err.Error(), option.String())
  143. }
  144. vl.AddGrowRequest()
  145. ms.volumeGrowthRequestChan <- &topology.VolumeGrowRequest{
  146. Option: option,
  147. Count: uint32(writableVolumeCount),
  148. Reason: "http assign",
  149. }
  150. }
  151. if err != nil {
  152. stats.MasterPickForWriteErrorCounter.Inc()
  153. lastErr = err
  154. time.Sleep(200 * time.Millisecond)
  155. continue
  156. } else {
  157. ms.maybeAddJwtAuthorization(w, fid, true)
  158. dn := dnList.Head()
  159. if dn == nil {
  160. continue
  161. }
  162. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  163. return
  164. }
  165. }
  166. if lastErr != nil {
  167. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: lastErr.Error()})
  168. } else {
  169. writeJsonQuiet(w, r, http.StatusRequestTimeout, operation.AssignResult{Error: "request timeout"})
  170. }
  171. }
  172. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  173. if fileId == "" {
  174. return
  175. }
  176. var encodedJwt security.EncodedJwt
  177. if isWrite {
  178. encodedJwt = security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  179. } else {
  180. encodedJwt = security.GenJwtForVolumeServer(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  181. }
  182. if encodedJwt == "" {
  183. return
  184. }
  185. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  186. }