Skip to content

Commit

Permalink
Refactor and enhance DirectoryVersion handling
Browse files Browse the repository at this point in the history
Added correlationId handling in ReceiveReminderAsync methods across multiple modules. Improved robustness and error handling in DirectoryVersion.Actor.fs, including handling of ReminderTypes.DeleteCachedState and logging in GetRecursiveDirectoryVersions. Removed IRemindable interface from DirectoryVersion.Actor.fs. Introduced private correlationId property in FileAppearance.Actor.fs. Replaced Directory.SaveDirectoryVersions with DirectoryVersion.SaveDirectoryVersions in CLI modules. Updated launchSettings.json to change working directory. Improved comments and logging in Common.SDK.fs and Services.CLI.fs. Updated Grace.SDK.fsproj to replace Directory.SDK.fs with DirectoryVersion.SDK.fs. Removed commented-out code and improved logging for better readability and maintainability.

Commented out logging in Storage.SDK.fs. Added System.IO namespace and cast return type in DirectoryVersion.Server.fs. Commented out Kestrel server options in Program.Server.fs. Moved logic for graceIgnoreEntries and updated graceIgnoreFullPath in Configuration.Shared.fs. Changed GraceIgnoreFileName value in Constants.Shared.fs. Removed logging in normalizeFilePath function in Utilities.Shared.fs. Added new DirectoryVersion.SDK.fs with DirectoryVersion class and static methods.
  • Loading branch information
ScottArbeit committed Jan 17, 2025
1 parent e59c862 commit 93bc0b6
Show file tree
Hide file tree
Showing 26 changed files with 157 additions and 130 deletions.
2 changes: 2 additions & 0 deletions src/Grace.Actors/Branch.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ module Branch =

/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
this.correlationId <- reminder.CorrelationId

task {
match reminder.ReminderType with
| ReminderTypes.PhysicalDeletion ->
Expand Down
2 changes: 2 additions & 0 deletions src/Grace.Actors/Diff.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ module Diff =
/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
task {
this.correlationId <- reminder.CorrelationId

match reminder.ReminderType with
| ReminderTypes.DeleteCachedState ->
// Get values from state.
Expand Down
1 change: 1 addition & 0 deletions src/Grace.Actors/DirectoryAppearance.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module DirectoryAppearance =

/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
this.correlationId <- reminder.CorrelationId
logToConsole $"Received a reminder: {reminder}."
Ok() |> returnTask

Expand Down
96 changes: 45 additions & 51 deletions src/Grace.Actors/DirectoryVersion.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,39 @@ module DirectoryVersion =
/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
task {
this.correlationId <- reminder.CorrelationId

match reminder.ReminderType with
| ReminderTypes.DeleteCachedState ->
// Get values from state.
let (deleteReason, correlationId) = deserialize<PhysicalDeletionReminderState> reminder.State

this.correlationId <- correlationId

// Delete saved state for this actor.
let! deleted = Storage.DeleteState stateManager directoryVersionCacheStateName

if deleted then
log.LogInformation(
"{CurrentInstant}: Node: {HostName}; CorrelationId: {correlationId}; Deleted cache for directory version; RepositoryId: {RepositoryId}; DirectoryVersionId: {DirectoryVersionId}; deleteReason: {deleteReason}.",
getCurrentInstantExtended (),
getMachineName,
correlationId,
directoryVersionDto.DirectoryVersion.RepositoryId,
directoryVersionDto.DirectoryVersion.DirectoryVersionId,
deleteReason
)
else
log.LogWarning(
"{CurrentInstant}: Node: {HostName}; CorrelationId: {correlationId}; Failed to delete cache for directory version (it may have already been deleted); RepositoryId: {RepositoryId}; DirectoryVersionId: {DirectoryVersionId}; deleteReason: {deleteReason}.",
getCurrentInstantExtended (),
getMachineName,
correlationId,
directoryVersionDto.DirectoryVersion.RepositoryId,
directoryVersionDto.DirectoryVersion.DirectoryVersionId,
deleteReason
)
if not <| String.IsNullOrEmpty reminder.State then
let (deleteReason, correlationId) = deserialize<PhysicalDeletionReminderState> reminder.State

this.correlationId <- correlationId

// Delete saved state for this actor.
let! deleted = Storage.DeleteState stateManager directoryVersionCacheStateName

if deleted then
log.LogInformation(
"{CurrentInstant}: Node: {HostName}; CorrelationId: {correlationId}; Deleted cache for directory version; RepositoryId: {RepositoryId}; DirectoryVersionId: {DirectoryVersionId}; deleteReason: {deleteReason}.",
getCurrentInstantExtended (),
getMachineName,
correlationId,
directoryVersionDto.DirectoryVersion.RepositoryId,
directoryVersionDto.DirectoryVersion.DirectoryVersionId,
deleteReason
)
else
log.LogWarning(
"{CurrentInstant}: Node: {HostName}; CorrelationId: {correlationId}; Failed to delete cache for directory version (it may have already been deleted); RepositoryId: {RepositoryId}; DirectoryVersionId: {DirectoryVersionId}; deleteReason: {deleteReason}.",
getCurrentInstantExtended (),
getMachineName,
correlationId,
directoryVersionDto.DirectoryVersion.RepositoryId,
directoryVersionDto.DirectoryVersion.DirectoryVersionId,
deleteReason
)

// Mark the actor as disposed, in case someone tries to use it before Dapr GC's it.
isDisposed <- true
Expand Down Expand Up @@ -375,12 +378,21 @@ module DirectoryVersion =
(fun directoryId ct ->
ValueTask(
task {
let subdirectoryActor = DirectoryVersion.CreateActorProxy directoryId correlationId

let! subdirectoryContents = subdirectoryActor.GetRecursiveDirectoryVersions forceRegenerate correlationId

for directoryVersion in subdirectoryContents do
subdirectoryVersions.Enqueue(directoryVersion)
try
let subdirectoryActor = DirectoryVersion.CreateActorProxy directoryId correlationId

let! subdirectoryContents = subdirectoryActor.GetRecursiveDirectoryVersions forceRegenerate correlationId

for directoryVersion in subdirectoryContents do
subdirectoryVersions.Enqueue(directoryVersion)
with ex ->
log.LogError(
"{CurrentInstant}: Error in {methodName}; DirectoryId: {directoryId}; Exception: {exception}",
getCurrentInstantExtended (),
"GetRecursiveDirectoryVersions",
directoryId,
ExceptionResponse.Create ex
)
}
))
)
Expand All @@ -389,7 +401,6 @@ module DirectoryVersion =
subdirectoryVersions.ToArray()
|> Array.sortBy (fun directoryVersion -> directoryVersion.RelativePath)

logToConsole $"In DirectoryVersionActor.GetDirectoryVersionsRecursive({this.Id}); Storing subdirectoryVersion list."
do! Storage.SaveState stateManager directoryVersionCacheStateName subdirectoryVersionsList this.correlationId

log.LogDebug("In DirectoryVersionActor.GetDirectoryVersionsRecursive({id}); Storing subdirectoryVersion list.", this.Id)
Expand Down Expand Up @@ -506,20 +517,3 @@ module DirectoryVersion =
else
return directoryVersionDto.RecursiveSize
}

interface IRemindable with
member this.ReceiveReminderAsync(reminderName, state, dueTime, period) =
match reminderName with
| ReminderType.DeleteCachedState ->
task {
try
let deleteReason = fromByteArray<PhysicalDeletionReminderState> state
this.correlationId <- this.correlationId
with ex ->
()

let! deleteSucceeded = Storage.DeleteState stateManager directoryVersionCacheStateName
()
}
:> Task
| _ -> Task.CompletedTask
3 changes: 3 additions & 0 deletions src/Grace.Actors/FileAppearance.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ module FileAppearance =
let dtoStateName = StateName.FileAppearance
let mutable dto = FileAppearanceDto()

member val private correlationId: CorrelationId = String.Empty with get, set

override this.OnActivateAsync() =
stateManager <- this.StateManager

Expand All @@ -71,6 +73,7 @@ module FileAppearance =

/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
this.correlationId <- reminder.CorrelationId
logToConsole $"Received a reminder: {reminder}."
Ok() |> returnTask

Expand Down
2 changes: 2 additions & 0 deletions src/Grace.Actors/Organization.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ module Organization =
/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
task {
this.correlationId <- reminder.CorrelationId

match reminder.ReminderType with
| ReminderTypes.PhysicalDeletion ->
// Get values from state.
Expand Down
2 changes: 2 additions & 0 deletions src/Grace.Actors/Owner.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ module Owner =
/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
task {
this.correlationId <- reminder.CorrelationId

match reminder.ReminderType with
| ReminderTypes.PhysicalDeletion ->
// Get values from state.
Expand Down
2 changes: 2 additions & 0 deletions src/Grace.Actors/Reference.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ module Reference =
/// Receives a Grace reminder.
member this.ReceiveReminderAsync(reminder: ReminderDto) : Task<Result<unit, GraceError>> =
task {
this.correlationId <- reminder.CorrelationId

match reminder.ReminderType with
| ReminderTypes.PhysicalDeletion ->
// Get values from state.
Expand Down
9 changes: 5 additions & 4 deletions src/Grace.Actors/Services.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,10 +1300,11 @@ module Services =
| Reference.ReferenceEventType.Created refDto -> refDto
| _ -> ReferenceDto.Default

Activity.Current
.SetTag("indexMetrics", $"{indexMetrics.Remove(indexMetrics.Length - 2, 2)}")
.SetTag("requestCharge", $"{requestCharge.Remove(requestCharge.Length - 2, 2)}")
|> ignore
if (indexMetrics.Length >= 2) && (requestCharge.Length >= 2) then
Activity.Current
.SetTag("indexMetrics", $"{indexMetrics.Remove(indexMetrics.Length - 2, 2)}")
.SetTag("requestCharge", $"{requestCharge.Remove(requestCharge.Length - 2, 2)}")
|> ignore

if referenceDto.ReferenceId <> ReferenceDto.Default.ReferenceId then
return Some referenceDto
Expand Down
16 changes: 8 additions & 8 deletions src/Grace.CLI/Command/Branch.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ module Branch =

saveParameters.CorrelationId <- getCorrelationId parseResult

let! uploadDirectoryVersions = Directory.SaveDirectoryVersions saveParameters
let! uploadDirectoryVersions = DirectoryVersion.SaveDirectoryVersions saveParameters

lastDirectoryVersionUpload <- getCurrentInstant ()

Expand Down Expand Up @@ -1007,7 +1007,7 @@ module Branch =

saveParameters.DirectoryVersions <- newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()

let! uploadDirectoryVersions = Directory.SaveDirectoryVersions saveParameters
let! uploadDirectoryVersions = DirectoryVersion.SaveDirectoryVersions saveParameters
let rootDirectoryVersion = getRootDirectoryVersion previousGraceStatus

let sdkParameters =
Expand Down Expand Up @@ -2031,9 +2031,9 @@ module Branch =

saveParameters.DirectoryVersions <- newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()

let! uploadDirectoryVersions = Directory.SaveDirectoryVersions saveParameters
let! uploadDirectoryVersions = DirectoryVersion.SaveDirectoryVersions saveParameters

match! Directory.SaveDirectoryVersions saveParameters with
match! DirectoryVersion.SaveDirectoryVersions saveParameters with
| Ok returnValue ->
t |> setProgressTaskValue showOutput 100.0

Expand Down Expand Up @@ -2247,7 +2247,7 @@ module Branch =
CorrelationId = parameters.CorrelationId
)

match! Directory.GetByDirectoryIds getByDirectoryIdParameters with
match! DirectoryVersion.GetByDirectoryIds getByDirectoryIdParameters with
| Ok returnValue ->
//logToAnsiConsole Colors.Verbose $"Succeeded calling Directory.GetByDirectoryIds."
// Create a new version of GraceStatus that includes the new DirectoryVersions.
Expand Down Expand Up @@ -2570,9 +2570,9 @@ module Branch =
)

// Get the directory versions for the parent promotion that we're rebasing on, and the latest reference.
let! d1 = Directory.GetDirectoryVersionsRecursive(getParentLatestPromotionDirectoryParameters)
let! d1 = DirectoryVersion.GetDirectoryVersionsRecursive(getParentLatestPromotionDirectoryParameters)

let! d2 = Directory.GetDirectoryVersionsRecursive(getLatestReferenceDirectoryParameters)
let! d2 = DirectoryVersion.GetDirectoryVersionsRecursive(getLatestReferenceDirectoryParameters)

let createFileVersionLookupDictionary (directoryVersions: IEnumerable<DirectoryVersion>) =
let lookup = Dictionary<RelativePath, LocalFileVersion>(StringComparer.OrdinalIgnoreCase)
Expand Down Expand Up @@ -2682,7 +2682,7 @@ module Branch =

saveParameters.DirectoryVersions <- newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()

match! Directory.SaveDirectoryVersions saveParameters with
match! DirectoryVersion.SaveDirectoryVersions saveParameters with
| Ok returnValue -> return Ok()
| Error error -> return Error error
else
Expand Down
4 changes: 2 additions & 2 deletions src/Grace.CLI/Command/Diff.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ module Diff =
saveDirectoryVersionsParameters.DirectoryVersions <-
newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()

match! Directory.SaveDirectoryVersions saveDirectoryVersionsParameters with
match! DirectoryVersion.SaveDirectoryVersions saveDirectoryVersionsParameters with
| Ok returnValue -> ()
| Error error -> logToAnsiConsole Colors.Error $"Failed to upload new directory versions. {error}"
})
Expand Down Expand Up @@ -701,7 +701,7 @@ module Diff =
saveDirectoryVersionsParameters.DirectoryVersions <-
newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()

match! Directory.SaveDirectoryVersions saveDirectoryVersionsParameters with
match! DirectoryVersion.SaveDirectoryVersions saveDirectoryVersionsParameters with
| Ok returnValue -> ()
| Error error -> logToAnsiConsole Colors.Error $"Failed to upload new directory versions. {error}"
})
Expand Down
Loading

0 comments on commit 93bc0b6

Please sign in to comment.