-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from peppy/pp-2024
Score processing command updates for upcoming (and future PP deploys)
- Loading branch information
Showing
6 changed files
with
242 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ver.Queues.ScoreStatisticsProcessor/Commands/Performance/Scores/UpdateAllScoresCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using osu.Server.QueueProcessor; | ||
using osu.Server.Queues.ScoreStatisticsProcessor.Helpers; | ||
using osu.Server.Queues.ScoreStatisticsProcessor.Models; | ||
|
||
namespace osu.Server.Queues.ScoreStatisticsProcessor.Commands.Performance.Scores | ||
{ | ||
[Command(Name = "all", Description = "Computes pp of all scores from all users.")] | ||
public class UpdateAllScoresCommand : PerformanceCommand | ||
{ | ||
private const int max_scores_per_query = 5000; | ||
|
||
[Option(Description = "Score ID to start processing from.")] | ||
public ulong From { get; set; } | ||
|
||
/// <summary> | ||
/// Whether to adjust processing rate based on slave latency. Defaults to <c>false</c>. | ||
/// </summary> | ||
[Option(CommandOptionType.SingleOrNoValue, Template = "--check-slave-latency")] | ||
public bool CheckSlaveLatency { get; set; } | ||
|
||
protected override async Task<int> ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
using var db = DatabaseAccess.GetConnection(); | ||
|
||
ulong currentScoreId = From; | ||
ulong? lastScoreId = await db.QuerySingleAsync<ulong>("SELECT MAX(id) FROM scores"); | ||
|
||
ulong processedCount = 0; | ||
ulong changedPp = 0; | ||
double rate = 0; | ||
Stopwatch sw = new Stopwatch(); | ||
|
||
var scoresQuery = db.Query<SoloScore>("SELECT * FROM scores WHERE `id` > @ScoreId AND `id` <= @LastScoreId ORDER BY `id`", new | ||
{ | ||
ScoreId = currentScoreId, | ||
LastScoreId = lastScoreId, | ||
}, buffered: false); | ||
|
||
using var scoresEnum = scoresQuery.GetEnumerator(); | ||
|
||
Console.WriteLine($"Processing all scores up to {lastScoreId}, starting from {currentScoreId}"); | ||
|
||
Task<List<SoloScore>> nextScores = getNextScores(); | ||
|
||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
if (CheckSlaveLatency) | ||
await SlaveLatencyChecker.CheckSlaveLatency(db, cancellationToken); | ||
|
||
sw.Restart(); | ||
|
||
var scores = await nextScores; | ||
nextScores = getNextScores(); | ||
|
||
if (scores.Count == 0) | ||
break; | ||
|
||
await Task.WhenAll(Partitioner.Create(scores).GetPartitions(Threads).Select(async partition => | ||
{ | ||
using (var connection = DatabaseAccess.GetConnection()) | ||
using (var transaction = await connection.BeginTransactionAsync(IsolationLevel.ReadUncommitted, cancellationToken)) | ||
using (partition) | ||
{ | ||
while (partition.MoveNext()) | ||
{ | ||
if (cancellationToken.IsCancellationRequested) | ||
return; | ||
|
||
bool changed = await ScoreProcessor.ProcessScoreAsync(partition.Current, connection, transaction); | ||
|
||
if (changed) | ||
Interlocked.Increment(ref changedPp); | ||
} | ||
|
||
await transaction.CommitAsync(cancellationToken); | ||
} | ||
})); | ||
|
||
if (cancellationToken.IsCancellationRequested) | ||
return -1; | ||
|
||
Interlocked.Add(ref processedCount, (ulong)scores.Count); | ||
|
||
currentScoreId = scores.Last().id; | ||
|
||
if (rate == 0) | ||
rate = ((double)scores.Count / sw.ElapsedMilliseconds * 1000); | ||
else | ||
rate = rate * 0.95 + 0.05 * ((double)scores.Count / sw.ElapsedMilliseconds * 1000); | ||
|
||
Console.WriteLine(ScoreProcessor.BeatmapStore?.GetCacheStats()); | ||
Console.WriteLine($"processed up to: {currentScoreId:N0} changed: {changedPp:N0} {(float)processedCount / (lastScoreId - From):P1} {rate:N0}/s"); | ||
} | ||
|
||
return 0; | ||
|
||
Task<List<SoloScore>> getNextScores() => Task.Run(() => | ||
{ | ||
List<SoloScore> scores = new List<SoloScore>(max_scores_per_query); | ||
|
||
for (int i = 0; i < max_scores_per_query && scoresEnum.MoveNext(); i++) | ||
scores.Add(scoresEnum.Current); | ||
|
||
return scores; | ||
}, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.