From d34c79febb0962e5dfc7d9b0b22cdb863e8e339c Mon Sep 17 00:00:00 2001 From: Colton Arabsky Date: Mon, 3 Feb 2025 16:00:22 -0800 Subject: [PATCH 1/6] If the BatchFrequency changes, update the Loop --- Runtime/Server/Analytics/ServerGameTelemetry.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Runtime/Server/Analytics/ServerGameTelemetry.cs b/Runtime/Server/Analytics/ServerGameTelemetry.cs index cde0e619..1a4b3aaf 100644 --- a/Runtime/Server/Analytics/ServerGameTelemetry.cs +++ b/Runtime/Server/Analytics/ServerGameTelemetry.cs @@ -65,6 +65,10 @@ public void SetBatchFrequency( TimeSpan interval ) SharedMemory?.Logger?.LogWarning($"Telemetry schedule interval is too small! Set to {minimumTelemetryInterval.TotalSeconds} seconds."); telemetryInterval = minimumTelemetryInterval; } + + cancelationTokenSource?.Cancel(); + cancelationTokenSource?.Dispse(); + InitializeTelemetryLoop(); } /// @@ -191,7 +195,11 @@ private void RunPeriodicTelemetry() } SendTelemetryBatch(callback: null); - + InitializeTelemetryLoop(); + } + + private void InitializeTelemetryLoop() + { cancelationTokenSource = new System.Threading.CancellationTokenSource(); IndefiniteLoopCommand telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token From 7c6d2bb47250e85e88783e81c585074da62e20bd Mon Sep 17 00:00:00 2001 From: Colton Arabsky Date: Mon, 3 Feb 2025 16:33:37 -0800 Subject: [PATCH 2/6] - Revert wrong file - useCache bool can be used to enable/disable SDK Telemetry caching behavior. --- Runtime/Api/Analytics/GameTelemetry.cs | 46 ++++++++++++++++--- .../Server/Analytics/ServerGameTelemetry.cs | 4 -- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Runtime/Api/Analytics/GameTelemetry.cs b/Runtime/Api/Analytics/GameTelemetry.cs index 134d5730..ef9a56be 100644 --- a/Runtime/Api/Analytics/GameTelemetry.cs +++ b/Runtime/Api/Analytics/GameTelemetry.cs @@ -33,6 +33,7 @@ public class GameTelemetry : WrapperBase IAccelByteDataStorage cacheStorage; string cacheTableName; + private bool useCache; private WaitTimeCommand telemetryLoopCommand; internal TimeSpan TelemetryInterval @@ -54,6 +55,7 @@ internal GameTelemetry( ClientGameTelemetryApi inApi session = inSession; + useCache = true; cacheStorage = session.DataStorage; cacheTableName = $"GameTelemetryCache/{AccelByteSDK.Environment.Current}.cache"; @@ -100,6 +102,10 @@ public void SetBatchFrequency( TimeSpan interval ) $"Set to {minimumTelemetryInterval.TotalSeconds} seconds."); telemetryInterval = minimumTelemetryInterval; } + + cancelationTokenSource?.Cancel(); + cancelationTokenSource?.Dispse(); + InitializeTelemetryLoop(); } /// @@ -113,6 +119,18 @@ public void SetBatchFrequency(int intervalSeconds) SetBatchFrequency(TimeSpan.FromSeconds(intervalSeconds)); } + /// + /// + public void SetUseCache(bool shouldUseCache) + { + useCache = shouldUseCache; + + if (!useCache) + { + DeleteCache(); + } + } + /// /// Set list of event that need to be sent immediately without the needs to jobQueue it. /// @@ -147,7 +165,11 @@ public void Send( TelemetryBody telemetryBody else { jobQueue.Enqueue(new Tuple(telemetryBody, callback)); - AppendEventToCache(telemetryBody); + + if (useCache) + { + AppendEventToCache(telemetryBody); + } if (cancelationTokenSource == null) { @@ -203,7 +225,10 @@ public void SendTelemetryBatch(ResultCallback callback) } else { - RemoveEventsFromCache(); + if (useCache) + { + RemoveEventsFromCache(); + } } foreach (var telemetryCallback in telemetryCallbacks) @@ -222,6 +247,11 @@ internal override void SetSharedMemory(ApiSharedMemory newSharedMemory) private void SendCachedTelemetry() { + if (!useCache) + { + return; + } + LoadEventsFromCache(telemetryBodies => { SendTelemetryRequest(telemetryBodies, cb => @@ -250,11 +280,15 @@ private void RunPeriodicTelemetry() } SendTelemetryBatch(callback: null); - - cancelationTokenSource = new CancellationTokenSource(); - telemetryLoopCommand = new WaitTimeCommand(waitTime: telemetryInterval.TotalSeconds + InitializeTelemetryLoop(); + } + + private void InitializeTelemetryLoop() + { + cancelationTokenSource = new System.Threading.CancellationTokenSource(); + IndefiniteLoopCommand telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token - , onDone: TelemetryLoop); + , onUpdate: TelemetryLoop); SharedMemory?.CoreHeartBeat?.Wait(telemetryLoopCommand); } diff --git a/Runtime/Server/Analytics/ServerGameTelemetry.cs b/Runtime/Server/Analytics/ServerGameTelemetry.cs index 1a4b3aaf..cb44d309 100644 --- a/Runtime/Server/Analytics/ServerGameTelemetry.cs +++ b/Runtime/Server/Analytics/ServerGameTelemetry.cs @@ -65,10 +65,6 @@ public void SetBatchFrequency( TimeSpan interval ) SharedMemory?.Logger?.LogWarning($"Telemetry schedule interval is too small! Set to {minimumTelemetryInterval.TotalSeconds} seconds."); telemetryInterval = minimumTelemetryInterval; } - - cancelationTokenSource?.Cancel(); - cancelationTokenSource?.Dispse(); - InitializeTelemetryLoop(); } /// From 36c33b8a0ca2c4a7f915e66447ae519ae8ec1b97 Mon Sep 17 00:00:00 2001 From: Colton Arabsky Date: Mon, 3 Feb 2025 16:35:36 -0800 Subject: [PATCH 3/6] Only Cancel/Initialize if we already have a Loop running. --- Runtime/Api/Analytics/GameTelemetry.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Runtime/Api/Analytics/GameTelemetry.cs b/Runtime/Api/Analytics/GameTelemetry.cs index ef9a56be..ebff96d3 100644 --- a/Runtime/Api/Analytics/GameTelemetry.cs +++ b/Runtime/Api/Analytics/GameTelemetry.cs @@ -102,10 +102,13 @@ public void SetBatchFrequency( TimeSpan interval ) $"Set to {minimumTelemetryInterval.TotalSeconds} seconds."); telemetryInterval = minimumTelemetryInterval; } - - cancelationTokenSource?.Cancel(); - cancelationTokenSource?.Dispse(); - InitializeTelemetryLoop(); + + if (cancelationTokenSource != null) + { + cancelationTokenSource.Cancel(); + cancelationTokenSource.Dispose(); + InitializeTelemetryLoop(); + } } /// From 644af724ac162b79e05a15a01110d3501c24b7b4 Mon Sep 17 00:00:00 2001 From: Colton Arabsky Date: Tue, 4 Feb 2025 11:51:23 -0800 Subject: [PATCH 4/6] This is a member var, sigh. --- Runtime/Api/Analytics/GameTelemetry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Api/Analytics/GameTelemetry.cs b/Runtime/Api/Analytics/GameTelemetry.cs index ebff96d3..554c9b42 100644 --- a/Runtime/Api/Analytics/GameTelemetry.cs +++ b/Runtime/Api/Analytics/GameTelemetry.cs @@ -289,7 +289,7 @@ private void RunPeriodicTelemetry() private void InitializeTelemetryLoop() { cancelationTokenSource = new System.Threading.CancellationTokenSource(); - IndefiniteLoopCommand telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds + telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token , onUpdate: TelemetryLoop); SharedMemory?.CoreHeartBeat?.Wait(telemetryLoopCommand); From ddccca3d9d5b8efa6decf7e29709d844cd1da274 Mon Sep 17 00:00:00 2001 From: Colton Arabsky Date: Tue, 4 Feb 2025 12:03:32 -0800 Subject: [PATCH 5/6] Revert ServerGameTelemetry, and fix up GameTelemetry which I set to use the wrong IWaitCommand. --- Runtime/Api/Analytics/GameTelemetry.cs | 33 +++++++++++++++++-- .../Server/Analytics/ServerGameTelemetry.cs | 5 --- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Runtime/Api/Analytics/GameTelemetry.cs b/Runtime/Api/Analytics/GameTelemetry.cs index 554c9b42..6fa122b3 100644 --- a/Runtime/Api/Analytics/GameTelemetry.cs +++ b/Runtime/Api/Analytics/GameTelemetry.cs @@ -288,10 +288,10 @@ private void RunPeriodicTelemetry() private void InitializeTelemetryLoop() { - cancelationTokenSource = new System.Threading.CancellationTokenSource(); - telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds + cancelationTokenSource = new CancellationTokenSource(); + telemetryLoopCommand = new WaitTimeCommand(waitTime: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token - , onUpdate: TelemetryLoop); + , onDone: TelemetryLoop); SharedMemory?.CoreHeartBeat?.Wait(telemetryLoopCommand); } @@ -333,11 +333,23 @@ internal void LoadEventsFromCache(Action> onLoadCacheDone) return; } + if (!useCache) + { + onLoadCacheDone?.Invoke(null); + return; + } + LoadEventsFromCache(session.UserId, onLoadCacheDone); } internal void LoadEventsFromCache(string key, Action> onLoadCacheDone) { + if (!useCache) + { + onLoadCacheDone?.Invoke(null); + return; + } + cacheStorage.GetItem( key , tableName: cacheTableName @@ -370,12 +382,23 @@ internal void AppendEventToCache(TelemetryBody telemetryBody, Action onSav onSaveCacheDone?.Invoke(false); return; } + if (!useCache) + { + onSaveCacheDone?.Invoke(false); + return; + } AppendEventToCache(session.UserId, telemetryBody, onSaveCacheDone); } internal void AppendEventToCache(string key, TelemetryBody telemetryBody, Action onSaveCacheDone = null) { + if (!useCache) + { + onSaveCacheDone?.Invoke(false); + return; + } + eventList.Add(telemetryBody); var telemetryEventsJson = System.Text.Encoding.UTF8.GetString(eventList.ToUtf8Json()); cacheStorage.SaveItem(key, telemetryEventsJson, onSaveCacheDone, tableName: cacheTableName); @@ -392,6 +415,10 @@ private void RemoveEventsFromCache() { return; } + if (!useCache) + { + return; + } cacheStorage.DeleteItem(session.UserId, onDone: null, tableName:cacheTableName); } diff --git a/Runtime/Server/Analytics/ServerGameTelemetry.cs b/Runtime/Server/Analytics/ServerGameTelemetry.cs index cb44d309..531e15c1 100644 --- a/Runtime/Server/Analytics/ServerGameTelemetry.cs +++ b/Runtime/Server/Analytics/ServerGameTelemetry.cs @@ -191,11 +191,6 @@ private void RunPeriodicTelemetry() } SendTelemetryBatch(callback: null); - InitializeTelemetryLoop(); - } - - private void InitializeTelemetryLoop() - { cancelationTokenSource = new System.Threading.CancellationTokenSource(); IndefiniteLoopCommand telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds , cancellationToken: cancelationTokenSource.Token From dbc902c6f742e7e66613ca057427b17a478fac6f Mon Sep 17 00:00:00 2001 From: Colton Arabsky Date: Tue, 4 Feb 2025 14:24:19 -0800 Subject: [PATCH 6/6] Some code cleanup and finalization. --- Runtime/Api/Analytics/GameTelemetry.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Runtime/Api/Analytics/GameTelemetry.cs b/Runtime/Api/Analytics/GameTelemetry.cs index 6fa122b3..b4972fa3 100644 --- a/Runtime/Api/Analytics/GameTelemetry.cs +++ b/Runtime/Api/Analytics/GameTelemetry.cs @@ -123,13 +123,19 @@ public void SetBatchFrequency(int intervalSeconds) } /// + /// Enable or disable the usage of the file storage cache. + /// The cache stores all events in JSON before being sent in case + /// of crash or errors and attempts to re-send them when it can. + /// The cache is enabled by default. /// + /// Enables or disables the usage of the cache. public void SetUseCache(bool shouldUseCache) { useCache = shouldUseCache; if (!useCache) { + eventList.Clear(); DeleteCache(); } } @@ -327,13 +333,12 @@ private void TelemetryLoop() internal void LoadEventsFromCache(Action> onLoadCacheDone) { - if (!session.IsValid()) + if (!useCache) { onLoadCacheDone?.Invoke(null); return; } - - if (!useCache) + if (!session.IsValid()) { onLoadCacheDone?.Invoke(null); return; @@ -377,12 +382,12 @@ internal void LoadEventsFromCache(string key, Action> onLoad internal void AppendEventToCache(TelemetryBody telemetryBody, Action onSaveCacheDone = null) { - if (!session.IsValid()) + if (!useCache) { onSaveCacheDone?.Invoke(false); return; } - if (!useCache) + if (!session.IsValid()) { onSaveCacheDone?.Invoke(false); return; @@ -411,11 +416,11 @@ internal void DeleteCache(Action onDone = null) private void RemoveEventsFromCache() { - if (session != null && !session.IsValid()) + if (!useCache) { return; } - if (!useCache) + if (session != null && !session.IsValid()) { return; }