Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Sunrise.Processing/Scores/Pipeline/ScoreCommitPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Sunrise.Shared/Application/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ public static string WebTokenSecret
public static int ScoreProcessingTimeoutSeconds =>
Config.GetSection("ScoreProcessing").GetValue<int?>("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<int?>("MaxConcurrency") ?? 3;
Config.GetSection("ScoreProcessing").GetValue<int?>("MaxConcurrency") ?? 1;

public static int ScoreProcessingPollerInterBatchDelaySeconds =>
Config.GetSection("ScoreProcessing").GetValue<int?>("PollerInterBatchDelaySeconds")
Expand Down
50 changes: 50 additions & 0 deletions Sunrise.Shared/Database/Repositories/ScoreRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public async Task<Result> UpdateScore(Score score)
});
}

public async Task<bool> 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<Score>, int)> GetBestScoresByGameMode(GameMode mode, QueryOptions? options = null, CancellationToken ct = default)
{
var groupedBestScores = dbContext.Scores
Expand Down Expand Up @@ -78,6 +96,38 @@ public async Task<Result> 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<Score?> GetScore(string scoreHash, QueryOptions? options = null, CancellationToken ct = default)
{
return await dbContext.Scores
Expand Down
Loading