2018-12-19 20:50:56 +01:00
|
|
|
|
using System;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
2019-05-02 13:26:59 +02:00
|
|
|
|
using System.Diagnostics;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
using Tapeti.Flow.FlowHelpers;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-02-05 23:22:34 +01:00
|
|
|
|
namespace Tapeti.Flow.Default
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
|
|
|
|
public class FlowStore : IFlowStore
|
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
private readonly ConcurrentDictionary<Guid, FlowState> flowStates = new ConcurrentDictionary<Guid, FlowState>();
|
|
|
|
|
private readonly ConcurrentDictionary<Guid, Guid> continuationLookup = new ConcurrentDictionary<Guid, Guid>();
|
|
|
|
|
private readonly LockCollection<Guid> locks = new LockCollection<Guid>(EqualityComparer<Guid>.Default);
|
2017-02-05 23:22:34 +01:00
|
|
|
|
|
2017-08-14 13:58:01 +02:00
|
|
|
|
private readonly IFlowRepository repository;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
private volatile bool inUse;
|
2019-05-02 13:26:59 +02:00
|
|
|
|
private volatile bool loaded;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-08-14 13:58:01 +02:00
|
|
|
|
public FlowStore(IFlowRepository repository)
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Load()
|
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
if (inUse)
|
2017-10-17 10:34:07 +02:00
|
|
|
|
throw new InvalidOperationException("Can only load the saved state once.");
|
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
inUse = true;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
flowStates.Clear();
|
|
|
|
|
continuationLookup.Clear();
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-08-14 13:58:01 +02:00
|
|
|
|
foreach (var flowStateRecord in await repository.GetStates<FlowState>())
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
flowStates.TryAdd(flowStateRecord.Key, flowStateRecord.Value);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-07-27 15:55:37 +02:00
|
|
|
|
foreach (var continuation in flowStateRecord.Value.Continuations)
|
2018-12-19 20:50:56 +01:00
|
|
|
|
continuationLookup.GetOrAdd(continuation.Key, flowStateRecord.Key);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
2019-05-02 13:26:59 +02:00
|
|
|
|
|
|
|
|
|
loaded = true;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public Task<Guid?> FindFlowID(Guid continuationID)
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
2019-05-02 13:26:59 +02:00
|
|
|
|
if (!loaded)
|
|
|
|
|
throw new InvalidOperationException("Flow store is not yet loaded.");
|
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
return Task.FromResult(continuationLookup.TryGetValue(continuationID, out var result) ? result : (Guid?)null);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public async Task<IFlowStateLock> LockFlowState(Guid flowID)
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
2019-05-02 13:32:03 +02:00
|
|
|
|
if (!loaded)
|
2019-05-02 13:26:59 +02:00
|
|
|
|
throw new InvalidOperationException("Flow store should be loaded before storing flows.");
|
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
inUse = true;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
var flowStatelock = new FlowStateLock(this, flowID, await locks.GetLock(flowID));
|
2017-10-17 10:34:07 +02:00
|
|
|
|
return flowStatelock;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class FlowStateLock : IFlowStateLock
|
|
|
|
|
{
|
|
|
|
|
private readonly FlowStore owner;
|
|
|
|
|
private readonly Guid flowID;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
private volatile IDisposable flowLock;
|
|
|
|
|
private FlowState flowState;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
public FlowStateLock(FlowStore owner, Guid flowID, IDisposable flowLock)
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
|
|
|
|
this.owner = owner;
|
|
|
|
|
this.flowID = flowID;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
this.flowLock = flowLock;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
owner.flowStates.TryGetValue(flowID, out flowState);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2017-10-17 10:34:07 +02:00
|
|
|
|
var l = flowLock;
|
|
|
|
|
flowLock = null;
|
|
|
|
|
l?.Dispose();
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public Guid FlowID => flowID;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
|
|
|
|
public Task<FlowState> GetFlowState()
|
|
|
|
|
{
|
2017-10-17 10:34:07 +02:00
|
|
|
|
if (flowLock == null)
|
|
|
|
|
throw new ObjectDisposedException("FlowStateLock");
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
return Task.FromResult(flowState?.Clone());
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task StoreFlowState(FlowState newFlowState)
|
|
|
|
|
{
|
2017-10-17 10:34:07 +02:00
|
|
|
|
if (flowLock == null)
|
|
|
|
|
throw new ObjectDisposedException("FlowStateLock");
|
|
|
|
|
|
|
|
|
|
// Ensure no one has a direct reference to the protected state in the dictionary
|
|
|
|
|
newFlowState = newFlowState.Clone();
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
// Update the lookup dictionary for the ContinuationIDs
|
|
|
|
|
if (flowState != null)
|
|
|
|
|
{
|
2017-02-05 23:22:34 +01:00
|
|
|
|
foreach (var removedContinuation in flowState.Continuations.Keys.Where(k => !newFlowState.Continuations.ContainsKey(k)))
|
2018-12-19 20:50:56 +01:00
|
|
|
|
owner.continuationLookup.TryRemove(removedContinuation, out _);
|
2017-10-17 10:34:07 +02:00
|
|
|
|
}
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
foreach (var addedContinuation in newFlowState.Continuations.Where(c => flowState == null || !flowState.Continuations.ContainsKey(c.Key)))
|
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
owner.continuationLookup.TryAdd(addedContinuation.Key, flowID);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
2017-02-05 23:22:34 +01:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
var isNew = flowState == null;
|
|
|
|
|
flowState = newFlowState;
|
2018-12-19 20:50:56 +01:00
|
|
|
|
owner.flowStates[flowID] = newFlowState;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
|
|
|
|
|
// Storing the flowstate in the underlying repository
|
2017-01-31 12:01:08 +01:00
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2017-07-27 15:55:37 +02:00
|
|
|
|
await owner.repository.CreateState(flowID, flowState, now);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-27 15:55:37 +02:00
|
|
|
|
await owner.repository.UpdateState(flowID, flowState);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFlowState()
|
|
|
|
|
{
|
2017-10-17 10:34:07 +02:00
|
|
|
|
if (flowLock == null)
|
|
|
|
|
throw new ObjectDisposedException("FlowStateLock");
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
if (flowState != null)
|
|
|
|
|
{
|
2017-01-31 12:01:08 +01:00
|
|
|
|
foreach (var removedContinuation in flowState.Continuations.Keys)
|
2018-12-19 20:50:56 +01:00
|
|
|
|
owner.continuationLookup.TryRemove(removedContinuation, out _);
|
2017-02-05 23:22:34 +01:00
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
owner.flowStates.TryRemove(flowID, out _);
|
2017-02-05 23:22:34 +01:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
if (flowState != null)
|
|
|
|
|
{
|
|
|
|
|
flowState = null;
|
|
|
|
|
await owner.repository.DeleteState(flowID);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
2017-02-05 23:22:34 +01:00
|
|
|
|
}
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|