Skip to content

Commit 2fed977

Browse files
committed
chore: Add read/write mode support for persistent store.
1 parent ac41228 commit 2fed977

File tree

3 files changed

+1029
-16
lines changed

3 files changed

+1029
-16
lines changed

pkgs/sdk/server/src/Internal/DataSystem/FDv2DataSystem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static FDv2DataSystem Create(Logger logger, Configuration configuration,
5959
var persistentStore =
6060
dataSystemConfiguration.PersistentStore?.Build(clientContext.WithDataStoreUpdates(dataStoreUpdates));
6161

62-
var writeThroughStore = new WriteThroughStore(memoryStore, persistentStore);
62+
var writeThroughStore = new WriteThroughStore(memoryStore, persistentStore,
63+
dataSystemConfiguration.PersistentDataStoreMode);
6364

6465
// TODO: When a persistent store is available we monitor it, is this a consistent choice.
6566
// TODO: Update the responses data store monitoring?

pkgs/sdk/server/src/Internal/DataSystem/WriteThroughStore.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ internal class WriteThroughStore : IDataStore, ITransactionalDataStore
2626
/// </para>
2727
/// </summary>
2828
private readonly AtomicBoolean _hasReceivedAnInitializingPayload = new AtomicBoolean(false);
29+
30+
private readonly DataSystemConfiguration.DataStoreMode _persistenceMode;
2931

30-
public WriteThroughStore(IDataStore memoryStore, IDataStore persistentStore)
32+
public WriteThroughStore(IDataStore memoryStore, IDataStore persistentStore, DataSystemConfiguration.DataStoreMode persistenceMode)
3133
{
3234
_memoryStore = memoryStore;
3335
_txMemoryStore = (ITransactionalDataStore)_memoryStore;
3436
_persistentStore = persistentStore;
3537
_hasPersistence = persistentStore != null;
3638
// During initializations read will happen from the persistent store.
3739
_activeReadStore = _hasPersistence ? _persistentStore : _memoryStore;
40+
_persistenceMode = persistenceMode;
3841
}
3942

4043
public void Dispose()
@@ -56,7 +59,11 @@ private void Dispose(bool disposing)
5659
public void Init(DataStoreTypes.FullDataSet<DataStoreTypes.ItemDescriptor> allData)
5760
{
5861
_memoryStore.Init(allData);
59-
_persistentStore?.Init(allData);
62+
if (_persistenceMode == DataSystemConfiguration.DataStoreMode.ReadWrite)
63+
{
64+
_persistentStore?.Init(allData);
65+
}
66+
6067
MaybeSwitchStore();
6168
}
6269

@@ -73,7 +80,7 @@ public void Init(DataStoreTypes.FullDataSet<DataStoreTypes.ItemDescriptor> allDa
7380
public bool Upsert(DataStoreTypes.DataKind kind, string key, DataStoreTypes.ItemDescriptor item)
7481
{
7582
var result = _memoryStore.Upsert(kind, key, item);
76-
if (_hasPersistence)
83+
if (_hasPersistence && _persistenceMode == DataSystemConfiguration.DataStoreMode.ReadWrite)
7784
{
7885
result &= _persistentStore.Upsert(kind, key, item);
7986
}
@@ -92,23 +99,24 @@ public bool Initialized()
9299
public void Apply(DataStoreTypes.ChangeSet<DataStoreTypes.ItemDescriptor> changeSet)
93100
{
94101
_txMemoryStore.Apply(changeSet);
102+
MaybeSwitchStore();
95103

96-
if (_hasPersistence)
104+
if (!_hasPersistence || _persistenceMode != DataSystemConfiguration.DataStoreMode.ReadWrite) return;
105+
106+
if (_persistentStore is ITransactionalDataStore txPersistentStore)
97107
{
98-
if (_persistentStore is ITransactionalDataStore txPersistentStore)
99-
{
100-
txPersistentStore.Apply(changeSet);
101-
}
102-
else
108+
txPersistentStore.Apply(changeSet);
109+
}
110+
else
111+
{
112+
// If an apply fails at init, that will throw on its own, but if it fails via an upsert, then
113+
// we need to throw something to work with the current data source updates implementation.
114+
if (!ApplyToLegacyPersistence(changeSet))
103115
{
104-
if (!ApplyToLegacyPersistence(changeSet))
105-
{
106-
// TODO: Probably throw?
107-
}
116+
// The exception type doesn't matter here, as it will be converted to data store status.
117+
throw new Exception("Failure to apply data set to persistent store.");
108118
}
109119
}
110-
111-
MaybeSwitchStore();
112120
}
113121

114122
public Selector Selector => _txMemoryStore.Selector;

0 commit comments

Comments
 (0)