Skip to content

Commit

Permalink
First part of fix for grace switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottArbeit committed Dec 1, 2023
1 parent af0218f commit 1221353
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 26 deletions.
21 changes: 12 additions & 9 deletions src/Grace.Actors/Branch.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Branch =
let mutable isDisposed = false

let updateDto branchEventType currentBranchDto =
let newOrganizationDto =
let newBranchDto =
match branchEventType with
| Created (branchId, branchName, parentBranchId, basedOn, repositoryId, initialPermissions) ->
let mutable branchDto = {BranchDto.Default with BranchId = branchId; BranchName = branchName; ParentBranchId = parentBranchId; BasedOn = basedOn; RepositoryId = repositoryId}
Expand All @@ -57,10 +57,10 @@ module Branch =
| ReferenceType.Save -> {branchDto with SaveEnabled = true}
| ReferenceType.Tag -> {branchDto with TagEnabled = true}
branchDto

| Rebased referenceId -> {currentBranchDto with BasedOn = referenceId}
| NameSet branchName -> {currentBranchDto with BranchName = branchName}
| Promoted (referenceId, directoryVersion, sha256Hash, referenceText) -> {currentBranchDto with LatestPromotion = referenceId}
| Promoted (referenceId, directoryVersion, sha256Hash, referenceText) ->
{currentBranchDto with LatestPromotion = referenceId; BasedOn = referenceId}
| Committed (referenceId, directoryVersion, sha256Hash, referenceText) -> {currentBranchDto with LatestCommit = referenceId}
| Checkpointed (referenceId, directoryVersion, sha256Hash, referenceText) -> {currentBranchDto with LatestCheckpoint = referenceId}
| Saved (referenceId, directoryVersion, sha256Hash, referenceText) -> {currentBranchDto with LatestSave = referenceId}
Expand All @@ -75,11 +75,11 @@ module Branch =
| PhysicalDeleted -> currentBranchDto // Do nothing because it's about to be deleted anyway.
| Undeleted -> {currentBranchDto with DeletedAt = None; DeleteReason = String.Empty}

{newOrganizationDto with UpdatedAt = Some (getCurrentInstant())}
{newBranchDto with UpdatedAt = Some (getCurrentInstant())}

member private this.BranchEvents =
member private this.BranchEvents() =
let stateManager = this.StateManager
task {
let stateManager = this.StateManager
if branchEvents = null then
let! retrievedEvents = (Storage.RetrieveState<List<BranchEvent>> stateManager eventsStateName)
branchEvents <- match retrievedEvents with
Expand Down Expand Up @@ -112,7 +112,7 @@ module Branch =
task {
//let! _ = Constants.DefaultAsyncRetryPolicy.ExecuteAsync(fun () -> this.SetMaintenanceReminder())
()
}
} :> Task

override this.OnPreActorMethodAsync(context) =
actorStartTime <- getCurrentInstant()
Expand Down Expand Up @@ -140,7 +140,7 @@ module Branch =
let stateManager = this.StateManager
task {
try
let! branchEvents = this.BranchEvents
let! branchEvents = this.BranchEvents()
if branchEvents.Count = 0 then
do! this.OnFirstWrite()

Expand Down Expand Up @@ -191,7 +191,7 @@ module Branch =
member this.Handle (command: BranchCommand) (metadata: EventMetadata) =
let isValid (command: BranchCommand) (metadata: EventMetadata) =
task {
let! branchEvents = this.BranchEvents
let! branchEvents = this.BranchEvents()
if branchEvents.Exists(fun ev -> ev.Metadata.CorrelationId = metadata.CorrelationId) && (branchEvents.Count > 3) then
return Error (GraceError.Create (BranchError.getErrorMessage DuplicateCorrelationId) metadata.CorrelationId)
else
Expand Down Expand Up @@ -316,6 +316,9 @@ module Branch =
} :> Task
| ReminderType.PhysicalDeletion ->
task {
// Delete the references for this branch.


// Delete saved state for this actor.
let! deletedDtoState = stateManager.TryRemoveStateAsync(dtoStateName)
let! deletedEventsState = stateManager.TryRemoveStateAsync(eventsStateName)
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.CLI/Command/Branch.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ module Branch =
| Ok result -> Ok (showOutput, parseResult, parameters) |> returnTask
| Error error -> Error error |> returnTask

// 0. Get the branchDto from the parameters.
// 0. Get the branchDto for the current branch.
let getCurrentBranch (t: ProgressTask) (showOutput, parseResult: ParseResult, parameters: CommonParameters) =
task {
t |> startProgressTask showOutput
Expand Down
9 changes: 8 additions & 1 deletion src/Grace.CLI/Command/Diff.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,14 @@ module Diff =
logToAnsiConsole Colors.Verbose $"In diffToReference / Promotion: Did not find the basedOn reference."
()
| Error error ->
logToAnsiConsole Colors.Error "Error in GetReferencesByReferenceId."
logToAnsiConsole Colors.Error (Markup.Escape($"{error}"))
if parseResult |> json || parseResult |> verbose then
logToAnsiConsole Colors.Verbose (serialize error)
| Error error ->
logToAnsiConsole Colors.Error (Markup.Escape($"{error}"))
logToAnsiConsole Colors.Error (Markup.Escape($"Error in Branch.Get: {error}"))
if parseResult |> json || parseResult |> verbose then
logToAnsiConsole Colors.Verbose (serialize error)

return Ok (GraceReturnValue.Create (promotions :> IEnumerable<ReferenceDto>) parameters.CorrelationId)
}
Expand Down Expand Up @@ -352,6 +357,8 @@ module Diff =
| Error error ->
let s = StringExtensions.EscapeMarkup($"{error.Error}")
logToAnsiConsole Colors.Error $"Error submitting diff: {s}"
if parseResult |> json || parseResult |> verbose then
logToAnsiConsole Colors.Verbose (serialize error)
t6.Increment(100.0)
//AnsiConsole.MarkupLine($"[{Colors.Important}]Differences: {differences.Count}.[/]")
//AnsiConsole.MarkupLine($"[{Colors.Error}]{error.Error.EscapeMarkup()}[/]")
Expand Down
1 change: 1 addition & 0 deletions src/Grace.CLI/Command/Maintenance.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ module Maintenance =
let fileCount = graceStatus.Index.Values.Select(fun directoryVersion -> directoryVersion.Files.Count).Sum()
let totalFileSize = graceStatus.Index.Values.Sum(fun directoryVersion -> directoryVersion.Files.Sum(fun f -> int64 f.Size))
let rootDirectoryVersion = graceStatus.Index.Values.First(fun d -> d.RelativePath = Constants.RootDirectoryPath)
AnsiConsole.MarkupLine($"[{Colors.Important}]All values taken from the local Grace status file.[/]")
AnsiConsole.MarkupLine($"[{Colors.Highlighted}]Number of directories: {directoryCount}.[/]")
AnsiConsole.MarkupLine($"[{Colors.Highlighted}]Number of files: {fileCount}; total file size: {totalFileSize:N0}.[/]")
AnsiConsole.MarkupLine($"[{Colors.Highlighted}]Root SHA-256 hash: {rootDirectoryVersion.Sha256Hash.Substring(0, 8)}[/]")
Expand Down
4 changes: 2 additions & 2 deletions src/Grace.Server/Branch.Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ module Branch =
(Guid.Parse(parameters.BranchId)),
(BranchName parameters.BranchName),
Constants.DefaultParentBranchId,
ReferenceId.Empty,
ReferenceId.Empty, // This is fucked.
Guid.Parse(parameters.RepositoryId),
parameters.InitialPermissions)
}
Expand Down Expand Up @@ -216,7 +216,7 @@ module Branch =
Branch.branchAllowsReferenceType parameters.OwnerId parameters.OwnerName parameters.OrganizationId parameters.OrganizationName parameters.RepositoryId parameters.RepositoryName parameters.BranchId parameters.BranchName ReferenceType.Promotion PromotionIsDisabled |]

let command (parameters: CreateReferenceParameters) =
BranchCommand.Promote(parameters.DirectoryId, parameters.Sha256Hash, ReferenceText parameters.Message) |> returnTask
Promote(parameters.DirectoryId, parameters.Sha256Hash, ReferenceText parameters.Message) |> returnTask

context.Items.Add("Command", nameof(Promote))
return! processCommand context validations command
Expand Down
6 changes: 4 additions & 2 deletions src/Grace.Server/Grace.Server.fsproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
<LangVersion>preview</LangVersion>
<PublishReadyToRun>false</PublishReadyToRun>
<Version>0.1</Version>
<Description>The server module for Grace Version Control System.</Description>
<UserSecretsId>f1167a88-7f15-49c3-8ea1-30c2608081c9</UserSecretsId>
Expand All @@ -17,13 +17,15 @@
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
<NoWarn>NETSDK1057</NoWarn>
<Platforms>AnyCPU;x64</Platforms>
<UseAppHost>false</UseAppHost>
<PackageProjectUrl>https://github.com/ScottArbeit/Grace</PackageProjectUrl>
<OtherFlags>--test:GraphBasedChecking</OtherFlags>
<OtherFlags>--test:ParallelOptimization</OtherFlags>
<OtherFlags>--test:ParallelIlxGen</OtherFlags>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<ContainerPort Include="5000" Type="tcp" />
<ContainerPort Include="5001" Type="tcp" />
Expand Down
15 changes: 7 additions & 8 deletions src/Grace.Server/Middleware/ValidateIds.Middleware.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@ type ValidateIdsMiddleware(next: RequestDelegate) =
/// Gets the parameter type for the endpoint from the endpoint metadata created in Startup.Server.fs.
let getBodyType (context: HttpContext) =
let path = context.Request.Path.ToString()
if not <| path.StartsWith("/healthz") && not <| path.StartsWith("/actors") && not <| path.StartsWith("/dapr") then
if not <| path.StartsWith("/healthz") && not <| path.StartsWith("/actors") && not <| path.StartsWith("/dapr") && not <| path.StartsWith("/notifications") then
let endpoint = context.GetEndpoint()
if isNull(endpoint) then
log.LogDebug("{currentInstant}: Path: {context.Request.Path}; Endpoint: null.", getCurrentInstantExtended(), context.Request.Path)
None
elif endpoint.Metadata.Count > 0 then
//logToConsole $"Path: {context.Request.Path}; endpoint.Metadata.Count: {endpoint.Metadata.Count}."
//endpoint.Metadata |> Seq.iter (fun m -> logToConsole (sprintf "%A: %A" m (m.GetType())))
let requestBodyType = endpoint.Metadata
|> Seq.tryFind (fun m -> m.GetType().FullName = "System.RuntimeType")
|> Option.map (fun m -> m :?> Type)
if requestBodyType |> Option.isSome then log.LogDebug("{currentInstant}: Path: {context.Request.Path}; Endpoint: {endpoint.DisplayName}; RequestBodyType: {requestBodyType.Value.Name}.", getCurrentInstantExtended(), context.Request.Path, endpoint.DisplayName, requestBodyType.Value.Name)
|> Seq.tryFind (fun metadataItem -> metadataItem.GetType().FullName = "System.RuntimeType") // The types that we add in Startup.Server.fs show up here as "System.RuntimeType".
|> Option.map (fun metadataItem -> metadataItem :?> Type) // Convert the metadata item to a Type.
if requestBodyType |> Option.isSome then
log.LogDebug("{currentInstant}: Path: {context.Request.Path}; Endpoint: {endpoint.DisplayName}; RequestBodyType: {requestBodyType.Value.Name}.", getCurrentInstantExtended(), context.Request.Path, endpoint.DisplayName, requestBodyType.Value.Name)
requestBodyType
else
log.LogDebug("{currentInstant}: Path: {context.Request.Path}; endpoint.Metadata.Count = 0.", getCurrentInstantExtended(), context.Request.Path)
Expand Down Expand Up @@ -115,7 +114,7 @@ type ValidateIdsMiddleware(next: RequestDelegate) =
typeLookup.TryAdd(path, null) |> ignore

// If we have a parameter type for the endpoint, parse the body of the request to get the Ids and Names.
// If we don't have a parameter type for the endpoint, it's an endpoint like /healthz or whatever.
// If the parameter type is null, it's an endpoint like /healthz where we don't take these parameters.
if not <| isNull(requestBodyType) then
context.Request.EnableBuffering()
match! context |> parseType requestBodyType with
Expand Down Expand Up @@ -265,7 +264,7 @@ type ValidateIdsMiddleware(next: RequestDelegate) =
context.Request.Headers["X-MiddlewareTraceOut"] <- $"{middlewareTraceOutHeader}{nameof(ValidateIdsMiddleware)} --> ";

let elapsed = getCurrentInstant().Minus(startTime).TotalMilliseconds
if not <| path.StartsWith("/healthz") && not <| path.StartsWith("/actors") && not <| path.StartsWith("/dapr") then
if not <| path.StartsWith("/healthz") && not <| path.StartsWith("/actors") && not <| path.StartsWith("/dapr") && not <| path.StartsWith("/notifications") then
log.LogDebug("{currentInstant}: Path: {path}; Elapsed: {elapsed}ms; Status code: {statusCode}; graceIds: {graceIds}",
getCurrentInstantExtended(), context.Request.Path, elapsed, context.Response.StatusCode, serialize graceIds)
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.Server/Owner.Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Owner =
let processCommand<'T when 'T :> OwnerParameters> (context: HttpContext) (validations: Validations<'T>) (command: 'T -> Task<OwnerCommand>) =
task {
try
let commandName = context.Items["Command"] :?> string
let commandName = context.Items["Command"] :?> string
use activity = activitySource.StartActivity("processCommand", ActivityKind.Server)
let! parameters = context |> parse<'T>

Expand Down
3 changes: 2 additions & 1 deletion src/Grace.Server/Repository.Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ module Repository =

let query (context: HttpContext) (maxCount: int) (actorProxy: IRepositoryActor) =
task {
let repositoryId = RepositoryId(context.Items[nameof(RepositoryId)] :?> String)
let graceIds = context.Items[nameof(GraceIds)] :?> GraceIds
let repositoryId = Guid.Parse(graceIds.RepositoryId)
let includeDeleted = context.Items["IncludeDeleted"] :?> bool
return! getBranches repositoryId maxCount includeDeleted
}
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.Server/Services.Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Services =
.AddTag("http.status_code", statusCode) |> ignore
context.SetStatusCode(statusCode)

log.LogDebug("{currentInstant}: In returnResult: StatusCode: {statusCode}; result: {result}", getCurrentInstantExtended(), statusCode, serialize result)
//log.LogDebug("{currentInstant}: In returnResult: StatusCode: {statusCode}; result: {result}", getCurrentInstantExtended(), statusCode, serialize result)

// .WriteJsonAsync() uses Grace's Constants.JsonSerializerOptions through DI.
return! context.WriteJsonAsync(result)
Expand Down
3 changes: 3 additions & 0 deletions src/Grace.Shared/Utilities.Shared.fs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ module Utilities =
}).Unwrap()

/// Custom monadic bind operator for the nested monad Task<Result<'T, 'TError>>.
//let inline (>>=!) (result: Task<Result<'T, 'TError>>) (f: 'T -> Task<Result<'U, 'TError>>) =
// bindTaskResult result f

let inline (>>=!) (result: Task<Result<'T, 'TError>>) (f: 'T -> Task<Result<'U, 'TError>>) =
bindTaskResult result f

Expand Down

0 comments on commit 1221353

Please sign in to comment.