Compare commits
48
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8847de116d | ||
|
|
7d9dcb263d | ||
|
|
b3771cce32 | ||
|
|
9717591224 | ||
|
|
64ae7c8efe | ||
|
|
d8d7605ce5 | ||
|
|
6995cbfb23 | ||
|
|
c6a757d6fb | ||
|
|
c01f9dbb21 | ||
|
|
0ec43a9325 | ||
|
|
266c661958 | ||
|
|
8ea4c4ee37 | ||
|
|
725dfc23a6 | ||
|
|
39ff757b39 | ||
|
|
e187bfc941 | ||
|
|
dd36929a81 | ||
|
|
2902f6baab | ||
|
|
f38f7861a0 | ||
|
|
84a68521b0 | ||
|
|
ce7ba47a32 | ||
|
|
a20e9a30cf | ||
|
|
52cec0e9f3 | ||
|
|
9a6866cf8e | ||
|
|
15d7e6483e | ||
|
|
e1c710a093 | ||
|
|
0c700ded9d | ||
|
|
50b7e2300d | ||
|
|
a6379d02f1 | ||
|
|
6545f3469f | ||
|
|
58f7f9384a | ||
|
|
7ec9934751 | ||
|
|
c4c3f61a23 | ||
|
|
c856f404e3 | ||
|
|
eed989cbf5 | ||
|
|
630dc2814a | ||
|
|
570b174a00 | ||
|
|
700fc0ea40 | ||
|
|
23ebd4166b | ||
|
|
3683044bf7 | ||
|
|
fb10906816 | ||
|
|
86074dab6a | ||
|
|
015d8da21d | ||
|
|
d847c4f3ec | ||
|
|
a2e4869403 | ||
|
|
5ffe34bb83 | ||
|
|
e3b16fd742 | ||
|
|
80261959e7 | ||
|
|
7db9360ba4 |
@@ -4,9 +4,8 @@ namespace ArgsUniform
|
||||
{
|
||||
public class ArgsUniform<T>
|
||||
{
|
||||
private readonly Assigner<T> assigner;
|
||||
private readonly Action printAppInfo;
|
||||
private readonly object? defaultsProvider;
|
||||
private readonly IEnv.IEnv env;
|
||||
private readonly string[] args;
|
||||
private const int cliStart = 8;
|
||||
private const int shortStart = 38;
|
||||
@@ -31,9 +30,9 @@ namespace ArgsUniform
|
||||
public ArgsUniform(Action printAppInfo, object defaultsProvider, IEnv.IEnv env, params string[] args)
|
||||
{
|
||||
this.printAppInfo = printAppInfo;
|
||||
this.defaultsProvider = defaultsProvider;
|
||||
this.env = env;
|
||||
this.args = args;
|
||||
|
||||
assigner = new Assigner<T>(env, args, defaultsProvider);
|
||||
}
|
||||
|
||||
public T Parse(bool printResult = false)
|
||||
@@ -42,7 +41,7 @@ namespace ArgsUniform
|
||||
{
|
||||
printAppInfo();
|
||||
PrintHelp();
|
||||
throw new Exception();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
var result = Activator.CreateInstance<T>();
|
||||
@@ -53,18 +52,16 @@ namespace ArgsUniform
|
||||
var attr = uniformProperty.GetCustomAttribute<UniformAttribute>();
|
||||
if (attr != null)
|
||||
{
|
||||
if (!UniformAssign(result, attr, uniformProperty) && attr.Required)
|
||||
if (!assigner.UniformAssign(result, attr, uniformProperty) && attr.Required)
|
||||
{
|
||||
{
|
||||
missingRequired.Add(uniformProperty);
|
||||
}
|
||||
missingRequired.Add(uniformProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (missingRequired.Any())
|
||||
{
|
||||
PrintResults(result, uniformProperties);
|
||||
PrintResults(printResult,result, uniformProperties);
|
||||
Print("");
|
||||
foreach (var missing in missingRequired)
|
||||
{
|
||||
@@ -75,37 +72,39 @@ namespace ArgsUniform
|
||||
}
|
||||
|
||||
PrintHelp();
|
||||
throw new ArgumentException("Unable to assemble all required arguments");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
if (printResult)
|
||||
{
|
||||
PrintResults(result, uniformProperties);
|
||||
}
|
||||
PrintResults(printResult, result, uniformProperties);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void PrintResults(T result, PropertyInfo[] uniformProperties)
|
||||
{
|
||||
Print("");
|
||||
foreach (var p in uniformProperties)
|
||||
{
|
||||
Print($"\t{p.Name} = {p.GetValue(result)}");
|
||||
}
|
||||
Print("");
|
||||
}
|
||||
|
||||
public void PrintHelp()
|
||||
{
|
||||
Print("");
|
||||
PrintAligned("CLI option:", "(short)", "Environment variable:", "Description");
|
||||
var attrs = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(UniformAttribute), false).Length == 1).Select(p => p.GetCustomAttribute<UniformAttribute>()).Where(a => a != null).ToArray();
|
||||
foreach (var attr in attrs)
|
||||
PrintAligned("CLI option:", "(short)", "Environment variable:", "Description", "(default)");
|
||||
var props = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(UniformAttribute), false).Length == 1).ToArray();
|
||||
foreach (var prop in props)
|
||||
{
|
||||
var a = attr!;
|
||||
var optional = !a.Required ? " *" : "";
|
||||
PrintAligned($"--{a.Arg}=...", $"({a.ArgShort})", a.EnvVar, a.Description + optional);
|
||||
var a = prop.GetCustomAttribute<UniformAttribute>();
|
||||
if (a != null)
|
||||
{
|
||||
var optional = !a.Required ? " (optional)" : "";
|
||||
var def = assigner.DescribeDefaultFor(prop);
|
||||
PrintAligned($"--{a.Arg}=...", $"({a.ArgShort})", a.EnvVar, a.Description + optional, $"({def})");
|
||||
}
|
||||
}
|
||||
Print("");
|
||||
}
|
||||
|
||||
private void PrintResults(bool printResult, T result, PropertyInfo[] uniformProperties)
|
||||
{
|
||||
if (!printResult) return;
|
||||
Print("");
|
||||
foreach (var p in uniformProperties)
|
||||
{
|
||||
Print($"\t{p.Name} = {p.GetValue(result)}");
|
||||
}
|
||||
Print("");
|
||||
}
|
||||
@@ -115,7 +114,7 @@ namespace ArgsUniform
|
||||
Console.WriteLine(msg);
|
||||
}
|
||||
|
||||
private void PrintAligned(string cli, string s, string env, string desc)
|
||||
private void PrintAligned(string cli, string s, string env, string desc, string def)
|
||||
{
|
||||
Console.CursorLeft = cliStart;
|
||||
Console.Write(cli);
|
||||
@@ -124,132 +123,8 @@ namespace ArgsUniform
|
||||
Console.CursorLeft = envStart;
|
||||
Console.Write(env);
|
||||
Console.CursorLeft = descStart;
|
||||
Console.Write(desc + Environment.NewLine);
|
||||
}
|
||||
|
||||
private object GetDefaultValue(Type t)
|
||||
{
|
||||
if (t.IsValueType) return Activator.CreateInstance(t)!;
|
||||
return null!;
|
||||
}
|
||||
|
||||
private bool UniformAssign(T result, UniformAttribute attr, PropertyInfo uniformProperty)
|
||||
{
|
||||
if (AssignFromArgsIfAble(result, attr, uniformProperty)) return true;
|
||||
if (AssignFromEnvVarIfAble(result, attr, uniformProperty)) return true;
|
||||
if (AssignFromDefaultsIfAble(result, uniformProperty)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty)
|
||||
{
|
||||
var currentValue = uniformProperty.GetValue(result);
|
||||
var isEmptryString = (currentValue as string) == string.Empty;
|
||||
if (currentValue != GetDefaultValue(uniformProperty.PropertyType) && !isEmptryString) return true;
|
||||
|
||||
if (defaultsProvider == null) return false;
|
||||
|
||||
var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name);
|
||||
if (defaultProperty == null) return false;
|
||||
|
||||
var value = defaultProperty.GetValue(defaultsProvider);
|
||||
if (value != null)
|
||||
{
|
||||
return Assign(result, uniformProperty, value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool AssignFromEnvVarIfAble(T result, UniformAttribute attr, PropertyInfo uniformProperty)
|
||||
{
|
||||
var e = env.GetEnvVarOrDefault(attr.EnvVar, string.Empty);
|
||||
if (!string.IsNullOrEmpty(e))
|
||||
{
|
||||
return Assign(result, uniformProperty, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool AssignFromArgsIfAble(T result, UniformAttribute attr, PropertyInfo uniformProperty)
|
||||
{
|
||||
var fromArg = GetFromArgs(attr.Arg);
|
||||
if (fromArg != null)
|
||||
{
|
||||
return Assign(result, uniformProperty, fromArg);
|
||||
}
|
||||
var fromShort = GetFromArgs(attr.ArgShort);
|
||||
if (fromShort != null)
|
||||
{
|
||||
return Assign(result, uniformProperty, fromShort);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool Assign(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
if (uniformProperty.PropertyType == value.GetType())
|
||||
{
|
||||
uniformProperty.SetValue(result, value);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (uniformProperty.PropertyType == typeof(string) || uniformProperty.PropertyType == typeof(int))
|
||||
{
|
||||
uniformProperty.SetValue(result, Convert.ChangeType(value, uniformProperty.PropertyType));
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (uniformProperty.PropertyType == typeof(int?)) return AssignOptionalInt(result, uniformProperty, value);
|
||||
if (uniformProperty.PropertyType.IsEnum) return AssignEnum(result, uniformProperty, value);
|
||||
if (uniformProperty.PropertyType == typeof(bool)) return AssignBool(result, uniformProperty, value);
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool AssignEnum(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
var s = value.ToString();
|
||||
if (Enum.TryParse(uniformProperty.PropertyType, s, out var e))
|
||||
{
|
||||
uniformProperty.SetValue(result, e);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool AssignOptionalInt(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
if (int.TryParse(value.ToString(), out int i))
|
||||
{
|
||||
uniformProperty.SetValue(result, i);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool AssignBool(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
var s = value.ToString();
|
||||
if (s == "1" || (s != null && s.ToLowerInvariant() == "true"))
|
||||
{
|
||||
uniformProperty.SetValue(result, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private string? GetFromArgs(string key)
|
||||
{
|
||||
var argKey = $"--{key}=";
|
||||
var arg = args.FirstOrDefault(a => a.StartsWith(argKey));
|
||||
if (arg != null)
|
||||
{
|
||||
return arg.Substring(argKey.Length);
|
||||
}
|
||||
return null;
|
||||
Console.Write(desc + " ");
|
||||
Console.Write(def + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace ArgsUniform
|
||||
{
|
||||
public class Assigner<T>
|
||||
{
|
||||
private readonly IEnv.IEnv env;
|
||||
private readonly string[] args;
|
||||
private readonly object? defaultsProvider;
|
||||
|
||||
public Assigner(IEnv.IEnv env, string[] args, object? defaultsProvider)
|
||||
{
|
||||
this.env = env;
|
||||
this.args = args;
|
||||
this.defaultsProvider = defaultsProvider;
|
||||
}
|
||||
|
||||
public bool UniformAssign(T result, UniformAttribute attr, PropertyInfo uniformProperty)
|
||||
{
|
||||
if (AssignFromArgsIfAble(result, attr, uniformProperty)) return true;
|
||||
if (AssignFromEnvVarIfAble(result, attr, uniformProperty)) return true;
|
||||
if (AssignFromDefaultsIfAble(result, uniformProperty)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public string DescribeDefaultFor(PropertyInfo property)
|
||||
{
|
||||
var obj = Activator.CreateInstance<T>();
|
||||
var defaultValue = GetDefaultValue(obj, property);
|
||||
if (defaultValue == null) return "";
|
||||
if (defaultValue is string str)
|
||||
{
|
||||
return "\"" + str + "\"";
|
||||
}
|
||||
return defaultValue.ToString() ?? string.Empty;
|
||||
}
|
||||
|
||||
private object? GetDefaultValue(T result, PropertyInfo uniformProperty)
|
||||
{
|
||||
// Get value from object's static initializer if it's there.
|
||||
var currentValue = uniformProperty.GetValue(result);
|
||||
if (currentValue != null) return currentValue;
|
||||
|
||||
// Get value from defaults-provider object if it's there.
|
||||
if (defaultsProvider == null) return null;
|
||||
var defaultProperty = defaultsProvider.GetType().GetProperties().SingleOrDefault(p => p.Name == uniformProperty.Name);
|
||||
if (defaultProperty == null) return null;
|
||||
return defaultProperty.GetValue(defaultsProvider);
|
||||
}
|
||||
|
||||
private bool AssignFromDefaultsIfAble(T result, PropertyInfo uniformProperty)
|
||||
{
|
||||
var defaultValue = GetDefaultValue(result, uniformProperty);
|
||||
var isEmptryString = (defaultValue as string) == string.Empty;
|
||||
if (defaultValue != null && defaultValue != GetDefaultValueForType(uniformProperty.PropertyType) && !isEmptryString)
|
||||
{
|
||||
return Assign(result, uniformProperty, defaultValue);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool AssignFromEnvVarIfAble(T result, UniformAttribute attr, PropertyInfo uniformProperty)
|
||||
{
|
||||
var e = env.GetEnvVarOrDefault(attr.EnvVar, string.Empty);
|
||||
if (!string.IsNullOrEmpty(e))
|
||||
{
|
||||
return Assign(result, uniformProperty, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool AssignFromArgsIfAble(T result, UniformAttribute attr, PropertyInfo uniformProperty)
|
||||
{
|
||||
var fromArg = GetFromArgs(attr.Arg);
|
||||
if (fromArg != null)
|
||||
{
|
||||
return Assign(result, uniformProperty, fromArg);
|
||||
}
|
||||
var fromShort = GetFromArgs(attr.ArgShort);
|
||||
if (fromShort != null)
|
||||
{
|
||||
return Assign(result, uniformProperty, fromShort);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool Assign(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
if (uniformProperty.PropertyType == value.GetType())
|
||||
{
|
||||
uniformProperty.SetValue(result, value);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (uniformProperty.PropertyType == typeof(string) || uniformProperty.PropertyType == typeof(int))
|
||||
{
|
||||
uniformProperty.SetValue(result, Convert.ChangeType(value, uniformProperty.PropertyType));
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (uniformProperty.PropertyType == typeof(int?)) return AssignOptionalInt(result, uniformProperty, value);
|
||||
if (uniformProperty.PropertyType.IsEnum) return AssignEnum(result, uniformProperty, value);
|
||||
if (uniformProperty.PropertyType == typeof(bool)) return AssignBool(result, uniformProperty, value);
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool AssignEnum(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
var s = value.ToString();
|
||||
if (Enum.TryParse(uniformProperty.PropertyType, s, out var e))
|
||||
{
|
||||
uniformProperty.SetValue(result, e);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool AssignOptionalInt(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
if (int.TryParse(value.ToString(), out int i))
|
||||
{
|
||||
uniformProperty.SetValue(result, i);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool AssignBool(T result, PropertyInfo uniformProperty, object value)
|
||||
{
|
||||
var s = value.ToString();
|
||||
if (s == "1" || (s != null && s.ToLowerInvariant() == "true"))
|
||||
{
|
||||
uniformProperty.SetValue(result, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private string? GetFromArgs(string key)
|
||||
{
|
||||
var argKey = $"--{key}=";
|
||||
var arg = args.FirstOrDefault(a => a.StartsWith(argKey));
|
||||
if (arg != null)
|
||||
{
|
||||
return arg.Substring(argKey.Length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static object GetDefaultValueForType(Type t)
|
||||
{
|
||||
if (t.IsValueType) return Activator.CreateInstance(t)!;
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Core
|
||||
{
|
||||
public interface IDownloadedLog
|
||||
{
|
||||
void IterateLines(Action<string> action);
|
||||
string[] GetLinesContaining(string expectedString);
|
||||
string[] FindLinesThatContain(params string[] tags);
|
||||
void DeleteFile();
|
||||
@@ -18,6 +19,19 @@ namespace Core
|
||||
this.logFile = logFile;
|
||||
}
|
||||
|
||||
public void IterateLines(Action<string> action)
|
||||
{
|
||||
using var file = File.OpenRead(logFile.FullFilename);
|
||||
using var streamReader = new StreamReader(file);
|
||||
|
||||
var line = streamReader.ReadLine();
|
||||
while (line != null)
|
||||
{
|
||||
action(line);
|
||||
line = streamReader.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
public string[] GetLinesContaining(string expectedString)
|
||||
{
|
||||
using var file = File.OpenRead(logFile.FullFilename);
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace Core
|
||||
{
|
||||
lock (httpLock)
|
||||
{
|
||||
return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description);
|
||||
return Time.Retry(operation, timeSet.HttpRetryTimeout(), timeSet.HttpCallRetryDelay(), description);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+33
-12
@@ -2,10 +2,31 @@
|
||||
{
|
||||
public interface ITimeSet
|
||||
{
|
||||
/// <summary>
|
||||
/// Timeout for a single HTTP call.
|
||||
/// </summary>
|
||||
TimeSpan HttpCallTimeout();
|
||||
int HttpMaxNumberOfRetries();
|
||||
|
||||
/// <summary>
|
||||
/// Maximum total time to attempt to make a successful HTTP call to a service.
|
||||
/// When HTTP calls time out during this timespan, retries will be made.
|
||||
/// </summary>
|
||||
TimeSpan HttpRetryTimeout();
|
||||
|
||||
/// <summary>
|
||||
/// After a failed HTTP call, wait this long before trying again.
|
||||
/// </summary>
|
||||
TimeSpan HttpCallRetryDelay();
|
||||
TimeSpan WaitForK8sServiceDelay();
|
||||
|
||||
/// <summary>
|
||||
/// After a failed K8s operation, wait this long before trying again.
|
||||
/// </summary>
|
||||
TimeSpan K8sOperationRetryDelay();
|
||||
|
||||
/// <summary>
|
||||
/// Maximum total time to attempt to perform a successful k8s operation.
|
||||
/// If k8s operations fail during this timespan, retries will be made.
|
||||
/// </summary>
|
||||
TimeSpan K8sOperationTimeout();
|
||||
}
|
||||
|
||||
@@ -16,9 +37,9 @@
|
||||
return TimeSpan.FromMinutes(3);
|
||||
}
|
||||
|
||||
public int HttpMaxNumberOfRetries()
|
||||
public TimeSpan HttpRetryTimeout()
|
||||
{
|
||||
return 3;
|
||||
return TimeSpan.FromMinutes(10);
|
||||
}
|
||||
|
||||
public TimeSpan HttpCallRetryDelay()
|
||||
@@ -26,7 +47,7 @@
|
||||
return TimeSpan.FromSeconds(1);
|
||||
}
|
||||
|
||||
public TimeSpan WaitForK8sServiceDelay()
|
||||
public TimeSpan K8sOperationRetryDelay()
|
||||
{
|
||||
return TimeSpan.FromSeconds(10);
|
||||
}
|
||||
@@ -41,27 +62,27 @@
|
||||
{
|
||||
public TimeSpan HttpCallTimeout()
|
||||
{
|
||||
return TimeSpan.FromHours(2);
|
||||
return TimeSpan.FromMinutes(30);
|
||||
}
|
||||
|
||||
public int HttpMaxNumberOfRetries()
|
||||
public TimeSpan HttpRetryTimeout()
|
||||
{
|
||||
return 1;
|
||||
return TimeSpan.FromHours(2.2);
|
||||
}
|
||||
|
||||
public TimeSpan HttpCallRetryDelay()
|
||||
{
|
||||
return TimeSpan.FromSeconds(2);
|
||||
return TimeSpan.FromSeconds(20);
|
||||
}
|
||||
|
||||
public TimeSpan WaitForK8sServiceDelay()
|
||||
public TimeSpan K8sOperationRetryDelay()
|
||||
{
|
||||
return TimeSpan.FromSeconds(10);
|
||||
return TimeSpan.FromSeconds(30);
|
||||
}
|
||||
|
||||
public TimeSpan K8sOperationTimeout()
|
||||
{
|
||||
return TimeSpan.FromMinutes(15);
|
||||
return TimeSpan.FromHours(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
|
||||
public class MarketAverage
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public int NumberOfFinished { get; set; }
|
||||
public int TimeRangeSeconds { get; set; }
|
||||
public float Price { get; set; }
|
||||
public float Size { get; set; }
|
||||
public float Duration { get; set; }
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace FileUtils
|
||||
|
||||
if (readExpected == 0 && readActual == 0)
|
||||
{
|
||||
log.Log($"OK: '{Describe()}' is equal to '{actual.Describe()}'.");
|
||||
log.Log($"OK: {Describe()} is equal to {actual.Describe()}.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -705,14 +705,14 @@ namespace KubernetesWorkflow
|
||||
|
||||
private string GetPodName(RunningContainer container)
|
||||
{
|
||||
return GetPodForDeployment(container.RunningContainers.StartResult.Deployment).Metadata.Name;
|
||||
return GetPodForDeployment(container.RunningPod.StartResult.Deployment).Metadata.Name;
|
||||
}
|
||||
|
||||
private V1Pod GetPodForDeployment(RunningDeployment deployment)
|
||||
{
|
||||
return Time.Retry(() => GetPodForDeplomentInternal(deployment),
|
||||
// We will wait up to 1 minute, k8s might be moving pods around.
|
||||
maxRetries: 6,
|
||||
maxTimeout: TimeSpan.FromMinutes(1),
|
||||
retryTime: TimeSpan.FromSeconds(10),
|
||||
description: "Find pod by label for deployment.");
|
||||
}
|
||||
@@ -868,7 +868,7 @@ namespace KubernetesWorkflow
|
||||
|
||||
private void WaitUntilNamespaceCreated()
|
||||
{
|
||||
WaitUntil(() => IsNamespaceOnline(K8sNamespace));
|
||||
WaitUntil(() => IsNamespaceOnline(K8sNamespace), nameof(WaitUntilNamespaceCreated));
|
||||
}
|
||||
|
||||
private void WaitUntilDeploymentOnline(string deploymentName)
|
||||
@@ -877,7 +877,7 @@ namespace KubernetesWorkflow
|
||||
{
|
||||
var deployment = client.Run(c => c.ReadNamespacedDeployment(deploymentName, K8sNamespace));
|
||||
return deployment?.Status.AvailableReplicas != null && deployment.Status.AvailableReplicas > 0;
|
||||
});
|
||||
}, nameof(WaitUntilDeploymentOnline));
|
||||
}
|
||||
|
||||
private void WaitUntilDeploymentOffline(string deploymentName)
|
||||
@@ -887,7 +887,7 @@ namespace KubernetesWorkflow
|
||||
var deployments = client.Run(c => c.ListNamespacedDeployment(K8sNamespace));
|
||||
var deployment = deployments.Items.SingleOrDefault(d => d.Metadata.Name == deploymentName);
|
||||
return deployment == null || deployment.Status.AvailableReplicas == 0;
|
||||
});
|
||||
}, nameof(WaitUntilDeploymentOffline));
|
||||
}
|
||||
|
||||
private void WaitUntilPodsForDeploymentAreOffline(RunningDeployment deployment)
|
||||
@@ -896,19 +896,19 @@ namespace KubernetesWorkflow
|
||||
{
|
||||
var pods = FindPodsByLabel(deployment.PodLabel);
|
||||
return !pods.Any();
|
||||
});
|
||||
}, nameof(WaitUntilPodsForDeploymentAreOffline));
|
||||
}
|
||||
|
||||
private void WaitUntil(Func<bool> predicate)
|
||||
private void WaitUntil(Func<bool> predicate, string msg)
|
||||
{
|
||||
var sw = Stopwatch.Begin(log, true);
|
||||
try
|
||||
{
|
||||
Time.WaitUntil(predicate, cluster.K8sOperationTimeout(), cluster.K8sOperationRetryDelay());
|
||||
Time.WaitUntil(predicate, cluster.K8sOperationTimeout(), cluster.K8sOperationRetryDelay(), msg);
|
||||
}
|
||||
finally
|
||||
{
|
||||
sw.End("", 1);
|
||||
sw.End(msg, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,18 +5,18 @@ namespace KubernetesWorkflow
|
||||
{
|
||||
public interface IK8sHooks
|
||||
{
|
||||
void OnContainersStarted(RunningContainers runningContainers);
|
||||
void OnContainersStopped(RunningContainers runningContainers);
|
||||
void OnContainersStarted(RunningPod runningPod);
|
||||
void OnContainersStopped(RunningPod runningPod);
|
||||
void OnContainerRecipeCreated(ContainerRecipe recipe);
|
||||
}
|
||||
|
||||
public class DoNothingK8sHooks : IK8sHooks
|
||||
{
|
||||
public void OnContainersStarted(RunningContainers runningContainers)
|
||||
public void OnContainersStarted(RunningPod runningPod)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnContainersStopped(RunningContainers runningContainers)
|
||||
public void OnContainersStopped(RunningPod runningPod)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ namespace KubernetesWorkflow
|
||||
FutureContainers Start(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig);
|
||||
FutureContainers Start(int numberOfContainers, ILocation location, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig);
|
||||
PodInfo GetPodInfo(RunningContainer container);
|
||||
PodInfo GetPodInfo(RunningContainers containers);
|
||||
PodInfo GetPodInfo(RunningPod pod);
|
||||
CrashWatcher CreateCrashWatcher(RunningContainer container);
|
||||
void Stop(RunningContainers containers, bool waitTillStopped);
|
||||
void Stop(RunningPod pod, bool waitTillStopped);
|
||||
void DownloadContainerLog(RunningContainer container, ILogHandler logHandler, int? tailLines = null);
|
||||
string ExecuteCommand(RunningContainer container, string command, params string[] args);
|
||||
void DeleteNamespace();
|
||||
@@ -60,7 +60,7 @@ namespace KubernetesWorkflow
|
||||
var startResult = controller.BringOnline(recipes, location);
|
||||
var containers = CreateContainers(startResult, recipes, startupConfig);
|
||||
|
||||
var rc = new RunningContainers(startupConfig, startResult, containers);
|
||||
var rc = new RunningPod(startupConfig, startResult, containers);
|
||||
cluster.Configuration.Hooks.OnContainersStarted(rc);
|
||||
|
||||
if (startResult.ExternalService != null)
|
||||
@@ -71,7 +71,7 @@ namespace KubernetesWorkflow
|
||||
});
|
||||
}
|
||||
|
||||
public void WaitUntilOnline(RunningContainers rc)
|
||||
public void WaitUntilOnline(RunningPod rc)
|
||||
{
|
||||
K8s(controller =>
|
||||
{
|
||||
@@ -84,12 +84,12 @@ namespace KubernetesWorkflow
|
||||
|
||||
public PodInfo GetPodInfo(RunningContainer container)
|
||||
{
|
||||
return K8s(c => c.GetPodInfo(container.RunningContainers.StartResult.Deployment));
|
||||
return K8s(c => c.GetPodInfo(container.RunningPod.StartResult.Deployment));
|
||||
}
|
||||
|
||||
public PodInfo GetPodInfo(RunningContainers containers)
|
||||
public PodInfo GetPodInfo(RunningPod pod)
|
||||
{
|
||||
return K8s(c => c.GetPodInfo(containers.StartResult.Deployment));
|
||||
return K8s(c => c.GetPodInfo(pod.StartResult.Deployment));
|
||||
}
|
||||
|
||||
public CrashWatcher CreateCrashWatcher(RunningContainer container)
|
||||
@@ -97,12 +97,12 @@ namespace KubernetesWorkflow
|
||||
return K8s(c => c.CreateCrashWatcher(container));
|
||||
}
|
||||
|
||||
public void Stop(RunningContainers runningContainers, bool waitTillStopped)
|
||||
public void Stop(RunningPod runningPod, bool waitTillStopped)
|
||||
{
|
||||
K8s(controller =>
|
||||
{
|
||||
controller.Stop(runningContainers.StartResult, waitTillStopped);
|
||||
cluster.Configuration.Hooks.OnContainersStopped(runningContainers);
|
||||
controller.Stop(runningPod.StartResult, waitTillStopped);
|
||||
cluster.Configuration.Hooks.OnContainersStopped(runningPod);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,19 @@
|
||||
{
|
||||
public class FutureContainers
|
||||
{
|
||||
private readonly RunningContainers runningContainers;
|
||||
private readonly RunningPod runningPod;
|
||||
private readonly StartupWorkflow workflow;
|
||||
|
||||
public FutureContainers(RunningContainers runningContainers, StartupWorkflow workflow)
|
||||
public FutureContainers(RunningPod runningPod, StartupWorkflow workflow)
|
||||
{
|
||||
this.runningContainers = runningContainers;
|
||||
this.runningPod = runningPod;
|
||||
this.workflow = workflow;
|
||||
}
|
||||
|
||||
public RunningContainers WaitForOnline()
|
||||
public RunningPod WaitForOnline()
|
||||
{
|
||||
workflow.WaitUntilOnline(runningContainers);
|
||||
return runningContainers;
|
||||
workflow.WaitUntilOnline(runningPod);
|
||||
return runningPod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace KubernetesWorkflow.Types
|
||||
public ContainerAddress[] Addresses { get; }
|
||||
|
||||
[JsonIgnore]
|
||||
public RunningContainers RunningContainers { get; internal set; } = null!;
|
||||
public RunningPod RunningPod { get; internal set; } = null!;
|
||||
|
||||
public Address GetAddress(ILog log, string portTag)
|
||||
{
|
||||
|
||||
+5
-10
@@ -2,15 +2,15 @@
|
||||
|
||||
namespace KubernetesWorkflow.Types
|
||||
{
|
||||
public class RunningContainers
|
||||
public class RunningPod
|
||||
{
|
||||
public RunningContainers(StartupConfig startupConfig, StartResult startResult, RunningContainer[] containers)
|
||||
public RunningPod(StartupConfig startupConfig, StartResult startResult, RunningContainer[] containers)
|
||||
{
|
||||
StartupConfig = startupConfig;
|
||||
StartResult = startResult;
|
||||
Containers = containers;
|
||||
|
||||
foreach (var c in containers) c.RunningContainers = this;
|
||||
foreach (var c in containers) c.RunningPod = this;
|
||||
}
|
||||
|
||||
public StartupConfig StartupConfig { get; }
|
||||
@@ -20,7 +20,7 @@ namespace KubernetesWorkflow.Types
|
||||
[JsonIgnore]
|
||||
public string Name
|
||||
{
|
||||
get { return $"{Containers.Length}x '{Containers.First().Name}'"; }
|
||||
get { return $"'{string.Join("&", Containers.Select(c => c.Name).ToArray())}'"; }
|
||||
}
|
||||
|
||||
public string Describe()
|
||||
@@ -31,12 +31,7 @@ namespace KubernetesWorkflow.Types
|
||||
|
||||
public static class RunningContainersExtensions
|
||||
{
|
||||
public static RunningContainer[] Containers(this RunningContainers[] runningContainers)
|
||||
{
|
||||
return runningContainers.SelectMany(c => c.Containers).ToArray();
|
||||
}
|
||||
|
||||
public static string Describe(this RunningContainers[] runningContainers)
|
||||
public static string Describe(this RunningPod[] runningContainers)
|
||||
{
|
||||
return string.Join(",", runningContainers.Select(c => c.Describe()));
|
||||
}
|
||||
@@ -20,6 +20,7 @@ namespace NethereumWorkflow.BlockUtils
|
||||
|
||||
public BlockTimeEntry Get(ulong blockNumber)
|
||||
{
|
||||
bounds.Initialize();
|
||||
var b = cache.Get(blockNumber);
|
||||
if (b != null) return b;
|
||||
return GetBlock(blockNumber);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Utils
|
||||
using System.Globalization;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
public static class Formatter
|
||||
{
|
||||
@@ -10,7 +12,7 @@
|
||||
|
||||
var sizeOrder = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
|
||||
var digit = Math.Round(bytes / Math.Pow(1024, sizeOrder), 1);
|
||||
return digit.ToString() + sizeSuffixes[sizeOrder];
|
||||
return digit.ToString(CultureInfo.InvariantCulture) + sizeSuffixes[sizeOrder];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
{
|
||||
public class NumberSource
|
||||
{
|
||||
private readonly object @lock = new object();
|
||||
private int number;
|
||||
|
||||
public NumberSource(int start)
|
||||
@@ -11,8 +12,12 @@
|
||||
|
||||
public int GetNextNumber()
|
||||
{
|
||||
var n = number;
|
||||
number++;
|
||||
var n = -1;
|
||||
lock (@lock)
|
||||
{
|
||||
n = number;
|
||||
number++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
+52
-26
@@ -1,4 +1,6 @@
|
||||
namespace Utils
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
public static class Time
|
||||
{
|
||||
@@ -57,60 +59,66 @@
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void WaitUntil(Func<bool> predicate)
|
||||
public static void WaitUntil(Func<bool> predicate, string msg)
|
||||
{
|
||||
WaitUntil(predicate, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(1));
|
||||
WaitUntil(predicate, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(1), msg);
|
||||
}
|
||||
|
||||
public static void WaitUntil(Func<bool> predicate, TimeSpan timeout, TimeSpan retryDelay)
|
||||
public static void WaitUntil(Func<bool> predicate, TimeSpan timeout, TimeSpan retryDelay, string msg)
|
||||
{
|
||||
var start = DateTime.UtcNow;
|
||||
var tries = 1;
|
||||
var state = predicate();
|
||||
while (!state)
|
||||
{
|
||||
if (DateTime.UtcNow - start > timeout)
|
||||
var duration = DateTime.UtcNow - start;
|
||||
if (duration > timeout)
|
||||
{
|
||||
throw new TimeoutException("Operation timed out.");
|
||||
throw new TimeoutException($"Operation timed out after {tries} tries over (total) {FormatDuration(duration)}. '{msg}'");
|
||||
}
|
||||
|
||||
Sleep(retryDelay);
|
||||
state = predicate();
|
||||
tries++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Retry(Action action, string description)
|
||||
{
|
||||
Retry(action, 1, description);
|
||||
Retry(action, TimeSpan.FromSeconds(30), description);
|
||||
}
|
||||
|
||||
public static T Retry<T>(Func<T> action, string description)
|
||||
{
|
||||
return Retry(action, 1, description);
|
||||
return Retry(action, TimeSpan.FromSeconds(30), description);
|
||||
}
|
||||
|
||||
public static void Retry(Action action, int maxRetries, string description)
|
||||
public static void Retry(Action action, TimeSpan maxTimeout, string description)
|
||||
{
|
||||
Retry(action, maxRetries, TimeSpan.FromSeconds(5), description);
|
||||
Retry(action, maxTimeout, TimeSpan.FromSeconds(5), description);
|
||||
}
|
||||
|
||||
public static T Retry<T>(Func<T> action, int maxRetries, string description)
|
||||
public static T Retry<T>(Func<T> action, TimeSpan maxTimeout, string description)
|
||||
{
|
||||
return Retry(action, maxRetries, TimeSpan.FromSeconds(5), description);
|
||||
return Retry(action, maxTimeout, TimeSpan.FromSeconds(5), description);
|
||||
}
|
||||
|
||||
public static void Retry(Action action, int maxRetries, TimeSpan retryTime, string description)
|
||||
public static void Retry(Action action, TimeSpan maxTimeout, TimeSpan retryTime, string description)
|
||||
{
|
||||
var start = DateTime.UtcNow;
|
||||
var retries = 0;
|
||||
var exceptions = new List<Exception>();
|
||||
var tries = 1;
|
||||
var tryInfo = new List<(Exception, TimeSpan)>();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (retries > maxRetries)
|
||||
var duration = DateTime.UtcNow - start;
|
||||
if (duration > maxTimeout)
|
||||
{
|
||||
var duration = DateTime.UtcNow - start;
|
||||
throw new TimeoutException($"Retry '{description}' timed out after {maxRetries} tries over {Time.FormatDuration(duration)}.", new AggregateException(exceptions));
|
||||
var info = FormatTryInfos(tryInfo);
|
||||
throw new TimeoutException($"Retry '{description}' timed out after {tries} tries over {FormatDuration(duration)}.{Environment.NewLine}{info}");
|
||||
}
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
action();
|
||||
@@ -118,25 +126,42 @@
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
retries++;
|
||||
tryInfo.Add((ex, sw.Elapsed));
|
||||
tries++;
|
||||
}
|
||||
|
||||
Sleep(retryTime);
|
||||
}
|
||||
}
|
||||
|
||||
public static T Retry<T>(Func<T> action, int maxRetries, TimeSpan retryTime, string description)
|
||||
private static string FormatTryInfos(List<(Exception, TimeSpan)> tryInfo)
|
||||
{
|
||||
return string.Join(Environment.NewLine, tryInfo.Select(FormatTryInfo).ToArray());
|
||||
}
|
||||
|
||||
private static string FormatTryInfo((Exception, TimeSpan) info, int index)
|
||||
{
|
||||
return $"Attempt {index} took {FormatDuration(info.Item2)} and failed with exception {info.Item1}.";
|
||||
}
|
||||
|
||||
private static Action<int> failedCallback = i => { };
|
||||
public static void SetRetryFailedCallback(Action<int> onRetryFailed)
|
||||
{
|
||||
failedCallback = onRetryFailed;
|
||||
}
|
||||
|
||||
public static T Retry<T>(Func<T> action, TimeSpan maxTimeout, TimeSpan retryTime, string description)
|
||||
{
|
||||
var start = DateTime.UtcNow;
|
||||
var retries = 0;
|
||||
var tries = 1;
|
||||
var exceptions = new List<Exception>();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (retries > maxRetries)
|
||||
var duration = DateTime.UtcNow - start;
|
||||
if (duration > maxTimeout)
|
||||
{
|
||||
var duration = DateTime.UtcNow - start;
|
||||
throw new TimeoutException($"Retry '{description}' timed out after {maxRetries} tries over {Time.FormatDuration(duration)}.", new AggregateException(exceptions));
|
||||
throw new TimeoutException($"Retry '{description}' timed out after {tries} tries over {FormatDuration(duration)}.", new AggregateException(exceptions));
|
||||
}
|
||||
|
||||
try
|
||||
@@ -146,7 +171,8 @@
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
retries++;
|
||||
failedCallback(tries);
|
||||
tries++;
|
||||
}
|
||||
|
||||
Sleep(retryTime);
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace CodexContractsPlugin
|
||||
var logHandler = new ContractsReadyLogHandler(tools.GetLog());
|
||||
workflow.DownloadContainerLog(container, logHandler, 100);
|
||||
return logHandler.Found;
|
||||
});
|
||||
}, nameof(DeployContract));
|
||||
Log("Contracts deployed. Extracting addresses...");
|
||||
|
||||
var extractor = new ContractsContainerInfoExtractor(tools.GetLog(), workflow, container);
|
||||
@@ -71,7 +71,7 @@ namespace CodexContractsPlugin
|
||||
|
||||
Log("Extract completed. Checking sync...");
|
||||
|
||||
Time.WaitUntil(() => interaction.IsSynced(marketplaceAddress, abi));
|
||||
Time.WaitUntil(() => interaction.IsSynced(marketplaceAddress, abi), nameof(DeployContract));
|
||||
|
||||
Log("Synced. Codex SmartContracts deployed.");
|
||||
|
||||
@@ -83,9 +83,9 @@ namespace CodexContractsPlugin
|
||||
tools.GetLog().Log(msg);
|
||||
}
|
||||
|
||||
private void WaitUntil(Func<bool> predicate)
|
||||
private void WaitUntil(Func<bool> predicate, string msg)
|
||||
{
|
||||
Time.WaitUntil(predicate, TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(2));
|
||||
Time.WaitUntil(predicate, TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(2), msg);
|
||||
}
|
||||
|
||||
private StartupConfig CreateStartupConfig(IGethNode gethNode)
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +1,14 @@
|
||||
This code was generated using the Nethereum code generator, here: http://playground.nethereum.com
|
||||
|
||||
1. Go to site -> Abi Code Gen.
|
||||
1. Contract name = "Marketplace".
|
||||
1. In container, get "/hardhat/artifacts/contracts/Marketplace.sol/Marketplace.json".
|
||||
1. Save only ABI section as new JSON. (top-level is a json array.)
|
||||
1. From original JSON get byte code.
|
||||
1. Put ABI JSON and byte code into site.
|
||||
1. Generate.
|
||||
1. From site generated code, copy `public partial class MarketplaceDeployment` and everything after it. (be considerate of namespace brackets!)
|
||||
1. In Marketplace/Marketplace.cs, replace content of 'namespace CodexContractsPlugin.Marketplace'.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -29,19 +29,19 @@ namespace CodexDiscordBotPlugin
|
||||
{
|
||||
}
|
||||
|
||||
public RunningContainers Deploy(DiscordBotStartupConfig config)
|
||||
public RunningPod Deploy(DiscordBotStartupConfig config)
|
||||
{
|
||||
var workflow = tools.CreateWorkflow();
|
||||
return StartContainer(workflow, config);
|
||||
}
|
||||
|
||||
public RunningContainers DeployRewarder(RewarderBotStartupConfig config)
|
||||
public RunningPod DeployRewarder(RewarderBotStartupConfig config)
|
||||
{
|
||||
var workflow = tools.CreateWorkflow();
|
||||
return StartRewarderContainer(workflow, config);
|
||||
}
|
||||
|
||||
private RunningContainers StartContainer(IStartupWorkflow workflow, DiscordBotStartupConfig config)
|
||||
private RunningPod StartContainer(IStartupWorkflow workflow, DiscordBotStartupConfig config)
|
||||
{
|
||||
var startupConfig = new StartupConfig();
|
||||
startupConfig.NameOverride = config.Name;
|
||||
@@ -49,7 +49,7 @@ namespace CodexDiscordBotPlugin
|
||||
return workflow.Start(1, new DiscordBotContainerRecipe(), startupConfig).WaitForOnline();
|
||||
}
|
||||
|
||||
private RunningContainers StartRewarderContainer(IStartupWorkflow workflow, RewarderBotStartupConfig config)
|
||||
private RunningPod StartRewarderContainer(IStartupWorkflow workflow, RewarderBotStartupConfig config)
|
||||
{
|
||||
var startupConfig = new StartupConfig();
|
||||
startupConfig.Add(config);
|
||||
|
||||
@@ -5,12 +5,12 @@ namespace CodexDiscordBotPlugin
|
||||
{
|
||||
public static class CoreInterfaceExtensions
|
||||
{
|
||||
public static RunningContainers DeployCodexDiscordBot(this CoreInterface ci, DiscordBotStartupConfig config)
|
||||
public static RunningPod DeployCodexDiscordBot(this CoreInterface ci, DiscordBotStartupConfig config)
|
||||
{
|
||||
return Plugin(ci).Deploy(config);
|
||||
}
|
||||
|
||||
public static RunningContainers DeployRewarderBot(this CoreInterface ci, RewarderBotStartupConfig config)
|
||||
public static RunningPod DeployRewarderBot(this CoreInterface ci, RewarderBotStartupConfig config)
|
||||
{
|
||||
return Plugin(ci).DeployRewarder(config);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace CodexPlugin
|
||||
public class ApiChecker
|
||||
{
|
||||
// <INSERT-OPENAPI-YAML-HASH>
|
||||
private const string OpenApiYamlHash = "5A-B0-2A-AC-42-B1-A2-49-6F-9D-4E-D8-56-40-10-A6-67-F4-0D-2A-9F-E0-84-5C-EB-B8-2D-4F-D8-56-79-6C";
|
||||
private const string OpenApiYamlHash = "0F-C8-02-1E-2C-2C-15-F6-91-6A-01-31-11-49-95-06-79-26-25-BF-27-3C-A8-2E-5F-7F-34-FD-C0-57-A0-9A";
|
||||
private const string OpenApiFilePath = "/codex/openapi.yaml";
|
||||
private const string DisableEnvironmentVariable = "CODEXPLUGIN_DISABLE_APICHECK";
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace CodexPlugin
|
||||
if (string.IsNullOrEmpty(OpenApiYamlHash)) throw new Exception("OpenAPI yaml hash was not inserted by pre-build trigger.");
|
||||
}
|
||||
|
||||
public void CheckCompatibility(RunningContainers[] containers)
|
||||
public void CheckCompatibility(RunningPod[] containers)
|
||||
{
|
||||
if (checkPassed) return;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace CodexPlugin
|
||||
private readonly Mapper mapper = new Mapper();
|
||||
private bool hasContainerCrashed;
|
||||
|
||||
public CodexAccess(IPluginTools tools, RunningContainer container, CrashWatcher crashWatcher)
|
||||
public CodexAccess(IPluginTools tools, RunningPod container, CrashWatcher crashWatcher)
|
||||
{
|
||||
this.tools = tools;
|
||||
Container = container;
|
||||
@@ -23,7 +23,7 @@ namespace CodexPlugin
|
||||
CrashWatcher.Start(this);
|
||||
}
|
||||
|
||||
public RunningContainer Container { get; }
|
||||
public RunningPod Container { get; }
|
||||
public CrashWatcher CrashWatcher { get; }
|
||||
|
||||
public DebugInfo GetDebugInfo()
|
||||
@@ -136,7 +136,7 @@ namespace CodexPlugin
|
||||
|
||||
private Address GetAddress()
|
||||
{
|
||||
return Container.GetAddress(tools.GetLog(), CodexContainerRecipe.ApiPortTag);
|
||||
return Container.Containers.Single().GetAddress(tools.GetLog(), CodexContainerRecipe.ApiPortTag);
|
||||
}
|
||||
|
||||
private void CheckContainerCrashed(HttpClient client)
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace CodexPlugin
|
||||
{
|
||||
public class CodexContainerRecipe : ContainerRecipeFactory
|
||||
{
|
||||
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-1524803-dist-tests";
|
||||
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-267266a-dist-tests";
|
||||
|
||||
public const string ApiPortTag = "codex_api_port";
|
||||
public const string ListenPortTag = "codex_listen_port";
|
||||
public const string MetricsPortTag = "codex_metrics_port";
|
||||
|
||||
@@ -7,8 +7,8 @@ namespace CodexPlugin
|
||||
public class CodexDeployment
|
||||
{
|
||||
public CodexDeployment(CodexInstance[] codexInstances, GethDeployment gethDeployment,
|
||||
CodexContractsDeployment codexContractsDeployment, RunningContainers? prometheusContainer,
|
||||
RunningContainers? discordBotContainer, DeploymentMetadata metadata,
|
||||
CodexContractsDeployment codexContractsDeployment, RunningPod? prometheusContainer,
|
||||
RunningPod? discordBotContainer, DeploymentMetadata metadata,
|
||||
String id)
|
||||
{
|
||||
Id = id;
|
||||
@@ -24,20 +24,20 @@ namespace CodexPlugin
|
||||
public CodexInstance[] CodexInstances { get; }
|
||||
public GethDeployment GethDeployment { get; }
|
||||
public CodexContractsDeployment CodexContractsDeployment { get; }
|
||||
public RunningContainers? PrometheusContainer { get; }
|
||||
public RunningContainers? DiscordBotContainer { get; }
|
||||
public RunningPod? PrometheusContainer { get; }
|
||||
public RunningPod? DiscordBotContainer { get; }
|
||||
public DeploymentMetadata Metadata { get; }
|
||||
}
|
||||
|
||||
public class CodexInstance
|
||||
{
|
||||
public CodexInstance(RunningContainers containers, DebugInfo info)
|
||||
public CodexInstance(RunningPod pod, DebugInfo info)
|
||||
{
|
||||
Containers = containers;
|
||||
Pod = pod;
|
||||
Info = info;
|
||||
}
|
||||
|
||||
public RunningContainers Containers { get; }
|
||||
public RunningPod Pod { get; }
|
||||
public DebugInfo Info { get; }
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace CodexPlugin
|
||||
{
|
||||
public class CodexLogLine
|
||||
{
|
||||
public static CodexLogLine? Parse(string line)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(line) ||
|
||||
line.Length < 34 ||
|
||||
line[3] != ' ' ||
|
||||
line[33] != ' ') return null;
|
||||
|
||||
line = line.Replace(Environment.NewLine, string.Empty);
|
||||
|
||||
var level = line.Substring(0, 3);
|
||||
var dtLine = line.Substring(4, 23);
|
||||
|
||||
var firstEqualSign = line.IndexOf('=');
|
||||
var msgStart = 34;
|
||||
var msgEnd = line.Substring(0, firstEqualSign).LastIndexOf(' ');
|
||||
var msg = line.Substring(msgStart, msgEnd - msgStart).Trim();
|
||||
var attrsLine = line.Substring(msgEnd);
|
||||
|
||||
var attrs = SplitAttrs(attrsLine);
|
||||
|
||||
var format = "yyyy-MM-dd HH:mm:ss.fff";
|
||||
var dt = DateTime.ParseExact(dtLine, format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
|
||||
|
||||
return new CodexLogLine()
|
||||
{
|
||||
LogLevel = level,
|
||||
TimestampUtc = dt,
|
||||
Message = msg,
|
||||
Attributes = attrs
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string LogLevel { get; set; } = string.Empty;
|
||||
public DateTime TimestampUtc { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public Dictionary<string, string> Attributes { get; private set; } = new Dictionary<string, string>();
|
||||
|
||||
/// <summary>
|
||||
/// After too much time spent cursing at regexes, here's what I got:
|
||||
/// Parses input string into 'key=value' pair, considerate of quoted (") values.
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> SplitAttrs(string input)
|
||||
{
|
||||
input += " ";
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
var key = string.Empty;
|
||||
var value = string.Empty;
|
||||
var mode = 1;
|
||||
var inQuote = false;
|
||||
|
||||
foreach (var c in input)
|
||||
{
|
||||
if (mode == 1)
|
||||
{
|
||||
if (c == '=') mode = 2;
|
||||
else if (c == ' ')
|
||||
{
|
||||
if (string.IsNullOrEmpty(key)) continue;
|
||||
else
|
||||
{
|
||||
result.Add(key, string.Empty);
|
||||
key = string.Empty;
|
||||
value = string.Empty;
|
||||
}
|
||||
}
|
||||
else key += c;
|
||||
}
|
||||
else if (mode == 2)
|
||||
{
|
||||
if (c == ' ' && !inQuote)
|
||||
{
|
||||
result.Add(key, value);
|
||||
key = string.Empty;
|
||||
value = string.Empty;
|
||||
mode = 1;
|
||||
}
|
||||
else if (c == '\"')
|
||||
{
|
||||
inQuote = !inQuote;
|
||||
}
|
||||
else
|
||||
{
|
||||
value += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,9 @@ namespace CodexPlugin
|
||||
transferSpeeds = new TransferSpeeds();
|
||||
}
|
||||
|
||||
public RunningContainer Container { get { return CodexAccess.Container; } }
|
||||
public RunningPod Pod { get { return CodexAccess.Container; } }
|
||||
|
||||
public RunningContainer Container { get { return Pod.Containers.Single(); } }
|
||||
public CodexAccess CodexAccess { get; }
|
||||
public CrashWatcher CrashWatcher { get => CodexAccess.CrashWatcher; }
|
||||
public CodexNodeGroup Group { get; }
|
||||
@@ -56,7 +58,7 @@ namespace CodexPlugin
|
||||
{
|
||||
get
|
||||
{
|
||||
return new MetricsScrapeTarget(CodexAccess.Container, CodexContainerRecipe.MetricsPortTag);
|
||||
return new MetricsScrapeTarget(CodexAccess.Container.Containers.First(), CodexContainerRecipe.MetricsPortTag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +73,7 @@ namespace CodexPlugin
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return CodexAccess.Container.Name;
|
||||
return Container.Name;
|
||||
}
|
||||
|
||||
public DebugInfo GetDebugInfo()
|
||||
@@ -142,11 +144,13 @@ namespace CodexPlugin
|
||||
|
||||
public void Stop(bool waitTillStopped)
|
||||
{
|
||||
if (Group.Count() > 1) throw new InvalidOperationException("Codex-nodes that are part of a group cannot be " +
|
||||
"individually shut down. Use 'BringOffline()' on the group object to stop the group. This method is only " +
|
||||
"available for codex-nodes in groups of 1.");
|
||||
|
||||
Group.BringOffline(waitTillStopped);
|
||||
CrashWatcher.Stop();
|
||||
Group.Stop(this, waitTillStopped);
|
||||
// if (Group.Count() > 1) throw new InvalidOperationException("Codex-nodes that are part of a group cannot be " +
|
||||
// "individually shut down. Use 'BringOffline()' on the group object to stop the group. This method is only " +
|
||||
// "available for codex-nodes in groups of 1.");
|
||||
//
|
||||
// Group.BringOffline(waitTillStopped);
|
||||
}
|
||||
|
||||
public void EnsureOnlineGetVersionResponse()
|
||||
@@ -171,7 +175,7 @@ namespace CodexPlugin
|
||||
// The peer we want to connect is in a different pod.
|
||||
// We must replace the default IP with the pod IP in the multiAddress.
|
||||
var workflow = tools.CreateWorkflow();
|
||||
var podInfo = workflow.GetPodInfo(peer.Container);
|
||||
var podInfo = workflow.GetPodInfo(peer.Pod);
|
||||
|
||||
return peerInfo.Addrs.Select(a => a
|
||||
.Replace("0.0.0.0", podInfo.Ip))
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace CodexPlugin
|
||||
|
||||
private EthAddress? GetEthAddress(CodexAccess access)
|
||||
{
|
||||
var ethAccount = access.Container.Recipe.Additionals.Get<EthAccount>();
|
||||
var ethAccount = access.Container.Containers.Single().Recipe.Additionals.Get<EthAccount>();
|
||||
if (ethAccount == null) return null;
|
||||
return ethAccount.EthAddress;
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ namespace CodexPlugin
|
||||
{
|
||||
private readonly CodexStarter starter;
|
||||
|
||||
public CodexNodeGroup(CodexStarter starter, IPluginTools tools, RunningContainers[] containers, ICodexNodeFactory codexNodeFactory)
|
||||
public CodexNodeGroup(CodexStarter starter, IPluginTools tools, RunningPod[] containers, ICodexNodeFactory codexNodeFactory)
|
||||
{
|
||||
this.starter = starter;
|
||||
Containers = containers;
|
||||
Nodes = containers.Containers().Select(c => CreateOnlineCodexNode(c, tools, codexNodeFactory)).ToArray();
|
||||
Nodes = containers.Select(c => CreateOnlineCodexNode(c, tools, codexNodeFactory)).ToArray();
|
||||
Version = new DebugInfoVersion();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,14 @@ namespace CodexPlugin
|
||||
Containers = null!;
|
||||
}
|
||||
|
||||
public RunningContainers[] Containers { get; private set; }
|
||||
public void Stop(CodexNode node, bool waitTillStopped)
|
||||
{
|
||||
starter.Stop(node.Pod, waitTillStopped);
|
||||
Nodes = Nodes.Where(n => n != node).ToArray();
|
||||
Containers = Containers.Where(c => c != node.Pod).ToArray();
|
||||
}
|
||||
|
||||
public RunningPod[] Containers { get; private set; }
|
||||
public CodexNode[] Nodes { get; private set; }
|
||||
public DebugInfoVersion Version { get; private set; }
|
||||
public IMetricsScrapeTarget[] ScrapeTargets => Nodes.Select(n => n.MetricsScrapeTarget).ToArray();
|
||||
@@ -74,9 +81,9 @@ namespace CodexPlugin
|
||||
Version = first;
|
||||
}
|
||||
|
||||
private CodexNode CreateOnlineCodexNode(RunningContainer c, IPluginTools tools, ICodexNodeFactory factory)
|
||||
private CodexNode CreateOnlineCodexNode(RunningPod c, IPluginTools tools, ICodexNodeFactory factory)
|
||||
{
|
||||
var watcher = factory.CreateCrashWatcher(c);
|
||||
var watcher = factory.CreateCrashWatcher(c.Containers.Single());
|
||||
var access = new CodexAccess(tools, c, watcher);
|
||||
return factory.CreateOnlineCodexNode(access, this);
|
||||
}
|
||||
|
||||
@@ -32,13 +32,13 @@ namespace CodexPlugin
|
||||
{
|
||||
}
|
||||
|
||||
public RunningContainers[] DeployCodexNodes(int numberOfNodes, Action<ICodexSetup> setup)
|
||||
public RunningPod[] DeployCodexNodes(int numberOfNodes, Action<ICodexSetup> setup)
|
||||
{
|
||||
var codexSetup = GetSetup(numberOfNodes, setup);
|
||||
return codexStarter.BringOnline(codexSetup);
|
||||
}
|
||||
|
||||
public ICodexNodeGroup WrapCodexContainers(CoreInterface coreInterface, RunningContainers[] containers)
|
||||
public ICodexNodeGroup WrapCodexContainers(CoreInterface coreInterface, RunningPod[] containers)
|
||||
{
|
||||
containers = containers.Select(c => SerializeGate.Gate(c)).ToArray();
|
||||
return codexStarter.WrapCodexContainers(coreInterface, containers);
|
||||
|
||||
@@ -52,6 +52,7 @@ namespace CodexPlugin
|
||||
public CodexLogLevel Libp2p { get; set; }
|
||||
public CodexLogLevel ContractClock { get; set; } = CodexLogLevel.Warn;
|
||||
public CodexLogLevel? BlockExchange { get; }
|
||||
public CodexLogLevel JsonSerialize { get; set; } = CodexLogLevel.Warn;
|
||||
}
|
||||
|
||||
public class CodexSetup : CodexStartupConfig, ICodexSetup
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace CodexPlugin
|
||||
apiChecker = new ApiChecker(pluginTools);
|
||||
}
|
||||
|
||||
public RunningContainers[] BringOnline(CodexSetup codexSetup)
|
||||
public RunningPod[] BringOnline(CodexSetup codexSetup)
|
||||
{
|
||||
LogSeparator();
|
||||
Log($"Starting {codexSetup.Describe()}...");
|
||||
@@ -34,14 +34,14 @@ namespace CodexPlugin
|
||||
{
|
||||
var podInfo = GetPodInfo(rc);
|
||||
var podInfos = string.Join(", ", rc.Containers.Select(c => $"Container: '{c.Name}' runs at '{podInfo.K8SNodeName}'={podInfo.Ip}"));
|
||||
Log($"Started {codexSetup.NumberOfNodes} nodes of image '{containers.Containers().First().Recipe.Image}'. ({podInfos})");
|
||||
Log($"Started {codexSetup.NumberOfNodes} nodes of image '{containers.First().Containers.First().Recipe.Image}'. ({podInfos})");
|
||||
}
|
||||
LogSeparator();
|
||||
|
||||
return containers;
|
||||
}
|
||||
|
||||
public ICodexNodeGroup WrapCodexContainers(CoreInterface coreInterface, RunningContainers[] containers)
|
||||
public ICodexNodeGroup WrapCodexContainers(CoreInterface coreInterface, RunningPod[] containers)
|
||||
{
|
||||
var codexNodeFactory = new CodexNodeFactory(pluginTools);
|
||||
|
||||
@@ -65,6 +65,14 @@ namespace CodexPlugin
|
||||
Log("Stopped.");
|
||||
}
|
||||
|
||||
public void Stop(RunningPod pod, bool waitTillStopped)
|
||||
{
|
||||
Log($"Stopping node...");
|
||||
var workflow = pluginTools.CreateWorkflow();
|
||||
workflow.Stop(pod, waitTillStopped);
|
||||
Log("Stopped.");
|
||||
}
|
||||
|
||||
public string GetCodexId()
|
||||
{
|
||||
if (versionResponse != null) return versionResponse.Version;
|
||||
@@ -85,7 +93,7 @@ namespace CodexPlugin
|
||||
return startupConfig;
|
||||
}
|
||||
|
||||
private RunningContainers[] StartCodexContainers(StartupConfig startupConfig, int numberOfNodes, ILocation location)
|
||||
private RunningPod[] StartCodexContainers(StartupConfig startupConfig, int numberOfNodes, ILocation location)
|
||||
{
|
||||
var futureContainers = new List<FutureContainers>();
|
||||
for (var i = 0; i < numberOfNodes; i++)
|
||||
@@ -99,13 +107,13 @@ namespace CodexPlugin
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private PodInfo GetPodInfo(RunningContainers rc)
|
||||
private PodInfo GetPodInfo(RunningPod rc)
|
||||
{
|
||||
var workflow = pluginTools.CreateWorkflow();
|
||||
return workflow.GetPodInfo(rc);
|
||||
}
|
||||
|
||||
private CodexNodeGroup CreateCodexGroup(CoreInterface coreInterface, RunningContainers[] runningContainers, CodexNodeFactory codexNodeFactory)
|
||||
private CodexNodeGroup CreateCodexGroup(CoreInterface coreInterface, RunningPod[] runningContainers, CodexNodeFactory codexNodeFactory)
|
||||
{
|
||||
var group = new CodexNodeGroup(this, pluginTools, runningContainers, codexNodeFactory);
|
||||
|
||||
@@ -122,10 +130,10 @@ namespace CodexPlugin
|
||||
return group;
|
||||
}
|
||||
|
||||
private void CodexNodesNotOnline(CoreInterface coreInterface, RunningContainers[] runningContainers)
|
||||
private void CodexNodesNotOnline(CoreInterface coreInterface, RunningPod[] runningContainers)
|
||||
{
|
||||
Log("Codex nodes failed to start");
|
||||
foreach (var container in runningContainers.Containers()) coreInterface.DownloadLog(container);
|
||||
foreach (var container in runningContainers.First().Containers) coreInterface.DownloadLog(container);
|
||||
}
|
||||
|
||||
private void LogSeparator()
|
||||
|
||||
@@ -73,11 +73,18 @@ namespace CodexPlugin
|
||||
"contracts",
|
||||
"clock"
|
||||
};
|
||||
var jsonSerializeTopics = new[]
|
||||
{
|
||||
"serde",
|
||||
"json",
|
||||
"serialization"
|
||||
};
|
||||
|
||||
level = $"{level};" +
|
||||
$"{CustomTopics.DiscV5.ToString()!.ToLowerInvariant()}:{string.Join(",", discV5Topics)};" +
|
||||
$"{CustomTopics.Libp2p.ToString()!.ToLowerInvariant()}:{string.Join(",", libp2pTopics)};" +
|
||||
$"{CustomTopics.ContractClock.ToString().ToLowerInvariant()}:{string.Join(",", contractClockTopics)}";
|
||||
$"{CustomTopics.ContractClock.ToString().ToLowerInvariant()}:{string.Join(",", contractClockTopics)};" +
|
||||
$"{CustomTopics.JsonSerialize.ToString().ToLowerInvariant()}:{string.Join(",", jsonSerializeTopics)}";
|
||||
|
||||
if (CustomTopics.BlockExchange != null)
|
||||
{
|
||||
|
||||
@@ -5,12 +5,12 @@ namespace CodexPlugin
|
||||
{
|
||||
public static class CoreInterfaceExtensions
|
||||
{
|
||||
public static RunningContainers[] DeployCodexNodes(this CoreInterface ci, int number, Action<ICodexSetup> setup)
|
||||
public static RunningPod[] DeployCodexNodes(this CoreInterface ci, int number, Action<ICodexSetup> setup)
|
||||
{
|
||||
return Plugin(ci).DeployCodexNodes(number, setup);
|
||||
}
|
||||
|
||||
public static ICodexNodeGroup WrapCodexContainers(this CoreInterface ci, RunningContainers[] containers)
|
||||
public static ICodexNodeGroup WrapCodexContainers(this CoreInterface ci, RunningPod[] containers)
|
||||
{
|
||||
return Plugin(ci).WrapCodexContainers(ci, containers);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace CodexPlugin
|
||||
ProofProbability = ToDecInt(purchase.ProofProbability),
|
||||
Reward = ToDecInt(purchase.PricePerSlotPerSecond),
|
||||
Collateral = ToDecInt(purchase.RequiredCollateral),
|
||||
Expiry = ToDecInt(DateTimeOffset.UtcNow.ToUnixTimeSeconds() + purchase.Expiry.TotalSeconds),
|
||||
Expiry = ToDecInt(purchase.Expiry.TotalSeconds),
|
||||
Nodes = Convert.ToInt32(purchase.MinRequiredNumberOfNodes),
|
||||
Tolerance = Convert.ToInt32(purchase.NodeFailureTolerance)
|
||||
};
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace CodexPlugin
|
||||
{
|
||||
private readonly ILog log;
|
||||
private readonly CodexAccess codexAccess;
|
||||
private readonly TimeSpan gracePeriod = TimeSpan.FromSeconds(10);
|
||||
private readonly TimeSpan gracePeriod = TimeSpan.FromSeconds(30);
|
||||
private DateTime? contractStartUtc;
|
||||
|
||||
public StoragePurchaseContract(ILog log, CodexAccess codexAccess, string purchaseId, StoragePurchaseRequest purchase)
|
||||
|
||||
@@ -213,8 +213,7 @@ components:
|
||||
description: Number as decimal string that represents how much collateral is asked from hosts that wants to fill a slots
|
||||
expiry:
|
||||
type: string
|
||||
description: Number as decimal string that represents expiry time of the request (in unix timestamp)
|
||||
|
||||
description: Number as decimal string that represents expiry threshold in seconds from when the Request is submitted. When the threshold is reached and the Request does not find requested amount of nodes to host the data, the Request is voided. The number of seconds can not be higher then the Request's duration itself.
|
||||
StorageAsk:
|
||||
type: object
|
||||
required:
|
||||
|
||||
@@ -7,9 +7,9 @@ namespace GethPlugin
|
||||
{
|
||||
public class GethDeployment : IHasContainer
|
||||
{
|
||||
public GethDeployment(RunningContainers containers, Port discoveryPort, Port httpPort, Port wsPort, GethAccount account, string pubKey)
|
||||
public GethDeployment(RunningPod pod, Port discoveryPort, Port httpPort, Port wsPort, GethAccount account, string pubKey)
|
||||
{
|
||||
Containers = containers;
|
||||
Pod = pod;
|
||||
DiscoveryPort = discoveryPort;
|
||||
HttpPort = httpPort;
|
||||
WsPort = wsPort;
|
||||
@@ -17,9 +17,9 @@ namespace GethPlugin
|
||||
PubKey = pubKey;
|
||||
}
|
||||
|
||||
public RunningContainers Containers { get; }
|
||||
public RunningPod Pod { get; }
|
||||
[JsonIgnore]
|
||||
public RunningContainer Container { get { return Containers.Containers.Single(); } }
|
||||
public RunningContainer Container { get { return Pod.Containers.Single(); } }
|
||||
public Port DiscoveryPort { get; }
|
||||
public Port HttpPort { get; }
|
||||
public Port WsPort { get; }
|
||||
|
||||
@@ -6,24 +6,24 @@ namespace MetricsPlugin
|
||||
{
|
||||
public static class CoreInterfaceExtensions
|
||||
{
|
||||
public static RunningContainers DeployMetricsCollector(this CoreInterface ci, params IHasMetricsScrapeTarget[] scrapeTargets)
|
||||
public static RunningPod DeployMetricsCollector(this CoreInterface ci, params IHasMetricsScrapeTarget[] scrapeTargets)
|
||||
{
|
||||
return Plugin(ci).DeployMetricsCollector(scrapeTargets.Select(t => t.MetricsScrapeTarget).ToArray());
|
||||
}
|
||||
|
||||
public static RunningContainers DeployMetricsCollector(this CoreInterface ci, params IMetricsScrapeTarget[] scrapeTargets)
|
||||
public static RunningPod DeployMetricsCollector(this CoreInterface ci, params IMetricsScrapeTarget[] scrapeTargets)
|
||||
{
|
||||
return Plugin(ci).DeployMetricsCollector(scrapeTargets);
|
||||
}
|
||||
|
||||
public static IMetricsAccess WrapMetricsCollector(this CoreInterface ci, RunningContainers metricsContainer, IHasMetricsScrapeTarget scrapeTarget)
|
||||
public static IMetricsAccess WrapMetricsCollector(this CoreInterface ci, RunningPod metricsPod, IHasMetricsScrapeTarget scrapeTarget)
|
||||
{
|
||||
return ci.WrapMetricsCollector(metricsContainer, scrapeTarget.MetricsScrapeTarget);
|
||||
return ci.WrapMetricsCollector(metricsPod, scrapeTarget.MetricsScrapeTarget);
|
||||
}
|
||||
|
||||
public static IMetricsAccess WrapMetricsCollector(this CoreInterface ci, RunningContainers metricsContainer, IMetricsScrapeTarget scrapeTarget)
|
||||
public static IMetricsAccess WrapMetricsCollector(this CoreInterface ci, RunningPod metricsPod, IMetricsScrapeTarget scrapeTarget)
|
||||
{
|
||||
return Plugin(ci).WrapMetricsCollectorDeployment(metricsContainer, scrapeTarget);
|
||||
return Plugin(ci).WrapMetricsCollectorDeployment(metricsPod, scrapeTarget);
|
||||
}
|
||||
|
||||
public static IMetricsAccess[] GetMetricsFor(this CoreInterface ci, params IHasManyMetricScrapeTargets[] manyScrapeTargets)
|
||||
|
||||
@@ -31,15 +31,15 @@ namespace MetricsPlugin
|
||||
{
|
||||
}
|
||||
|
||||
public RunningContainers DeployMetricsCollector(IMetricsScrapeTarget[] scrapeTargets)
|
||||
public RunningPod DeployMetricsCollector(IMetricsScrapeTarget[] scrapeTargets)
|
||||
{
|
||||
return starter.CollectMetricsFor(scrapeTargets);
|
||||
}
|
||||
|
||||
public IMetricsAccess WrapMetricsCollectorDeployment(RunningContainers runningContainer, IMetricsScrapeTarget target)
|
||||
public IMetricsAccess WrapMetricsCollectorDeployment(RunningPod runningPod, IMetricsScrapeTarget target)
|
||||
{
|
||||
runningContainer = SerializeGate.Gate(runningContainer);
|
||||
return starter.CreateAccessForTarget(runningContainer, target);
|
||||
runningPod = SerializeGate.Gate(runningPod);
|
||||
return starter.CreateAccessForTarget(runningPod, target);
|
||||
}
|
||||
|
||||
public LogFile? DownloadAllMetrics(IMetricsAccess metricsAccess, string targetName)
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace MetricsPlugin
|
||||
this.tools = tools;
|
||||
}
|
||||
|
||||
public RunningContainers CollectMetricsFor(IMetricsScrapeTarget[] targets)
|
||||
public RunningPod CollectMetricsFor(IMetricsScrapeTarget[] targets)
|
||||
{
|
||||
if (!targets.Any()) throw new ArgumentException(nameof(targets) + " must not be empty.");
|
||||
|
||||
@@ -32,9 +32,9 @@ namespace MetricsPlugin
|
||||
return runningContainers;
|
||||
}
|
||||
|
||||
public MetricsAccess CreateAccessForTarget(RunningContainers metricsContainer, IMetricsScrapeTarget target)
|
||||
public MetricsAccess CreateAccessForTarget(RunningPod metricsPod, IMetricsScrapeTarget target)
|
||||
{
|
||||
var metricsQuery = new MetricsQuery(tools, metricsContainer.Containers.Single());
|
||||
var metricsQuery = new MetricsQuery(tools, metricsPod.Containers.Single());
|
||||
return new MetricsAccess(metricsQuery, target);
|
||||
}
|
||||
|
||||
|
||||
@@ -49,8 +49,8 @@ namespace ContinuousTests
|
||||
var start = startUtc.ToString("o");
|
||||
var end = endUtc.ToString("o");
|
||||
|
||||
var containerName = container.RunningContainers.StartResult.Deployment.Name;
|
||||
var namespaceName = container.RunningContainers.StartResult.Cluster.Configuration.KubernetesNamespace;
|
||||
var containerName = container.RunningPod.StartResult.Deployment.Name;
|
||||
var namespaceName = container.RunningPod.StartResult.Cluster.Configuration.KubernetesNamespace;
|
||||
|
||||
//container_name : codex3-5 - deploymentName as stored in pod
|
||||
// pod_namespace : codex - continuous - nolimits - tests - 1
|
||||
|
||||
@@ -125,8 +125,8 @@ namespace ContinuousTests
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
var container = node.Container;
|
||||
var deploymentName = container.RunningContainers.StartResult.Deployment.Name;
|
||||
var namespaceName = container.RunningContainers.StartResult.Cluster.Configuration.KubernetesNamespace;
|
||||
var deploymentName = container.RunningPod.StartResult.Deployment.Name;
|
||||
var namespaceName = container.RunningPod.StartResult.Cluster.Configuration.KubernetesNamespace;
|
||||
var openingLine =
|
||||
$"{namespaceName} - {deploymentName} = {node.Container.Name} = {node.GetDebugInfo().Id}";
|
||||
elasticSearchLogDownloader.Download(fixtureLog.CreateSubfile(), node.Container, effectiveStart,
|
||||
@@ -295,13 +295,13 @@ namespace ContinuousTests
|
||||
return entryPoint.CreateInterface().WrapCodexContainers(containers).ToArray();
|
||||
}
|
||||
|
||||
private RunningContainers[] SelectRandomContainers()
|
||||
private RunningPod[] SelectRandomContainers()
|
||||
{
|
||||
var number = handle.Test.RequiredNumberOfNodes;
|
||||
var containers = config.CodexDeployment.CodexInstances.Select(i => i.Containers).ToList();
|
||||
var containers = config.CodexDeployment.CodexInstances.Select(i => i.Pod).ToList();
|
||||
if (number == -1) return containers.ToArray();
|
||||
|
||||
var result = new RunningContainers[number];
|
||||
var result = new RunningPod[number];
|
||||
for (var i = 0; i < number; i++)
|
||||
{
|
||||
result[i] = containers.PickOneRandom();
|
||||
|
||||
@@ -43,13 +43,13 @@ namespace ContinuousTests
|
||||
var workflow = entryPoint.Tools.CreateWorkflow();
|
||||
foreach (var instance in deployment.CodexInstances)
|
||||
{
|
||||
foreach (var container in instance.Containers.Containers)
|
||||
foreach (var container in instance.Pod.Containers)
|
||||
{
|
||||
var podInfo = workflow.GetPodInfo(container);
|
||||
log.Log($"Codex environment variables for '{container.Name}':");
|
||||
log.Log(
|
||||
$"Namespace: {container.RunningContainers.StartResult.Cluster.Configuration.KubernetesNamespace} - " +
|
||||
$"Pod name: {podInfo.Name} - Deployment name: {instance.Containers.StartResult.Deployment.Name}");
|
||||
$"Namespace: {container.RunningPod.StartResult.Cluster.Configuration.KubernetesNamespace} - " +
|
||||
$"Pod name: {podInfo.Name} - Deployment name: {instance.Pod.StartResult.Deployment.Name}");
|
||||
var codexVars = container.Recipe.EnvVars;
|
||||
foreach (var vars in codexVars) log.Log(vars.ToString());
|
||||
log.Log("");
|
||||
@@ -92,7 +92,7 @@ namespace ContinuousTests
|
||||
private void CheckCodexNodes(BaseLog log, Configuration config)
|
||||
{
|
||||
var nodes = entryPoint.CreateInterface()
|
||||
.WrapCodexContainers(config.CodexDeployment.CodexInstances.Select(i => i.Containers).ToArray());
|
||||
.WrapCodexContainers(config.CodexDeployment.CodexInstances.Select(i => i.Pod).ToArray());
|
||||
var pass = true;
|
||||
foreach (var n in nodes)
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using CodexPlugin;
|
||||
using CodexTests;
|
||||
using DistTestCore;
|
||||
using FileUtils;
|
||||
@@ -7,35 +8,45 @@ using Utils;
|
||||
namespace CodexLongTests.BasicTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DownloadTests : CodexDistTest
|
||||
public class DownloadTests : AutoBootstrapDistTest
|
||||
{
|
||||
[TestCase(3, 500)]
|
||||
[TestCase(5, 100)]
|
||||
[TestCase(10, 256)]
|
||||
[Test]
|
||||
[Combinatorial]
|
||||
[UseLongTimeouts]
|
||||
public void ParallelDownload(int numberOfNodes, int filesizeMb)
|
||||
public void ParallelDownload(
|
||||
[Values(1, 3, 5)] int numberOfFiles,
|
||||
[Values(10, 50, 100)] int filesizeMb)
|
||||
{
|
||||
var group = AddCodex(numberOfNodes);
|
||||
var host = AddCodex();
|
||||
var host = StartCodex();
|
||||
var client = StartCodex();
|
||||
|
||||
foreach (var node in group)
|
||||
var testfiles = new List<TrackedFile>();
|
||||
var contentIds = new List<ContentId>();
|
||||
var downloadedFiles = new List<TrackedFile?>();
|
||||
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
host.ConnectToPeer(node);
|
||||
testfiles.Add(GenerateTestFile(filesizeMb.MB()));
|
||||
contentIds.Add(new ContentId());
|
||||
downloadedFiles.Add(null);
|
||||
}
|
||||
|
||||
var testFile = GenerateTestFile(filesizeMb.MB());
|
||||
var contentId = host.UploadFile(testFile);
|
||||
var list = new List<Task<TrackedFile?>>();
|
||||
|
||||
foreach (var node in group)
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
list.Add(Task.Run(() => { return node.DownloadContent(contentId); }));
|
||||
contentIds[i] = host.UploadFile(testfiles[i]);
|
||||
}
|
||||
|
||||
Task.WaitAll(list.ToArray());
|
||||
foreach (var task in list)
|
||||
var downloadTasks = new List<Task>();
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
testFile.AssertIsEqual(task.Result);
|
||||
downloadTasks.Add(Task.Run(() => { downloadedFiles[i] = client.DownloadContent(contentIds[i]); }));
|
||||
}
|
||||
|
||||
Task.WaitAll(downloadTasks.ToArray());
|
||||
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
testfiles[i].AssertIsEqual(downloadedFiles[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,9 @@ namespace CodexLongTests.BasicTests
|
||||
|
||||
var expectedFile = GenerateTestFile(sizeMB);
|
||||
|
||||
var node = AddCodex(s => s.WithStorageQuota((size + 10).MB()));
|
||||
var node = StartCodex(s => s.WithStorageQuota((size + 10).MB()));
|
||||
|
||||
Time.SetRetryFailedCallback(i => OnFailed(i, node));
|
||||
|
||||
var uploadStart = DateTime.UtcNow;
|
||||
var cid = node.UploadFile(expectedFile);
|
||||
@@ -60,6 +62,17 @@ namespace CodexLongTests.BasicTests
|
||||
AssertTimeConstraint(uploadStart, downloadStart, downloadFinished, size);
|
||||
}
|
||||
|
||||
private void OnFailed(int tries, ICodexNode node)
|
||||
{
|
||||
if (tries < 5) return;
|
||||
|
||||
if (tries % 10 == 0)
|
||||
{
|
||||
Log($"After try {tries}, downloading node log.");
|
||||
Ci.DownloadLog(node);
|
||||
}
|
||||
}
|
||||
|
||||
private void AssertTimeConstraint(DateTime uploadStart, DateTime downloadStart, DateTime downloadFinished, long size)
|
||||
{
|
||||
float sizeInMB = size;
|
||||
|
||||
@@ -6,10 +6,12 @@ namespace CodexLongTests.BasicTests
|
||||
{
|
||||
public class TestInfraTests : CodexDistTest
|
||||
{
|
||||
[Test, UseLongTimeouts]
|
||||
[Test]
|
||||
[UseLongTimeouts]
|
||||
[Ignore("Not supported atm")]
|
||||
public void TestInfraShouldHave1000AddressSpacesPerPod()
|
||||
{
|
||||
var group = AddCodex(1000, s => s.EnableMetrics());
|
||||
var group = StartCodex(1000, s => s.EnableMetrics());
|
||||
|
||||
var nodeIds = group.Select(n => n.GetDebugInfo().Id).ToArray();
|
||||
|
||||
@@ -17,12 +19,14 @@ namespace CodexLongTests.BasicTests
|
||||
"Not all created nodes provided a unique id.");
|
||||
}
|
||||
|
||||
[Test, UseLongTimeouts]
|
||||
[Test]
|
||||
[UseLongTimeouts]
|
||||
[Ignore("Not supported atm")]
|
||||
public void TestInfraSupportsManyConcurrentPods()
|
||||
{
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
var n = AddCodex();
|
||||
var n = StartCodex();
|
||||
|
||||
Assert.That(!string.IsNullOrEmpty(n.GetDebugInfo().Id));
|
||||
}
|
||||
|
||||
@@ -8,41 +8,39 @@ using Utils;
|
||||
namespace CodexLongTests.BasicTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class UploadTests : CodexDistTest
|
||||
public class UploadTests : AutoBootstrapDistTest
|
||||
{
|
||||
[TestCase(3, 50)]
|
||||
[TestCase(5, 75)]
|
||||
[TestCase(10, 25)]
|
||||
[Test]
|
||||
[Combinatorial]
|
||||
[UseLongTimeouts]
|
||||
public void ParallelUpload(int numberOfNodes, int filesizeMb)
|
||||
public void ParallelUpload(
|
||||
[Values(1, 3, 5)] int numberOfFiles,
|
||||
[Values(10, 50, 100)] int filesizeMb)
|
||||
{
|
||||
var group = AddCodex(numberOfNodes);
|
||||
var host = AddCodex();
|
||||
|
||||
foreach (var node in group)
|
||||
{
|
||||
host.ConnectToPeer(node);
|
||||
}
|
||||
var host = StartCodex();
|
||||
var client = StartCodex();
|
||||
|
||||
var testfiles = new List<TrackedFile>();
|
||||
var contentIds = new List<Task<ContentId>>();
|
||||
var contentIds = new List<ContentId>();
|
||||
|
||||
for (int i = 0; i < group.Count(); i++)
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
testfiles.Add(GenerateTestFile(filesizeMb.MB()));
|
||||
var n = i;
|
||||
contentIds.Add(Task.Run(() => { return host.UploadFile(testfiles[n]); }));
|
||||
contentIds.Add(new ContentId());
|
||||
}
|
||||
var downloads = new List<Task<TrackedFile?>>();
|
||||
for (int i = 0; i < group.Count(); i++)
|
||||
|
||||
var uploadTasks = new List<Task>();
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
var n = i;
|
||||
downloads.Add(Task.Run(() => { return group[n].DownloadContent(contentIds[n].Result); }));
|
||||
uploadTasks.Add(Task.Run(() => { contentIds[i] = host.UploadFile(testfiles[i]); }));
|
||||
}
|
||||
Task.WaitAll(downloads.ToArray());
|
||||
for (int i = 0; i < group.Count(); i++)
|
||||
|
||||
Task.WaitAll(uploadTasks.ToArray());
|
||||
|
||||
for (int i = 0; i < numberOfFiles; i++)
|
||||
{
|
||||
testfiles[i].AssertIsEqual(downloads[i].Result);
|
||||
var downloaded = client.DownloadContent(contentIds[i]);
|
||||
testfiles[i].AssertIsEqual(downloaded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace CodexLongTests.DownloadConnectivityTests
|
||||
[Values(10, 15, 20)] int numberOfNodes,
|
||||
[Values(10, 100)] int sizeMBs)
|
||||
{
|
||||
for (var i = 0; i < numberOfNodes; i++) AddCodex();
|
||||
for (var i = 0; i < numberOfNodes; i++) StartCodex();
|
||||
|
||||
CreatePeerDownloadTestHelpers().AssertFullDownloadInterconnectivity(GetAllOnlineCodexNodes(), sizeMBs.MB());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
using DistTestCore;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.ScalabilityTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class MultiPeerDownloadTests : AutoBootstrapDistTest
|
||||
{
|
||||
[Test]
|
||||
[DontDownloadLogs]
|
||||
[UseLongTimeouts]
|
||||
[Combinatorial]
|
||||
public void MultiPeerDownload(
|
||||
[Values(5, 10, 20)] int numberOfHosts,
|
||||
[Values(100, 1000)] int fileSize
|
||||
)
|
||||
{
|
||||
var hosts = StartCodex(numberOfHosts, s => s.WithLogLevel(CodexPlugin.CodexLogLevel.Trace));
|
||||
var file = GenerateTestFile(fileSize.MB());
|
||||
var cid = hosts[0].UploadFile(file);
|
||||
var tailOfManifestCid = cid.Id.Substring(cid.Id.Length - 6);
|
||||
|
||||
var uploadLog = Ci.DownloadLog(hosts[0]);
|
||||
var expectedNumberOfBlocks = RoundUp(fileSize.MB().SizeInBytes, 64.KB().SizeInBytes) + 1; // +1 for manifest block.
|
||||
var blockCids = uploadLog
|
||||
.FindLinesThatContain("Putting block into network store")
|
||||
.Select(s =>
|
||||
{
|
||||
var start = s.IndexOf("cid=") + 4;
|
||||
var end = s.IndexOf(" count=");
|
||||
var len = end - start;
|
||||
return s.Substring(start, len);
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
Assert.That(blockCids.Length, Is.EqualTo(expectedNumberOfBlocks));
|
||||
|
||||
foreach (var h in hosts) h.DownloadContent(cid);
|
||||
|
||||
var client = StartCodex(s => s.WithLogLevel(CodexPlugin.CodexLogLevel.Trace));
|
||||
var resultFile = client.DownloadContent(cid);
|
||||
resultFile!.AssertIsEqual(file);
|
||||
|
||||
var downloadLog = Ci.DownloadLog(client);
|
||||
var host = string.Empty;
|
||||
var blockCidHostMap = new Dictionary<string, string>();
|
||||
downloadLog.IterateLines(line =>
|
||||
{
|
||||
if (line.Contains("peer=") && line.Contains(" len="))
|
||||
{
|
||||
var start = line.IndexOf("peer=") + 5;
|
||||
var end = line.IndexOf(" len=");
|
||||
var len = end - start;
|
||||
host = line.Substring(start, len);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(host) && line.Contains("Storing block with key"))
|
||||
{
|
||||
var start = line.IndexOf("cid=") + 4;
|
||||
var end = line.IndexOf(" count=");
|
||||
var len = end - start;
|
||||
var blockCid = line.Substring(start, len);
|
||||
|
||||
blockCidHostMap.Add(blockCid, host);
|
||||
host = string.Empty;
|
||||
}
|
||||
});
|
||||
|
||||
var totalFetched = blockCidHostMap.Count(p => !string.IsNullOrEmpty(p.Value));
|
||||
//PrintFullMap(blockCidHostMap);
|
||||
PrintOverview(blockCidHostMap);
|
||||
|
||||
Log("Expected number of blocks: " + expectedNumberOfBlocks);
|
||||
Log("Total number of block CIDs found in dataset + manifest block: " + blockCids.Length);
|
||||
Log("Total blocks fetched by hosts: " + totalFetched);
|
||||
Assert.That(totalFetched, Is.EqualTo(expectedNumberOfBlocks));
|
||||
}
|
||||
|
||||
private void PrintOverview(Dictionary<string, string> blockCidHostMap)
|
||||
{
|
||||
var overview = new Dictionary<string, int>();
|
||||
foreach (var pair in blockCidHostMap)
|
||||
{
|
||||
if (!overview.ContainsKey(pair.Value)) overview.Add(pair.Value, 1);
|
||||
else overview[pair.Value]++;
|
||||
}
|
||||
|
||||
Log("Blocks fetched per host:");
|
||||
foreach (var pair in overview)
|
||||
{
|
||||
Log($"Host: {pair.Key} = {pair.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
private void PrintFullMap(Dictionary<string, string> blockCidHostMap)
|
||||
{
|
||||
Log("Per block, host it was fetched from:");
|
||||
foreach (var pair in blockCidHostMap)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pair.Value))
|
||||
{
|
||||
Log($"block: {pair.Key} = Not seen");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"block: {pair.Key} = '{pair.Value}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private long RoundUp(long filesize, long blockSize)
|
||||
{
|
||||
double f = filesize;
|
||||
double b = blockSize;
|
||||
|
||||
var result = Math.Ceiling(f / b);
|
||||
return Convert.ToInt64(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using CodexPlugin;
|
||||
using DistTestCore;
|
||||
using FileUtils;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.ScalabilityTests;
|
||||
|
||||
[TestFixture]
|
||||
public class ScalabilityTests : CodexDistTest
|
||||
{
|
||||
/// <summary>
|
||||
/// We upload a file to node A, then download it with B.
|
||||
/// Then we stop node A, and download again with node C.
|
||||
/// </summary>
|
||||
[Test]
|
||||
[Combinatorial]
|
||||
[UseLongTimeouts]
|
||||
[DontDownloadLogs]
|
||||
public void ShouldMaintainFileInNetwork(
|
||||
[Values(10, 40, 80, 100)] int numberOfNodes,
|
||||
[Values(100, 1000, 5000, 10000)] int fileSizeInMb
|
||||
)
|
||||
{
|
||||
var logLevel = CodexLogLevel.Info;
|
||||
|
||||
var bootstrap = StartCodex(s => s.WithLogLevel(logLevel));
|
||||
var nodes = StartCodex(numberOfNodes - 1, s => s
|
||||
.WithBootstrapNode(bootstrap)
|
||||
.WithLogLevel(logLevel)
|
||||
.WithStorageQuota((fileSizeInMb + 50).MB())
|
||||
).ToList();
|
||||
|
||||
var uploader = nodes.PickOneRandom();
|
||||
var downloader = nodes.PickOneRandom();
|
||||
|
||||
var testFile = GenerateTestFile(fileSizeInMb.MB());
|
||||
var contentId = uploader.UploadFile(testFile);
|
||||
var downloadedFile = downloader.DownloadContent(contentId);
|
||||
|
||||
downloadedFile!.AssertIsEqual(testFile);
|
||||
|
||||
uploader.Stop(true);
|
||||
|
||||
var otherDownloader = nodes.PickOneRandom();
|
||||
downloadedFile = otherDownloader.DownloadContent(contentId);
|
||||
|
||||
downloadedFile!.AssertIsEqual(testFile);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// We upload a file to each node, to put a more wide-spread load on the network.
|
||||
/// Then we run the same test as ShouldMaintainFileInNetwork.
|
||||
/// </summary>
|
||||
[Ignore("Fix ShouldMaintainFileInNetwork for all values first")]
|
||||
[Test]
|
||||
[Combinatorial]
|
||||
[UseLongTimeouts]
|
||||
[DontDownloadLogs]
|
||||
public void EveryoneGetsAFile(
|
||||
[Values(10, 40, 80, 100)] int numberOfNodes,
|
||||
[Values(100, 1000, 5000, 10000)] int fileSizeInMb
|
||||
)
|
||||
{
|
||||
var logLevel = CodexLogLevel.Info;
|
||||
|
||||
var bootstrap = StartCodex(s => s.WithLogLevel(logLevel));
|
||||
var nodes = StartCodex(numberOfNodes - 1, s => s
|
||||
.WithBootstrapNode(bootstrap)
|
||||
.WithLogLevel(logLevel)
|
||||
.WithStorageQuota((fileSizeInMb + 50).MB())
|
||||
).ToList();
|
||||
|
||||
var pairTasks = nodes.Select(n =>
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var file = GenerateTestFile(fileSizeInMb.MB());
|
||||
var cid = n.UploadFile(file);
|
||||
return new NodeFilePair(n, file, cid);
|
||||
});
|
||||
});
|
||||
|
||||
var pairs = pairTasks.Select(t => Time.Wait(t)).ToList();
|
||||
|
||||
RunDoubleDownloadTest(
|
||||
pairs.PickOneRandom(),
|
||||
pairs.PickOneRandom(),
|
||||
pairs.PickOneRandom()
|
||||
);
|
||||
}
|
||||
|
||||
private void RunDoubleDownloadTest(NodeFilePair source, NodeFilePair dl1, NodeFilePair dl2)
|
||||
{
|
||||
var expectedFile = source.File;
|
||||
var cid = source.Cid;
|
||||
|
||||
var file1 = dl1.Node.DownloadContent(cid);
|
||||
file1!.AssertIsEqual(expectedFile);
|
||||
|
||||
source.Node.Stop(true);
|
||||
|
||||
var file2 = dl2.Node.DownloadContent(cid);
|
||||
file2!.AssertIsEqual(expectedFile);
|
||||
}
|
||||
|
||||
public class NodeFilePair
|
||||
{
|
||||
public NodeFilePair(ICodexNode node, TrackedFile file, ContentId cid)
|
||||
{
|
||||
Node = node;
|
||||
File = file;
|
||||
Cid = cid;
|
||||
}
|
||||
|
||||
public ICodexNode Node { get; }
|
||||
public TrackedFile File { get; }
|
||||
public ContentId Cid { get; }
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace CodexTests
|
||||
[SetUp]
|
||||
public void SetUpBootstrapNode()
|
||||
{
|
||||
BootstrapNode = AddCodex(s => s.WithName("BOOTSTRAP"));
|
||||
BootstrapNode = StartCodex(s => s.WithName("BOOTSTRAP"));
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
|
||||
@@ -1,298 +0,0 @@
|
||||
using CodexContractsPlugin;
|
||||
using CodexPlugin;
|
||||
using GethPlugin;
|
||||
using KubernetesWorkflow.Types;
|
||||
using Logging;
|
||||
using MetricsPlugin;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.BasicTests
|
||||
{
|
||||
[Ignore("Used for debugging continuous tests")]
|
||||
[TestFixture]
|
||||
public class ContinuousSubstitute : AutoBootstrapDistTest
|
||||
{
|
||||
[Test]
|
||||
public void ContinuousTestSubstitute()
|
||||
{
|
||||
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("geth"));
|
||||
var contract = Ci.StartCodexContracts(geth);
|
||||
|
||||
var group = AddCodex(5, o => o
|
||||
.EnableMetrics()
|
||||
.EnableMarketplace(geth, contract, s => s
|
||||
.WithInitial(10.Eth(), 100000.TestTokens())
|
||||
.AsStorageNode()
|
||||
.AsValidator())
|
||||
.WithBlockTTL(TimeSpan.FromMinutes(5))
|
||||
.WithBlockMaintenanceInterval(TimeSpan.FromSeconds(10))
|
||||
.WithBlockMaintenanceNumber(100)
|
||||
.WithStorageQuota(1.GB()));
|
||||
|
||||
var nodes = group.Cast<CodexNode>().ToArray();
|
||||
|
||||
var rc = Ci.DeployMetricsCollector(nodes);
|
||||
|
||||
var availability = new StorageAvailability(
|
||||
totalSpace: 500.MB(),
|
||||
maxDuration: TimeSpan.FromMinutes(5),
|
||||
minPriceForTotalSpace: 500.TestTokens(),
|
||||
maxCollateral: 1024.TestTokens()
|
||||
);
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
node.Marketplace.MakeStorageAvailable(availability);
|
||||
}
|
||||
|
||||
var endTime = DateTime.UtcNow + TimeSpan.FromHours(10);
|
||||
while (DateTime.UtcNow < endTime)
|
||||
{
|
||||
var allNodes = nodes.ToList();
|
||||
var primary = allNodes.PickOneRandom();
|
||||
var secondary = allNodes.PickOneRandom();
|
||||
|
||||
Log("Run Test");
|
||||
PerformTest(primary, secondary, rc);
|
||||
|
||||
Thread.Sleep(TimeSpan.FromSeconds(5));
|
||||
}
|
||||
}
|
||||
|
||||
private void LogBytesPerMillisecond(Action action)
|
||||
{
|
||||
var sw = Stopwatch.Begin(GetTestLog());
|
||||
action();
|
||||
var duration = sw.End();
|
||||
double totalMs = duration.TotalMilliseconds;
|
||||
double totalBytes = fileSize.SizeInBytes;
|
||||
|
||||
var bytesPerMs = totalBytes / totalMs;
|
||||
Log($"Bytes per millisecond: {bytesPerMs}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PeerTest()
|
||||
{
|
||||
var group = AddCodex(5, o => o
|
||||
//.EnableMetrics()
|
||||
//.EnableMarketplace(100000.TestTokens(), 0.Eth(), isValidator: true)
|
||||
.WithBlockTTL(TimeSpan.FromMinutes(2))
|
||||
.WithBlockMaintenanceInterval(TimeSpan.FromMinutes(2))
|
||||
.WithBlockMaintenanceNumber(10000)
|
||||
.WithBlockTTL(TimeSpan.FromMinutes(2))
|
||||
.WithStorageQuota(1.GB()));
|
||||
|
||||
var nodes = group.Cast<CodexNode>().ToArray();
|
||||
|
||||
var checkTime = DateTime.UtcNow + TimeSpan.FromMinutes(1);
|
||||
var endTime = DateTime.UtcNow + TimeSpan.FromHours(10);
|
||||
while (DateTime.UtcNow < endTime)
|
||||
{
|
||||
//CreatePeerConnectionTestHelpers().AssertFullyConnected(GetAllOnlineCodexNodes());
|
||||
//CheckRoutingTables(GetAllOnlineCodexNodes());
|
||||
|
||||
var node = nodes.ToList().PickOneRandom();
|
||||
var file = GenerateTestFile(50.MB());
|
||||
node.UploadFile(file);
|
||||
|
||||
Thread.Sleep(20000);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckRoutingTables(IEnumerable<ICodexNode> nodes)
|
||||
{
|
||||
var all = nodes.ToArray();
|
||||
var allIds = all.Select(n => n.GetDebugInfo().Table.LocalNode.NodeId).ToArray();
|
||||
|
||||
var errors = all.Select(n => AreAllPresent(n, allIds)).Where(s => !string.IsNullOrEmpty(s)).ToArray();
|
||||
|
||||
if (errors.Any())
|
||||
{
|
||||
Assert.Fail(string.Join(Environment.NewLine, errors));
|
||||
}
|
||||
}
|
||||
|
||||
private string AreAllPresent(ICodexNode n, string[] allIds)
|
||||
{
|
||||
var info = n.GetDebugInfo();
|
||||
var known = info.Table.Nodes.Select(n => n.NodeId).ToArray();
|
||||
var expected = allIds.Where(i => i != info.Table.LocalNode.NodeId).ToArray();
|
||||
|
||||
if (!expected.All(ex => known.Contains(ex)))
|
||||
{
|
||||
return $"Not all of '{string.Join(",", expected)}' were present in routing table: '{string.Join(",", known)}'";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private ByteSize fileSize = 80.MB();
|
||||
|
||||
private const string BytesStoredMetric = "codexRepostoreBytesUsed";
|
||||
|
||||
private void PerformTest(ICodexNode primary, ICodexNode secondary, RunningContainers rc)
|
||||
{
|
||||
ScopedTestFiles(() =>
|
||||
{
|
||||
var testFile = GenerateTestFile(fileSize);
|
||||
|
||||
var metrics = Ci.WrapMetricsCollector(rc, primary);
|
||||
var beforeBytesStored = metrics.GetMetric(BytesStoredMetric);
|
||||
|
||||
ContentId contentId = null!;
|
||||
LogBytesPerMillisecond(() => contentId = primary.UploadFile(testFile));
|
||||
|
||||
var low = fileSize.SizeInBytes;
|
||||
var high = low * 1.2;
|
||||
Log("looking for: " + low + " < " + high);
|
||||
|
||||
Time.WaitUntil(() =>
|
||||
{
|
||||
var afterBytesStored = metrics.GetMetric(BytesStoredMetric);
|
||||
var newBytes = Convert.ToInt64(afterBytesStored.Values.Last().Value - beforeBytesStored.Values.Last().Value);
|
||||
|
||||
return high > newBytes && newBytes > low;
|
||||
}, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(2));
|
||||
|
||||
FileUtils.TrackedFile? downloadedFile = null;
|
||||
LogBytesPerMillisecond(() => downloadedFile = secondary.DownloadContent(contentId));
|
||||
|
||||
testFile.AssertIsEqual(downloadedFile);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HoldMyBeerTest()
|
||||
{
|
||||
var blockExpirationTime = TimeSpan.FromMinutes(3);
|
||||
var group = AddCodex(3, o => o
|
||||
.EnableMetrics()
|
||||
.WithBlockTTL(blockExpirationTime)
|
||||
.WithBlockMaintenanceInterval(TimeSpan.FromMinutes(2))
|
||||
.WithBlockMaintenanceNumber(10000)
|
||||
.WithStorageQuota(2000.MB()));
|
||||
|
||||
var nodes = group.Cast<CodexNode>().ToArray();
|
||||
|
||||
var endTime = DateTime.UtcNow + TimeSpan.FromHours(24);
|
||||
|
||||
var filesize = 80.MB();
|
||||
double codexDefaultBlockSize = 31 * 64 * 33;
|
||||
var numberOfBlocks = Convert.ToInt64(Math.Ceiling(filesize.SizeInBytes / codexDefaultBlockSize));
|
||||
var sizeInBytes = filesize.SizeInBytes;
|
||||
Assert.That(numberOfBlocks, Is.EqualTo(1282));
|
||||
|
||||
var startTime = DateTime.UtcNow;
|
||||
var successfulUploads = 0;
|
||||
var successfulDownloads = 0;
|
||||
|
||||
while (DateTime.UtcNow < endTime)
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.Sleep(TimeSpan.FromSeconds(5));
|
||||
|
||||
ScopedTestFiles(() =>
|
||||
{
|
||||
var uploadStartTime = DateTime.UtcNow;
|
||||
var file = GenerateTestFile(filesize);
|
||||
var cid = node.UploadFile(file);
|
||||
|
||||
var cidTag = cid.Id.Substring(cid.Id.Length - 6);
|
||||
Measure("upload-log-asserts", () =>
|
||||
{
|
||||
var uploadLog = Ci.DownloadLog(node, tailLines: 50000);
|
||||
|
||||
var storeLines = uploadLog.FindLinesThatContain("Stored data", "topics=\"codex node\"");
|
||||
uploadLog.DeleteFile();
|
||||
|
||||
var storeLine = GetLineForCidTag(storeLines, cidTag);
|
||||
AssertStoreLineContains(storeLine, numberOfBlocks, sizeInBytes);
|
||||
});
|
||||
successfulUploads++;
|
||||
|
||||
var uploadTimeTaken = DateTime.UtcNow - uploadStartTime;
|
||||
if (uploadTimeTaken >= blockExpirationTime.Subtract(TimeSpan.FromSeconds(10)))
|
||||
{
|
||||
Assert.Fail("Upload took too long. Blocks already expired.");
|
||||
}
|
||||
|
||||
var dl = node.DownloadContent(cid);
|
||||
file.AssertIsEqual(dl);
|
||||
|
||||
Measure("download-log-asserts", () =>
|
||||
{
|
||||
var downloadLog = Ci.DownloadLog(node, tailLines: 50000);
|
||||
|
||||
var sentLines = downloadLog.FindLinesThatContain("Sent bytes", "topics=\"codex restapi\"");
|
||||
downloadLog.DeleteFile();
|
||||
|
||||
var sentLine = GetLineForCidTag(sentLines, cidTag);
|
||||
AssertSentLineContains(sentLine, sizeInBytes);
|
||||
});
|
||||
successfulDownloads++;
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
var testDuration = DateTime.UtcNow - startTime;
|
||||
Log("Test failed. Delaying shut-down by 30 seconds to collect metrics.");
|
||||
Log($"Test failed after {Time.FormatDuration(testDuration)} and {successfulUploads} successful uploads and {successfulDownloads} successful downloads");
|
||||
Thread.Sleep(TimeSpan.FromSeconds(30));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(TimeSpan.FromSeconds(5));
|
||||
}
|
||||
}
|
||||
|
||||
private void AssertSentLineContains(string sentLine, long sizeInBytes)
|
||||
{
|
||||
var tag = "bytes=";
|
||||
var token = sentLine.Substring(sentLine.IndexOf(tag) + tag.Length);
|
||||
var bytes = Convert.ToInt64(token);
|
||||
Assert.AreEqual(sizeInBytes, bytes, $"Sent bytes: Number of bytes incorrect. Line: '{sentLine}'");
|
||||
}
|
||||
|
||||
private void AssertStoreLineContains(string storeLine, long numberOfBlocks, long sizeInBytes)
|
||||
{
|
||||
var tokens = storeLine.Split(" ");
|
||||
|
||||
var blocksToken = GetToken(tokens, "blocks=");
|
||||
var sizeToken = GetToken(tokens, "size=");
|
||||
if (blocksToken == null) Assert.Fail("blockToken not found in " + storeLine);
|
||||
if (sizeToken == null) Assert.Fail("sizeToken not found in " + storeLine);
|
||||
|
||||
var blocks = Convert.ToInt64(blocksToken);
|
||||
var size = Convert.ToInt64(sizeToken?.Replace("'NByte", ""));
|
||||
|
||||
var lineLog = $" Line: '{storeLine}'";
|
||||
Assert.AreEqual(numberOfBlocks, blocks, "Stored data: Number of blocks incorrect." + lineLog);
|
||||
Assert.AreEqual(sizeInBytes, size, "Stored data: Number of blocks incorrect." + lineLog);
|
||||
}
|
||||
|
||||
private string GetLineForCidTag(string[] lines, string cidTag)
|
||||
{
|
||||
var result = lines.SingleOrDefault(l => l.Contains(cidTag));
|
||||
if (result == null)
|
||||
{
|
||||
Assert.Fail($"Failed to find '{cidTag}' in lines: '{string.Join(",", lines)}'");
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private string? GetToken(string[] tokens, string tag)
|
||||
{
|
||||
var token = tokens.SingleOrDefault(t => t.StartsWith(tag));
|
||||
if (token == null) return null;
|
||||
return token.Substring(tag.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace CodexTests.BasicTests
|
||||
[Test]
|
||||
public void CodexLogExample()
|
||||
{
|
||||
var primary = AddCodex(s => s.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Warn, CodexLogLevel.Warn)));
|
||||
var primary = StartCodex(s => s.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Warn, CodexLogLevel.Warn)));
|
||||
|
||||
var cid = primary.UploadFile(GenerateTestFile(5.MB()));
|
||||
|
||||
@@ -28,8 +28,8 @@ namespace CodexTests.BasicTests
|
||||
[Test]
|
||||
public void TwoMetricsExample()
|
||||
{
|
||||
var group = AddCodex(2, s => s.EnableMetrics());
|
||||
var group2 = AddCodex(2, s => s.EnableMetrics());
|
||||
var group = StartCodex(2, s => s.EnableMetrics());
|
||||
var group2 = StartCodex(2, s => s.EnableMetrics());
|
||||
|
||||
var primary = group[0];
|
||||
var secondary = group[1];
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace CodexTests.BasicTests
|
||||
var contracts = Ci.StartCodexContracts(geth);
|
||||
|
||||
var numberOfHosts = 5;
|
||||
var hosts = AddCodex(numberOfHosts, s => s
|
||||
var hosts = StartCodex(numberOfHosts, s => s
|
||||
.WithName("Host")
|
||||
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn)
|
||||
{
|
||||
@@ -49,7 +49,7 @@ namespace CodexTests.BasicTests
|
||||
|
||||
var testFile = GenerateTestFile(fileSize);
|
||||
|
||||
var client = AddCodex(s => s
|
||||
var client = StartCodex(s => s
|
||||
.WithName("Client")
|
||||
.EnableMarketplace(geth, contracts, m => m
|
||||
.WithInitial(10.Eth(), clientInitialBalance)));
|
||||
@@ -95,7 +95,7 @@ namespace CodexTests.BasicTests
|
||||
Log($"SlotFilledEvents: {slotFilledEvents.Length} - NumSlots: {purchase.MinRequiredNumberOfNodes}");
|
||||
|
||||
if (slotFilledEvents.Length != purchase.MinRequiredNumberOfNodes) throw new Exception();
|
||||
}, Convert.ToInt32(purchase.Duration.TotalSeconds / 5) + 10, TimeSpan.FromSeconds(5), "Checking SlotFilled events");
|
||||
}, purchase.Expiry + TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(5), "Checking SlotFilled events");
|
||||
}
|
||||
|
||||
private void AssertStorageRequest(Request request, StoragePurchaseRequest purchase, ICodexContracts contracts, ICodexNode buyer)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using CodexPlugin;
|
||||
using DistTestCore;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
@@ -11,7 +10,7 @@ namespace CodexTests.BasicTests
|
||||
[Test]
|
||||
public void OneClientTest()
|
||||
{
|
||||
var primary = Ci.StartCodexNode();
|
||||
var primary = StartCodex();
|
||||
|
||||
PerformOneClientTest(primary);
|
||||
}
|
||||
@@ -19,11 +18,11 @@ namespace CodexTests.BasicTests
|
||||
[Test]
|
||||
public void RestartTest()
|
||||
{
|
||||
var primary = Ci.StartCodexNode();
|
||||
var primary = StartCodex();
|
||||
|
||||
primary.Stop(waitTillStopped: true);
|
||||
|
||||
primary = Ci.StartCodexNode();
|
||||
primary = StartCodex();
|
||||
|
||||
PerformOneClientTest(primary);
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ namespace CodexTests.BasicTests
|
||||
[Test]
|
||||
public void ThreeClient()
|
||||
{
|
||||
var primary = AddCodex();
|
||||
var secondary = AddCodex();
|
||||
var primary = StartCodex();
|
||||
var secondary = StartCodex();
|
||||
|
||||
var testFile = GenerateTestFile(10.MB());
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ namespace CodexTests.BasicTests
|
||||
[Test]
|
||||
public void TwoClientTest()
|
||||
{
|
||||
var uploader = AddCodex(s => s.WithName("Uploader"));
|
||||
var downloader = AddCodex(s => s.WithName("Downloader").WithBootstrapNode(uploader));
|
||||
var uploader = StartCodex(s => s.WithName("Uploader"));
|
||||
var downloader = StartCodex(s => s.WithName("Downloader").WithBootstrapNode(uploader));
|
||||
|
||||
PerformTwoClientTest(uploader, downloader);
|
||||
}
|
||||
|
||||
@@ -39,22 +39,22 @@ namespace CodexTests
|
||||
onlineCodexNodes.Remove(lifecycle);
|
||||
}
|
||||
|
||||
public ICodexNode AddCodex()
|
||||
public ICodexNode StartCodex()
|
||||
{
|
||||
return AddCodex(s => { });
|
||||
return StartCodex(s => { });
|
||||
}
|
||||
|
||||
public ICodexNode AddCodex(Action<ICodexSetup> setup)
|
||||
public ICodexNode StartCodex(Action<ICodexSetup> setup)
|
||||
{
|
||||
return AddCodex(1, setup)[0];
|
||||
return StartCodex(1, setup)[0];
|
||||
}
|
||||
|
||||
public ICodexNodeGroup AddCodex(int numberOfNodes)
|
||||
public ICodexNodeGroup StartCodex(int numberOfNodes)
|
||||
{
|
||||
return AddCodex(numberOfNodes, s => { });
|
||||
return StartCodex(numberOfNodes, s => { });
|
||||
}
|
||||
|
||||
public ICodexNodeGroup AddCodex(int numberOfNodes, Action<ICodexSetup> setup)
|
||||
public ICodexNodeGroup StartCodex(int numberOfNodes, Action<ICodexSetup> setup)
|
||||
{
|
||||
var group = Ci.StartCodexNodes(numberOfNodes, s =>
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace CodexTests.DownloadConnectivityTests
|
||||
[Test]
|
||||
public void MetricsDoesNotInterfereWithPeerDownload()
|
||||
{
|
||||
AddCodex(2, s => s.EnableMetrics());
|
||||
StartCodex(2, s => s.EnableMetrics());
|
||||
|
||||
AssertAllNodesConnected();
|
||||
}
|
||||
@@ -21,7 +21,7 @@ namespace CodexTests.DownloadConnectivityTests
|
||||
{
|
||||
var geth = Ci.StartGethNode(s => s.IsMiner());
|
||||
var contracts = Ci.StartCodexContracts(geth);
|
||||
AddCodex(2, s => s.EnableMarketplace(geth, contracts, m => m
|
||||
StartCodex(2, s => s.EnableMarketplace(geth, contracts, m => m
|
||||
.WithInitial(10.Eth(), 1000.TestTokens())));
|
||||
|
||||
AssertAllNodesConnected();
|
||||
@@ -33,7 +33,7 @@ namespace CodexTests.DownloadConnectivityTests
|
||||
[Values(2, 5)] int numberOfNodes,
|
||||
[Values(1, 10)] int sizeMBs)
|
||||
{
|
||||
AddCodex(numberOfNodes);
|
||||
StartCodex(numberOfNodes);
|
||||
|
||||
AssertAllNodesConnected(sizeMBs);
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
[Test]
|
||||
public void TwoLayersTest()
|
||||
{
|
||||
var root = AddCodex();
|
||||
var l1Source = AddCodex(s => s.WithBootstrapNode(root));
|
||||
var l1Node = AddCodex(s => s.WithBootstrapNode(root));
|
||||
var l2Target = AddCodex(s => s.WithBootstrapNode(l1Node));
|
||||
var root = StartCodex();
|
||||
var l1Source = StartCodex(s => s.WithBootstrapNode(root));
|
||||
var l1Node = StartCodex(s => s.WithBootstrapNode(root));
|
||||
var l2Target = StartCodex(s => s.WithBootstrapNode(l1Node));
|
||||
|
||||
AssertAllNodesConnected();
|
||||
}
|
||||
@@ -19,11 +19,11 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
[Test]
|
||||
public void ThreeLayersTest()
|
||||
{
|
||||
var root = AddCodex();
|
||||
var l1Source = AddCodex(s => s.WithBootstrapNode(root));
|
||||
var l1Node = AddCodex(s => s.WithBootstrapNode(root));
|
||||
var l2Node = AddCodex(s => s.WithBootstrapNode(l1Node));
|
||||
var l3Target = AddCodex(s => s.WithBootstrapNode(l2Node));
|
||||
var root = StartCodex();
|
||||
var l1Source = StartCodex(s => s.WithBootstrapNode(root));
|
||||
var l1Node = StartCodex(s => s.WithBootstrapNode(root));
|
||||
var l2Node = StartCodex(s => s.WithBootstrapNode(l1Node));
|
||||
var l3Target = StartCodex(s => s.WithBootstrapNode(l2Node));
|
||||
|
||||
AssertAllNodesConnected();
|
||||
}
|
||||
@@ -33,10 +33,10 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
[TestCase(10)]
|
||||
public void NodeChainTest(int chainLength)
|
||||
{
|
||||
var node = AddCodex();
|
||||
var node = StartCodex();
|
||||
for (var i = 1; i < chainLength; i++)
|
||||
{
|
||||
node = AddCodex(s => s.WithBootstrapNode(node));
|
||||
node = StartCodex(s => s.WithBootstrapNode(node));
|
||||
}
|
||||
|
||||
AssertAllNodesConnected();
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
public void CanReportUnknownPeerId()
|
||||
{
|
||||
var unknownId = "16Uiu2HAkv2CHWpff3dj5iuVNERAp8AGKGNgpGjPexJZHSqUstfsK";
|
||||
var node = AddCodex();
|
||||
var node = StartCodex();
|
||||
|
||||
var result = node.GetDebugPeer(unknownId);
|
||||
Assert.That(result.IsPeerFound, Is.False);
|
||||
@@ -21,7 +21,7 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
[Test]
|
||||
public void MetricsDoesNotInterfereWithPeerDiscovery()
|
||||
{
|
||||
AddCodex(2, s => s.EnableMetrics());
|
||||
StartCodex(2, s => s.EnableMetrics());
|
||||
|
||||
AssertAllNodesConnected();
|
||||
}
|
||||
@@ -31,7 +31,7 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
{
|
||||
var geth = Ci.StartGethNode(s => s.IsMiner());
|
||||
var contracts = Ci.StartCodexContracts(geth);
|
||||
AddCodex(2, s => s.EnableMarketplace(geth, contracts, m => m
|
||||
StartCodex(2, s => s.EnableMarketplace(geth, contracts, m => m
|
||||
.WithInitial(10.Eth(), 1000.TestTokens())));
|
||||
|
||||
AssertAllNodesConnected();
|
||||
@@ -42,7 +42,7 @@ namespace CodexTests.PeerDiscoveryTests
|
||||
[TestCase(10)]
|
||||
public void VariableNodes(int number)
|
||||
{
|
||||
AddCodex(number);
|
||||
StartCodex(number);
|
||||
|
||||
AssertAllNodesConnected();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
using DistTestCore;
|
||||
using Logging;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.ScalabilityTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class ClusterDiscSpeedTests : DistTest
|
||||
{
|
||||
private readonly Random random = new Random();
|
||||
|
||||
[Test]
|
||||
[Combinatorial]
|
||||
[Ignore("Used to measure disc io speeds in cluster.")]
|
||||
public void DiscSpeedTest(
|
||||
[Values(1, 10, 100, 1024, 1024 * 10, 1024 * 100, 1024 * 1024)] int bufferSizeKb
|
||||
)
|
||||
{
|
||||
long targetSize = (long)(1024 * 1024 * 1024) * 2;
|
||||
long bufferSizeBytes = ((long)bufferSizeKb) * 1024;
|
||||
|
||||
var filename = nameof(DiscSpeedTest);
|
||||
|
||||
Thread.Sleep(2000);
|
||||
if (File.Exists(filename)) File.Delete(filename);
|
||||
Thread.Sleep(2000);
|
||||
var writeSpeed = PerformWrite(targetSize, bufferSizeBytes, filename);
|
||||
Thread.Sleep(2000);
|
||||
var readSpeed = PerformRead(targetSize, bufferSizeBytes, filename);
|
||||
|
||||
Log($"Write speed: {writeSpeed} per second.");
|
||||
Log($"Read speed: {readSpeed} per second.");
|
||||
}
|
||||
|
||||
private ByteSize PerformWrite(long targetSize, long bufferSizeBytes, string filename)
|
||||
{
|
||||
long bytesWritten = 0;
|
||||
var buffer = new byte[bufferSizeBytes];
|
||||
random.NextBytes(buffer);
|
||||
|
||||
var sw = Stopwatch.Begin(GetTestLog());
|
||||
using (var stream = File.OpenWrite(filename))
|
||||
{
|
||||
while (bytesWritten < targetSize)
|
||||
{
|
||||
long remaining = targetSize - bytesWritten;
|
||||
long toWrite = Math.Min(bufferSizeBytes, remaining);
|
||||
|
||||
stream.Write(buffer, 0, Convert.ToInt32(toWrite));
|
||||
bytesWritten += toWrite;
|
||||
}
|
||||
}
|
||||
var duration = sw.End("WriteTime");
|
||||
double totalSeconds = duration.TotalSeconds;
|
||||
double totalBytes = bytesWritten;
|
||||
double bytesPerSecond = totalBytes / totalSeconds;
|
||||
return new ByteSize(Convert.ToInt64(bytesPerSecond));
|
||||
}
|
||||
|
||||
private ByteSize PerformRead(long targetSize, long bufferSizeBytes, string filename)
|
||||
{
|
||||
long bytesRead = 0;
|
||||
var buffer = new byte[bufferSizeBytes];
|
||||
var sw = Stopwatch.Begin(GetTestLog());
|
||||
using (var stream = File.OpenRead(filename))
|
||||
{
|
||||
while (bytesRead < targetSize)
|
||||
{
|
||||
long remaining = targetSize - bytesRead;
|
||||
long toRead = Math.Min(bufferSizeBytes, remaining);
|
||||
|
||||
var r = stream.Read(buffer, 0, Convert.ToInt32(toRead));
|
||||
bytesRead += r;
|
||||
}
|
||||
}
|
||||
var duration = sw.End("ReadTime");
|
||||
double totalSeconds = duration.TotalSeconds;
|
||||
double totalBytes = bytesRead;
|
||||
double bytesPerSecond = totalBytes / totalSeconds;
|
||||
return new ByteSize(Convert.ToInt64(bytesPerSecond));
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -5,12 +5,13 @@ using GethPlugin;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.BasicTests
|
||||
namespace CodexTests.UtilityTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DiscordBotTests : AutoBootstrapDistTest
|
||||
{
|
||||
[Test]
|
||||
[Ignore("Used for debugging bots")]
|
||||
public void BotRewardTest()
|
||||
{
|
||||
var myAccount = EthAccount.GenerateNew();
|
||||
@@ -57,7 +58,7 @@ namespace CodexTests.BasicTests
|
||||
|
||||
for (var i = 0; i < numberOfHosts; i++)
|
||||
{
|
||||
var seller = AddCodex(s => s
|
||||
var seller = StartCodex(s => s
|
||||
.WithName("Seller")
|
||||
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn)
|
||||
{
|
||||
@@ -81,7 +82,7 @@ namespace CodexTests.BasicTests
|
||||
|
||||
var testFile = GenerateTestFile(fileSize);
|
||||
|
||||
var buyer = AddCodex(s => s
|
||||
var buyer = StartCodex(s => s
|
||||
.WithName("Buyer")
|
||||
.EnableMarketplace(geth, contracts, m => m
|
||||
.WithAccount(myAccount)
|
||||
@@ -0,0 +1,56 @@
|
||||
using CodexPlugin;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.UtilityTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class LogHelperTests : AutoBootstrapDistTest
|
||||
{
|
||||
[Test]
|
||||
[Ignore("Used to find the most common log messages.")]
|
||||
public void FindMostCommonLogMessages()
|
||||
{
|
||||
var uploader = StartCodex(s => s.WithName("uploader").WithLogLevel(CodexLogLevel.Trace));
|
||||
var downloader = StartCodex(s => s.WithName("downloader").WithLogLevel(CodexLogLevel.Trace));
|
||||
|
||||
var cid = uploader.UploadFile(GenerateTestFile(100.MB()));
|
||||
|
||||
Thread.Sleep(1000);
|
||||
var logStartUtc = DateTime.UtcNow;
|
||||
Thread.Sleep(1000);
|
||||
|
||||
downloader.DownloadContent(cid);
|
||||
|
||||
var map = GetLogMap(downloader, logStartUtc).OrderByDescending(p => p.Value);
|
||||
Log("Downloader - Receive");
|
||||
foreach (var entry in map)
|
||||
{
|
||||
if (entry.Value > 9)
|
||||
{
|
||||
Log($"'{entry.Key}' = {entry.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, int> GetLogMap(ICodexNode node, DateTime? startUtc = null)
|
||||
{
|
||||
var log = Ci.DownloadLog(node);
|
||||
var map = new Dictionary<string, int>();
|
||||
log.IterateLines(line =>
|
||||
{
|
||||
var log = CodexLogLine.Parse(line);
|
||||
if (log == null) return;
|
||||
|
||||
if (startUtc.HasValue)
|
||||
{
|
||||
if (log.TimestampUtc < startUtc) return;
|
||||
}
|
||||
|
||||
if (map.ContainsKey(log.Message)) map[log.Message] += 1;
|
||||
else map.Add(log.Message, 1);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -3,7 +3,7 @@ using DistTestCore;
|
||||
using NUnit.Framework;
|
||||
using Utils;
|
||||
|
||||
namespace CodexTests.BasicTests
|
||||
namespace CodexTests.UtilityTests
|
||||
{
|
||||
// Warning!
|
||||
// This is a test to check network-isolation in the test-infrastructure.
|
||||
@@ -19,7 +19,7 @@ namespace CodexTests.BasicTests
|
||||
{
|
||||
node = Ci.StartCodexNode();
|
||||
|
||||
Time.WaitUntil(() => node == null, TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(5));
|
||||
Time.WaitUntil(() => node == null, TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(5), nameof(SetUpANodeAndWait));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -27,7 +27,7 @@ namespace CodexTests.BasicTests
|
||||
{
|
||||
var myNode = Ci.StartCodexNode();
|
||||
|
||||
Time.WaitUntil(() => node != null, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(5));
|
||||
Time.WaitUntil(() => node != null, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(5), nameof(ForeignNodeConnects));
|
||||
|
||||
try
|
||||
{
|
||||
@@ -24,6 +24,9 @@ namespace DistTestCore
|
||||
this.dataFilesPath = dataFilesPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does not override [DontDownloadLogs] attribute.
|
||||
/// </summary>
|
||||
public bool AlwaysDownloadContainerLogs { get; set; }
|
||||
|
||||
public KubernetesWorkflow.Configuration GetK8sConfiguration(ITimeSet timeSet, string k8sNamespace)
|
||||
@@ -36,7 +39,7 @@ namespace DistTestCore
|
||||
var config = new KubernetesWorkflow.Configuration(
|
||||
kubeConfigFile: kubeConfigFile,
|
||||
operationTimeout: timeSet.K8sOperationTimeout(),
|
||||
retryDelay: timeSet.WaitForK8sServiceDelay(),
|
||||
retryDelay: timeSet.K8sOperationRetryDelay(),
|
||||
kubernetesNamespace: k8sNamespace
|
||||
);
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ namespace DistTestCore
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
fixtureLog.Error("Cleanup failed: " + ex.Message);
|
||||
fixtureLog.Error("Cleanup failed: " + ex);
|
||||
GlobalTestFailure.HasFailed = true;
|
||||
}
|
||||
}
|
||||
@@ -236,9 +236,19 @@ namespace DistTestCore
|
||||
}
|
||||
|
||||
private bool ShouldUseLongTimeouts()
|
||||
{
|
||||
return CurrentTestMethodHasAttribute<UseLongTimeoutsAttribute>();
|
||||
}
|
||||
|
||||
private bool HasDontDownloadAttribute()
|
||||
{
|
||||
return CurrentTestMethodHasAttribute<DontDownloadLogsAttribute>();
|
||||
}
|
||||
|
||||
private bool CurrentTestMethodHasAttribute<T>() where T : PropertyAttribute
|
||||
{
|
||||
// Don't be fooled! TestContext.CurrentTest.Test allows you easy access to the attributes of the current test.
|
||||
// But this doesn't work for tests making use of [TestCase]. So instead, we use reflection here to figure out
|
||||
// But this doesn't work for tests making use of [TestCase] or [Combinatorial]. So instead, we use reflection here to figure out
|
||||
// if the attribute is present.
|
||||
var currentTest = TestContext.CurrentContext.Test;
|
||||
var className = currentTest.ClassName;
|
||||
@@ -247,7 +257,7 @@ namespace DistTestCore
|
||||
var testClasses = testAssemblies.SelectMany(a => a.GetTypes()).Where(c => c.FullName == className).ToArray();
|
||||
var testMethods = testClasses.SelectMany(c => c.GetMethods()).Where(m => m.Name == methodName).ToArray();
|
||||
|
||||
return testMethods.Any(m => m.GetCustomAttribute<UseLongTimeoutsAttribute>() != null);
|
||||
return testMethods.Any(m => m.GetCustomAttribute<T>() != null);
|
||||
}
|
||||
|
||||
private void IncludeLogsOnTestFailure(TestLifecycle lifecycle)
|
||||
@@ -268,9 +278,10 @@ namespace DistTestCore
|
||||
private bool ShouldDownloadAllLogs(TestStatus testStatus)
|
||||
{
|
||||
if (configuration.AlwaysDownloadContainerLogs) return true;
|
||||
if (!IsDownloadingLogsEnabled()) return false;
|
||||
if (testStatus == TestStatus.Failed)
|
||||
{
|
||||
return IsDownloadingLogsEnabled();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -288,8 +299,7 @@ namespace DistTestCore
|
||||
|
||||
private bool IsDownloadingLogsEnabled()
|
||||
{
|
||||
var testProperties = TestContext.CurrentContext.Test.Properties;
|
||||
return !testProperties.ContainsKey(DontDownloadLogsOnFailureAttribute.DontDownloadKey);
|
||||
return !HasDontDownloadAttribute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -3,11 +3,11 @@
|
||||
namespace DistTestCore
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||
public class DontDownloadLogsOnFailureAttribute : PropertyAttribute
|
||||
public class DontDownloadLogsAttribute : PropertyAttribute
|
||||
{
|
||||
public const string DontDownloadKey = "DontDownloadLogs";
|
||||
|
||||
public DontDownloadLogsOnFailureAttribute()
|
||||
public DontDownloadLogsAttribute()
|
||||
: base(DontDownloadKey)
|
||||
{
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace DistTestCore.Helpers
|
||||
Time.WaitUntil(() => {
|
||||
var c = constraint.Resolve();
|
||||
return c.ApplyTo(actual()).IsSuccess;
|
||||
});
|
||||
}, "RetryAssert: " + message);
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace DistTestCore
|
||||
private const string TestsType = "dist-tests";
|
||||
private readonly EntryPoint entryPoint;
|
||||
private readonly Dictionary<string, string> metadata;
|
||||
private readonly List<RunningContainers> runningContainers = new();
|
||||
private readonly List<RunningPod> runningContainers = new();
|
||||
private readonly string deployId;
|
||||
|
||||
public TestLifecycle(TestLog log, Configuration configuration, ITimeSet timeSet, string testNamespace, string deployId)
|
||||
@@ -65,12 +65,12 @@ namespace DistTestCore
|
||||
return DateTime.UtcNow - TestStart;
|
||||
}
|
||||
|
||||
public void OnContainersStarted(RunningContainers rc)
|
||||
public void OnContainersStarted(RunningPod rc)
|
||||
{
|
||||
runningContainers.Add(rc);
|
||||
}
|
||||
|
||||
public void OnContainersStopped(RunningContainers rc)
|
||||
public void OnContainersStopped(RunningPod rc)
|
||||
{
|
||||
runningContainers.Remove(rc);
|
||||
}
|
||||
@@ -93,13 +93,20 @@ namespace DistTestCore
|
||||
|
||||
public void DownloadAllLogs()
|
||||
{
|
||||
foreach (var rc in runningContainers)
|
||||
try
|
||||
{
|
||||
foreach (var c in rc.Containers)
|
||||
foreach (var rc in runningContainers)
|
||||
{
|
||||
CoreInterface.DownloadLog(c);
|
||||
foreach (var c in rc.Containers)
|
||||
{
|
||||
CoreInterface.DownloadLog(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Exception during log download: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace BiblioTech
|
||||
|
||||
public bool IsAdminChannel(IChannel channel)
|
||||
{
|
||||
return channel.Name == Program.Config.AdminChannelName;
|
||||
return channel.Id == Program.Config.AdminChannelId;
|
||||
}
|
||||
|
||||
public ISocketMessageChannel GetAdminChannel()
|
||||
@@ -45,7 +45,7 @@ namespace BiblioTech
|
||||
private void UpdateAdminIds()
|
||||
{
|
||||
lastUpdate = DateTime.UtcNow;
|
||||
var adminRole = guild.Roles.Single(r => r.Name == Program.Config.AdminRoleName);
|
||||
var adminRole = guild.Roles.Single(r => r.Id == Program.Config.AdminRoleId);
|
||||
adminIds = adminRole.Members.Select(m => m.Id).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace BiblioTech
|
||||
|
||||
private async Task Client_Ready()
|
||||
{
|
||||
var guild = client.Guilds.Single(g => g.Name == Program.Config.ServerName);
|
||||
var guild = client.Guilds.Single(g => g.Id == Program.Config.ServerId);
|
||||
Program.AdminChecker.SetGuild(guild);
|
||||
Program.Log.Log($"Initializing for guild: '{guild.Name}'");
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using BiblioTech.Options;
|
||||
using DiscordRewards;
|
||||
using System.Globalization;
|
||||
using Utils;
|
||||
|
||||
namespace BiblioTech.Commands
|
||||
{
|
||||
@@ -30,7 +31,13 @@ namespace BiblioTech.Commands
|
||||
|
||||
private string[] GetInsight(MarketAverage avg)
|
||||
{
|
||||
var headerLine = $"[{avg.Title}]";
|
||||
var timeRange = TimeSpan.FromSeconds(avg.TimeRangeSeconds);
|
||||
var headerLine = $"[Last {Time.FormatDuration(timeRange)}] ({avg.NumberOfFinished} Contracts finished)";
|
||||
|
||||
if (avg.NumberOfFinished == 0)
|
||||
{
|
||||
return new[] { headerLine };
|
||||
}
|
||||
|
||||
return new[]
|
||||
{
|
||||
|
||||
@@ -7,31 +7,31 @@ namespace BiblioTech
|
||||
[Uniform("token", "t", "TOKEN", true, "Discord Application Token")]
|
||||
public string ApplicationToken { get; set; } = string.Empty;
|
||||
|
||||
[Uniform("server-name", "sn", "SERVERNAME", true, "Name of the Discord server")]
|
||||
public string ServerName { get; set; } = string.Empty;
|
||||
[Uniform("server-id", "sn", "SERVERID", true, "ID of the Discord server")]
|
||||
public ulong ServerId { get; set; }
|
||||
|
||||
[Uniform("datapath", "dp", "DATAPATH", false, "Root path where all data files will be saved.")]
|
||||
[Uniform("datapath", "dp", "DATAPATH", true, "Root path where all data files will be saved.")]
|
||||
public string DataPath { get; set; } = "datapath";
|
||||
|
||||
[Uniform("admin-role", "a", "ADMINROLE", true, "Name of the Discord server admin role")]
|
||||
public string AdminRoleName { get; set; } = string.Empty;
|
||||
[Uniform("admin-role-id", "a", "ADMINROLEID", true, "ID of the Discord server admin role")]
|
||||
public ulong AdminRoleId { get; set; }
|
||||
|
||||
[Uniform("admin-channel-name", "ac", "ADMINCHANNELNAME", true, "Name of the Discord server channel where admin commands are allowed.")]
|
||||
public string AdminChannelName { get; set; } = "admin-channel";
|
||||
[Uniform("admin-channel-id", "ac", "ADMINCHANNELID", true, "ID of the Discord server channel where admin commands are allowed.")]
|
||||
public ulong AdminChannelId{ get; set; }
|
||||
|
||||
[Uniform("rewards-channel-name", "rc", "REWARDSCHANNELNAME", false, "Name of the Discord server channel where participation rewards will be announced.")]
|
||||
public string RewardsChannelName { get; set; } = "";
|
||||
[Uniform("rewards-channel-id", "rc", "REWARDSCHANNELID", false, "ID of the Discord server channel where participation rewards will be announced.")]
|
||||
public ulong RewardsChannelId { get; set; }
|
||||
|
||||
[Uniform("chain-events-channel-name", "cc", "CHAINEVENTSCHANNELNAME", false, "Name of the Discord server channel where chain events will be posted.")]
|
||||
public string ChainEventsChannelName { get; set; } = "";
|
||||
[Uniform("chain-events-channel-id", "cc", "CHAINEVENTSCHANNELID", false, "ID of the Discord server channel where chain events will be posted.")]
|
||||
public ulong ChainEventsChannelId { get; set; }
|
||||
|
||||
[Uniform("reward-api-port", "rp", "REWARDAPIPORT", false, "TCP listen port for the reward API.")]
|
||||
[Uniform("reward-api-port", "rp", "REWARDAPIPORT", true, "TCP listen port for the reward API.")]
|
||||
public int RewardApiPort { get; set; } = 31080;
|
||||
|
||||
[Uniform("send-eth", "se", "SENDETH", false, "Amount of Eth send by the mint command. Default: 10.")]
|
||||
[Uniform("send-eth", "se", "SENDETH", true, "Amount of Eth send by the mint command.")]
|
||||
public int SendEth { get; set; } = 10;
|
||||
|
||||
[Uniform("mint-tt", "mt", "MINTTT", false, "Amount of TestTokens minted by the mint command. Default: 1073741824")]
|
||||
[Uniform("mint-tt", "mt", "MINTTT", true, "Amount of TestTokens minted by the mint command.")]
|
||||
public int MintTT { get; set; } = 1073741824;
|
||||
|
||||
public string EndpointsPath
|
||||
|
||||
@@ -21,6 +21,8 @@ namespace BiblioTech
|
||||
|
||||
public static Task Main(string[] args)
|
||||
{
|
||||
Log = new ConsoleLog();
|
||||
|
||||
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
|
||||
Config = uniformArgs.Parse();
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ namespace BiblioTech.Rewards
|
||||
{
|
||||
this.client = client;
|
||||
|
||||
rewardsChannel = GetChannel(Program.Config.RewardsChannelName);
|
||||
eventsChannel = GetChannel(Program.Config.ChainEventsChannelName);
|
||||
rewardsChannel = GetChannel(Program.Config.RewardsChannelId);
|
||||
eventsChannel = GetChannel(Program.Config.ChainEventsChannelId);
|
||||
}
|
||||
|
||||
public async Task GiveRewards(GiveRewardsCommand rewards)
|
||||
@@ -45,10 +45,10 @@ namespace BiblioTech.Rewards
|
||||
await context.ProcessGiveRewardsCommand(LookUpUsers(rewards));
|
||||
}
|
||||
|
||||
private SocketTextChannel? GetChannel(string name)
|
||||
private SocketTextChannel? GetChannel(ulong id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name)) return null;
|
||||
return GetGuild().TextChannels.SingleOrDefault(c => c.Name == name);
|
||||
if (id == 0) return null;
|
||||
return GetGuild().TextChannels.SingleOrDefault(c => c.Id == id);
|
||||
}
|
||||
|
||||
private async Task ProcessChainEvents(string[] eventsOverview)
|
||||
@@ -147,11 +147,11 @@ namespace BiblioTech.Rewards
|
||||
|
||||
private SocketGuild GetGuild()
|
||||
{
|
||||
var guild = client.Guilds.SingleOrDefault(g => g.Name == Program.Config.ServerName);
|
||||
var guild = client.Guilds.SingleOrDefault(g => g.Id == Program.Config.ServerId);
|
||||
if (guild == null)
|
||||
{
|
||||
throw new Exception($"Unable to find guild by name: '{Program.Config.ServerName}'. " +
|
||||
$"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name))}]");
|
||||
throw new Exception($"Unable to find guild by id: '{Program.Config.ServerId}'. " +
|
||||
$"Known guilds: [{string.Join(",", client.Guilds.Select(g => g.Name + " (" + g.Id + ")"))}]");
|
||||
}
|
||||
return guild;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ namespace CodexNetDeployer
|
||||
});
|
||||
}
|
||||
|
||||
private RunningContainers? DeployDiscordBot(CoreInterface ci, GethDeployment gethDeployment,
|
||||
private RunningPod? DeployDiscordBot(CoreInterface ci, GethDeployment gethDeployment,
|
||||
CodexContractsDeployment contractsDeployment)
|
||||
{
|
||||
if (!config.DeployDiscordBot) return null;
|
||||
@@ -155,7 +155,7 @@ namespace CodexNetDeployer
|
||||
return rc;
|
||||
}
|
||||
|
||||
private RunningContainers? StartMetricsService(CoreInterface ci, List<CodexNodeStartResult> startResults)
|
||||
private RunningPod? StartMetricsService(CoreInterface ci, List<CodexNodeStartResult> startResults)
|
||||
{
|
||||
if (!config.MetricsScraper || !startResults.Any()) return null;
|
||||
|
||||
@@ -180,7 +180,7 @@ namespace CodexNetDeployer
|
||||
|
||||
private CodexInstance CreateCodexInstance(ICodexNode node)
|
||||
{
|
||||
return new CodexInstance(node.Container.RunningContainers, node.GetDebugInfo());
|
||||
return new CodexInstance(node.Container.RunningPod, node.GetDebugInfo());
|
||||
}
|
||||
|
||||
private string? GetKubeConfig(string kubeConfigFile)
|
||||
@@ -255,9 +255,9 @@ namespace CodexNetDeployer
|
||||
return TimeSpan.FromSeconds(2);
|
||||
}
|
||||
|
||||
public int HttpMaxNumberOfRetries()
|
||||
public TimeSpan HttpRetryTimeout()
|
||||
{
|
||||
return 2;
|
||||
return TimeSpan.FromSeconds(30);
|
||||
}
|
||||
|
||||
public TimeSpan HttpCallTimeout()
|
||||
@@ -270,7 +270,7 @@ namespace CodexNetDeployer
|
||||
return TimeSpan.FromMinutes(10);
|
||||
}
|
||||
|
||||
public TimeSpan WaitForK8sServiceDelay()
|
||||
public TimeSpan K8sOperationRetryDelay()
|
||||
{
|
||||
return TimeSpan.FromSeconds(30);
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@ namespace CodexNetDeployer
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public void OnContainersStarted(RunningContainers rc)
|
||||
public void OnContainersStarted(RunningPod rc)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnContainersStopped(RunningContainers rc)
|
||||
public void OnContainersStopped(RunningPod rc)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -4,22 +4,25 @@ namespace TestNetRewarder
|
||||
{
|
||||
public class Configuration
|
||||
{
|
||||
[Uniform("datapath", "dp", "DATAPATH", false, "Root path where all data files will be saved.")]
|
||||
[Uniform("datapath", "dp", "DATAPATH", true, "Root path where all data files will be saved.")]
|
||||
public string DataPath { get; set; } = "datapath";
|
||||
|
||||
[Uniform("discordbot-host", "dh", "DISCORDBOTHOST", true, "http address of the discord bot.")]
|
||||
public string DiscordHost { get; set; } = "host";
|
||||
|
||||
[Uniform("discordbot-port", "dp", "DISCORDBOTPORT", true, "port number of the discord bot reward API. (31080 by default)")]
|
||||
[Uniform("discordbot-port", "dp", "DISCORDBOTPORT", true, "port number of the discord bot reward API.")]
|
||||
public int DiscordPort { get; set; } = 31080;
|
||||
|
||||
[Uniform("interval-minutes", "im", "INTERVALMINUTES", false, "time in minutes between reward updates. (default 15)")]
|
||||
[Uniform("interval-minutes", "im", "INTERVALMINUTES", true, "time in minutes between reward updates.")]
|
||||
public int IntervalMinutes { get; set; } = 15;
|
||||
|
||||
[Uniform("check-history", "ch", "CHECKHISTORY", true, "Unix epoc timestamp of a moment in history on which processing begins. Required for hosting rewards. Should be 'launch of the testnet'.")]
|
||||
public int CheckHistoryTimestamp { get; set; } = 0;
|
||||
|
||||
[Uniform("events-overview", "eo", "EVENTSOVERVIEW", false, "When greater than zero, chain event summary will be generated. (default 1)")]
|
||||
[Uniform("market-insights", "mi", "MARKETINSIGHTS", false, "Semi-colon separated integers. Each represents a multiple of intervals, for which a market insights average will be generated.")]
|
||||
public string MarketInsights { get; set; } = "1;96";
|
||||
|
||||
[Uniform("events-overview", "eo", "EVENTSOVERVIEW", false, "When greater than zero, chain event summary will be generated.")]
|
||||
public int CreateChainEventsOverview { get; set; } = 1;
|
||||
|
||||
public string LogPath
|
||||
|
||||
@@ -6,47 +6,66 @@ namespace TestNetRewarder
|
||||
{
|
||||
public class MarketTracker
|
||||
{
|
||||
private readonly MarketAverage MostRecent = new MarketAverage
|
||||
{
|
||||
Title = "Most recent"
|
||||
};
|
||||
private readonly MarketAverage Irf = new MarketAverage
|
||||
{
|
||||
Title = "Recent average"
|
||||
};
|
||||
private readonly List<ChainState> buffer = new List<ChainState>();
|
||||
|
||||
public MarketAverage[] ProcessChainState(ChainState chainState)
|
||||
{
|
||||
UpdateMostRecent(chainState);
|
||||
UpdateIrf(chainState);
|
||||
var intervalCounts = GetInsightCounts();
|
||||
if (!intervalCounts.Any()) return Array.Empty<MarketAverage>();
|
||||
|
||||
return new[]
|
||||
UpdateBuffer(chainState, intervalCounts.Max());
|
||||
var result = intervalCounts
|
||||
.Select(GenerateMarketAverage)
|
||||
.Where(a => a != null)
|
||||
.Cast<MarketAverage>()
|
||||
.ToArray();
|
||||
|
||||
if (!result.Any()) result = Array.Empty<MarketAverage>();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void UpdateBuffer(ChainState chainState, int maxNumberOfIntervals)
|
||||
{
|
||||
buffer.Add(chainState);
|
||||
while (buffer.Count > maxNumberOfIntervals)
|
||||
{
|
||||
MostRecent,
|
||||
Irf
|
||||
};
|
||||
buffer.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateIrf(ChainState chainState)
|
||||
private MarketAverage? GenerateMarketAverage(int numberOfIntervals)
|
||||
{
|
||||
if (!chainState.FinishedRequests.Any()) return;
|
||||
|
||||
MostRecent.Price = GetIrf(MostRecent.Price, chainState, s => s.Request.Ask.Reward);
|
||||
MostRecent.Duration = GetIrf(MostRecent.Duration, chainState, s => s.Request.Ask.Duration);
|
||||
MostRecent.Size = GetIrf(MostRecent.Size, chainState, s => GetTotalSize(s.Request.Ask));
|
||||
MostRecent.Collateral = GetIrf(MostRecent.Collateral, chainState, s => s.Request.Ask.Collateral);
|
||||
MostRecent.ProofProbability = GetIrf(MostRecent.ProofProbability, chainState, s => s.Request.Ask.ProofProbability);
|
||||
var states = SelectStates(numberOfIntervals);
|
||||
return CreateAverage(states);
|
||||
}
|
||||
|
||||
private void UpdateMostRecent(ChainState chainState)
|
||||
private ChainState[] SelectStates(int numberOfIntervals)
|
||||
{
|
||||
if (!chainState.FinishedRequests.Any()) return;
|
||||
if (numberOfIntervals < 1) return Array.Empty<ChainState>();
|
||||
if (numberOfIntervals > buffer.Count) return Array.Empty<ChainState>();
|
||||
return buffer.TakeLast(numberOfIntervals).ToArray();
|
||||
}
|
||||
|
||||
MostRecent.Price = Average(chainState, s => s.Request.Ask.Reward);
|
||||
MostRecent.Duration = Average(chainState, s => s.Request.Ask.Duration);
|
||||
MostRecent.Size = Average(chainState, s => GetTotalSize(s.Request.Ask));
|
||||
MostRecent.Collateral = Average(chainState, s => s.Request.Ask.Collateral);
|
||||
MostRecent.ProofProbability = Average(chainState, s => s.Request.Ask.ProofProbability);
|
||||
private MarketAverage? CreateAverage(ChainState[] states)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new MarketAverage
|
||||
{
|
||||
NumberOfFinished = CountNumberOfFinishedRequests(states),
|
||||
TimeRangeSeconds = GetTotalTimeRange(states),
|
||||
Price = Average(states, s => s.Request.Ask.Reward),
|
||||
Duration = Average(states, s => s.Request.Ask.Duration),
|
||||
Size = Average(states, s => GetTotalSize(s.Request.Ask)),
|
||||
Collateral = Average(states, s => s.Request.Ask.Collateral),
|
||||
ProofProbability = Average(states, s => s.Request.Ask.ProofProbability)
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Program.Log.Error($"Exception in CreateAverage: {ex}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetTotalSize(Ask ask)
|
||||
@@ -56,40 +75,50 @@ namespace TestNetRewarder
|
||||
return nSlots * slotSize;
|
||||
}
|
||||
|
||||
private float Average(ChainState state, Func<StorageRequest, BigInteger> getValue)
|
||||
private float Average(ChainState[] states, Func<StorageRequest, BigInteger> getValue)
|
||||
{
|
||||
return Average(state, s => Convert.ToInt32(getValue(s)));
|
||||
return Average(states, s => Convert.ToInt32(getValue(s)));
|
||||
}
|
||||
|
||||
private float GetIrf(float current, ChainState state, Func<StorageRequest, BigInteger> getValue)
|
||||
{
|
||||
return GetIrf(current, state, s => Convert.ToInt32(getValue(s)));
|
||||
}
|
||||
|
||||
private float Average(ChainState state, Func<StorageRequest, int> getValue)
|
||||
private float Average(ChainState[] states, Func<StorageRequest, int> getValue)
|
||||
{
|
||||
var sum = 0.0f;
|
||||
var count = 0.0f;
|
||||
foreach (var finishedRequest in state.FinishedRequests)
|
||||
foreach (var state in states)
|
||||
{
|
||||
sum += getValue(finishedRequest);
|
||||
count++;
|
||||
foreach (var finishedRequest in state.FinishedRequests)
|
||||
{
|
||||
sum += getValue(finishedRequest);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count < 1.0f) return 0.0f;
|
||||
return sum / count;
|
||||
}
|
||||
|
||||
private float GetIrf(float current, ChainState state, Func<StorageRequest, int> getValue)
|
||||
private int GetTotalTimeRange(ChainState[] states)
|
||||
{
|
||||
var result = current;
|
||||
foreach (var finishedRequest in state.FinishedRequests)
|
||||
{
|
||||
float v = getValue(finishedRequest);
|
||||
result = (result + v) / 2.0f;
|
||||
}
|
||||
return Convert.ToInt32((Program.Config.Interval * states.Length).TotalSeconds);
|
||||
}
|
||||
|
||||
return result;
|
||||
private int CountNumberOfFinishedRequests(ChainState[] states)
|
||||
{
|
||||
return states.Sum(s => s.FinishedRequests.Length);
|
||||
}
|
||||
|
||||
private int[] GetInsightCounts()
|
||||
{
|
||||
try
|
||||
{
|
||||
var tokens = Program.Config.MarketInsights.Split(';').ToArray();
|
||||
return tokens.Select(t => Convert.ToInt32(t)).ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Program.Log.Error($"Exception when parsing MarketInsights config parameters: {ex}");
|
||||
}
|
||||
return Array.Empty<int>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user