Repository navigation
Add endpoint to upload desync reports #1210
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7c255d7
Add endpoint to get a presigned upload url for desync reports
Brutus5000 3786eba
Bump faf-db-migrations used by the integration tests to v146
Brutus5000 cf4a3d5
Expose game_desync read-only via Elide and bump migrations to v147
Brutus5000 65ae522
Accept desync reports as upload and forward them to the object storage
Brutus5000 41594ee
Bump faf-db-migrations to v148
Brutus5000 9b54713
Expose the report file name of a desync as computed attribute
Brutus5000 af7d8a5
Fill the desync report file name in an entity listener
Brutus5000 acafa2a
Make the desync report file name configurable and build it in the ser…
Brutus5000 cbe39c7
Use constructor injection in GameDesyncEnricher
Brutus5000 2d0403a
Revert "Use constructor injection in GameDesyncEnricher"
Brutus5000 7f76448
Assert the outcome in the max size desync report test
Brutus5000 a8a4b58
Require db schema version 148
Brutus5000 1cbd764
Disable chunked encoding on the desync report client
Brutus5000 72f1c26
Upload desync reports from a repeatable stream
Brutus5000 a6dc40e
Make the desync report object key layout fixed
Brutus5000 4c3879f
Close the previous desync report stream when the SDK reads again
Brutus5000 870ada4
Drop the redundant desync prefix from the report object key
Brutus5000 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
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
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
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
102 changes: 102 additions & 0 deletions
102
src/main/java/com/faforever/api/data/domain/GameDesync.java
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,102 @@ | ||
| package com.faforever.api.data.domain; | ||
|
|
||
| import com.faforever.api.data.checks.Prefab; | ||
| import com.faforever.api.data.listeners.GameDesyncEnricher; | ||
| import com.yahoo.elide.annotation.ComputedAttribute; | ||
| import com.yahoo.elide.annotation.CreatePermission; | ||
| import com.yahoo.elide.annotation.DeletePermission; | ||
| import com.yahoo.elide.annotation.Include; | ||
| import com.yahoo.elide.annotation.UpdatePermission; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EntityListeners; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.Transient; | ||
| import lombok.Setter; | ||
| import org.hibernate.annotations.Immutable; | ||
|
|
||
| import java.time.OffsetDateTime; | ||
|
|
||
| /** | ||
| * A player whose client reported a desync in a game via GPGNet. Written by the lobby server only; one row per game and | ||
| * player, recording the first report. | ||
| */ | ||
| @Entity | ||
| @Table(name = "game_desync") | ||
| @Include(name = "gameDesync") | ||
| @Immutable | ||
| @Setter | ||
| @EntityListeners(GameDesyncEnricher.class) | ||
| @CreatePermission(expression = Prefab.NONE) | ||
| @UpdatePermission(expression = Prefab.NONE) | ||
| @DeletePermission(expression = Prefab.NONE) | ||
| public class GameDesync { | ||
|
|
||
| private int id; | ||
| private Game game; | ||
| private Player player; | ||
| private OffsetDateTime reportedAt; | ||
| private Integer gameTime; | ||
| private OffsetDateTime reportUploadedAt; | ||
| private String reportFileName; | ||
|
|
||
| @Id | ||
| @Column(name = "id") | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "game_id") | ||
| public Game getGame() { | ||
| return game; | ||
| } | ||
|
|
||
| /** | ||
| * The player whose client reported the desync. Every client that detects the mismatch reports it, so this is not the | ||
| * culprit. | ||
| */ | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "player_id") | ||
| public Player getPlayer() { | ||
| return player; | ||
| } | ||
|
|
||
| @Column(name = "reported_at") | ||
| public OffsetDateTime getReportedAt() { | ||
| return reportedAt; | ||
| } | ||
|
|
||
| /** | ||
| * Seconds since the game was launched, derived by the lobby server. Not a simulation tick. | ||
| */ | ||
| @Column(name = "game_time") | ||
| public Integer getGameTime() { | ||
| return gameTime; | ||
| } | ||
|
|
||
| /** | ||
| * Time the player uploaded the desync report file through the API, null if there is none. | ||
| */ | ||
| @Column(name = "report_uploaded_at") | ||
| public OffsetDateTime getReportUploadedAt() { | ||
| return reportUploadedAt; | ||
| } | ||
|
|
||
| /** | ||
| * Object key under which the report of this player is stored in the object storage, filled by the | ||
| * {@link GameDesyncEnricher}. Only points to an existing file if {@link #getReportUploadedAt()} is set. | ||
| */ | ||
| @Transient | ||
| @ComputedAttribute | ||
| public String getReportFileName() { | ||
| return reportFileName; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/faforever/api/data/listeners/GameDesyncEnricher.java
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,23 @@ | ||
| package com.faforever.api.data.listeners; | ||
|
|
||
| import com.faforever.api.data.domain.GameDesync; | ||
| import com.faforever.api.desync.DesyncReportService; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.persistence.PostLoad; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class GameDesyncEnricher { | ||
|
|
||
| private static DesyncReportService desyncReportService; | ||
|
|
||
| @Inject | ||
| public void init(DesyncReportService desyncReportService) { | ||
| GameDesyncEnricher.desyncReportService = desyncReportService; | ||
| } | ||
|
|
||
| @PostLoad | ||
| public void enrich(GameDesync gameDesync) { | ||
| gameDesync.setReportFileName(desyncReportService.getReportFileName(gameDesync.getGame().getId(), gameDesync.getPlayer().getId())); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/faforever/api/desync/DesyncReportController.java
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,42 @@ | ||
| package com.faforever.api.desync; | ||
|
|
||
| import com.faforever.api.player.PlayerService; | ||
| import com.faforever.api.security.OAuthScope; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.security.access.prepost.PreAuthorize; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @RestController | ||
| @RequestMapping(path = "/game/{gameId}/desync") | ||
| @RequiredArgsConstructor | ||
| public class DesyncReportController { | ||
|
|
||
| private final PlayerService playerService; | ||
| private final DesyncReportService desyncReportService; | ||
|
|
||
| @Operation(summary = "Upload the desync report (a zip file) of the authenticated player for a game", | ||
| description = "A repeated upload for the same game replaces the previous report.") | ||
| @ApiResponse(responseCode = "204", description = "Report stored") | ||
| @ApiResponse(responseCode = "403", description = "The player did not participate in this game") | ||
| @ApiResponse(responseCode = "422", description = "The file is empty or larger than the allowed size") | ||
| @PostMapping(path = "/report", consumes = "multipart/form-data") | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| @PreAuthorize("hasScope('" + OAuthScope._UPLOAD_DESYNC_REPORT + "')") | ||
| public void uploadReport(Authentication authentication, | ||
| @PathVariable("gameId") int gameId, | ||
| @RequestParam("file") MultipartFile file) throws IOException { | ||
| desyncReportService.storeReport(playerService.getPlayer(authentication).getId(), gameId, file); | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
src/main/java/com/faforever/api/desync/DesyncReportService.java
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,90 @@ | ||
| package com.faforever.api.desync; | ||
|
|
||
| import com.faforever.api.config.FafApiProperties; | ||
| import com.faforever.api.config.FafApiProperties.DesyncReport; | ||
| import com.faforever.api.error.ApiException; | ||
| import com.faforever.api.error.Error; | ||
| import com.faforever.api.error.ErrorCode; | ||
| import com.faforever.api.game.GameDesyncRepository; | ||
| import com.faforever.api.game.GamePlayerStatsRepository; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.security.access.AccessDeniedException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import software.amazon.awssdk.core.sync.RequestBody; | ||
| import software.amazon.awssdk.http.ContentStreamProvider; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| public class DesyncReportService { | ||
| static final String CONTENT_TYPE = "application/zip"; | ||
| private static final String REPORT_FILE_NAME_FORMAT = "%d/%d.zip"; | ||
|
|
||
| private final DesyncReport properties; | ||
| private final GamePlayerStatsRepository gamePlayerStatsRepository; | ||
| private final GameDesyncRepository gameDesyncRepository; | ||
| private final S3Client s3Client; | ||
|
|
||
| public DesyncReportService(FafApiProperties properties, | ||
| GamePlayerStatsRepository gamePlayerStatsRepository, | ||
| GameDesyncRepository gameDesyncRepository, | ||
| @Qualifier("desyncReportS3Client") S3Client s3Client) { | ||
| this.properties = properties.getDesyncReport(); | ||
| this.gamePlayerStatsRepository = gamePlayerStatsRepository; | ||
| this.gameDesyncRepository = gameDesyncRepository; | ||
| this.s3Client = s3Client; | ||
| } | ||
|
|
||
| /** | ||
| * Object key of a report in the bucket. Deliberately not configurable: the key is not persisted but derived, so | ||
| * changing it would orphan every report that was already uploaded. | ||
| */ | ||
| public String getReportFileName(int gameId, int playerId) { | ||
| return REPORT_FILE_NAME_FORMAT.formatted(gameId, playerId); | ||
| } | ||
|
|
||
| /** | ||
| * Stores the desync report of a player and marks it as uploaded. A repeated upload for the same game replaces the | ||
| * previous report. | ||
| */ | ||
| public void storeReport(int playerId, int gameId, MultipartFile report) throws IOException { | ||
| long maxSize = properties.getMaxUploadSize().toBytes(); | ||
| long size = report.getSize(); | ||
| if (size < 1 || size > maxSize) { | ||
| throw new ApiException(new Error(ErrorCode.DESYNC_REPORT_INVALID_SIZE, maxSize)); | ||
| } | ||
|
|
||
| if (!gamePlayerStatsRepository.existsByGameIdAndPlayerId(gameId, playerId)) { | ||
| throw new AccessDeniedException("Player %d did not participate in game %d".formatted(playerId, gameId)); | ||
| } | ||
|
|
||
| String key = getReportFileName(gameId, playerId); | ||
| log.info("Player {} uploads a desync report for game {} ({} bytes)", playerId, gameId, size); | ||
|
|
||
| // the SDK reads the body more than once (checksum, then upload), so every read needs a fresh stream; the factory | ||
| // also closes the previous stream, as the provider contract requires | ||
| ContentStreamProvider streamProvider = ContentStreamProvider.fromInputStreamSupplier(() -> { | ||
| try { | ||
| return report.getInputStream(); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| }); | ||
|
|
||
| s3Client.putObject(PutObjectRequest.builder() | ||
| .bucket(properties.getBucket()) | ||
| .key(key) | ||
| .contentType(CONTENT_TYPE) | ||
| .contentLength(size) | ||
| .build(), RequestBody.fromContentProvider(streamProvider, size, CONTENT_TYPE)); | ||
|
|
||
| // only after the file is stored, so the column can be relied on to filter for existing reports | ||
| gameDesyncRepository.markReportUploaded(gameId, playerId); | ||
| } | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.