Retrofitting autoclient to support folder-uploader mode

This commit is contained in:
Ben 2024-10-30 08:56:31 +01:00
parent ff4711e802
commit b54c9ff9a3
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
6 changed files with 105 additions and 42 deletions

View File

@ -6,23 +6,16 @@ using Utils;
namespace AutoClient
{
public class Purchaser
public class AutomaticPurchaser
{
private readonly App app;
private readonly string nodeId;
private readonly ILog log;
private readonly HttpClient client;
private readonly Address address;
private readonly CodexApi codex;
private readonly ICodexInstance codex;
private Task workerTask = Task.CompletedTask;
private App app => codex.App;
public Purchaser(App app, string nodeId, ILog log, HttpClient client, Address address, CodexApi codex)
public AutomaticPurchaser(ILog log, ICodexInstance codex)
{
this.app = app;
this.nodeId = nodeId;
this.log = log;
this.client = client;
this.address = address;
this.codex = codex;
}
@ -57,7 +50,7 @@ namespace AutoClient
private async Task DownloadForeignCid()
{
var cid = app.CidRepo.GetForeignCid(nodeId);
var cid = app.CidRepo.GetForeignCid(codex.NodeId);
if (cid == null) return;
var size = app.CidRepo.GetSizeForCid(cid);
if (size == null) return;
@ -68,7 +61,7 @@ namespace AutoClient
var filename = Guid.NewGuid().ToString().ToLowerInvariant();
{
using var fileStream = File.OpenWrite(filename);
var fileResponse = await codex.DownloadNetworkStreamAsync(cid);
var fileResponse = await codex.Codex.DownloadNetworkStreamAsync(cid);
fileResponse.Stream.CopyTo(fileStream);
}
var time = sw.Elapsed;
@ -122,7 +115,7 @@ namespace AutoClient
var cid = await UploadStream(fileStream, filename);
var time = sw.Elapsed;
app.Performance.UploadSuccessful(info.Length, time);
app.CidRepo.Add(nodeId, cid.Id, info.Length);
app.CidRepo.Add(codex.NodeId, cid.Id, info.Length);
return cid;
}
catch (Exception exc)
@ -135,7 +128,7 @@ namespace AutoClient
private async Task<ContentId> UploadStream(FileStream fileStream, string filename)
{
log.Debug($"Uploading file...");
var response = await codex.UploadAsync(
var response = await codex.Codex.UploadAsync(
content_type: "application/x-binary",
content_disposition: $"attachment; filename=\"{filename}\"",
fileStream, app.Cts.Token);
@ -150,7 +143,7 @@ namespace AutoClient
private async Task<string> RequestStorage(ContentId cid)
{
log.Debug("Requesting storage for " + cid.Id);
var result = await codex.CreateStorageRequestAsync(cid.Id, new StorageRequestCreation()
var result = await codex.Codex.CreateStorageRequestAsync(cid.Id, new StorageRequestCreation()
{
Collateral = app.Config.RequiredCollateral.ToString(),
Duration = (app.Config.ContractDurationMinutes * 60).ToString(),
@ -186,7 +179,7 @@ namespace AutoClient
private async Task<StoragePurchase?> GetStoragePurchase(string pid)
{
// openapi still don't match code.
var str = await client.GetStringAsync($"{address.Host}:{address.Port}/api/codex/v1/storage/purchases/{pid}");
var str = await codex.Client.GetStringAsync($"{codex.Address.Host}:{codex.Address.Port}/api/codex/v1/storage/purchases/{pid}");
if (string.IsNullOrEmpty(str)) return null;
return JsonConvert.DeserializeObject<StoragePurchase>(str);
}

View File

@ -0,0 +1,33 @@
using CodexOpenApi;
using Logging;
using Utils;
namespace AutoClient
{
public interface ICodexInstance
{
string NodeId { get; }
App App { get; }
CodexApi Codex { get; }
HttpClient Client { get; }
Address Address { get; }
}
public class CodexInstance : ICodexInstance
{
public CodexInstance(App app, CodexApi codex, HttpClient client, Address address)
{
App = app;
Codex = codex;
Client = client;
Address = address;
NodeId = Guid.NewGuid().ToString();
}
public string NodeId { get; }
public App App { get; }
public CodexApi Codex { get; }
public HttpClient Client { get; }
public Address Address { get; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutoClient.Modes
{
public class FolderStoreMode : IMode
{
public void Start(ICodexInstance instance, int index)
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutoClient.Modes
{
public interface IMode
{
void Start(ICodexInstance instance, int index);
void Stop();
}
}

View File

@ -1,32 +1,23 @@
using CodexOpenApi;
using Logging;
using Utils;
using Logging;
namespace AutoClient
namespace AutoClient.Modes
{
public class CodexUser
public class PurchasingMode : IMode
{
private readonly List<AutomaticPurchaser> purchasers = new List<AutomaticPurchaser>();
private readonly App app;
private readonly CodexApi codex;
private readonly HttpClient client;
private readonly Address address;
private readonly List<Purchaser> purchasers = new List<Purchaser>();
private Task starterTask = Task.CompletedTask;
private readonly string nodeId = Guid.NewGuid().ToString();
public CodexUser(App app, CodexApi codex, HttpClient client, Address address)
public PurchasingMode(App app)
{
this.app = app;
this.codex = codex;
this.client = client;
this.address = address;
}
public void Start(int index)
public void Start(ICodexInstance instance, int index)
{
for (var i = 0; i < app.Config.NumConcurrentPurchases; i++)
{
purchasers.Add(new Purchaser(app, nodeId, new LogPrefixer(app.Log, $"({i}) "), client, address, codex));
purchasers.Add(new AutomaticPurchaser(new LogPrefixer(app.Log, $"({i}) "), instance));
}
var delayPerPurchaser =

View File

@ -1,11 +1,13 @@
using ArgsUniform;
using AutoClient;
using AutoClient.Modes;
using CodexOpenApi;
using Utils;
public class Program
{
private readonly App app;
private readonly List<IMode> modes = new List<IMode>();
public Program(Configuration config)
{
@ -31,26 +33,35 @@ public class Program
public async Task Run()
{
var codexUsers = await CreateUsers();
var codexInstances = await CreateCodexInstances();
var i = 0;
foreach (var user in codexUsers)
foreach (var cdx in codexInstances)
{
user.Start(i);
var mode = CreateMode();
modes.Add(mode);
mode.Start(cdx, i);
i++;
}
app.Cts.Token.WaitHandle.WaitOne();
foreach (var user in codexUsers) user.Stop();
foreach (var mode in modes) mode.Stop();
modes.Clear();
app.Log.Log("Done");
}
private async Task<CodexUser[]> CreateUsers()
private IMode CreateMode()
{
return new PurchasingMode(app);
}
private async Task<CodexInstance[]> CreateCodexInstances()
{
var endpointStrs = app.Config.CodexEndpoints.Split(";", StringSplitOptions.RemoveEmptyEntries);
var result = new List<CodexUser>();
var result = new List<CodexInstance>();
foreach (var e in endpointStrs)
{
@ -60,7 +71,7 @@ public class Program
return result.ToArray();
}
private async Task<CodexUser> CreateUser(string endpoint)
private async Task<CodexInstance> CreateUser(string endpoint)
{
var splitIndex = endpoint.LastIndexOf(':');
var host = endpoint.Substring(0, splitIndex);
@ -79,7 +90,7 @@ public class Program
await CheckCodex(codex);
app.Log.Log("OK");
return new CodexUser(
return new CodexInstance(
app,
codex,
client,