Skip to content

Commit 7c1eecb

Browse files
committed
Tightened Unit semantics
1 parent 79d5c09 commit 7c1eecb

File tree

7 files changed

+86
-70
lines changed

7 files changed

+86
-70
lines changed

Core/Cleipnir.ResilientFunctions/CoreRuntime/Invocation/InvocationHelper.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Cleipnir.ResilientFunctions.CoreRuntime.ParameterSerialization;
55
using Cleipnir.ResilientFunctions.Domain;
66
using Cleipnir.ResilientFunctions.Domain.Exceptions;
7+
using Cleipnir.ResilientFunctions.Helpers;
78
using Cleipnir.ResilientFunctions.Messaging;
89
using Cleipnir.ResilientFunctions.Storage;
910

@@ -39,9 +40,7 @@ public async Task<Tuple<bool, IDisposable>> PersistFunctionInStore(
3940
var runningFunction = _shutdownCoordinator.RegisterRunningRFunc();
4041
try
4142
{
42-
var storedParameter = param is null
43-
? null
44-
: Serializer.SerializeParameter(param);
43+
var storedParameter = SerializeParameter(param);
4544

4645
var utcNowTicks = DateTime.UtcNow.Ticks;
4746
var created = await _functionStore.CreateFunction(
@@ -110,7 +109,7 @@ public async Task PersistFailure(FunctionId functionId, Exception exception, TPa
110109
timestamp: DateTime.UtcNow.Ticks,
111110
expectedEpoch,
112111
complimentaryState: new ComplimentaryState(
113-
() => serializer.SerializeParameter(param),
112+
() => SerializeParameter(param),
114113
_settings.LeaseLength.Ticks
115114
)
116115
);
@@ -126,17 +125,15 @@ public async Task<PersistResultOutcome> PersistResult(
126125
int expectedEpoch)
127126
{
128127
var complementaryState = new ComplimentaryState(
129-
() => Serializer.SerializeParameter(param),
128+
() => SerializeParameter(param),
130129
_settings.LeaseLength.Ticks
131130
);
132131
switch (result.Outcome)
133132
{
134133
case Outcome.Succeed:
135134
return await _functionStore.SucceedFunction(
136135
functionId,
137-
result: result.SucceedWithValue == null
138-
? null
139-
: Serializer.SerializeResult(result.SucceedWithValue),
136+
result: SerializeResult(result.SucceedWithValue),
140137
defaultState,
141138
timestamp: DateTime.UtcNow.Ticks,
142139
expectedEpoch,
@@ -276,8 +273,8 @@ int expectedEpoch
276273
return await _functionStore.SetFunctionState(
277274
functionId,
278275
status,
279-
param: serializer.SerializeParameter(param),
280-
result: result == null ? null : serializer.SerializeResult(result),
276+
param: SerializeParameter(param),
277+
result: SerializeResult(result),
281278
exception == null ? null : serializer.SerializeException(exception),
282279
postponeUntil?.Ticks,
283280
expectedEpoch
@@ -290,11 +287,10 @@ public async Task<bool> SaveControlPanelChanges(
290287
TReturn? @return,
291288
int expectedEpoch)
292289
{
293-
var serializer = _settings.Serializer;
294290
return await _functionStore.SetParameters(
295291
functionId,
296-
param: serializer.SerializeParameter(param),
297-
result: serializer.SerializeResult(@return),
292+
param: SerializeParameter(param),
293+
result: SerializeResult(@return),
298294
expectedEpoch
299295
);
300296
}
@@ -428,4 +424,24 @@ public async Task<ExistingTimeouts> GetExistingTimeouts(FunctionId functionId)
428424
_functionStore.TimeoutStore,
429425
await _functionStore.TimeoutStore.GetTimeouts(functionId)
430426
);
427+
428+
private string? SerializeParameter(TParam param)
429+
{
430+
if (typeof(TParam) == typeof(Unit))
431+
return null;
432+
433+
return param is null
434+
? null
435+
: Serializer.SerializeParameter(param);
436+
}
437+
438+
private string? SerializeResult(TReturn? result)
439+
{
440+
if (typeof(TReturn) == typeof(Unit))
441+
return null;
442+
443+
return result is null
444+
? null
445+
: Serializer.SerializeResult(result);
446+
}
431447
}

Core/Cleipnir.ResilientFunctions/Domain/ControlPanel.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@
66

77
namespace Cleipnir.ResilientFunctions.Domain;
88

9-
public class ControlPanel : BaseControlPanel<Unit?, Unit?>
9+
public class ControlPanel : BaseControlPanel<Unit, Unit>
1010
{
1111
internal ControlPanel(
12-
Invoker<Unit?, Unit?> invoker,
13-
InvocationHelper<Unit?, Unit?> invocationHelper,
12+
Invoker<Unit, Unit> invoker,
13+
InvocationHelper<Unit, Unit> invocationHelper,
1414
FunctionId functionId,
1515
Status status, int epoch, long leaseExpiration,
1616
DateTime? postponedUntil, ExistingEffects effects,
1717
ExistingStates states, ExistingMessages messages, ExistingTimeouts timeouts,
1818
PreviouslyThrownException? previouslyThrownException
1919
) : base(
2020
invoker, invocationHelper, functionId, status, epoch,
21-
leaseExpiration, innerParam: null, innerResult: null, postponedUntil,
21+
leaseExpiration, innerParam: Unit.Instance, innerResult: Unit.Instance, postponedUntil,
2222
effects, states, messages, timeouts, previouslyThrownException
2323
) { }
2424

25-
public Task Succeed() => InnerSucceed(result: null);
25+
public Task Succeed() => InnerSucceed(result: Unit.Instance);
2626
}
2727

28-
public class ControlPanel<TParam> : BaseControlPanel<TParam, Unit?> where TParam : notnull
28+
public class ControlPanel<TParam> : BaseControlPanel<TParam, Unit> where TParam : notnull
2929
{
3030
internal ControlPanel(
31-
Invoker<TParam, Unit?> invoker,
32-
InvocationHelper<TParam, Unit?> invocationHelper,
31+
Invoker<TParam, Unit> invoker,
32+
InvocationHelper<TParam, Unit> invocationHelper,
3333
FunctionId functionId,
3434
Status status, int epoch, long leaseExpiration, TParam innerParam,
3535
DateTime? postponedUntil, ExistingEffects effects,
3636
ExistingStates states, ExistingMessages messages, ExistingTimeouts timeouts,
3737
PreviouslyThrownException? previouslyThrownException
3838
) : base(
3939
invoker, invocationHelper, functionId, status, epoch,
40-
leaseExpiration, innerParam, innerResult: null, postponedUntil,
40+
leaseExpiration, innerParam, innerResult: Unit.Instance, postponedUntil,
4141
effects, states, messages, timeouts, previouslyThrownException
4242
) { }
4343

@@ -47,7 +47,7 @@ public TParam Param
4747
set => InnerParam = value;
4848
}
4949

50-
public Task Succeed() => InnerSucceed(result: null);
50+
public Task Succeed() => InnerSucceed(result: Unit.Instance);
5151
}
5252

5353
public class ControlPanel<TParam, TReturn> : BaseControlPanel<TParam, TReturn> where TParam : notnull

Core/Cleipnir.ResilientFunctions/Domain/ControlPanelFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace Cleipnir.ResilientFunctions.Domain;
77
public class ControlPanelFactory
88
{
99
private readonly FunctionTypeId _functionTypeId;
10-
private readonly Invoker<Unit?, Unit?> _invoker;
11-
private readonly InvocationHelper<Unit?, Unit?> _invocationHelper;
10+
private readonly Invoker<Unit, Unit> _invoker;
11+
private readonly InvocationHelper<Unit, Unit> _invocationHelper;
1212

13-
internal ControlPanelFactory(FunctionTypeId functionTypeId, Invoker<Unit?, Unit?> invoker, InvocationHelper<Unit?, Unit?> invocationHelper)
13+
internal ControlPanelFactory(FunctionTypeId functionTypeId, Invoker<Unit, Unit> invoker, InvocationHelper<Unit, Unit> invocationHelper)
1414
{
1515
_invoker = invoker;
1616
_invocationHelper = invocationHelper;
@@ -45,10 +45,10 @@ await _invocationHelper.GetExistingTimeouts(functionId),
4545
public class ControlPanelFactory<TParam> where TParam : notnull
4646
{
4747
private readonly FunctionTypeId _functionTypeId;
48-
private readonly Invoker<TParam, Unit?> _invoker;
49-
private readonly InvocationHelper<TParam, Unit?> _invocationHelper;
48+
private readonly Invoker<TParam, Unit> _invoker;
49+
private readonly InvocationHelper<TParam, Unit> _invocationHelper;
5050

51-
internal ControlPanelFactory(FunctionTypeId functionTypeId, Invoker<TParam, Unit?> invoker, InvocationHelper<TParam, Unit?> invocationHelper)
51+
internal ControlPanelFactory(FunctionTypeId functionTypeId, Invoker<TParam, Unit> invoker, InvocationHelper<TParam, Unit> invocationHelper)
5252
{
5353
_invoker = invoker;
5454
_invocationHelper = invocationHelper;

Core/Cleipnir.ResilientFunctions/Domain/Result.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ private Result(bool succeed, Postpone? postpone, Exception? fail, Suspend? suspe
3939
public static implicit operator Result(Suspend suspend) => new Result(suspend);
4040
public static Result<T> SucceedWithValue<T>(T value) => new(value);
4141

42-
public Result<Unit?> ToUnit() => Outcome switch
42+
public Result<Unit> ToUnit() => Outcome switch
4343
{
44-
Outcome.Succeed => new Result<Unit?>(succeedWithValue: default),
45-
Outcome.Postpone => new Result<Unit?>(Postpone!),
46-
Outcome.Fail => new Result<Unit?>(Fail!),
47-
Outcome.Suspend => new Result<Unit?>(Suspend!),
44+
Outcome.Succeed => new Result<Unit>(Unit.Instance),
45+
Outcome.Postpone => new Result<Unit>(Postpone!),
46+
Outcome.Fail => new Result<Unit>(Fail!),
47+
Outcome.Suspend => new Result<Unit>(Suspend!),
4848
_ => throw new ArgumentOutOfRangeException()
4949
};
5050
}

Core/Cleipnir.ResilientFunctions/FunctionsRegistry.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public FuncRegistration<TParam, TReturn> RegisterFunc<TParam, TReturn>(
307307

308308
private ParamlessRegistration RegisterParamless(
309309
FunctionTypeId functionTypeId,
310-
Func<Unit?, Workflow, Task<Result<Unit?>>> inner,
310+
Func<Unit, Workflow, Task<Result<Unit>>> inner,
311311
Settings? settings = null
312312
)
313313
{
@@ -320,8 +320,8 @@ private ParamlessRegistration RegisterParamless(
320320
return (ParamlessRegistration)_functions[functionTypeId];
321321

322322
var settingsWithDefaults = _settings.Merge(settings);
323-
var invocationHelper = new InvocationHelper<Unit?, Unit?>(isNullParamAllowed: true, settingsWithDefaults, _functionStore, _shutdownCoordinator);
324-
var invoker = new Invoker<Unit?, Unit?>(
323+
var invocationHelper = new InvocationHelper<Unit, Unit>(isNullParamAllowed: true, settingsWithDefaults, _functionStore, _shutdownCoordinator);
324+
var invoker = new Invoker<Unit, Unit>(
325325
functionTypeId,
326326
inner,
327327
invocationHelper,
@@ -345,9 +345,9 @@ private ParamlessRegistration RegisterParamless(
345345
);
346346
var registration = new ParamlessRegistration(
347347
functionTypeId,
348-
invoke: id => invoker.Invoke(id.Value, param: null),
349-
schedule: id => invoker.ScheduleInvoke(id.Value, param: null),
350-
scheduleAt: (id, at) => invoker.ScheduleAt(id.Value, param: null, at),
348+
invoke: id => invoker.Invoke(id.Value, param: Unit.Instance),
349+
schedule: id => invoker.ScheduleInvoke(id.Value, param: Unit.Instance),
350+
scheduleAt: (id, at) => invoker.ScheduleAt(id.Value, param: Unit.Instance, at),
351351
controlPanels,
352352
new MessageWriters(functionTypeId, _functionStore, settingsWithDefaults.Serializer, invoker.ScheduleReInvoke),
353353
new StateFetcher(_functionStore, settingsWithDefaults.Serializer)
@@ -360,7 +360,7 @@ private ParamlessRegistration RegisterParamless(
360360

361361
private ActionRegistration<TParam> RegisterAction<TParam>(
362362
FunctionTypeId functionTypeId,
363-
Func<TParam, Workflow, Task<Result<Unit?>>> inner,
363+
Func<TParam, Workflow, Task<Result<Unit>>> inner,
364364
Settings? settings = null
365365
) where TParam : notnull
366366
{
@@ -373,8 +373,8 @@ private ActionRegistration<TParam> RegisterAction<TParam>(
373373
return (ActionRegistration<TParam>)_functions[functionTypeId];
374374

375375
var settingsWithDefaults = _settings.Merge(settings);
376-
var invocationHelper = new InvocationHelper<TParam, Unit?>(isNullParamAllowed: false, settingsWithDefaults, _functionStore, _shutdownCoordinator);
377-
var rActionInvoker = new Invoker<TParam, Unit?>(
376+
var invocationHelper = new InvocationHelper<TParam, Unit>(isNullParamAllowed: false, settingsWithDefaults, _functionStore, _shutdownCoordinator);
377+
var rActionInvoker = new Invoker<TParam, Unit>(
378378
functionTypeId,
379379
inner,
380380
invocationHelper,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Cleipnir.ResilientFunctions.Helpers;
22

3-
public sealed class Unit
3+
public struct Unit
44
{
55
public static Unit Instance { get; } = new();
66
}

0 commit comments

Comments
 (0)