From 1f57510bb70bebfbda2bdcffd6b7cca3bdf70a37 Mon Sep 17 00:00:00 2001 From: Loporrit <141286461+loporrit@users.noreply.github.com> Date: Mon, 9 Sep 2024 07:03:08 +0000 Subject: [PATCH] oops, touch requests are concurrent... --- .../Services/CachedFileProvider.cs | 2 +- .../Services/ColdTouchHashService.cs | 48 ++++++++++++------- .../Services/ShardTouchMessageService.cs | 2 +- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs index 28f5059..0139a4e 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs @@ -114,7 +114,7 @@ public sealed class CachedFileProvider : IDisposable if (string.IsNullOrEmpty(_coldStoragePath)) return false; var coldStorageFilePath = FilePathUtil.GetFilePath(_coldStoragePath, hash); - if (coldStorageFilePath == null) return false; + if (!File.Exists(coldStorageFilePath)) return false; try { diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs index 0ebffa6..b9c3d00 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs @@ -1,5 +1,6 @@ using MareSynchronosShared.Services; using MareSynchronosStaticFilesServer.Utils; +using System.Collections.Concurrent; namespace MareSynchronosStaticFilesServer.Services; @@ -10,12 +11,13 @@ public class ColdTouchHashService : ITouchHashService private readonly IConfigurationService _configuration; private readonly bool _useColdStorage; - private readonly string _coldStoragePath; + private readonly string _coldStoragePath; - // Debounce multiple updates towards the same file - private readonly Dictionary _lastUpdateTimesUtc = new(1009, StringComparer.Ordinal); - private int _cleanupCounter = 0; - private const double _debounceTimeSecs = 90.0; + // Debounce multiple updates towards the same file + private readonly ConcurrentDictionary _lastUpdateTimesUtc = new(StringComparer.Ordinal); + private int _cleanupCounter = 0; + private object _cleanupLockObj = new(); + private const double _debounceTimeSecs = 900.0; public ColdTouchHashService(ILogger logger, IConfigurationService configuration) { @@ -37,31 +39,41 @@ public class ColdTouchHashService : ITouchHashService public void TouchColdHash(string hash) { - if (!_useColdStorage) - return; + if (!_useColdStorage) + return; - var nowUtc = DateTime.UtcNow; + var nowUtc = DateTime.UtcNow; - // Clean up debounce dictionary regularly - if (_cleanupCounter++ >= 1000) - { - foreach (var entry in _lastUpdateTimesUtc.Where(entry => (nowUtc - entry.Value).TotalSeconds >= _debounceTimeSecs).ToList()) - _lastUpdateTimesUtc.Remove(entry.Key); + // Clean up debounce dictionary regularly + if (_cleanupCounter++ >= 1000) + { _cleanupCounter = 0; - } + if (Monitor.TryEnter(_cleanupLockObj)) + { + try + { + foreach (var entry in _lastUpdateTimesUtc.Where(entry => (nowUtc - entry.Value).TotalSeconds >= _debounceTimeSecs).ToList()) + _lastUpdateTimesUtc.TryRemove(entry.Key, out _); + } + finally + { + Monitor.Exit(_cleanupLockObj); + } + } + } - // Ignore multiple updates within a 90 second window of the first - if (_lastUpdateTimesUtc.TryGetValue(hash, out var lastUpdateTimeUtc) && (nowUtc - lastUpdateTimeUtc).TotalSeconds < _debounceTimeSecs) + // Ignore multiple updates within a time window of the first + if (_lastUpdateTimesUtc.TryGetValue(hash, out var lastUpdateTimeUtc) && (nowUtc - lastUpdateTimeUtc).TotalSeconds < _debounceTimeSecs) { _logger.LogDebug($"Debounced touch for {hash}"); - return; + return; } var fileInfo = FilePathUtil.GetFileInfoForHash(_coldStoragePath, hash); if (fileInfo != null) { _logger.LogDebug($"Touching {fileInfo.Name}"); - fileInfo.LastAccessTimeUtc = nowUtc; + fileInfo.LastAccessTimeUtc = nowUtc; _lastUpdateTimesUtc.TryAdd(hash, nowUtc); } } diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs index 1c22e3e..2a88b36 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs @@ -101,7 +101,7 @@ public class ShardTouchMessageService : ITouchHashService } if (hashes.Count > 0) await SendTouches(hashes); - await Task.Delay(TimeSpan.FromSeconds(30), ct).ConfigureAwait(false); + await Task.Delay(TimeSpan.FromSeconds(60), ct).ConfigureAwait(false); } catch (Exception e) {