volume_server_handlers.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "sync/atomic"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "github.com/seaweedfs/seaweedfs/weed/util/version"
  12. "github.com/seaweedfs/seaweedfs/weed/glog"
  13. "github.com/seaweedfs/seaweedfs/weed/security"
  14. "github.com/seaweedfs/seaweedfs/weed/stats"
  15. )
  16. /*
  17. If volume server is started with a separated public port, the public port will
  18. be more "secure".
  19. Public port currently only supports reads.
  20. Later writes on public port can have one of the 3
  21. security settings:
  22. 1. not secured
  23. 2. secured by white list
  24. 3. secured by JWT(Json Web Token)
  25. */
  26. // checkDownloadLimit handles download concurrency limiting with timeout and proxy fallback.
  27. //
  28. // Returns:
  29. // - true: Request should proceed with normal processing (limit not exceeded,
  30. // or successfully waited for available capacity)
  31. // - false: Request was already handled by this function (proxied to replica,
  32. // timed out with 429 response, cancelled with 499 response, or
  33. // failed with error response). Caller should NOT continue processing.
  34. //
  35. // Control Flow:
  36. // - No limit configured → return true (proceed normally)
  37. // - Within limit → return true (proceed normally)
  38. // - Over limit + has replicas → proxy to replica, return false (already handled)
  39. // - Over limit + no replicas → wait with timeout:
  40. // - Timeout → send 429 response, return false (already handled)
  41. // - Cancelled → send 499 response, return false (already handled)
  42. // - Capacity available → return true (proceed normally)
  43. func (vs *VolumeServer) checkDownloadLimit(w http.ResponseWriter, r *http.Request) bool {
  44. inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  45. stats.VolumeServerInFlightDownloadSize.Set(float64(inFlightDownloadSize))
  46. if vs.concurrentDownloadLimit == 0 || inFlightDownloadSize <= vs.concurrentDownloadLimit {
  47. return true // no limit configured or within limit - proceed normally
  48. }
  49. stats.VolumeServerHandlerCounter.WithLabelValues(stats.DownloadLimitCond).Inc()
  50. glog.V(4).Infof("request %s wait because inflight download data %d > %d",
  51. r.URL.Path, inFlightDownloadSize, vs.concurrentDownloadLimit)
  52. // Try to proxy to replica if available
  53. if vs.tryProxyToReplica(w, r) {
  54. return false // handled by proxy
  55. }
  56. // Wait with timeout
  57. return vs.waitForDownloadSlot(w, r)
  58. }
  59. // tryProxyToReplica attempts to proxy the request to a replica server if the volume has replication.
  60. // Returns:
  61. // - true: Request was handled (either proxied successfully or failed with error response)
  62. // - false: No proxy available (volume has no replicas or request already proxied)
  63. func (vs *VolumeServer) tryProxyToReplica(w http.ResponseWriter, r *http.Request) bool {
  64. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  65. volumeId, err := needle.NewVolumeId(vid)
  66. if err != nil {
  67. glog.V(1).Infof("parsing vid %s: %v", r.URL.Path, err)
  68. w.WriteHeader(http.StatusBadRequest)
  69. return true // handled (with error)
  70. }
  71. volume := vs.store.GetVolume(volumeId)
  72. if volume != nil && volume.ReplicaPlacement != nil && volume.ReplicaPlacement.HasReplication() && r.URL.Query().Get(reqIsProxied) != "true" {
  73. vs.proxyReqToTargetServer(w, r)
  74. return true // handled by proxy
  75. }
  76. return false // no proxy available
  77. }
  78. // waitForDownloadSlot waits for available download capacity with timeout.
  79. //
  80. // This function implements a blocking wait mechanism with timeout for download capacity.
  81. // It continuously checks if download capacity becomes available and handles timeout
  82. // and cancellation scenarios appropriately.
  83. //
  84. // Returns:
  85. // - true: Download capacity became available, request should proceed
  86. // - false: Request failed (timeout or cancellation), error response already sent
  87. //
  88. // HTTP Status Codes:
  89. // - 429 Too Many Requests: Wait timeout exceeded
  90. // - 499 Client Closed Request: Request cancelled by client
  91. func (vs *VolumeServer) waitForDownloadSlot(w http.ResponseWriter, r *http.Request) bool {
  92. timerDownload := time.NewTimer(vs.inflightDownloadDataTimeout)
  93. defer timerDownload.Stop()
  94. inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  95. for inFlightDownloadSize > vs.concurrentDownloadLimit {
  96. switch util.WaitWithTimeout(r.Context(), vs.inFlightDownloadDataLimitCond, timerDownload) {
  97. case http.StatusTooManyRequests:
  98. err := fmt.Errorf("request %s because inflight download data %d > %d, and wait timeout",
  99. r.URL.Path, inFlightDownloadSize, vs.concurrentDownloadLimit)
  100. glog.V(1).Infof("too many requests: %v", err)
  101. writeJsonError(w, r, http.StatusTooManyRequests, err)
  102. return false
  103. case util.HttpStatusCancelled:
  104. glog.V(1).Infof("request %s cancelled from %s: %v", r.URL.Path, r.RemoteAddr, r.Context().Err())
  105. w.WriteHeader(util.HttpStatusCancelled)
  106. return false
  107. }
  108. inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  109. stats.VolumeServerInFlightDownloadSize.Set(float64(inFlightDownloadSize))
  110. }
  111. return true
  112. }
  113. // checkUploadLimit handles upload concurrency limiting with timeout.
  114. //
  115. // This function implements upload throttling to prevent overwhelming the volume server
  116. // with too many concurrent uploads. It excludes replication traffic from limits.
  117. //
  118. // Returns:
  119. // - true: Request should proceed with upload processing (no limit, within limit,
  120. // or successfully waited for capacity)
  121. // - false: Request failed (timeout or cancellation), error response already sent
  122. //
  123. // Special Handling:
  124. // - Replication requests (type=replicate) bypass upload limits
  125. // - No upload limit configured (concurrentUploadLimit=0) allows all uploads
  126. func (vs *VolumeServer) checkUploadLimit(w http.ResponseWriter, r *http.Request) bool {
  127. // exclude the replication from the concurrentUploadLimitMB
  128. if vs.concurrentUploadLimit == 0 || r.URL.Query().Get("type") == "replicate" {
  129. return true
  130. }
  131. inFlightUploadDataSize := atomic.LoadInt64(&vs.inFlightUploadDataSize)
  132. stats.VolumeServerInFlightUploadSize.Set(float64(inFlightUploadDataSize))
  133. if inFlightUploadDataSize <= vs.concurrentUploadLimit {
  134. return true
  135. }
  136. return vs.waitForUploadSlot(w, r)
  137. }
  138. // waitForUploadSlot waits for available upload capacity with timeout.
  139. //
  140. // Returns:
  141. // - true: Upload capacity became available, request should proceed
  142. // - false: Request failed (timeout or cancellation), error response already sent
  143. //
  144. // HTTP Status Codes:
  145. // - 429 Too Many Requests: Wait timeout exceeded
  146. // - 499 Client Closed Request: Request cancelled by client
  147. func (vs *VolumeServer) waitForUploadSlot(w http.ResponseWriter, r *http.Request) bool {
  148. var timerUpload *time.Timer
  149. inFlightUploadDataSize := atomic.LoadInt64(&vs.inFlightUploadDataSize)
  150. for inFlightUploadDataSize > vs.concurrentUploadLimit {
  151. if timerUpload == nil {
  152. timerUpload = time.NewTimer(vs.inflightUploadDataTimeout)
  153. defer timerUpload.Stop()
  154. }
  155. glog.V(4).Infof("wait because inflight upload data %d > %d", inFlightUploadDataSize, vs.concurrentUploadLimit)
  156. stats.VolumeServerHandlerCounter.WithLabelValues(stats.UploadLimitCond).Inc()
  157. switch util.WaitWithTimeout(r.Context(), vs.inFlightUploadDataLimitCond, timerUpload) {
  158. case http.StatusTooManyRequests:
  159. err := fmt.Errorf("reject because inflight upload data %d > %d, and wait timeout",
  160. inFlightUploadDataSize, vs.concurrentUploadLimit)
  161. glog.V(1).Infof("too many requests: %v", err)
  162. writeJsonError(w, r, http.StatusTooManyRequests, err)
  163. return false
  164. case util.HttpStatusCancelled:
  165. glog.V(1).Infof("request cancelled from %s: %v", r.RemoteAddr, r.Context().Err())
  166. writeJsonError(w, r, util.HttpStatusCancelled, r.Context().Err())
  167. return false
  168. }
  169. inFlightUploadDataSize = atomic.LoadInt64(&vs.inFlightUploadDataSize)
  170. stats.VolumeServerInFlightUploadSize.Set(float64(inFlightUploadDataSize))
  171. }
  172. return true
  173. }
  174. // handleGetRequest processes GET/HEAD requests with download limiting.
  175. //
  176. // This function orchestrates the complete GET/HEAD request handling workflow:
  177. // 1. Records read request statistics
  178. // 2. Applies download concurrency limits with proxy fallback
  179. // 3. Delegates to GetOrHeadHandler for actual file serving (if limits allow)
  180. //
  181. // The download limiting logic may handle the request completely (via proxy,
  182. // timeout, or error), in which case normal file serving is skipped.
  183. func (vs *VolumeServer) handleGetRequest(w http.ResponseWriter, r *http.Request) {
  184. stats.ReadRequest()
  185. if vs.checkDownloadLimit(w, r) {
  186. vs.GetOrHeadHandler(w, r)
  187. }
  188. }
  189. // handleUploadRequest processes PUT/POST requests with upload limiting.
  190. //
  191. // This function manages the complete upload request workflow:
  192. // 1. Extracts content length from request headers
  193. // 2. Applies upload concurrency limits with timeout handling
  194. // 3. Tracks in-flight upload data size for monitoring
  195. // 4. Delegates to PostHandler for actual file processing
  196. // 5. Ensures proper cleanup of in-flight counters
  197. //
  198. // The upload limiting logic may reject the request with appropriate HTTP
  199. // status codes (429 for timeout, 499 for cancellation).
  200. func (vs *VolumeServer) handleUploadRequest(w http.ResponseWriter, r *http.Request) {
  201. contentLength := getContentLength(r)
  202. if !vs.checkUploadLimit(w, r) {
  203. return
  204. }
  205. atomic.AddInt64(&vs.inFlightUploadDataSize, contentLength)
  206. defer func() {
  207. atomic.AddInt64(&vs.inFlightUploadDataSize, -contentLength)
  208. if vs.concurrentUploadLimit != 0 {
  209. vs.inFlightUploadDataLimitCond.Broadcast()
  210. }
  211. }()
  212. // processes uploads
  213. stats.WriteRequest()
  214. vs.guard.WhiteList(vs.PostHandler)(w, r)
  215. }
  216. func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
  217. inFlightGauge := stats.VolumeServerInFlightRequestsGauge.WithLabelValues(r.Method)
  218. inFlightGauge.Inc()
  219. defer inFlightGauge.Dec()
  220. statusRecorder := stats.NewStatusResponseWriter(w)
  221. w = statusRecorder
  222. w.Header().Set("Server", "SeaweedFS Volume "+version.VERSION)
  223. if r.Header.Get("Origin") != "" {
  224. w.Header().Set("Access-Control-Allow-Origin", "*")
  225. w.Header().Set("Access-Control-Allow-Credentials", "true")
  226. }
  227. start := time.Now()
  228. requestMethod := r.Method
  229. defer func(start time.Time, method *string, statusRecorder *stats.StatusRecorder) {
  230. stats.VolumeServerRequestCounter.WithLabelValues(*method, strconv.Itoa(statusRecorder.Status)).Inc()
  231. stats.VolumeServerRequestHistogram.WithLabelValues(*method).Observe(time.Since(start).Seconds())
  232. }(start, &requestMethod, statusRecorder)
  233. switch r.Method {
  234. case http.MethodGet, http.MethodHead:
  235. vs.handleGetRequest(w, r)
  236. case http.MethodDelete:
  237. stats.DeleteRequest()
  238. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  239. case http.MethodPut, http.MethodPost:
  240. vs.handleUploadRequest(w, r)
  241. case http.MethodOptions:
  242. stats.ReadRequest()
  243. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  244. w.Header().Add("Access-Control-Allow-Headers", "*")
  245. default:
  246. requestMethod = "INVALID"
  247. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("unsupported method %s", r.Method))
  248. }
  249. }
  250. func getContentLength(r *http.Request) int64 {
  251. contentLength := r.Header.Get("Content-Length")
  252. if contentLength != "" {
  253. length, err := strconv.ParseInt(contentLength, 10, 64)
  254. if err != nil {
  255. return 0
  256. }
  257. return length
  258. }
  259. return 0
  260. }
  261. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  262. statusRecorder := stats.NewStatusResponseWriter(w)
  263. w = statusRecorder
  264. w.Header().Set("Server", "SeaweedFS Volume "+version.VERSION)
  265. if r.Header.Get("Origin") != "" {
  266. w.Header().Set("Access-Control-Allow-Origin", "*")
  267. w.Header().Set("Access-Control-Allow-Credentials", "true")
  268. }
  269. start := time.Now()
  270. requestMethod := r.Method
  271. defer func(start time.Time, method *string, statusRecorder *stats.StatusRecorder) {
  272. stats.VolumeServerRequestCounter.WithLabelValues(*method, strconv.Itoa(statusRecorder.Status)).Inc()
  273. stats.VolumeServerRequestHistogram.WithLabelValues(*method).Observe(time.Since(start).Seconds())
  274. }(start, &requestMethod, statusRecorder)
  275. switch r.Method {
  276. case http.MethodGet, http.MethodHead:
  277. vs.handleGetRequest(w, r)
  278. case http.MethodOptions:
  279. stats.ReadRequest()
  280. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  281. w.Header().Add("Access-Control-Allow-Headers", "*")
  282. }
  283. }
  284. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  285. var signingKey security.SigningKey
  286. if isWrite {
  287. if len(vs.guard.SigningKey) == 0 {
  288. return true
  289. } else {
  290. signingKey = vs.guard.SigningKey
  291. }
  292. } else {
  293. if len(vs.guard.ReadSigningKey) == 0 {
  294. return true
  295. } else {
  296. signingKey = vs.guard.ReadSigningKey
  297. }
  298. }
  299. tokenStr := security.GetJwt(r)
  300. if tokenStr == "" {
  301. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  302. return false
  303. }
  304. token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
  305. if err != nil {
  306. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  307. return false
  308. }
  309. if !token.Valid {
  310. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  311. return false
  312. }
  313. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  314. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  315. fid = fid[:sepIndex]
  316. }
  317. return sc.Fid == vid+","+fid
  318. }
  319. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  320. return false
  321. }