From df6da29a6978aaef5462940ab7657678d4d7a62b Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 8 Sep 2023 09:39:56 +0200 Subject: [PATCH] Moves ByteSize to Utils assembly. --- DistTestCore/Codex/CodexContainerRecipe.cs | 9 ++-- DistTestCore/Codex/CodexStartupConfig.cs | 1 + KubernetesWorkflow/ByteSizeExtensions.cs | 41 +++++++++++++++++++ KubernetesWorkflow/ContainerRecipeFactory.cs | 29 +++---------- .../FullyConnectedDownloadTests.cs | 1 + {DistTestCore => Utils}/ByteSize.cs | 12 ++++-- 6 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 KubernetesWorkflow/ByteSizeExtensions.cs rename {DistTestCore => Utils}/ByteSize.cs (89%) diff --git a/DistTestCore/Codex/CodexContainerRecipe.cs b/DistTestCore/Codex/CodexContainerRecipe.cs index b38b3e7..37d1033 100644 --- a/DistTestCore/Codex/CodexContainerRecipe.cs +++ b/DistTestCore/Codex/CodexContainerRecipe.cs @@ -1,5 +1,6 @@ using DistTestCore.Marketplace; using KubernetesWorkflow; +using Utils; namespace DistTestCore.Codex { @@ -94,11 +95,11 @@ namespace DistTestCore.Codex } } - private long GetVolumeCapacity(CodexStartupConfig config) + private ByteSize GetVolumeCapacity(CodexStartupConfig config) { - if (config.StorageQuota != null) return config.StorageQuota.SizeInBytes; - // Default Codex quota: 8 Gb, using 9 to be safe. - return 9.GB().SizeInBytes; + if (config.StorageQuota != null) return config.StorageQuota; + // Default Codex quota: 8 Gb, using +20% to be safe. + return 8.GB().Multiply(1.2); } private int GetAccountIndex(MarketplaceInitialConfig marketplaceConfig) diff --git a/DistTestCore/Codex/CodexStartupConfig.cs b/DistTestCore/Codex/CodexStartupConfig.cs index c09f066..36e4757 100644 --- a/DistTestCore/Codex/CodexStartupConfig.cs +++ b/DistTestCore/Codex/CodexStartupConfig.cs @@ -1,6 +1,7 @@ using DistTestCore.Marketplace; using DistTestCore.Metrics; using KubernetesWorkflow; +using Utils; namespace DistTestCore.Codex { diff --git a/KubernetesWorkflow/ByteSizeExtensions.cs b/KubernetesWorkflow/ByteSizeExtensions.cs new file mode 100644 index 0000000..c3cca24 --- /dev/null +++ b/KubernetesWorkflow/ByteSizeExtensions.cs @@ -0,0 +1,41 @@ +using Utils; + +namespace KubernetesWorkflow +{ + public static class ByteSizeExtensions + { + public static string ToSuffixNotation(this ByteSize b) + { + long x = 1024; + var map = new Dictionary + { + { Pow(x, 4), "Ti" }, + { Pow(x, 3), "Gi" }, + { Pow(x, 2), "Mi" }, + { (x), "Ki" }, + }; + + var bytes = b.SizeInBytes; + foreach (var pair in map) + { + if (bytes > pair.Key) + { + double bytesD = bytes; + double divD = pair.Key; + double numD = Math.Ceiling(bytesD / divD); + var v = Convert.ToInt64(numD); + return $"{v}{pair.Value}"; + } + } + + return $"{bytes}"; + } + + private static long Pow(long x, int v) + { + long result = 1; + for (var i = 0; i < v; i++) result *= x; + return result; + } + } +} diff --git a/KubernetesWorkflow/ContainerRecipeFactory.cs b/KubernetesWorkflow/ContainerRecipeFactory.cs index d5c75a4..ad3e7de 100644 --- a/KubernetesWorkflow/ContainerRecipeFactory.cs +++ b/KubernetesWorkflow/ContainerRecipeFactory.cs @@ -1,4 +1,6 @@ -namespace KubernetesWorkflow +using Utils; + +namespace KubernetesWorkflow { public abstract class ContainerRecipeFactory { @@ -97,38 +99,17 @@ podAnnotations.Add(name, value); } - protected void AddVolume(string mountPath, long capacityBytes) + protected void AddVolume(string mountPath, ByteSize volumeSize) { volumeMounts.Add(new VolumeMount( $"autovolume-{Guid.NewGuid().ToString().ToLowerInvariant()}", mountPath, - FormatBytesQuantity(capacityBytes))); + volumeSize.ToSuffixNotation())); } protected void Additional(object userData) { additionals.Add(userData); } - - private static string FormatBytesQuantity(long capacityBytes) - { - var map = new Dictionary - { - { (1024*1024*1024), "Gi" }, - { (1024*1024), "Mi" }, - { (1024), "Ki" }, - }; - - foreach (var pair in map) - { - if (capacityBytes > pair.Key) - { - var v = (capacityBytes / pair.Key) + 1; - return $"{v}{pair.Value}"; - } - } - - return $"{capacityBytes * 2}Ki"; - } } } diff --git a/Tests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs b/Tests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs index 7cf546b..02ad9b0 100644 --- a/Tests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs +++ b/Tests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs @@ -1,5 +1,6 @@ using DistTestCore; using NUnit.Framework; +using Utils; namespace Tests.DownloadConnectivityTests { diff --git a/DistTestCore/ByteSize.cs b/Utils/ByteSize.cs similarity index 89% rename from DistTestCore/ByteSize.cs rename to Utils/ByteSize.cs index bbf01cc..b069b04 100644 --- a/DistTestCore/ByteSize.cs +++ b/Utils/ByteSize.cs @@ -1,10 +1,7 @@ -using Utils; - -namespace DistTestCore +namespace Utils { public class ByteSize { - public ByteSize(long sizeInBytes) { if (sizeInBytes < 0) throw new ArgumentException("Cannot create ByteSize object with size less than 0. Was: " + sizeInBytes); @@ -18,6 +15,13 @@ namespace DistTestCore return SizeInBytes / (1024 * 1024); } + public ByteSize Multiply(double factor) + { + double bytes = SizeInBytes; + double result = Math.Round(bytes * factor); + return new ByteSize(Convert.ToInt64(result)); + } + public override bool Equals(object? obj) { return obj is ByteSize size && SizeInBytes == size.SizeInBytes;