-
Notifications
You must be signed in to change notification settings - Fork 15
Add command for recalculating mod multipliers #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+149
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cc70124
Implement command to recalculate score multipliers
bdach 83bcb78
Merge branch 'master' into recalculate-mod-multipliers
peppy e51f93b
Check for potential missed backpopulations in a different way
bdach 4541e42
Merge branch 'master' into recalculate-mod-multipliers
peppy e6512ea
Use async flow
peppy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
....Queues.ScoreStatisticsProcessor/Commands/Maintenance/RecalculateModMultipliersCommand.cs
This file contains hidden or 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,147 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Dapper; | ||
| using JetBrains.Annotations; | ||
| using McMaster.Extensions.CommandLineUtils; | ||
| using MySqlConnector; | ||
| using osu.Server.QueueProcessor; | ||
| using osu.Server.Queues.ScoreStatisticsProcessor.Models; | ||
|
|
||
| namespace osu.Server.Queues.ScoreStatisticsProcessor.Commands.Maintenance | ||
| { | ||
| [Command("recalculate-mod-multipliers", Description = "Recalculates total score after a change to mod multipliers")] | ||
| public class RecalculateModMultipliersCommand | ||
| { | ||
| [Option(CommandOptionType.SingleValue, Template = "--start-id")] | ||
| public ulong? StartId { get; set; } | ||
|
|
||
| [Option(CommandOptionType.SingleValue, Template = "--batch-size")] | ||
| public int BatchSize { get; set; } = 5000; | ||
|
|
||
| [Option(CommandOptionType.SingleOrNoValue, Template = "--dry-run")] | ||
| public bool DryRun { get; set; } | ||
|
|
||
| private readonly StringBuilder sqlBuffer = new StringBuilder(); | ||
|
|
||
| private readonly ElasticQueuePusher elasticQueuePusher = new ElasticQueuePusher(); | ||
| private readonly List<ElasticQueuePusher.ElasticScoreItem> elasticItems = new List<ElasticQueuePusher.ElasticScoreItem>(); | ||
|
|
||
| [UsedImplicitly] | ||
| public async Task<int> OnExecuteAsync(CancellationToken cancellationToken) | ||
| { | ||
| ulong lastId = StartId ?? 0; | ||
| ulong updatedScores = 0; | ||
|
|
||
| using var conn = await DatabaseAccess.GetConnectionAsync(cancellationToken); | ||
|
|
||
| Console.WriteLine(); | ||
| Console.WriteLine($"Recalculating total score in line with new mod multipliers, starting from ID {lastId}"); | ||
| Console.WriteLine($"Indexing to elastic queue(s) {elasticQueuePusher.ActiveQueues}"); | ||
|
|
||
| if (DryRun) | ||
| Console.WriteLine("RUNNING IN DRY RUN MODE."); | ||
|
|
||
| await Task.Delay(5000, cancellationToken); | ||
|
|
||
| while (!cancellationToken.IsCancellationRequested) | ||
| { | ||
| var scoresWithMods = (await conn.QueryAsync<SoloScore>( | ||
| "SELECT * FROM `scores` WHERE `id` BETWEEN @lastId AND (@lastId + @batchSize - 1) AND JSON_LENGTH(`data`, '$.mods') > 0", | ||
| new | ||
| { | ||
| lastId, | ||
| batchSize = BatchSize, | ||
| })).ToArray(); | ||
|
|
||
| if (scoresWithMods.Length == 0) | ||
| { | ||
| if (lastId > await conn.QuerySingleAsync<ulong>("SELECT MAX(id) FROM scores")) | ||
| { | ||
| Console.WriteLine("All done!"); | ||
| break; | ||
| } | ||
|
|
||
| lastId += (ulong)BatchSize; | ||
| continue; | ||
| } | ||
|
|
||
| uint[] beatmapIds = scoresWithMods.Select(score => score.beatmap_id).Distinct().ToArray(); | ||
| var beatmapsById = (await conn.QueryAsync<Beatmap>(@"SELECT * FROM `osu_beatmaps` WHERE `beatmap_id` IN @ids", new { ids = beatmapIds })) | ||
| .ToDictionary(beatmap => beatmap.beatmap_id); | ||
|
|
||
| foreach (var score in scoresWithMods) | ||
| { | ||
| score.beatmap = beatmapsById[score.beatmap_id]; | ||
| var scoreInfo = score.ToScoreInfo(); | ||
|
|
||
| if (scoreInfo.TotalScoreWithoutMods == 0 && scoreInfo.TotalScore != 0) | ||
| { | ||
| throw new InvalidOperationException($"Score with ID {score.id} has {scoreInfo.TotalScore} total score but {scoreInfo.TotalScoreWithoutMods} total score without mods. " | ||
| + $"This is likely to indicate that {nameof(scoreInfo.TotalScoreWithoutMods)} was not correctly backpopulated on all scores " | ||
| + "(or there is a process pushing new scores that was not updated to populate the field)."); | ||
| } | ||
|
|
||
| double multiplier = 1; | ||
|
|
||
| foreach (var mod in scoreInfo.Mods) | ||
| multiplier *= mod.ScoreMultiplier; | ||
|
Comment on lines
+91
to
+94
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depends on client changes (the new multipliers actually being present in client source), and will require upgrading in line with the new API |
||
|
|
||
| long newTotalScore = (long)Math.Round(scoreInfo.TotalScoreWithoutMods * multiplier); | ||
|
|
||
| if (newTotalScore == scoreInfo.TotalScore) | ||
| continue; | ||
|
|
||
| Console.WriteLine($"Updating score {score.id}. Without mods: {scoreInfo.TotalScoreWithoutMods}. With mods: {scoreInfo.TotalScore} (old) -> {newTotalScore} (new)"); | ||
|
|
||
| sqlBuffer.Append($@"UPDATE `scores` SET `total_score` = {newTotalScore} WHERE `id` = {score.id};"); | ||
| elasticItems.Add(new ElasticQueuePusher.ElasticScoreItem { ScoreId = (long?)score.id }); | ||
| updatedScores++; | ||
| } | ||
|
|
||
| lastId += (ulong)BatchSize; | ||
|
|
||
| Console.WriteLine($"Processed up to {lastId - 1} ({updatedScores} updated)"); | ||
|
|
||
| flush(conn); | ||
| } | ||
|
|
||
| flush(conn, true); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private void flush(MySqlConnection conn, bool force = false) | ||
| { | ||
| int bufferLength = sqlBuffer.Length; | ||
|
|
||
| if (bufferLength == 0) | ||
| return; | ||
|
|
||
| if (bufferLength > 1024 || force) | ||
| { | ||
| if (!DryRun) | ||
| { | ||
| Console.WriteLine(); | ||
| Console.WriteLine($"Flushing sql batch ({bufferLength:N0} bytes)"); | ||
| conn.Execute(sqlBuffer.ToString()); | ||
|
|
||
| if (elasticItems.Count > 0) | ||
| { | ||
| elasticQueuePusher.PushToQueue(elasticItems.ToList()); | ||
| Console.WriteLine($"Queued {elasticItems.Count} items for indexing"); | ||
| } | ||
| } | ||
|
|
||
| elasticItems.Clear(); | ||
| sqlBuffer.Clear(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will likely need further adjusting:
total_score_without_modspopulation failing if beatmap was deleted #371 was fixingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To recap, we're currently populating the majority of scores, but there could be some which don't get the population due to deleted/missing beatmaps or other edge case issues.
I think skipping here with log output is fine for such scores.