master_server_handlers.go 5.4 KB

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