| 1 | CRT S3 client was designed with throughput as a primary goal. As such, the client |
| 2 | scales resource usage, such as number of parallel requests in flight, to achieve |
| 3 | target throughput. The client creates buffers to hold data it is sending or |
| 4 | receiving for each request and scaling requests in flight has direct impact on |
| 5 | memory used. In practice, setting high target throughput or larger part size can |
| 6 | lead to high observed memory usage. |
| 7 | |
| 8 | To mitigate high memory usages, memory reuse improvements were added to |
| 9 | the client along with options to limit max memory used. The following sections |
| 10 | will go into more detail on aspects of those changes and how the affect the |
| 11 | client. |
| 12 | |
| 13 | ### Memory Reuse |
| 14 | At the basic level, CRT S3 client starts with a meta request for operation like |
| 15 | put or get, breaks it into smaller part-sized requests and executes those in |
| 16 | parallel. CRT S3 client used to allocate part sized buffer for each of those |
| 17 | requests and release it right after the request was done. That approach, |
| 18 | resulted in a lot of very short lived allocations and allocator thrashing, |
| 19 | overall leading to memory use spikes considerably higher than whats needed. To |
| 20 | address that, the client is switching to a pooled buffer approach, discussed |
| 21 | below. |
| 22 | |
| 23 | Note: approach described below is work in progress and concentrates on improving |
| 24 | the common cases (default 8mb part sizes and part sizes smaller than 64mb). |
| 25 | |
| 26 | Several observations about the client usage of buffers: |
| 27 | - Client does not automatically switch to buffers above default 8mb for upload, until |
| 28 | upload passes 10,000 parts (~80 GB). |
| 29 | - Get operations always use either the configured part size or default of 8mb. |
| 30 | Part size for get is not adjusted, since there is no 10,000 part limitation. |
| 31 | - Both Put and Get operations go through fill and drain phases. Ex. for Put, the |
| 32 | client first schedules a number of reads to 'fill' the buffers from the source |
| 33 | and as those reads complete, the buffer are send over to the networking layer |
| 34 | are 'drained' |
| 35 | - individual uploadParts or ranged gets operations typically have a similar |
| 36 | lifespan (with some caveats). in practice part buffers are acquired/released |
| 37 | in bulk at the same time |
| 38 | |
| 39 | The buffer pooling takes advantage of some of those allocation patterns and |
| 40 | works as follows. |
| 41 | The memory is split into primary and secondary areas. Secondary area is used for |
| 42 | requests with part size bigger than a predefined value (currently 4 times part size) |
| 43 | allocations from it got directly to allocator and are effectively old way of |
| 44 | doing things. |
| 45 | |
| 46 | Primary memory area is split into blocks of fixed size (part size if defined or |
| 47 | 8mb if not times 16). Blocks are allocated on demand. Each block is logically |
| 48 | subdivided into part sized chunks. Pool allocates and releases in chunk sizes |
| 49 | only, and supports acquiring several chunks (up to 4) at once. |
| 50 | |
| 51 | Blocks are kept around while there are ongoing requests and are released async, |
| 52 | when there is low pressure on memory. |
| 53 | |
| 54 | One complication is "forced" buffers. A forced buffer is one that |
| 55 | comes from primary or secondary storage as usual, but it is allowed to exceed |
| 56 | the memory limit. Forced buffers are only used when waiting for a normal ticket |
| 57 | reservation could cause deadlock. (At time of writing, they're only used for |
| 58 | async-writes.) A custom buffer pool may instead *defer* such a reservation, returning a |
| 59 | pending future it completes later when memory frees; see the `can_block` contract in |
| 60 | `s3_buffer_pool.h`. |
| 61 | |
| 62 | ### Scheduling |
| 63 | Running out of memory is a terminal condition within CRT and in general its not |
| 64 | practical to try to set overall memory limit on all allocations, since it |
| 65 | dramatically increases the complexity of the code that deals with cases where |
| 66 | only part of a memory was allocated for a task. |
| 67 | |
| 68 | Comparatively, majority of memory usage within S3 Client comes from buffers |
| 69 | allocated for Put/Get parts. So to control memory usage, the client will |
| 70 | concentrate on controlling the number of buffers allocated. Effectively, this |
| 71 | boils down to a back pressure mechanism of limiting the number of parts |
| 72 | scheduled as memory gets closer to the limit. Memory used for other resources, |
| 73 | ex. http connections data, various supporting structures, are not actively |
| 74 | controlled and instead some memory is taken out from overall limit. |
| 75 | |
| 76 | Overall, scheduling does a best-effort memory limiting. At the time of |
| 77 | scheduling, the client reserves memory by using buffer pool ticketing mechanism. |
| 78 | Buffer is acquired from the pool using the ticket as close to the usage as |
| 79 | possible (this approach peaks at lower mem usage than preallocating all mem |
| 80 | upfront because buffers cannot be used right away, ex reading from file will |
| 81 | fill buffers slower than they are sent, leading to decent amount of buffer reuse) |
| 82 | Reservation mechanism is approximate and in some cases can lead to actual memory |
| 83 | usage being higher once tickets are redeemed. The client reserves some memory to |
| 84 | mitigate overflows like that. |