Skip to content

Commit 7d20c19

Browse files
alexp8GrantBartlettCopilotSneer-ra2rohsyl
authored
optimize getRecentLadderGames (#422)
* add debug logs in PodiumController * filter out observers from players_in queue count * Add back missing cache to player stat views, tweak cache to 10 mins (#418) * remvoe yuri check (#419) * remvoe yuri check * just remove the commented code for now, if need it later, fetch from git history * optimize getRecentLadderGames * Update cncnet-api/app/Http/Services/LadderService.php Co-authored-by: Copilot <[email protected]> * Hotfix for the faction policy service. * Fixed getRecentLadderGames. * monitor duration * removing caching from getRecentLadderGames * add more details in the duration log in matchMeUp * revert max connections back to 500 * Add a missing index on games table --------- Co-authored-by: Grant Bartlett <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Sneer <[email protected]> Co-authored-by: rohs <[email protected]>
1 parent 51e86a1 commit 7d20c19

File tree

6 files changed

+86
-20
lines changed

6 files changed

+86
-20
lines changed

cncnet-api/app/Http/Controllers/Api/V2/Qm/MatchUpController.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ private function onUpdate(Player $player, $request)
234234
*/
235235
private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?QmMatchPlayer $qmPlayer)
236236
{
237+
$startTime = microtime(true);
237238

238239
// Log::debug('Username : ' . $player->username . ' on ladder ' . $ladder->name);
239240
// Log::debug('Match Me Up Request Body : ' . json_encode($request->all()));
@@ -247,11 +248,12 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
247248
}
248249
catch (\RuntimeException $ex)
249250
{
250-
Log::error('Failed to create QM Player: ' . $ex->getMessage(), [
251+
$duration = microtime(true) - $startTime;
252+
Log::error('Failed to create QM Player: ' . $ex->getMessage() . " | onMatchMeUp exit: exception | duration: {$duration} seconds", [
251253
'player_id' => $player->id,
252254
'username' => $player->username,
255+
'ladder' => $ladder->abbreviation
253256
]);
254-
255257
return $this->quickMatchService->onFatalError($ex->getMessage());
256258
}
257259

@@ -260,6 +262,13 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
260262

261263
if (!$validSides)
262264
{
265+
$duration = microtime(true) - $startTime;
266+
Log::info("onMatchMeUp exit: invalid side | duration: {$duration} seconds", [
267+
'player_id' => $player->id,
268+
'username' => $player->username,
269+
'ladder' => $ladder->abbreviation,
270+
'side' => $request->side
271+
]);
263272
return $this->quickMatchService->onFatalError(
264273
'Side (' . $request->side . ') is not allowed'
265274
);
@@ -271,6 +280,13 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
271280
{
272281
$qmPlayer->ai_dat = $request->ai_dat;
273282
$qmPlayer->save();
283+
$duration = microtime(true) - $startTime;
284+
Log::info("onMatchMeUp exit: ai_dat error | duration: {$duration} seconds", [
285+
'player_id' => $player->id,
286+
'username' => $player->username,
287+
'ladder' => $ladder->abbreviation,
288+
'ai_dat' => $request->ai_dat
289+
]);
274290
return $this->quickMatchService->onFatalError(
275291
'Error, please contact us on the CnCNet Discord'
276292
);
@@ -302,6 +318,12 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
302318
$spawnStruct = QuickMatchSpawnService::createSpawnStruct($qmMatch, $qmPlayer, $ladder, $ladder->qmLadderRules);
303319
$spawnStruct = QuickMatchSpawnService::addQuickMatchAISpawnIni($spawnStruct, $ladder, AIHelper::BRUTAL_AI);
304320

321+
$duration = microtime(true) - $startTime;
322+
Log::info("onMatchMeUp exit: ai match | duration: {$duration} seconds", [
323+
'player_id' => $player->id,
324+
'username' => $player->username,
325+
'ladder' => $ladder->abbreviation
326+
]);
305327
return response()->json($spawnStruct);
306328
}
307329

@@ -331,6 +353,12 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
331353

332354
$qmPlayer->touch();
333355

356+
$duration = microtime(true) - $startTime;
357+
Log::info("onMatchMeUp exit: queued opponent | duration: {$duration} seconds", [
358+
'player_id' => $player->id,
359+
'username' => $player->username,
360+
'ladder' => $ladder->abbreviation
361+
]);
334362
return $this->quickMatchService->onCheckback($alert);
335363
}
336364

@@ -353,6 +381,12 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
353381
$qmPlayer->waiting = false;
354382
$qmPlayer->save();
355383
Log::info("MatchUpController ** Player Check: QMPlayer: $qmPlayer - QMMatch: $qmMatch");
384+
$duration = microtime(true) - $startTime;
385+
Log::info("onMatchMeUp exit: not enough players | duration: {$duration} seconds", [
386+
'player_id' => $player->id,
387+
'username' => $player->username,
388+
'ladder' => $ladder->abbreviation
389+
]);
356390
return $this->quickMatchService->onCheckback($alert);
357391
}
358392

@@ -389,6 +423,12 @@ private function onMatchMeUp(Request $request, Ladder $ladder, Player $player, ?
389423
$qmPlayer->waiting = false;
390424
$qmPlayer->save();
391425

426+
$duration = microtime(true) - $startTime;
427+
Log::info("onMatchMeUp exit: match found | duration: {$duration} seconds", [
428+
'player_id' => $player->id,
429+
'username' => $player->username,
430+
'ladder' => $ladder->abbreviation
431+
]);
392432
return response()->json($spawnStruct);
393433
}
394434
}

cncnet-api/app/Http/Services/FactionPolicyService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function applyPolicy1v1(MapPool $pool, Ladder $ladder, LadderHistory $his
103103
return;
104104
}
105105

106-
$currentSides = [$p1->actual_side, $p2->actual_side];
106+
$currentSides = [(int)$p1->actual_side, (int)$p2->actual_side];
107107
Log::debug('applyPolicy1v1: currentSides', $currentSides);
108108

109109
if ($currentSides[0] < 0 || $currentSides[1] < 0)

cncnet-api/app/Http/Services/LadderService.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -403,24 +403,23 @@ public function getLadderByGameAbbreviation($game, $limit = 25)
403403

404404
public function getRecentLadderGames(LadderHistory $history, $limit = 4)
405405
{
406-
return Game::where("ladder_history_id", "=", $history->id)
406+
return Game::where("ladder_history_id", $history->id)
407407
->whereNotNull('game_report_id')
408-
->orderBy("games.id", "DESC")
409-
->limit($limit)
408+
->orderByDesc("id")
409+
->select(['id', 'ladder_history_id', 'game_report_id', 'qm_match_id', 'hash', 'game_type', 'updated_at'])
410410
->with([
411-
'report',
412-
'report.playerGameReports.player',
413-
'report.playerGameReports.player.qmPlayer',
414-
'report.playerGameReports.clan',
415-
'report.playerGameReports.stats',
416-
'qmMatch.map.map',
417-
'qmMatch.players'
411+
'report:id,game_id,duration,fps',
412+
'report.playerGameReports:game_report_id,player_id,clan_id,points,stats_id,won,spectator',
413+
'report.playerGameReports.player:id,username',
414+
'report.playerGameReports.player.qmPlayer:id,player_id,team',
415+
'report.playerGameReports.clan:id,short',
416+
'report.playerGameReports.stats:id,sid,cty',
417+
'qmMatch:id,qm_map_id',
418+
'qmMatch.map:id,description,map_id',
419+
'qmMatch.map.map:id,name,hash,image_path,image_hash,filename',
420+
'qmMatch.players:id,qm_match_id,player_id,team'
418421
])
419-
->when(
420-
$history->ladder->clans_allowed,
421-
fn($q) => $q->with([]),
422-
fn($q) => $q->with([]),
423-
)
422+
->limit($limit)
424423
->get();
425424
}
426425

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('games', function ($table) {
15+
$table->index('ladder_history_id');
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('games', function ($table) {
25+
$table->dropIndex('games_ladder_history_id_index');
26+
});
27+
}
28+
};

cncnet-api/routes/api.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
Route::get('/{game}', [\App\Http\Controllers\ApiLadderController::class, 'getLadder']);
9292
Route::get('/{game}/game/{gameId}', [\App\Http\Controllers\ApiLadderController::class, 'getLadderGame']);
9393
Route::get('/{game}/winners/', [\App\Http\Controllers\ApiLadderController::class, 'getLadderWinners']);
94-
Route::get('/{game}/games/recent/{count}', [\App\Http\Controllers\ApiLadderController::class, 'getLadderRecentGamesList']);
9594

9695
Route::get('/{abbreviation}/players', [\App\Http\Controllers\ApiIrcController::class, 'getPlayerNames']);
9796
Route::get('/{abbreviation}/active', [\App\Http\Controllers\ApiIrcController::class, 'getActivePlayers']);

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ services:
8282
image: mariadb:latest
8383
container_name: cncnet_ladder_mysql${ENV_SUFFIX}
8484
restart: unless-stopped
85-
command: --max_connections=200 --max_allowed_packet=128M --log_warnings=3 --wait_timeout=600 --slow-query-log --interactive_timeout=600 --long_query_time=2
85+
command: --max_connections=500 --max_allowed_packet=128M --log_warnings=3 --wait_timeout=600 --slow-query-log --interactive_timeout=600 --long_query_time=2
8686
environment:
8787
MYSQL_DATABASE: ${MYSQL_DATABASE}
8888
MYSQL_USER: ${MYSQL_USER}

0 commit comments

Comments
 (0)