Pluggable Session Storage
Pluggable session storage allows customizing the persistence of a session state and messages completely.
For example, it could be used to implement High-Availability (HA) Solutions, one can implement a Pluggable Session Storage which stores data to shareable storage and if one server, with the FIX Engine, fails and another reserve server stands up then the FIX Engine can restore session states from the same shareable storage.
Switching to Pluggable Session Storage
To create such a session, it is needed to implement the ISessionStorage interface and use one of Session's constructors that accepts this interface as the parameter.
For example:
private class MySessionStorage : ISessionStorage
{
public void Clear()
{
outboundMessages.Clear();
}
public void Close(bool keepSequenceNumbersBetweenFixConnections, bool doBackup)
{
}
public string Id => "MyStorage";
public int InSeqNum { get; set; }
public int OutSeqNum { get; set; }
private readonly Dictionary<int, IMessage> outboundMessages = new Dictionary<int, IMessage>();
public MessageMode MessageMode { get; set; }
public DateTime SessionCreationTime { get; set; }
public void Flush()
{
}
public IList<IMessage> GetOutboundMessages(int beginSequenceNumber, int endSequenceNumber)
{
return outboundMessages.Where((pair) => pair.Key >= beginSequenceNumber &&
pair.Key <= endSequenceNumber).
OrderBy((pair) => pair.Key).
Select((pair) => pair.Value).ToList();
}
public void StoreInboundMessage(ReadOnlySpan<byte> rawMessage, int msgSeqNum, bool isOriginal)
{
InSeqNum = msgSeqNum;
}
public void StoreOutboundMessage(ReadOnlySpan<byte> rawMessage, int msgSeqNum, bool isOriginal = true,
bool warmUp = false, DateTime timestamp = default)
{
OutSeqNum = msgSeqNum;
if (!warmUp && !outboundMessages.ContainsKey(msgSeqNum))
{
outboundMessages.Add(msgSeqNum, Message.Parse(rawMessage, Engine.Instance.MessageInfoDictionaryManager));
}
}
public void Dispose()
{
}
public SessionStorageType Type => SessionStorageType.PluggableStorage;
public bool Terminated { get; set; }
public long MaxStorageSize { get; set; }
}
public static void Run()
{
const string sender = "Me";
const string target = "Counterpart";
ProtocolVersion version = ProtocolVersion.Fix44;
bool keepSequenceNumbersBetweenFixConnections = true;
MySessionStorage storage = new MySessionStorage();
Session session = new Session(sender, target, version, keepSequenceNumbersBetweenFixConnections, storage);
}
See Also
- The PluggableSessionStorage sample from the FIX Engine distribution package