Skip to content

Commit

Permalink
Enable auto configuration of network throughput based on EC2 instance…
Browse files Browse the repository at this point in the history
… type (#193)

* Enable auto configure network throughput.

We promised customer to maximize network throughput. The change enables
automatic network throughput configuration if customer specified
throughput is missing.

Signed-off-by: Charles Zhang <[email protected]>

* Addressing feedbacks.

1. Simplify the throughput configuration logic - main.rs(commit#:65432b)
2. Introduce "version" to network_performance.json.

* Unit tests for function `get_maximum_network_throughput`.

The program reads network throughput form network_performance.json,
adds unit tests for checking throughput correctness.

Signed-off-by: Charles Zhang <[email protected]>

* Simplify unit tests

By using #[test_case] in stead of #[test]

Signed-off-by: Charles Zhang <[email protected]>

---------

Signed-off-by: Charles Zhang <[email protected]>
Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
eslrahc-swa and Ubuntu authored Apr 9, 2023
1 parent 7d3f5c8 commit 9ad4854
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mountpoint-s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ nix = "0.26.2"
time = { version = "0.3.17", features = ["macros", "formatting"] }
const_format = "0.2.30"
home = "0.5.4"
serde_json = "1.0.95"

[dev-dependencies]
assert_cmd = "2.0.6"
Expand Down
1 change: 1 addition & 0 deletions mountpoint-s3/scripts/network_performance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":{"region":"us-east-1","timestamp":"2023-04-08T05:21:13Z","version_number":"0.0.1"},"instance_throughput":{"m6gd.12xlarge":20,"c6in.xlarge":30,"m5dn.2xlarge":25,"m5n.16xlarge":75,"c5n.2xlarge":25,"i3en.3xlarge":25,"r5dn.8xlarge":25,"i4i.16xlarge":37.5,"r6a.xlarge":12.5,"m5dn.large":25,"t1.micro":null,"m5zn.2xlarge":25,"r6in.metal":200,"c3.large":null,"c6i.8xlarge":12.5,"m6i.large":12.5,"d3en.2xlarge":25,"i3en.12xlarge":50,"r6i.xlarge":12.5,"m5ad.12xlarge":10,"c5ad.24xlarge":20,"i2.2xlarge":null,"m5d.8xlarge":10,"m5dn.12xlarge":50,"r6idn.16xlarge":100,"m5a.4xlarge":10,"t2.2xlarge":null,"c6i.32xlarge":50,"r6in.24xlarge":150,"z1d.2xlarge":10,"r6id.2xlarge":12.5,"m6idn.large":25,"c3.8xlarge":10,"r6id.xlarge":12.5,"m7g.large":12.5,"r5n.12xlarge":50,"m6idn.12xlarge":75,"t4g.nano":5,"c4.4xlarge":null,"c6g.16xlarge":25,"x2iedn.2xlarge":25,"c5d.large":10,"is4gen.4xlarge":25,"r6a.metal":50,"im4gn.8xlarge":50,"m6gd.medium":10,"m6id.2xlarge":12.5,"f1.2xlarge":10,"g5.2xlarge":10,"m6gd.xlarge":10,"c6i.large":12.5,"r6g.medium":10,"g5.4xlarge":25,"m4.4xlarge":null,"r6gd.medium":10,"c6gd.metal":25,"c6i.4xlarge":12.5,"c6a.48xlarge":50,"m5a.16xlarge":12,"r6g.8xlarge":12,"m5a.2xlarge":10,"r6g.16xlarge":25,"c6a.24xlarge":37.5,"c6a.16xlarge":25,"r6id.16xlarge":25,"m2.4xlarge":null,"m5d.xlarge":10,"c6in.24xlarge":150,"m7g.4xlarge":15,"r6g.large":10,"m6gd.4xlarge":10,"c7g.8xlarge":15,"x2gd.8xlarge":12,"m5.8xlarge":10,"trn1.32xlarge":800,"r6gd.2xlarge":10,"r6a.2xlarge":12.5,"r6in.12xlarge":75,"r5ad.24xlarge":20,"r3.large":null,"m6id.xlarge":12.5,"i3en.24xlarge":100,"im4gn.2xlarge":25,"r6idn.2xlarge":40,"r5a.2xlarge":10,"is4gen.xlarge":25,"c6id.8xlarge":12.5,"r6gd.xlarge":10,"r5a.xlarge":10,"x1e.2xlarge":10,"d2.8xlarge":10,"g3.16xlarge":25,"m6a.2xlarge":12.5,"x2iezn.4xlarge":25,"r5.24xlarge":25,"x2iedn.xlarge":25,"m6a.large":12.5,"x2gd.metal":25,"c6gn.16xlarge":100,"x1e.8xlarge":10,"r5a.12xlarge":10,"d2.2xlarge":null,"c6in.16xlarge":100,"c7g.16xlarge":30,"g4ad.4xlarge":10,"t3a.small":5,"x2iezn.8xlarge":75,"m5zn.large":25,"r6in.large":25,"c7g.4xlarge":15,"c7g.xlarge":12.5,"r6id.12xlarge":18.75,"r4.16xlarge":25,"m5dn.xlarge":25,"m5dn.8xlarge":25,"r5b.metal":25,"im4gn.xlarge":25,"r6a.12xlarge":18.75,"inf1.xlarge":25,"c5a.4xlarge":10,"m6in.12xlarge":75,"c6i.2xlarge":12.5,"a1.medium":10,"d3en.8xlarge":50,"r6idn.8xlarge":50,"r5n.4xlarge":25,"m6idn.4xlarge":50,"x1.16xlarge":10,"d3.8xlarge":25,"m6a.metal":50,"z1d.xlarge":10,"m5ad.2xlarge":10,"c6in.12xlarge":75,"r6a.32xlarge":50,"m5.xlarge":10,"r5ad.12xlarge":10,"r5b.12xlarge":12,"c6g.12xlarge":20,"c5.4xlarge":10,"c5ad.12xlarge":12,"x2idn.24xlarge":75,"r6gd.large":10,"c6id.24xlarge":37.5,"c5n.4xlarge":25,"c6i.24xlarge":37.5,"c6a.32xlarge":50,"c6id.large":12.5,"r3.8xlarge":10,"m6g.xlarge":10,"r6idn.xlarge":30,"x2gd.4xlarge":10,"r5a.large":10,"m5d.metal":25,"g4dn.metal":100,"c5ad.4xlarge":10,"trn1.2xlarge":12.5,"r6i.24xlarge":37.5,"m5d.16xlarge":20,"i4i.metal":75,"c4.8xlarge":10,"m5ad.8xlarge":10,"m5.24xlarge":25,"t3.xlarge":5,"g4ad.8xlarge":15,"m5ad.4xlarge":10,"r6i.4xlarge":12.5,"c6gn.xlarge":25,"m5.metal":25,"c5n.large":25,"m6id.12xlarge":18.75,"m5.12xlarge":12,"r5ad.8xlarge":10,"m6in.32xlarge":200,"m2.2xlarge":null,"t3a.2xlarge":5,"m5d.large":10,"m6g.metal":25,"m6in.24xlarge":150,"t4g.medium":5,"r7g.medium":12.5,"m7g.12xlarge":22.5,"t3.2xlarge":5,"m6i.8xlarge":12.5,"m6i.24xlarge":37.5,"g4ad.2xlarge":10,"m5zn.6xlarge":50,"g5.24xlarge":50,"m5ad.large":10,"c5d.12xlarge":12,"c6gn.4xlarge":25,"x1e.32xlarge":25,"r6idn.24xlarge":150,"m6gd.metal":25,"r7g.metal":30,"m6id.32xlarge":50,"c5ad.16xlarge":20,"c6g.xlarge":10,"m6in.16xlarge":100,"c5.xlarge":10,"r6id.4xlarge":12.5,"c5.12xlarge":12,"r5ad.large":10,"c6in.8xlarge":50,"p2.8xlarge":10,"g4dn.12xlarge":50,"r5dn.large":25,"t3a.medium":5,"m1.xlarge":null,"r5.metal":25,"c6g.large":10,"t3.large":5,"r6gd.4xlarge":10,"r5dn.metal":100,"x2gd.2xlarge":10,"u-12tb1.112xlarge":100,"r5a.16xlarge":12,"m5n.large":25,"a1.large":10,"r5a.4xlarge":10,"m6idn.xlarge":30,"c5a.24xlarge":20,"mac2.metal":10,"c6gn.12xlarge":75,"c1.xlarge":null,"t2.large":null,"c6i.16xlarge":25,"h1.2xlarge":10,"c5d.2xlarge":10,"r6g.4xlarge":10,"r3.4xlarge":null,"r5b.xlarge":10,"g3.8xlarge":10,"d3.2xlarge":15,"x2iedn.metal":100,"r7g.2xlarge":15,"u-6tb1.56xlarge":100,"g5.12xlarge":40,"g3.4xlarge":10,"c5ad.xlarge":10,"r5.4xlarge":10,"r6id.24xlarge":37.5,"c5a.xlarge":10,"t3.medium":5,"r5b.2xlarge":10,"c5a.2xlarge":10,"vt1.6xlarge":6.25,"c5d.24xlarge":25,"c6a.8xlarge":12.5,"c5d.4xlarge":10,"x2gd.16xlarge":25,"m6gd.16xlarge":25,"c6id.16xlarge":25,"c1.medium":null,"m7g.16xlarge":30,"m5n.8xlarge":25,"inf1.2xlarge":25,"r6gd.metal":25,"c5n.9xlarge":50,"m6i.metal":50,"g5g.2xlarge":10,"d3en.6xlarge":40,"d3.xlarge":15,"m4.large":null,"r5b.24xlarge":25,"r5dn.xlarge":25,"t4g.2xlarge":5,"c6a.12xlarge":18.75,"r5b.large":10,"r5.large":10,"g2.2xlarge":null,"d3en.4xlarge":25,"u-3tb1.56xlarge":50,"r6a.8xlarge":12.5,"x2gd.medium":10,"m5zn.3xlarge":25,"r6id.32xlarge":50,"m4.xlarge":null,"m5n.12xlarge":50,"r5d.4xlarge":10,"vt1.3xlarge":3.12,"c6gd.16xlarge":25,"i4i.4xlarge":25,"mac1.metal":25,"c6gn.medium":16,"r6g.12xlarge":20,"i4i.2xlarge":12,"m7g.8xlarge":15,"p3.2xlarge":10,"a1.xlarge":10,"g5g.16xlarge":25,"c6in.2xlarge":40,"r5n.16xlarge":75,"c4.large":null,"r7g.xlarge":12.5,"m6i.16xlarge":25,"inf1.6xlarge":25,"t2.micro":null,"r3.2xlarge":null,"c5a.12xlarge":12,"h1.8xlarge":10,"m5.4xlarge":10,"t3.nano":5,"t4g.large":5,"m6g.8xlarge":12,"t3a.large":5,"m5ad.24xlarge":20,"t4g.small":5,"c7g.2xlarge":15,"r4.xlarge":10,"r7g.8xlarge":15,"h1.16xlarge":25,"m6a.4xlarge":12.5,"x2gd.12xlarge":20,"g5.16xlarge":25,"x2gd.xlarge":10,"a1.2xlarge":10,"m5a.8xlarge":10,"m6id.16xlarge":25,"r5.8xlarge":10,"m5zn.12xlarge":100,"m7g.xlarge":12.5,"r6i.16xlarge":25,"c5.9xlarge":12,"r4.2xlarge":10,"m6idn.2xlarge":40,"u-6tb1.112xlarge":100,"c6in.large":25,"m6a.16xlarge":25,"r5d.24xlarge":25,"x2idn.16xlarge":50,"r3.xlarge":null,"m4.16xlarge":25,"m6i.4xlarge":12.5,"h1.4xlarge":10,"r5ad.16xlarge":12,"m6g.4xlarge":10,"m5zn.metal":100,"g5g.metal":25,"r5n.24xlarge":100,"m3.xlarge":null,"r7g.4xlarge":15,"m5n.metal":100,"r6a.4xlarge":12.5,"x2iedn.4xlarge":25,"i2.xlarge":null,"r5b.4xlarge":10,"c3.xlarge":null,"x2idn.metal":100,"c5.24xlarge":25,"x2gd.large":10,"r5d.2xlarge":10,"r6idn.4xlarge":50,"c4.2xlarge":null,"m6g.large":10,"i4i.large":10,"c7g.medium":12.5,"c6a.metal":50,"m6i.32xlarge":50,"m5dn.metal":100,"r5d.8xlarge":10,"c6gd.xlarge":10,"m5ad.xlarge":10,"r5d.16xlarge":20,"c5n.18xlarge":100,"r6in.32xlarge":200,"g5g.8xlarge":12,"m6id.metal":50,"p4d.24xlarge":400,"m6in.metal":200,"c5.metal":25,"c6id.32xlarge":50,"u-24tb1.112xlarge":100,"r5.16xlarge":20,"g4dn.xlarge":25,"i3.xlarge":10,"is4gen.2xlarge":25,"c5ad.2xlarge":10,"m5n.4xlarge":25,"r6idn.large":25,"r5n.2xlarge":25,"m6in.4xlarge":50,"c6a.xlarge":12.5,"t4g.micro":5,"c5a.16xlarge":20,"c5d.18xlarge":25,"r5d.xlarge":10,"t4g.xlarge":5,"t3.micro":5,"r6in.4xlarge":50,"m6idn.24xlarge":150,"r5.xlarge":10,"m6idn.32xlarge":200,"r6a.48xlarge":50,"c4.xlarge":null,"a1.metal":10,"r5ad.2xlarge":10,"x2iezn.12xlarge":100,"m5d.24xlarge":25,"c6a.4xlarge":12.5,"r5d.large":10,"m5dn.4xlarge":25,"c5.large":10,"r6a.24xlarge":37.5,"i2.8xlarge":10,"r6i.metal":50,"t2.medium":null,"m5zn.xlarge":25,"c5n.metal":100,"x2idn.32xlarge":100,"m5dn.24xlarge":100,"m6g.12xlarge":20,"r6gd.16xlarge":25,"vt1.24xlarge":25,"c7g.metal":30,"i3.8xlarge":10,"g5.xlarge":10,"c6in.4xlarge":50,"m4.2xlarge":null,"z1d.metal":25,"c7g.large":12.5,"c3.4xlarge":null,"m6g.16xlarge":25,"r5dn.16xlarge":75,"m5n.24xlarge":100,"g4dn.16xlarge":50,"m5d.12xlarge":12,"i3.large":10,"m7g.metal":30,"m7g.2xlarge":15,"r5dn.12xlarge":50,"m5n.xlarge":25,"c5.2xlarge":10,"r6id.large":12.5,"z1d.6xlarge":12,"r6id.8xlarge":12.5,"im4gn.large":25,"c6gn.large":25,"m5d.2xlarge":10,"c3.2xlarge":null,"m4.10xlarge":10,"r6i.12xlarge":18.75,"m6i.xlarge":12.5,"r5.2xlarge":10,"r4.large":10,"r5.12xlarge":12,"c5d.metal":25,"r5n.xlarge":25,"g4ad.16xlarge":25,"c6gn.2xlarge":25,"c7g.12xlarge":22.5,"r6in.16xlarge":100,"r4.8xlarge":10,"c5d.xlarge":10,"c6in.32xlarge":200,"r6gd.8xlarge":12,"dl1.24xlarge":400,"x2iezn.6xlarge":50,"m5n.2xlarge":25,"r6a.16xlarge":25,"m3.2xlarge":null,"i2.4xlarge":null,"m5dn.16xlarge":75,"r6id.metal":50,"r6idn.metal":200,"c6g.medium":10,"t2.xlarge":null,"d2.4xlarge":null,"m6in.xlarge":30,"x2iedn.16xlarge":50,"r6idn.32xlarge":200,"m6in.large":25,"r5n.8xlarge":25,"c5ad.8xlarge":10,"m5.16xlarge":20,"m5.2xlarge":10,"m5.large":10,"x2iezn.2xlarge":25,"x1e.16xlarge":10,"r6in.8xlarge":50,"c6gn.8xlarge":50,"c6id.12xlarge":18.75,"i3en.2xlarge":25,"x2iezn.metal":100,"i3.16xlarge":25,"r5b.16xlarge":20,"p3.16xlarge":25,"is4gen.large":25,"m5a.large":10,"m6idn.metal":200,"r6g.xlarge":10,"m7g.medium":12.5,"c6id.metal":50,"z1d.12xlarge":25,"im4gn.4xlarge":25,"m6gd.8xlarge":12,"a1.4xlarge":10,"r5d.metal":25,"z1d.large":10,"r5ad.xlarge":10,"m6id.4xlarge":12.5,"t3.small":5,"m6in.8xlarge":50,"x1.32xlarge":25,"r5a.8xlarge":10,"m6g.2xlarge":10,"r6g.metal":25,"u-9tb1.112xlarge":100,"i4i.xlarge":10,"i3.metal":25,"m5ad.16xlarge":12,"c5ad.large":10,"r6a.large":12.5,"g5g.4xlarge":10,"m2.xlarge":null,"p3.8xlarge":10,"p2.xlarge":null,"m5d.4xlarge":10,"r7g.large":12.5,"c6gd.2xlarge":10,"r5ad.4xlarge":10,"c6i.xlarge":12.5,"g5.48xlarge":100,"c6gd.8xlarge":12,"f1.16xlarge":25,"r7g.16xlarge":30,"c5n.xlarge":25,"d3.4xlarge":15,"r5b.8xlarge":10,"inf1.24xlarge":100,"x2iedn.32xlarge":100,"m6id.large":12.5,"c6id.xlarge":12.5,"p2.16xlarge":25,"i4i.32xlarge":75,"c6g.8xlarge":12,"m6id.8xlarge":12.5,"m6gd.2xlarge":10,"t2.small":null,"r6in.2xlarge":40,"r6in.xlarge":30,"m6a.24xlarge":37.5,"i3en.large":25,"m6idn.16xlarge":100,"c6gd.4xlarge":10,"r5dn.4xlarge":25,"r7g.12xlarge":22.5,"r6i.2xlarge":12.5,"z1d.3xlarge":10,"t3a.nano":5,"r5d.12xlarge":12,"g5.8xlarge":25,"i3.2xlarge":10,"g4ad.xlarge":10,"m6gd.large":10,"r6i.8xlarge":12.5,"r4.4xlarge":10,"m6g.medium":10,"m6i.2xlarge":12.5,"c6i.metal":50,"c5a.8xlarge":10,"m3.medium":null,"m5a.xlarge":10,"r6g.2xlarge":10,"r5dn.2xlarge":25,"c6id.4xlarge":12.5,"m5a.24xlarge":20,"m6a.32xlarge":50,"is4gen.8xlarge":50,"is4gen.medium":25,"m6idn.8xlarge":50,"x1e.xlarge":10,"m6in.2xlarge":40,"x2iedn.24xlarge":75,"m6id.24xlarge":37.5,"r6i.large":12.5,"i3en.xlarge":25,"p3dn.24xlarge":100,"c6gd.large":10,"i3.4xlarge":10,"m6a.8xlarge":12.5,"x1e.4xlarge":10,"c6g.4xlarge":10,"r6gd.12xlarge":20,"x2iedn.8xlarge":25,"c5a.large":10,"m3.large":null,"r5a.24xlarge":20,"c6id.2xlarge":12.5,"f1.4xlarge":10,"c6g.2xlarge":10,"c6i.12xlarge":18.75,"c6gd.medium":10,"c6a.2xlarge":12.5,"r6i.32xlarge":50,"m6a.xlarge":12.5,"t2.nano":null,"g3s.xlarge":10,"r6idn.12xlarge":75,"u-18tb1.112xlarge":100,"im4gn.16xlarge":100,"r5n.metal":100,"m1.small":null,"d3en.12xlarge":75,"g2.8xlarge":null,"g4dn.2xlarge":25,"c6g.metal":25,"d3en.xlarge":25,"c6gd.12xlarge":20,"g4dn.4xlarge":25,"m6a.48xlarge":50,"i4i.8xlarge":18.75,"c6in.metal":200,"r5dn.24xlarge":100,"m6a.12xlarge":18.75,"t3a.micro":5,"c5d.9xlarge":12,"g4dn.8xlarge":50,"t3a.xlarge":5,"c6a.large":12.5,"m1.medium":null,"c5.18xlarge":25,"i3en.metal":100,"m6i.12xlarge":18.75,"g5g.xlarge":10,"d2.xlarge":null,"m5a.12xlarge":10,"m1.large":null,"r5n.large":25,"i3en.6xlarge":25}}
80 changes: 80 additions & 0 deletions mountpoint-s3/scripts/network_performance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

# Please ensure 'jq' and AWS CLI v2 are installed before executing this script.
# The purpose of this script is to generate a json file contains network throughput numbers of each EC2 instance types.

version=v0.0.1 # release version
result_file=network_performance.json
temp_file=/tmp/temporary
timestamp="$(date --utc +%FT%TZ)"
region=us-east-1
version_number=0.0.1

# Network throughput query
query_ec2_instance_network_throughput="aws ec2 describe-instance-types
--filters \"Name=instance-type,Values=*\"
--query \"InstanceTypes[].[InstanceType, NetworkInfo.NetworkPerformance]\"
--region ${region}
--output json"

# Special case handling:
# The results for the following three instances are:
# dll.24xlarge --> 4x 100 Gbps
# p4d.24xlarge --> 4x 100 Gbps
# trn1.32xlarge --> 8x 100 Gbps
# Convert them to numbers when write to json entries.
declare -r -A THROUGHPUT_OVERRIDE=(
["dl1.24xlarge"]=400
["p4d.24xlarge"]=400
["trn1.32xlarge"]=800
)

(> ${temp_file})

generate_json_entry() {
echo "{"
echo "\"version\":{\"region\":\"${region}\",\"timestamp\":\"${timestamp}\",\"version_number\":\"${version_number}\"},"
echo "\"instance_throughput\": {"
eval $query_ec2_instance_network_throughput | \
jq -c '.[]' | \
while read line;do
# extract instance type as a string. eg: "m5dn.12xlarge"
instance_type=`echo $line | cut -d ',' -f1 | sed 's/\[//' | sed 's/\"//g'`

# special cases handling
if [ -v THROUGHPUT_OVERRIDE[$instance_type] ]; then
instance_throughput=`echo ${THROUGHPUT_OVERRIDE[$instance_type]}`
else
# extract instance throughput as a number. eg: 50
instance_throughput=`echo $line | cut -d ',' -f2 | sed 's/\s*[a-zA-Z]\s*//g' | sed 's/\]//' | sed 's/\"//g'`
fi

# concatenate both as a key:value pair. eg: "m5dn.12xlarge":50
type_throughput_pair="\"$instance_type\":$instance_throughput"

# some instance types is missing throughput numbers, appending null to make it a legal json element.
[[ $type_throughput_pair =~ .*:$ ]] && echo $type_throughput_pair'null,' >> ${temp_file} || echo $type_throughput_pair',' >> ${temp_file}
done;

# bind string in temp_file to a variable.
result=$(cat ${temp_file})

# remove last comma otherwise result invalid json format
result=${result%*,}

echo $result
echo "}}"
}

# validate json format and write to a file.
generate_json_entry | jq -c . > >(tee ${result_file}) 2>&1

if [ $? -eq 0 ]
then
echo "generate ${result_file} succeeded"
else
echo "generate ${result_file} failed"
rm ${result_file}
fi

rm ${temp_file}
67 changes: 56 additions & 11 deletions mountpoint-s3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ struct CliArgs {

#[clap(
long,
help = "Desired throughput in Gbps",
help = "Desired throughput in Gbps [default: 10]",
value_name = "N",
default_value = "10",
value_parser = value_parser!(u64).range(1..),
help_heading = CLIENT_OPTIONS_HEADER
)]
Expand Down Expand Up @@ -336,23 +335,29 @@ fn main() -> anyhow::Result<()> {
}

fn mount(args: CliArgs) -> anyhow::Result<FuseSession> {
const DEFAULT_TARGET_THROUGHPUT: f64 = 10.0;

let addressing_style = args.addressing_style();
let endpoint = args
.endpoint_url
.map(|uri| Endpoint::from_uri(&uri, addressing_style))
.transpose()
.context("Failed to parse endpoint URL")?;

match retrieve_instance_type() {
Ok(ec2_instance_type) => {
// TODO: config throughtput_target_gbps here.
tracing::info!("The EC2 instance type is: {:?}", ec2_instance_type);
}
Err(e) => tracing::error!("failed to detect EC2 instance type. {:?}", e),
}
let throughput_target_gbps =
args.throughput_target_gbps
.map(|t| t as f64)
.unwrap_or_else(|| match calculate_network_throughput() {
Ok(throughput) => throughput,
Err(e) => {
tracing::warn!("failed to detect network throughput: {:?}", e);
DEFAULT_TARGET_THROUGHPUT
}
});
tracing::info!("target network throughput {throughput_target_gbps} Gbps");

let client_config = S3ClientConfig {
throughput_target_gbps: args.throughput_target_gbps.map(|t| t as f64),
throughput_target_gbps: Some(throughput_target_gbps),
part_size: args.part_size.map(|t| t as usize),
endpoint,
user_agent_prefix: Some(format!("mountpoint-s3/{}", build_info::FULL_VERSION)),
Expand Down Expand Up @@ -485,13 +490,53 @@ fn parse_perm_bits(perm_bit_str: &str) -> Result<u16, anyhow::Error> {
}
}

pub fn retrieve_instance_type() -> anyhow::Result<String> {
fn calculate_network_throughput() -> anyhow::Result<f64> {
let instance_type = retrieve_instance_type().context("failed to retrieve instance type")?;
let throughput = get_maximum_network_throughput(&instance_type).context("failed to get network throughput")?;
Ok(throughput)
}

fn retrieve_instance_type() -> anyhow::Result<String> {
let imds_crt_client = ImdsCrtClient::new().context("failed to create IMDS client")?;

let query = imds_crt_client
.make_instance_type_query()
.context("failed to send IMDS query")?;

let result = futures::executor::block_on(query).context("IMDS query failed")?;
tracing::debug!("detected EC2 instance type {result}");
Ok(result)
}

fn get_maximum_network_throughput(ec2_instance_type: &str) -> anyhow::Result<f64> {
const INSTANCE_THROUGHPUT: &str = "instance_throughput";
let file = include_str!("../scripts/network_performance.json");

let data: serde_json::Value = serde_json::from_str(file).context("failed to parse network_performance.json")?;
let instance_throughput = data
.get(INSTANCE_THROUGHPUT)
.context("instance throughput missing from json")?;
instance_throughput
.get(ec2_instance_type)
.and_then(|t| t.as_f64())
.ok_or_else(|| anyhow!("no throughput configuration for EC2 instance type {ec2_instance_type}"))
}

#[cfg(test)]
mod tests {
use super::get_maximum_network_throughput;
use test_case::test_case;

#[test_case("c4.large", None)] // We let "Moderate" fall through to default
#[test_case("c5.large", Some(10.0))]
#[test_case("c5n.large", Some(25.0))]
#[test_case("c5n.18xlarge", Some(100.0))]
#[test_case("c6i.large", Some(12.5))]
#[test_case("p4d.24xlarge", Some(400.0))] // 4x 100 Gigabit
#[test_case("trn1.32xlarge", Some(800.0))] // 8x 100 Gigabit
#[test_case("dl1.24xlarge", Some(400.0))] // 4x 100 Gigabit
fn test_get_maximum_network_throughput(instance_type: &str, throughput: Option<f64>) {
let actual = get_maximum_network_throughput(instance_type).ok();
assert_eq!(actual, throughput);
}
}

0 comments on commit 9ad4854

Please sign in to comment.