master_server_handlers.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Reason: "http assign",
  142. }
  143. }
  144. if err != nil {
  145. stats.MasterPickForWriteErrorCounter.Inc()
  146. lastErr = err
  147. time.Sleep(200 * time.Millisecond)
  148. continue
  149. } else {
  150. ms.maybeAddJwtAuthorization(w, fid, true)
  151. dn := dnList.Head()
  152. if dn == nil {
  153. continue
  154. }
  155. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  156. return
  157. }
  158. }
  159. if lastErr != nil {
  160. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: lastErr.Error()})
  161. } else {
  162. writeJsonQuiet(w, r, http.StatusRequestTimeout, operation.AssignResult{Error: "request timeout"})
  163. }
  164. }
  165. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  166. if fileId == "" {
  167. return
  168. }
  169. var encodedJwt security.EncodedJwt
  170. if isWrite {
  171. encodedJwt = security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  172. } else {
  173. encodedJwt = security.GenJwtForVolumeServer(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  174. }
  175. if encodedJwt == "" {
  176. return
  177. }
  178. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  179. }