Skip to content

Commit

Permalink
Added caching to more server validations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottArbeit committed Dec 3, 2023
1 parent a36c037 commit be05ef6
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 154 deletions.
10 changes: 5 additions & 5 deletions src/Grace.Actors/Branch.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Branch =
inherit Actor(host)

let actorName = ActorName.Branch
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("Branch.Actor")
let dtoStateName = "branchDtoState"
let eventsStateName = "branchEventsState"

Expand Down Expand Up @@ -128,11 +128,11 @@ module Branch =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds).ToString("F3")
if String.IsNullOrEmpty(currentCommand) then
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
else
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down Expand Up @@ -179,7 +179,7 @@ module Branch =
}

member private this.SchedulePhysicalDeletion(deleteReason) =
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Wait()
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Result |> ignore

interface IBranchActor with
member this.Exists() =
Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/BranchName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module BranchName =

let actorName = Constants.ActorName.BranchName

let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("BranchName.Actor")

let mutable cachedBranchId: string option = None

Expand All @@ -31,8 +31,8 @@ module BranchName =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/Diff.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Diff =
let dtoStateName = "DiffDtoState"
let mutable diffDto: DiffDto = DiffDto.Default
let actorName = Constants.ActorName.Diff
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("Diff.Actor")
let mutable actorStartTime = Instant.MinValue
let mutable logScope: IDisposable = null

Expand Down Expand Up @@ -152,8 +152,8 @@ module Diff =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/DirectoryVersion.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module DirectoryVersion =
inherit Actor(host)

let actorName = ActorName.DirectoryVersion
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("DirectoryVersion.Actor")
let dtoStateName = "DirectoryVersionState"
let directoryVersionCacheStateName = "DirectoryVersionCacheState"

Expand Down Expand Up @@ -61,8 +61,8 @@ module DirectoryVersion =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down
10 changes: 5 additions & 5 deletions src/Grace.Actors/Organization.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Organization =
inherit Actor(host)

let actorName = ActorName.Organization
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("Organization.Actor")
let mutable actorStartTime = Instant.MinValue
let mutable logScope: IDisposable = null
let mutable currentCommand = String.Empty
Expand Down Expand Up @@ -100,11 +100,11 @@ module Organization =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds).ToString("F3")
if String.IsNullOrEmpty(currentCommand) then
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
else
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down Expand Up @@ -176,7 +176,7 @@ module Organization =
}

member private this.SchedulePhysicalDeletion(deleteReason) =
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Wait()
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Result |> ignore

interface IOrganizationActor with
member this.Exists() =
Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/OrganizationName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module OrganizationName =
inherit Actor(host)

let actorName = ActorName.OrganizationName
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("OrganizationName.Actor")

let mutable cachedOrganizationId: string option = None

Expand All @@ -32,8 +32,8 @@ module OrganizationName =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down
27 changes: 11 additions & 16 deletions src/Grace.Actors/Owner.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Owner =
inherit Actor(host)

let actorName = Constants.ActorName.Owner
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("Owner.Actor")
let mutable actorStartTime = Instant.MinValue
let mutable logScope: IDisposable = null
let mutable currentCommand = String.Empty
Expand Down Expand Up @@ -102,11 +102,11 @@ module Owner =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds).ToString("F3")
if String.IsNullOrEmpty(currentCommand) then
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
else
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down Expand Up @@ -179,23 +179,18 @@ module Owner =

/// Sets a Dapr Actor reminder to perform a physical deletion of this owner.
member private this.SchedulePhysicalDeletion(deleteReason) =
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Wait()
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Result |> ignore

interface IOwnerActor with
member this.Exists() =
Task.FromResult(if ownerDto.UpdatedAt.IsSome then true else false)
member this.Exists() = ownerDto.UpdatedAt.IsSome |> returnTask

member this.IsDeleted() =
Task.FromResult(if ownerDto.DeletedAt.IsSome then true else false)
member this.IsDeleted() = ownerDto.DeletedAt.IsSome |> returnTask

member this.Get() =
Task.FromResult(ownerDto)
member this.Get() = ownerDto |> returnTask

member this.OrganizationExists organizationName =
Task.FromResult(ownerDto.Organizations.ContainsValue(OrganizationName organizationName))
member this.OrganizationExists organizationName = ownerDto.Organizations.ContainsValue(OrganizationName organizationName) |> returnTask

member this.ListOrganizations() =
Task.FromResult(ownerDto.Organizations :> IReadOnlyDictionary<OrganizationId, OrganizationName>)
member this.ListOrganizations() = ownerDto.Organizations :> IReadOnlyDictionary<OrganizationId, OrganizationName> |> returnTask

member this.Handle command metadata =
let isValid command (metadata: EventMetadata) =
Expand Down Expand Up @@ -239,7 +234,7 @@ module Owner =
| Ok _ ->
this.SchedulePhysicalDeletion(deleteReason)
return Ok (LogicalDeleted (force, deleteReason))
| Error error -> return Error error
| Error error -> return Error error
| OwnerCommand.DeletePhysical ->
isDisposed <- true
return Ok (OwnerEventType.PhysicalDeleted)
Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/OwnerName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module OwnerName =
inherit Actor(host)

let actorName = ActorName.OwnerName
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("OwnerName.Actor")

let mutable cachedOwnerId: string option = None

Expand All @@ -33,8 +33,8 @@ module OwnerName =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down
7 changes: 5 additions & 2 deletions src/Grace.Actors/Reference.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ module Reference =
inherit Actor (host)

let actorName = ActorName.Reference
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("Reference.Actor")
let dtoStateName = "ReferenceDtoState"
let mutable referenceDto = None

override this.OnActivateAsync() =
let activateStartTime = getCurrentInstant()
let stateManager = this.StateManager
log.LogInformation($"{getCurrentInstantExtended()} Activated ReferenceActor {host.Id}.")
task {
let! retrievedDto = Storage.RetrieveState<ReferenceDto> stateManager dtoStateName
match retrievedDto with
| Some retrievedDto -> referenceDto <- Some retrievedDto
| None -> ()

let duration = getCurrentInstant().Minus(activateStartTime)
log.LogInformation("{CurrentInstant}: Activated {ActorType} {ActorId}. Retrieved from storage in {duration}ms.", getCurrentInstantExtended(), actorName, host.Id, duration.TotalMilliseconds.ToString("F3"))
} :> Task

interface IReferenceActor with
Expand Down
12 changes: 7 additions & 5 deletions src/Grace.Actors/Repository.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module Repository =
inherit Actor(host)

let actorName = ActorName.Repository
let log = host.LoggerFactory.CreateLogger(actorName)
let log = host.LoggerFactory.CreateLogger("Repository.Actor")
let mutable actorStartTime = Instant.MinValue
let mutable logScope: IDisposable = null
let mutable currentCommand = String.Empty
Expand Down Expand Up @@ -80,11 +80,11 @@ module Repository =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds).ToString("F3")
if String.IsNullOrEmpty(currentCommand) then
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
else
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, durationμs)
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, currentCommand, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down Expand Up @@ -250,7 +250,7 @@ module Repository =

/// Schedule an actor reminder to delete the repository from the database.
member private this.SchedulePhysicalDeletion(deleteReason) =
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Wait()
this.RegisterReminderAsync(ReminderType.PhysicalDeletion, convertToByteArray deleteReason, Constants.DefaultPhysicalDeletionReminderTime, TimeSpan.FromMilliseconds(-1)).Result |> ignore

interface IExportable<RepositoryEvent> with
member this.Export() =
Expand Down Expand Up @@ -398,6 +398,8 @@ module Repository =
// Get the list of branches that aren't already deleted.
let! branches = getBranches repositoryDto.RepositoryId Int32.MaxValue false

this.SchedulePhysicalDeletion(deleteReason)

// If any branches are not already deleted, and we're not forcing the deletion, then throw an exception.
if not <| force && branches.Count > 0 && branches.Any(fun branch -> branch.DeletedAt |> Option.isNone) then
//raise (ApplicationException($"{error}"))
Expand Down
4 changes: 2 additions & 2 deletions src/Grace.Actors/RepositoryName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module RepositoryName =
Task.CompletedTask

override this.OnPostActorMethodAsync(context) =
let durationμs = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}μs.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, durationμs)
let duration_ms = (getCurrentInstant().Minus(actorStartTime).TotalMilliseconds * 1000.0).ToString("F0")
log.LogInformation("{CurrentInstant}: Finished {ActorName}.{MethodName}; Id: {Id}; Duration: {duration}ms.", getCurrentInstantExtended(), actorName, context.MethodName, this.Id, duration_ms)
logScope.Dispose()
Task.CompletedTask

Expand Down
Loading

0 comments on commit be05ef6

Please sign in to comment.