| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2022-09-20 02:05:16 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2021-11-18 01:55:45 +08:00
										 |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 	"sync" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 04:02:16 +08:00
										 |  |  | 	"github.com/shirou/gopsutil/v3/mem" | 
					
						
							| 
									
										
										
										
											2021-09-30 07:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/config/api" | 
					
						
							| 
									
										
										
										
											2021-09-30 07:40:28 +08:00
										 |  |  | 	xioutil "github.com/minio/minio/internal/ioutil" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							| 
									
										
										
										
											2023-04-07 22:51:12 +08:00
										 |  |  | 	"github.com/minio/minio/internal/mcontext" | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | type apiConfig struct { | 
					
						
							|  |  |  | 	mu sync.RWMutex | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	requestsDeadline time.Duration | 
					
						
							|  |  |  | 	requestsPool     chan struct{} | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	clusterDeadline  time.Duration | 
					
						
							| 
									
										
										
										
											2022-03-26 07:29:45 +08:00
										 |  |  | 	listQuorum       string | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	corsAllowOrigins []string | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | 	// total drives per erasure set across pools.
 | 
					
						
							| 
									
										
										
										
											2022-09-25 07:20:28 +08:00
										 |  |  | 	totalDriveCount     int | 
					
						
							|  |  |  | 	replicationPriority string | 
					
						
							|  |  |  | 	transitionWorkers   int | 
					
						
							| 
									
										
										
										
											2021-10-05 01:52:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	staleUploadsExpiry          time.Duration | 
					
						
							|  |  |  | 	staleUploadsCleanupInterval time.Duration | 
					
						
							|  |  |  | 	deleteCleanupInterval       time.Duration | 
					
						
							| 
									
										
										
										
											2022-01-18 00:34:14 +08:00
										 |  |  | 	disableODirect              bool | 
					
						
							| 
									
										
										
										
											2022-02-15 01:19:01 +08:00
										 |  |  | 	gzipObjects                 bool | 
					
						
							| 
									
										
										
										
											2023-04-29 03:24:14 +08:00
										 |  |  | 	rootAccess                  bool | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 01:55:45 +08:00
										 |  |  | const cgroupLimitFile = "/sys/fs/cgroup/memory/memory.limit_in_bytes" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func cgroupLimit(limitFile string) (limit uint64) { | 
					
						
							| 
									
										
										
										
											2022-09-20 02:05:16 +08:00
										 |  |  | 	buf, err := os.ReadFile(limitFile) | 
					
						
							| 
									
										
										
										
											2021-11-18 01:55:45 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 9223372036854771712 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	limit, err = strconv.ParseUint(string(buf), 10, 64) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return 9223372036854771712 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return limit | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func availableMemory() (available uint64) { | 
					
						
							|  |  |  | 	available = 8 << 30 // Default to 8 GiB when we can't find the limits.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if runtime.GOOS == "linux" { | 
					
						
							|  |  |  | 		available = cgroupLimit(cgroupLimitFile) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// No limit set, It's the highest positive signed 64-bit
 | 
					
						
							|  |  |  | 		// integer (2^63-1), rounded down to multiples of 4096 (2^12),
 | 
					
						
							|  |  |  | 		// the most common page size on x86 systems - for cgroup_limits.
 | 
					
						
							|  |  |  | 		if available != 9223372036854771712 { | 
					
						
							|  |  |  | 			// This means cgroup memory limit is configured.
 | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} // no-limit set proceed to set the limits based on virtual memory.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} // for all other platforms limits are based on virtual memory.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memStats, err := mem.VirtualMemory() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	available = memStats.Available / 2 | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | func (t *apiConfig) init(cfg api.Config, setDriveCounts []int) { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	t.mu.Lock() | 
					
						
							|  |  |  | 	defer t.mu.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	t.clusterDeadline = cfg.ClusterDeadline | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	t.corsAllowOrigins = cfg.CorsAllowOrigin | 
					
						
							| 
									
										
										
										
											2021-08-25 00:14:46 +08:00
										 |  |  | 	maxSetDrives := 0 | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | 	for _, setDriveCount := range setDriveCounts { | 
					
						
							|  |  |  | 		t.totalDriveCount += setDriveCount | 
					
						
							| 
									
										
										
										
											2021-08-25 00:14:46 +08:00
										 |  |  | 		if setDriveCount > maxSetDrives { | 
					
						
							|  |  |  | 			maxSetDrives = setDriveCount | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 	var apiRequestsMaxPerNode int | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	if cfg.RequestsMax <= 0 { | 
					
						
							| 
									
										
										
										
											2021-11-18 01:55:45 +08:00
										 |  |  | 		maxMem := availableMemory() | 
					
						
							| 
									
										
										
										
											2021-08-25 00:14:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 		// max requests per node is calculated as
 | 
					
						
							|  |  |  | 		// total_ram / ram_per_request
 | 
					
						
							| 
									
										
											  
											
												[feat]: change erasure coding default block size from 10MiB to 1MiB (#11721)
major performance improvements in range GETs to avoid large
read amplification when ranges are tiny and random
```
-------------------
Operation: GET
Operations: 142014 -> 339421
Duration: 4m50s -> 4m56s
* Average: +139.41% (+1177.3 MiB/s) throughput, +139.11% (+658.4) obj/s
* Fastest: +125.24% (+1207.4 MiB/s) throughput, +132.32% (+612.9) obj/s
* 50% Median: +139.06% (+1175.7 MiB/s) throughput, +133.46% (+660.9) obj/s
* Slowest: +203.40% (+1267.9 MiB/s) throughput, +198.59% (+753.5) obj/s
```
TTFB from 10MiB BlockSize
```
* First Access TTFB: Avg: 81ms, Median: 61ms, Best: 20ms, Worst: 2.056s
```
TTFB from 1MiB BlockSize
```
* First Access TTFB: Avg: 22ms, Median: 21ms, Best: 8ms, Worst: 91ms
```
Full object reads however do see a slight change which won't be
noticeable in real world, so not doing any comparisons
TTFB still had improvements with full object reads with 1MiB
```
* First Access TTFB: Avg: 68ms, Median: 35ms, Best: 11ms, Worst: 1.16s
```
v/s
TTFB with 10MiB
```
* First Access TTFB: Avg: 388ms, Median: 98ms, Best: 20ms, Worst: 4.156s
```
This change should affect all new uploads, previous uploads should
continue to work with business as usual. But dramatic improvements can
be seen with these changes.
											
										 
											2021-03-07 06:09:34 +08:00
										 |  |  | 		// ram_per_request is (2MiB+128KiB) * driveCount \
 | 
					
						
							|  |  |  | 		//    + 2 * 10MiB (default erasure block size v1) + 2 * 1MiB (default erasure block size v2)
 | 
					
						
							| 
									
										
										
										
											2021-09-30 07:40:28 +08:00
										 |  |  | 		blockSize := xioutil.BlockSizeLarge + xioutil.BlockSizeSmall | 
					
						
							|  |  |  | 		apiRequestsMaxPerNode = int(maxMem / uint64(maxSetDrives*blockSize+int(blockSizeV1*2+blockSizeV2*2))) | 
					
						
							| 
									
										
										
										
											2022-06-30 07:32:04 +08:00
										 |  |  | 		if globalIsDistErasure { | 
					
						
							|  |  |  | 			logger.Info("Automatically configured API requests per node based on available memory on the system: %d", apiRequestsMaxPerNode) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 		apiRequestsMaxPerNode = cfg.RequestsMax | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 		if len(globalEndpoints.Hostnames()) > 0 { | 
					
						
							|  |  |  | 			apiRequestsMaxPerNode /= len(globalEndpoints.Hostnames()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-25 00:14:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 02:28:54 +08:00
										 |  |  | 	if cap(t.requestsPool) != apiRequestsMaxPerNode { | 
					
						
							| 
									
										
										
										
											2020-12-05 01:32:35 +08:00
										 |  |  | 		// Only replace if needed.
 | 
					
						
							|  |  |  | 		// Existing requests will use the previous limit,
 | 
					
						
							|  |  |  | 		// but new requests will use the new limit.
 | 
					
						
							|  |  |  | 		// There will be a short overlap window,
 | 
					
						
							|  |  |  | 		// but this shouldn't last long.
 | 
					
						
							|  |  |  | 		t.requestsPool = make(chan struct{}, apiRequestsMaxPerNode) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	t.requestsDeadline = cfg.RequestsDeadline | 
					
						
							| 
									
										
										
										
											2022-03-26 07:29:45 +08:00
										 |  |  | 	t.listQuorum = cfg.ListQuorum | 
					
						
							| 
									
										
										
										
											2021-03-09 18:56:42 +08:00
										 |  |  | 	if globalReplicationPool != nil && | 
					
						
							| 
									
										
										
										
											2022-09-25 07:20:28 +08:00
										 |  |  | 		cfg.ReplicationPriority != t.replicationPriority { | 
					
						
							|  |  |  | 		globalReplicationPool.ResizeWorkerPriority(cfg.ReplicationPriority) | 
					
						
							| 
									
										
										
										
											2021-03-09 18:56:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-09-25 07:20:28 +08:00
										 |  |  | 	t.replicationPriority = cfg.ReplicationPriority | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 13:23:56 +08:00
										 |  |  | 	if globalTransitionState != nil && cfg.TransitionWorkers != t.transitionWorkers { | 
					
						
							|  |  |  | 		globalTransitionState.UpdateWorkers(cfg.TransitionWorkers) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	t.transitionWorkers = cfg.TransitionWorkers | 
					
						
							| 
									
										
										
										
											2021-10-05 01:52:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	t.staleUploadsExpiry = cfg.StaleUploadsExpiry | 
					
						
							|  |  |  | 	t.staleUploadsCleanupInterval = cfg.StaleUploadsCleanupInterval | 
					
						
							|  |  |  | 	t.deleteCleanupInterval = cfg.DeleteCleanupInterval | 
					
						
							| 
									
										
										
										
											2022-01-18 00:34:14 +08:00
										 |  |  | 	t.disableODirect = cfg.DisableODirect | 
					
						
							| 
									
										
										
										
											2022-02-15 01:19:01 +08:00
										 |  |  | 	t.gzipObjects = cfg.GzipObjects | 
					
						
							| 
									
										
										
										
											2023-04-29 03:24:14 +08:00
										 |  |  | 	t.rootAccess = cfg.RootAccess | 
					
						
							| 
									
										
										
										
											2022-01-18 00:34:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) isDisableODirect() bool { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.disableODirect | 
					
						
							| 
									
										
										
										
											2020-11-03 09:21:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 01:19:01 +08:00
										 |  |  | func (t *apiConfig) shouldGzipObjects() bool { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.gzipObjects | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-29 03:24:14 +08:00
										 |  |  | func (t *apiConfig) permitRootAccess() bool { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.rootAccess | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 07:29:45 +08:00
										 |  |  | func (t *apiConfig) getListQuorum() string { | 
					
						
							| 
									
										
										
										
											2020-11-03 09:21:56 +08:00
										 |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.listQuorum | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getCorsAllowOrigins() []string { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	corsAllowOrigins := make([]string, len(t.corsAllowOrigins)) | 
					
						
							|  |  |  | 	copy(corsAllowOrigins, t.corsAllowOrigins) | 
					
						
							|  |  |  | 	return corsAllowOrigins | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 01:52:28 +08:00
										 |  |  | func (t *apiConfig) getStaleUploadsCleanupInterval() time.Duration { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t.staleUploadsCleanupInterval == 0 { | 
					
						
							|  |  |  | 		return 6 * time.Hour // default 6 hours
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.staleUploadsCleanupInterval | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getStaleUploadsExpiry() time.Duration { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t.staleUploadsExpiry == 0 { | 
					
						
							|  |  |  | 		return 24 * time.Hour // default 24 hours
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.staleUploadsExpiry | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getDeleteCleanupInterval() time.Duration { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t.deleteCleanupInterval == 0 { | 
					
						
							|  |  |  | 		return 5 * time.Minute // every 5 minutes
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.deleteCleanupInterval | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | func (t *apiConfig) getClusterDeadline() time.Duration { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	if t.clusterDeadline == 0 { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 		return 10 * time.Second | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	return t.clusterDeadline | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 00:34:52 +08:00
										 |  |  | func (t *apiConfig) getRequestsPoolCapacity() int { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return cap(t.requestsPool) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | func (t *apiConfig) getRequestsPool() (chan struct{}, time.Duration) { | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	if t.requestsPool == nil { | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		return nil, time.Duration(0) | 
					
						
							| 
									
										
										
										
											2020-11-05 00:25:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 	return t.requestsPool, t.requestsDeadline | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // maxClients throttles the S3 API calls
 | 
					
						
							|  |  |  | func maxClients(f http.HandlerFunc) http.HandlerFunc { | 
					
						
							|  |  |  | 	return func(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2022-02-08 08:30:14 +08:00
										 |  |  | 		globalHTTPStats.incS3RequestsIncoming() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 12:20:40 +08:00
										 |  |  | 		if r.Header.Get(globalObjectPerfUserMetadata) == "" { | 
					
						
							|  |  |  | 			if val := globalServiceFreeze.Load(); val != nil { | 
					
						
							|  |  |  | 				if unlock, ok := val.(chan struct{}); ok && unlock != nil { | 
					
						
							|  |  |  | 					// Wait until unfrozen.
 | 
					
						
							| 
									
										
										
										
											2022-06-09 20:06:47 +08:00
										 |  |  | 					select { | 
					
						
							|  |  |  | 					case <-unlock: | 
					
						
							|  |  |  | 					case <-r.Context().Done(): | 
					
						
							|  |  |  | 						// if client canceled we don't need to wait here forever.
 | 
					
						
							|  |  |  | 						return | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2022-04-08 12:20:40 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-11-24 04:02:16 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		pool, deadline := globalAPIConfig.getRequestsPool() | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		if pool == nil { | 
					
						
							|  |  |  | 			f.ServeHTTP(w, r) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-20 16:21:55 +08:00
										 |  |  | 		globalHTTPStats.addRequestsInQueue(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 22:51:12 +08:00
										 |  |  | 		if tc, ok := r.Context().Value(mcontext.ContextTraceKey).(*mcontext.TraceCtxt); ok { | 
					
						
							|  |  |  | 			tc.FuncName = "s3.MaxClients" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		deadlineTimer := time.NewTimer(deadline) | 
					
						
							|  |  |  | 		defer deadlineTimer.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		select { | 
					
						
							|  |  |  | 		case pool <- struct{}{}: | 
					
						
							|  |  |  | 			defer func() { <-pool }() | 
					
						
							| 
									
										
										
										
											2021-02-20 16:21:55 +08:00
										 |  |  | 			globalHTTPStats.addRequestsInQueue(-1) | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 			f.ServeHTTP(w, r) | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		case <-deadlineTimer.C: | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 			// Send a http timeout message
 | 
					
						
							|  |  |  | 			writeErrorResponse(r.Context(), w, | 
					
						
							|  |  |  | 				errorCodes.ToAPIErr(ErrOperationMaxedOut), | 
					
						
							| 
									
										
										
										
											2021-06-18 11:27:04 +08:00
										 |  |  | 				r.URL) | 
					
						
							| 
									
										
										
										
											2021-02-20 16:21:55 +08:00
										 |  |  | 			globalHTTPStats.addRequestsInQueue(-1) | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		case <-r.Context().Done(): | 
					
						
							| 
									
										
										
										
											2023-04-07 22:51:12 +08:00
										 |  |  | 			// When the client disconnects before getting the S3 handler
 | 
					
						
							|  |  |  | 			// status code response, set the status code to 499 so this request
 | 
					
						
							|  |  |  | 			// will be properly audited and traced.
 | 
					
						
							|  |  |  | 			w.WriteHeader(499) | 
					
						
							| 
									
										
										
										
											2021-02-20 16:21:55 +08:00
										 |  |  | 			globalHTTPStats.addRequestsInQueue(-1) | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-02-02 19:15:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 07:20:28 +08:00
										 |  |  | func (t *apiConfig) getReplicationPriority() string { | 
					
						
							| 
									
										
										
										
											2021-02-02 19:15:06 +08:00
										 |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 07:20:28 +08:00
										 |  |  | 	return t.replicationPriority | 
					
						
							| 
									
										
										
										
											2021-02-02 19:15:06 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-08-12 13:23:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getTransitionWorkers() int { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.transitionWorkers | 
					
						
							|  |  |  | } |