Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Storage performance fixes #3313

Merged
merged 15 commits into from
Jul 24, 2023
12 changes: 3 additions & 9 deletions src/ApiService/ApiService/Functions/Containers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private async Async.Task<HttpResponseData> Get(HttpRequestData req) {
req,
Error.Create(
ErrorCode.INVALID_REQUEST,
"invalid container"),
$"invalid container '{get.Name}'"),
context: get.Name.String);
}

Expand Down Expand Up @@ -75,13 +75,7 @@ private async Async.Task<HttpResponseData> Delete(HttpRequestData req) {

var delete = request.OkV;
_logger.LogInformation("deleting {ContainerName}", delete.Name);
var container = await _context.Containers.FindContainer(delete.Name, StorageType.Corpus);

var deleted = false;
if (container is not null) {
deleted = await container.DeleteIfExistsAsync();
}

var deleted = await _context.Containers.DeleteContainerIfExists(delete.Name, StorageType.Corpus);
return await RequestHandling.Ok(req, deleted);
}

Expand All @@ -93,7 +87,7 @@ private async Async.Task<HttpResponseData> Post(HttpRequestData req) {

var post = request.OkV;
_logger.LogInformation("creating {ContainerName}", post.Name);
var sas = await _context.Containers.CreateContainer(
var sas = await _context.Containers.GetOrCreateNewContainer(
post.Name,
StorageType.Corpus,
post.Metadata);
Expand Down
25 changes: 13 additions & 12 deletions src/ApiService/ApiService/Functions/Jobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ private async Task<HttpResponseData> Post(HttpRequestData req, FunctionContext c
var userInfo = context.GetUserAuthInfo();

var create = request.OkV;
var cfg = new JobConfig(
Build: create.Build,
Duration: create.Duration,
Logs: create.Logs,
Name: create.Name,
Project: create.Project);

var job = new Job(
JobId: Guid.NewGuid(),
State: JobState.Init,
Config: cfg,
Config: new(
Build: create.Build,
Duration: create.Duration,
Logs: create.Logs,
Name: create.Name,
Project: create.Project),
UserInfo: new(
ObjectId: userInfo.UserInfo.ObjectId,
ApplicationId: userInfo.UserInfo.ApplicationId));
Expand All @@ -56,8 +55,11 @@ private async Task<HttpResponseData> Post(HttpRequestData req, FunctionContext c
{ "container_type", "logs" }, // TODO: use ContainerType.Logs enum somehow; needs snake case name
};

var containerName = Container.Parse($"logs-{job.JobId}");
var containerSas = await _context.Containers.CreateContainer(containerName, StorageType.Corpus, metadata);
var containerSas = await _context.Containers.CreateNewContainer(
Container.Parse($"logs-{job.JobId}"),
StorageType.Corpus,
metadata);

if (containerSas is null) {
return await _context.RequestHandling.NotOk(
req,
Expand All @@ -76,9 +78,8 @@ private async Task<HttpResponseData> Post(HttpRequestData req, FunctionContext c
return await _context.RequestHandling.NotOk(
req,
Error.Create(
ErrorCode.UNABLE_TO_CREATE,
"unable to create job"
),
ErrorCode.UNABLE_TO_CREATE,
"unable to create job"),
"job");
}

Expand Down
7 changes: 6 additions & 1 deletion src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static Error Create(ErrorCode code, params string[] errors)
=> new(code, errors.ToList());

public sealed override string ToString() {
var errorsString = Errors != null ? string.Concat("; ", Errors) : string.Empty;
var errorsString = Errors != null ? string.Join("; ", Errors) : string.Empty;
return $"Error {{ Code = {Code}, Errors = {errorsString} }}";
}
};
Expand Down Expand Up @@ -767,6 +767,11 @@ public record ClientCredentials
string ClientSecret
);

public record ContainerInformation(
[PartitionKey] StorageType Type,
[RowKey] Container Name,
string ResourceId // full ARM resource ID for the container
) : EntityBase;

public record AgentConfig(
ClientCredentials? ClientCredentials,
Expand Down
22 changes: 20 additions & 2 deletions src/ApiService/ApiService/OneFuzzTypes/ReturnTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

namespace Microsoft.OneFuzz.Service {

public static class Result {
public readonly record struct OkPlaceholder();
public readonly record struct OkPlaceholder<T>(T Value);
public readonly record struct ErrPlaceholder<T>(T Value);

public static OkPlaceholder Ok() => new();
public static OkPlaceholder<T> Ok<T>(T value) => new(value);
public static ErrPlaceholder<T> Error<T>(T value) => new(value);
}

public readonly struct ResultVoid<T_Error> {
public static ResultVoid<T_Error> Ok() => new();
public static ResultVoid<T_Error> Error(T_Error err) => new(err);
Expand All @@ -13,15 +23,16 @@ public readonly struct ResultVoid<T_Error> {
public bool IsOk { get; }

public T_Error? ErrorV { get; }
}

public static implicit operator ResultVoid<T_Error>(Result.OkPlaceholder _ok) => Ok();
public static implicit operator ResultVoid<T_Error>(Result.ErrPlaceholder<T_Error> err) => Error(err.Value);
}

public readonly struct Result<T_Ok, T_Error> {
public static Result<T_Ok, T_Error> Ok(T_Ok ok) => new(ok);
public static Result<T_Ok, T_Error> Error(T_Error err) => new(err);

private Result(T_Ok ok) => (OkV, ErrorV, IsOk) = (ok, default, true);

private Result(T_Error error) => (ErrorV, OkV, IsOk) = (error, default, false);

[MemberNotNullWhen(returnValue: true, member: nameof(OkV))]
Expand All @@ -31,6 +42,9 @@ public readonly struct Result<T_Ok, T_Error> {
public T_Error? ErrorV { get; }

public T_Ok? OkV { get; }

public static implicit operator Result<T_Ok, T_Error>(Result.OkPlaceholder<T_Ok> ok) => Ok(ok.Value);
public static implicit operator Result<T_Ok, T_Error>(Result.ErrPlaceholder<T_Error> err) => Error(err.Value);
}

public static class OneFuzzResult {
Expand Down Expand Up @@ -61,6 +75,8 @@ public readonly struct OneFuzzResult<T_Ok> {

// Allow simple conversion of Errors to Results.
public static implicit operator OneFuzzResult<T_Ok>(Error err) => new(err);
public static implicit operator OneFuzzResult<T_Ok>(Result.OkPlaceholder<T_Ok> ok) => Ok(ok.Value);
public static implicit operator OneFuzzResult<T_Ok>(Result.ErrPlaceholder<Error> err) => Error(err.Value);
}

public readonly struct OneFuzzResultVoid {
Expand All @@ -83,5 +99,7 @@ public readonly struct OneFuzzResultVoid {

// Allow simple conversion of Errors to Results.
public static implicit operator OneFuzzResultVoid(Error err) => new(err);
public static implicit operator OneFuzzResultVoid(Result.OkPlaceholder _ok) => Ok;
public static implicit operator OneFuzzResultVoid(Result.ErrPlaceholder<Error> err) => Error(err.Value);
}
}
Loading