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
base: develop
Are you sure you want to change the base?
Changes from all commits
7c255d7
3786eba
cf4a3d5
65ae522
41594ee
9b54713
af7d8a5
acafa2a
cbe39c7
2d0403a
7f76448
a8a4b58
1cbd764
72f1c26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Primary; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.regions.Region; | ||
|
|
@@ -19,6 +20,7 @@ public class S3Config { | |
| private final FafApiProperties properties; | ||
|
|
||
| @Bean | ||
| @Primary | ||
| public S3Client s3Client() { | ||
| return S3Client.builder() | ||
| .endpointOverride(URI.create(properties.getS3().getEndpoint())) | ||
|
|
@@ -46,4 +48,23 @@ public S3Presigner s3Presigner() { | |
| .build()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Client for the dedicated desync report bucket, which has its own endpoint and credentials. | ||
| */ | ||
| @Bean | ||
| public S3Client desyncReportS3Client() { | ||
| FafApiProperties.DesyncReport desyncReport = properties.getDesyncReport(); | ||
| return S3Client.builder() | ||
| .endpointOverride(URI.create(desyncReport.getEndpoint())) | ||
| .region(Region.of(desyncReport.getRegion())) | ||
| .credentialsProvider(StaticCredentialsProvider.create( | ||
| AwsBasicCredentials.create(desyncReport.getAccessKey(), desyncReport.getSecretKey()) | ||
| )) | ||
| .serviceConfiguration(S3Configuration.builder() | ||
| .pathStyleAccessEnabled(true) | ||
| .chunkedEncodingEnabled(false) // R2 rejects the chunked signature of putObject with 403 | ||
| .build()) | ||
| .build(); | ||
| } | ||
|
Comment on lines
+63
to
+69
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. Use a repeatable stream for report uploads. With the default checksum settings, reports above 5 MB can fail on both HTTP and HTTPS. Tomcat stores these reports on disk, and the SDK reads the non-resettable stream for the checksum before requesting it again for upload. Supply a provider that reopens the multipart file for each read.
|
||
| } | ||
| 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; | ||
| } | ||
| } |
| 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())); | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| 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 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; | ||
| } | ||
|
|
||
| public String getReportFileName(int gameId, int playerId) { | ||
| return properties.getReportFileNameFormat().formatted(gameId, playerId); | ||
|
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. Preserve the object key used by each upload. If Persist the uploaded object key, or require an object migration when the format changes.
|
||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| ContentStreamProvider streamProvider = () -> { | ||
| 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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.