Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add history info for workflow #123

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Temporalio/Worker/WorkflowInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ public WorkflowInstance(WorkflowInstanceDetails details)
/// <inheritdoc />
public CancellationToken CancellationToken => cancellationTokenSource.Token;

/// <inheritdoc />
public bool ContinueAsNewSuggested { get; private set; }

/// <inheritdoc />
public int CurrentHistoryLength { get; private set; }

/// <inheritdoc />
public int CurrentHistorySize { get; private set; }

/// <inheritdoc />
public WorkflowQueryDefinition? DynamicQuery
{
Expand Down Expand Up @@ -451,6 +460,9 @@ public WorkflowActivationCompletion Activate(WorkflowActivation act)
{
completion = new() { RunId = act.RunId, Successful = new() };
currentActivationException = null;
ContinueAsNewSuggested = act.ContinueAsNewSuggested;
CurrentHistoryLength = checked((int)act.HistoryLength);
CurrentHistorySize = checked((int)act.HistorySizeBytes);
IsReplaying = act.IsReplaying;
UtcNow = act.Timestamp.ToDateTime();

Expand Down
15 changes: 15 additions & 0 deletions src/Temporalio/Workflows/IWorkflowContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ internal interface IWorkflowContext
/// </summary>
CancellationToken CancellationToken { get; }

/// <summary>
/// Gets a value indicating whether <see cref="Workflow.ContinueAsNewSuggested" /> is true.
/// </summary>
bool ContinueAsNewSuggested { get; }

/// <summary>
/// Gets value for <see cref="Workflow.CurrentHistoryLength" />.
/// </summary>
int CurrentHistoryLength { get; }

/// <summary>
/// Gets value for <see cref="Workflow.CurrentHistorySize" />.
/// </summary>
int CurrentHistorySize { get; }

/// <summary>
/// Gets or sets value for <see cref="Workflow.DynamicQuery" />.
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions src/Temporalio/Workflows/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ public static class Workflow
/// </remarks>
public static CancellationToken CancellationToken => Context.CancellationToken;

/// <summary>
/// Gets a value indicating whether continue as new is suggested.
/// </summary>
/// <remarks>
/// This value is the current continue-as-new suggestion up until the current task. Note,
/// this value may not be up to date when accessed in a query. When continue as new is
/// suggested is based on server-side configuration.
/// </remarks>
public static bool ContinueAsNewSuggested => Context.ContinueAsNewSuggested;

/// <summary>
/// Gets the current number of events in history.
/// </summary>
/// <remarks>
/// This value is the current history event count up until the current task. Note, this
/// value may not be up to date when accessed in a query.
/// </remarks>
public static int CurrentHistoryLength => Context.CurrentHistoryLength;

/// <summary>
/// Gets the current history size in bytes.
/// </summary>
/// <remarks>
/// This value is the current history size up until the current task. Note, this value may
/// not be up to date when accessed in a query.
/// </remarks>
public static int CurrentHistorySize => Context.CurrentHistorySize;

/// <summary>
/// Gets or sets the current dynamic query handler. This can be null for no dynamic query
/// handling.
Expand Down
55 changes: 55 additions & 0 deletions tests/Temporalio.Tests/Worker/WorkflowWorkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,61 @@ await ExecuteWorkerAsync<InfoWorkflow>(async worker =>
});
}

public record HistoryInfo(int HistoryLength, int HistorySize, bool ContinueAsNewSuggested);

[Workflow]
public class HistoryInfoWorkflow
{
// Just wait forever
[WorkflowRun]
public Task RunAsync() => Workflow.WaitConditionAsync(() => false);

[WorkflowSignal]
public async Task BunchOfEventsAsync(int count)
{
// Create a lot of one-day timers
for (var i = 0; i < count; i++)
{
_ = Workflow.DelayAsync(TimeSpan.FromDays(1));
}
}

[WorkflowQuery]
public HistoryInfo HistoryInfo => new(
HistoryLength: Workflow.CurrentHistoryLength,
HistorySize: Workflow.CurrentHistorySize,
ContinueAsNewSuggested: Workflow.ContinueAsNewSuggested);
}

[Fact]
public async Task ExecuteWorkflowAsync_HistoryInfo_IsAccurate()
{
await ExecuteWorkerAsync<HistoryInfoWorkflow>(async worker =>
{
var handle = await Env.Client.StartWorkflowAsync(
(HistoryInfoWorkflow wf) => wf.RunAsync(),
new(id: $"workflow-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!));
// Issue query before anything else, which should mean only a history size of 3, at
// least 100 bytes of history, and no continue as new suggestion
var origInfo = await handle.QueryAsync(wf => wf.HistoryInfo);
Assert.Equal(3, origInfo.HistoryLength);
Assert.True(origInfo.HistorySize > 100);
Assert.False(origInfo.ContinueAsNewSuggested);

// Now send a lot of events
await handle.SignalAsync(wf =>
wf.BunchOfEventsAsync(WorkflowEnvironment.ContinueAsNewSuggestedHistoryCount));
// Send one more event to trigger the WFT update. We have to do this because just a
// query will have a stale representation of history counts, but signal forces a new
// WFT.
await handle.SignalAsync(wf => wf.BunchOfEventsAsync(1));
var newInfo = await handle.QueryAsync(wf => wf.HistoryInfo);
Assert.True(newInfo.HistoryLength > WorkflowEnvironment.ContinueAsNewSuggestedHistoryCount);
Assert.True(newInfo.HistorySize > origInfo.HistorySize);
Assert.True(newInfo.ContinueAsNewSuggested);
});
}

[Workflow]
public class MultiParamWorkflow
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Temporalio.Tests/WorkflowEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Temporalio.Tests;

public class WorkflowEnvironment : IAsyncLifetime
{
public const int ContinueAsNewSuggestedHistoryCount = 50;

private readonly Lazy<KitchenSinkWorker> kitchenSinkWorker;
private Temporalio.Testing.WorkflowEnvironment? env;

Expand Down Expand Up @@ -68,6 +70,8 @@ public async Task InitializeAsync()
"frontend.workerVersioningWorkflowAPIs=true",
"--dynamic-config-value",
"worker.buildIdScavengerEnabled=true",
"--dynamic-config-value",
$"limit.historyCount.suggestContinueAsNew={ContinueAsNewSuggestedHistoryCount}",
},
},
});
Expand Down