diff --git a/Sunrise.Processing/Scores/Pipeline/ScoreCommitPipeline.cs b/Sunrise.Processing/Scores/Pipeline/ScoreCommitPipeline.cs index 06572843..41c8643f 100644 --- a/Sunrise.Processing/Scores/Pipeline/ScoreCommitPipeline.cs +++ b/Sunrise.Processing/Scores/Pipeline/ScoreCommitPipeline.cs @@ -38,6 +38,19 @@ private async Task ExecuteCommitAsync( { var score = ctx.Score; + await _database.Users.Stats.LockAndRefreshUserStats(ctx.UserStats, ct); + await _database.Users.Grades.LockAndRefreshUserGrades(ctx.UserGrades, ct); + + if (ctx.TaskType != ScoreTaskType.Submission) + { + var newPerformancePoints = score.PerformancePoints; + var locked = await _database.Scores.LockAndRefreshScore(score, ct); // TODO: This will cause the deadlocks, since we are first locking the main score and then peers. I'm partially fine with this since we have retries, but we will need to refactor this system some day for one single lock. + if (!locked) + throw new ApplicationException($"Score {score.Id} was not found while locking score commit target"); + + score.PerformancePoints = newPerformancePoints; + } + score.LocalProperties = new LocalProperties().FromScore(score); ctx.OriginalState = ScoreStateSnapshot.Capture(score); @@ -56,9 +69,6 @@ private async Task ExecuteCommitAsync( ctx.UserPersonalBestScores = peers; - await _database.Users.Stats.LockAndRefreshUserStats(ctx.UserStats, ct); - await _database.Users.Grades.LockAndRefreshUserGrades(ctx.UserGrades, ct); - foreach (var processor in _processors) { await DispatchProcessor(processor, ctx); diff --git a/Sunrise.Shared/Application/Configuration.cs b/Sunrise.Shared/Application/Configuration.cs index 2211bb24..f47cf911 100644 --- a/Sunrise.Shared/Application/Configuration.cs +++ b/Sunrise.Shared/Application/Configuration.cs @@ -195,8 +195,9 @@ public static string WebTokenSecret public static int ScoreProcessingTimeoutSeconds => Config.GetSection("ScoreProcessing").GetValue("TimeoutSeconds") ?? 10; + // NOTE: Concurrency is disabled by default. See https://github.com/SunriseCommunity/Sunrise/pull/127 for reason public static int ScoreProcessingMaxConcurrency => - Config.GetSection("ScoreProcessing").GetValue("MaxConcurrency") ?? 3; + Config.GetSection("ScoreProcessing").GetValue("MaxConcurrency") ?? 1; public static int ScoreProcessingPollerInterBatchDelaySeconds => Config.GetSection("ScoreProcessing").GetValue("PollerInterBatchDelaySeconds") diff --git a/Sunrise.Shared/Database/Repositories/ScoreRepository.cs b/Sunrise.Shared/Database/Repositories/ScoreRepository.cs index 8e089571..d1145a70 100644 --- a/Sunrise.Shared/Database/Repositories/ScoreRepository.cs +++ b/Sunrise.Shared/Database/Repositories/ScoreRepository.cs @@ -43,6 +43,24 @@ public async Task UpdateScore(Score score) }); } + public async Task LockAndRefreshScore(Score score, CancellationToken ct = default) + { + if (score.Id == 0) + return false; + + var lockedScore = await dbContext.Scores + .AsNoTracking() + .Where(s => s.Id == score.Id) + .ForUpdate() + .SingleOrDefaultAsync(ct); + + if (lockedScore == null) + return false; + + CopyScoreValues(lockedScore, score); + return true; + } + public async Task<(List, int)> GetBestScoresByGameMode(GameMode mode, QueryOptions? options = null, CancellationToken ct = default) { var groupedBestScores = dbContext.Scores @@ -78,6 +96,38 @@ public async Task UpdateScore(Score score) .FirstOrDefaultAsync(cancellationToken: ct); } + private static void CopyScoreValues(Score source, Score target) + { + target.Id = source.Id; + target.UserId = source.UserId; + target.BeatmapId = source.BeatmapId; + target.ScoreHash = source.ScoreHash; + target.BeatmapHash = source.BeatmapHash; + target.ReplayFileId = source.ReplayFileId; + target.TotalScore = source.TotalScore; + target.MaxCombo = source.MaxCombo; + target.Count300 = source.Count300; + target.Count100 = source.Count100; + target.Count50 = source.Count50; + target.CountMiss = source.CountMiss; + target.CountKatu = source.CountKatu; + target.CountGeki = source.CountGeki; + target.Perfect = source.Perfect; + target.Mods = source.Mods; + target.Grade = source.Grade; + target.IsPassed = source.IsPassed; + target.IsScoreable = source.IsScoreable; + target.SubmissionStatus = source.SubmissionStatus; + target.GameMode = source.GameMode; + target.WhenPlayed = source.WhenPlayed; + target.OsuVersion = source.OsuVersion; + target.BeatmapStatus = source.BeatmapStatus; + target.ClientTime = source.ClientTime; + target.Accuracy = source.Accuracy; + target.PerformancePoints = source.PerformancePoints; + target.TimeElapsed = source.TimeElapsed; + } + public async Task GetScore(string scoreHash, QueryOptions? options = null, CancellationToken ct = default) { return await dbContext.Scores