master_server_handlers.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package weed_server
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/stats"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/topology"
  13. )
  14. func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
  15. volumeLocations = make(map[string]operation.LookupResult)
  16. for _, vid := range vids {
  17. commaSep := strings.Index(vid, ",")
  18. if commaSep > 0 {
  19. vid = vid[0:commaSep]
  20. }
  21. if _, ok := volumeLocations[vid]; ok {
  22. continue
  23. }
  24. volumeLocations[vid] = ms.findVolumeLocation(collection, vid)
  25. }
  26. return
  27. }
  28. // If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
  29. // If "volumeId" is provided, this only returns the volumeId location
  30. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  31. vid := r.FormValue("volumeId")
  32. if vid != "" {
  33. // backward compatible
  34. commaSep := strings.Index(vid, ",")
  35. if commaSep > 0 {
  36. vid = vid[0:commaSep]
  37. }
  38. }
  39. fileId := r.FormValue("fileId")
  40. if fileId != "" {
  41. commaSep := strings.Index(fileId, ",")
  42. if commaSep > 0 {
  43. vid = fileId[0:commaSep]
  44. }
  45. }
  46. collection := r.FormValue("collection") // optional, but can be faster if too many collections
  47. location := ms.findVolumeLocation(collection, vid)
  48. httpStatus := http.StatusOK
  49. if location.Error != "" || location.Locations == nil {
  50. httpStatus = http.StatusNotFound
  51. } else {
  52. forRead := r.FormValue("read")
  53. isRead := forRead == "yes"
  54. ms.maybeAddJwtAuthorization(w, fileId, !isRead)
  55. }
  56. writeJsonQuiet(w, r, httpStatus, location)
  57. }
  58. // findVolumeLocation finds the volume location from master topo if it is leader,
  59. // or from master client if not leader
  60. func (ms *MasterServer) findVolumeLocation(collection, vid string) operation.LookupResult {
  61. var locations []operation.Location
  62. var err error
  63. if ms.Topo.IsLeader() {
  64. volumeId, newVolumeIdErr := needle.NewVolumeId(vid)
  65. if newVolumeIdErr != nil {
  66. err = fmt.Errorf("Unknown volume id %s", vid)
  67. } else {
  68. machines := ms.Topo.Lookup(collection, volumeId)
  69. for _, loc := range machines {
  70. locations = append(locations, operation.Location{Url: loc.Url(), PublicUrl: loc.PublicUrl})
  71. }
  72. }
  73. } else {
  74. machines, getVidLocationsErr := ms.MasterClient.GetVidLocations(vid)
  75. for _, loc := range machines {
  76. locations = append(locations, operation.Location{Url: loc.Url, PublicUrl: loc.PublicUrl})
  77. }
  78. err = getVidLocationsErr
  79. }
  80. if len(locations) == 0 && err == nil {
  81. err = fmt.Errorf("volume id %s not found", vid)
  82. }
  83. ret := operation.LookupResult{
  84. VolumeOrFileId: vid,
  85. Locations: locations,
  86. }
  87. if err != nil {
  88. ret.Error = err.Error()
  89. }
  90. return ret
  91. }
  92. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  93. stats.AssignRequest()
  94. requestedCount, e := strconv.ParseUint(r.FormValue("count"), 10, 64)
  95. if e != nil || requestedCount == 0 {
  96. requestedCount = 1
  97. }
  98. writableVolumeCount, e := strconv.Atoi(r.FormValue("writableVolumeCount"))
  99. if e != nil {
  100. writableVolumeCount = 0
  101. }
  102. option, err := ms.getVolumeGrowOption(r)
  103. if err != nil {
  104. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  105. return
  106. }
  107. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  108. if !vl.HasGrowRequest() && vl.ShouldGrowVolumes(option) {
  109. glog.V(0).Infof("dirAssign volume growth %v from %v", option.String(), r.RemoteAddr)
  110. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  111. writeJsonQuiet(w, r, http.StatusNotFound, operation.AssignResult{Error: "No free volumes left for " + option.String()})
  112. return
  113. }
  114. errCh := make(chan error, 1)
  115. vl.AddGrowRequest()
  116. ms.vgCh <- &topology.VolumeGrowRequest{
  117. Option: option,
  118. Count: writableVolumeCount,
  119. ErrCh: errCh,
  120. }
  121. if err := <-errCh; err != nil {
  122. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("cannot grow volume group! %v", err))
  123. return
  124. }
  125. }
  126. fid, count, dnList, err := ms.Topo.PickForWrite(requestedCount, option)
  127. if err == nil {
  128. ms.maybeAddJwtAuthorization(w, fid, true)
  129. dn := dnList.Head()
  130. writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
  131. } else {
  132. writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
  133. }
  134. }
  135. func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string, isWrite bool) {
  136. if fileId == "" {
  137. return
  138. }
  139. var encodedJwt security.EncodedJwt
  140. if isWrite {
  141. encodedJwt = security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fileId)
  142. } else {
  143. encodedJwt = security.GenJwtForVolumeServer(ms.guard.ReadSigningKey, ms.guard.ReadExpiresAfterSec, fileId)
  144. }
  145. if encodedJwt == "" {
  146. return
  147. }
  148. w.Header().Set("Authorization", "BEARER "+string(encodedJwt))
  149. }