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.

194 lines
5.6 KiB

3 years ago
8 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. locations = append(locations, operation.Location{
  72. Url: loc.Url(),
  73. PublicUrl: loc.PublicUrl,
  74. DataCenter: loc.GetDataCenterId(),
  75. GrpcPort: loc.GrpcPort,
  76. })
  77. }
  78. }
  79. } else {
  80. machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
  81. for _, loc := range machines {
  82. locations = append(locations, operation.Location{
  83. Url: loc.Url,
  84. PublicUrl: loc.PublicUrl,
  85. DataCenter: loc.DataCenter,
  86. GrpcPort: loc.GrpcPort,
  87. })
  88. }
  89. err = getVidLocationsErr
  90. }
  91. if len(locations) == 0 && err == nil {
  92. err = fmt.Errorf("volume id %s not found", vid)
  93. }
  94. ret := operation.LookupResult{
  95. VolumeOrFileId: vid,
  96. Locations: locations,
  97. }
  98. if err != nil {
  99. ret.Error = err.Error()
  100. }
  101. return ret
  102. }
  103. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  104. stats.AssignRequest()
  105. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  106. if e != nil || requestedCount == 0 {
  107. requestedCount = 1
  108. }
  109. writableVolumeCount, e := strconv.ParseUint(r.FormValue("writableVolumeCount"), 10, 32)
  110. if e != nil {
  111. writableVolumeCount = 0
  112. }
  113. option, err := ms.getVolumeGrowOption(r)
  114. if err != nil {
  115. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  116. return
  117. }
  118. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  119. var (
  120. lastErr error
  121. maxTimeout = time.Second * 10
  122. startTime = time.Now()
  123. )
  124. if !ms.Topo.DataCenterExists(option.DataCenter) {
  125. writeJsonQuiet(w, r, http.StatusBadRequest, operation.AssignResult{
  126. Error: fmt.Sprintf("data center %v not found in topology", option.DataCenter),
  127. })
  128. return
  129. }
  130. for time.Now().Sub(startTime) < maxTimeout {
  131. fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(requestedCount, option, vl)
  132. if shouldGrow && !vl.HasGrowRequest() {
  133. glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr)
  134. if err != nil && ms.Topo.AvailableSpaceFor(option) <= 0 {
  135. err = fmt.Errorf("%s and no free volumes left for %s", err.Error(), option.String())
  136. }
  137. vl.AddGrowRequest()
  138. ms.volumeGrowthRequestChan <- &topology.VolumeGrowRequest{
  139. Option: option,
  140. Count: uint32(writableVolumeCount),
  141. }
  142. }
  143. if err != nil {
  144. stats.MasterPickForWriteErrorCounter.Inc()
  145. lastErr = err
  146. time.Sleep(200 * time.Millisecond)
  147. continue
  148. } else {
  149. ms.maybeAddJwtAuthorization(w, fid, true)
  150. dn := dnList.Head()
  151. if dn == nil {
  152. continue
  153. }
  154. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  155. return
  156. }
  157. }
  158. if lastErr != nil {
  159. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: lastErr.Error()})
  160. } else {
  161. writeJsonQuiet(w, r, http.StatusRequestTimeout, operation.AssignResult{Error: "request timeout"})
  162. }
  163. }
  164. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  165. if fileId == "" {
  166. return
  167. }
  168. var encodedJwt security.EncodedJwt
  169. if isWrite {
  170. encodedJwt = security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  171. } else {
  172. encodedJwt = security.GenJwtForVolumeServer(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  173. }
  174. if encodedJwt == "" {
  175. return
  176. }
  177. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  178. }