|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.s3.crt; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | +import software.amazon.awssdk.annotations.Immutable; |
| 20 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 21 | +import software.amazon.awssdk.annotations.ThreadSafe; |
| 22 | +import software.amazon.awssdk.utils.builder.CopyableBuilder; |
| 23 | +import software.amazon.awssdk.utils.builder.ToCopyableBuilder; |
| 24 | + |
| 25 | +/** |
| 26 | + * Configuration options which controls how client performs file I/O operations. Only applies to file-based workloads. |
| 27 | + */ |
| 28 | +@SdkPublicApi |
| 29 | +@Immutable |
| 30 | +@ThreadSafe |
| 31 | +public final class S3CrtFileIoConfiguration implements ToCopyableBuilder<S3CrtFileIoConfiguration.Builder, S3CrtFileIoConfiguration> { |
| 32 | + private final Boolean shouldStream; |
| 33 | + private final Double diskThroughputGbps; |
| 34 | + private final Boolean directIo; |
| 35 | + |
| 36 | + private S3CrtFileIoConfiguration(DefaultBuilder builder) { |
| 37 | + this.shouldStream = builder.shouldStream; |
| 38 | + this.diskThroughputGbps = builder.diskThroughputGbps; |
| 39 | + this.directIo = builder.directIo; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a default builder for {@link S3CrtFileIoConfiguration}. |
| 44 | + */ |
| 45 | + public static Builder builder() { |
| 46 | + return new DefaultBuilder(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Skip buffering the part in memory before sending the request. |
| 51 | + * If set, set the {@code diskThroughputGbps} to reasonably align with the available disk throughput. |
| 52 | + * Otherwise, the transfer may fail with connection starvation. |
| 53 | + * Defaults to false. |
| 54 | + * |
| 55 | + * @return if client should skip buffering in memory before sending the request. |
| 56 | + */ |
| 57 | + public Boolean shouldStream() { |
| 58 | + return shouldStream; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * The estimated disk throughput in gigabits per second (Gbps). |
| 63 | + * Only applied when {@code shouldStream} is true. |
| 64 | + * |
| 65 | + * When doing upload with streaming, it's important to set the disk throughput to prevent connection starvation. |
| 66 | + * Note: There are possibilities that cannot reach all available disk throughput: |
| 67 | + * 1. Disk is busy with other applications |
| 68 | + * 2. OS Cache may cap the throughput, use {@code directIo} to get around this. |
| 69 | + * |
| 70 | + * @return disk throughput value in Gpbs. |
| 71 | + */ |
| 72 | + public Double diskThroughputGbps() { |
| 73 | + return diskThroughputGbps; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Enable direct I/O to bypass the OS cache. Helpful when the disk I/O outperforms the kernel cache. |
| 78 | + * Notes: |
| 79 | + * - Only supported on Linux for now. |
| 80 | + * - Only supports upload for now. |
| 81 | + * - Uses it as a potentially powerful tool that should be used with caution. Read NOTES for O_DIRECT |
| 82 | + * for additional info <a href="https://man7.org/linux/man-pages/man2/openat.2.html">open</a> |
| 83 | + * |
| 84 | + * @return directIO value |
| 85 | + */ |
| 86 | + public Boolean directIo() { |
| 87 | + return directIo; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object o) { |
| 92 | + if (this == o) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + if (o == null || getClass() != o.getClass()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + S3CrtFileIoConfiguration that = (S3CrtFileIoConfiguration) o; |
| 100 | + |
| 101 | + if (!Objects.equals(shouldStream, that.shouldStream)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + if (!Objects.equals(diskThroughputGbps, that.diskThroughputGbps)) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + return Objects.equals(directIo, that.directIo); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int hashCode() { |
| 112 | + int result = shouldStream != null ? shouldStream.hashCode() : 0; |
| 113 | + result = 31 * result + (diskThroughputGbps != null ? diskThroughputGbps.hashCode() : 0); |
| 114 | + result = 31 * result + (directIo != null ? directIo.hashCode() : 0); |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Builder toBuilder() { |
| 120 | + return new DefaultBuilder(this); |
| 121 | + } |
| 122 | + |
| 123 | + public interface Builder extends CopyableBuilder<Builder, S3CrtFileIoConfiguration> { |
| 124 | + /** |
| 125 | + * Skip buffering the part in memory before sending the request. |
| 126 | + * If set, set the {@code diskThroughputGbps} to reasonably align with the available disk throughput. |
| 127 | + * Otherwise, the transfer may fail with connection starvation. |
| 128 | + * Defaults to false. |
| 129 | + * |
| 130 | + * @param shouldStream whether to stream the file |
| 131 | + * @return The builder for method chaining. |
| 132 | + */ |
| 133 | + Builder shouldStream(Boolean shouldStream); |
| 134 | + |
| 135 | + /** |
| 136 | + * The estimated disk throughput in gigabits per second (Gbps). |
| 137 | + * Only applied when {@code shouldStream} is true. |
| 138 | + * |
| 139 | + * When doing upload with streaming, it's important to set the disk throughput to prevent connection starvation. |
| 140 | + * Note: There are possibilities that cannot reach all available disk throughput: |
| 141 | + * 1. Disk is busy with other applications |
| 142 | + * 2. OS Cache may cap the throughput, use {@code directIo} to get around this. |
| 143 | + * |
| 144 | + * @param diskThroughputGbps the disk throughput in Gbps |
| 145 | + * @return The builder for method chaining. |
| 146 | + */ |
| 147 | + Builder diskThroughputGbps(Double diskThroughputGbps); |
| 148 | + |
| 149 | + /** |
| 150 | + * Enable direct I/O to bypass the OS cache. Helpful when the disk I/O outperforms the kernel cache. |
| 151 | + * Notes: |
| 152 | + * - Only supported on Linux for now. |
| 153 | + * - Only supports upload for now. |
| 154 | + * - Uses it as a potentially powerful tool that should be used with caution. Read NOTES for O_DIRECT |
| 155 | + * for additional info https://man7.org/linux/man-pages/man2/openat.2.html |
| 156 | + * |
| 157 | + * @param directIo whether to enable direct I/O |
| 158 | + * @return The builder for method chaining. |
| 159 | + */ |
| 160 | + Builder directIo(Boolean directIo); |
| 161 | + |
| 162 | + @Override |
| 163 | + S3CrtFileIoConfiguration build(); |
| 164 | + } |
| 165 | + |
| 166 | + private static final class DefaultBuilder implements Builder { |
| 167 | + private Boolean shouldStream; |
| 168 | + private Double diskThroughputGbps; |
| 169 | + private Boolean directIo; |
| 170 | + |
| 171 | + private DefaultBuilder() { |
| 172 | + } |
| 173 | + |
| 174 | + private DefaultBuilder(S3CrtFileIoConfiguration fileIoOptions) { |
| 175 | + this.shouldStream = fileIoOptions.shouldStream; |
| 176 | + this.diskThroughputGbps = fileIoOptions.diskThroughputGbps; |
| 177 | + this.directIo = fileIoOptions.directIo; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public Builder shouldStream(Boolean shouldStream) { |
| 182 | + this.shouldStream = shouldStream; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public Builder diskThroughputGbps(Double diskThroughputGbps) { |
| 188 | + this.diskThroughputGbps = diskThroughputGbps; |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public Builder directIo(Boolean directIo) { |
| 194 | + this.directIo = directIo; |
| 195 | + return this; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public S3CrtFileIoConfiguration build() { |
| 200 | + return new S3CrtFileIoConfiguration(this); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments