perf: optimize user internal history reads
This commit is contained in:
1 parent
e80313e459
commit
d6e0bbf0e8
4 files changed
+293
-44
No files matched your search
@@ -205,24 +205,36 @@ describe('CommunityStatsService', () => {
|
|||||||
get: jest.fn().mockResolvedValue(null),
|
get: jest.fn().mockResolvedValue(null),
|
||||||
set: jest.fn().mockResolvedValue(undefined),
|
set: jest.fn().mockResolvedValue(undefined),
|
||||||
} as unknown as jest.Mocked<CommunityStatsCacheService>;
|
} as unknown as jest.Mocked<CommunityStatsCacheService>;
|
||||||
|
const matchStatsRepository = {
|
||||||
|
findUserRecordFragments: jest
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue([{ matchId, records: [participant] }]),
|
||||||
|
findDetailsByMatches: jest
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue([{ matchId, gameDetails: [] }]),
|
||||||
|
} as unknown as jest.Mocked<MatchStatsRepository>;
|
||||||
const service = new CommunityStatsService(
|
const service = new CommunityStatsService(
|
||||||
{} as ConnectionService,
|
{} as ConnectionService,
|
||||||
cacheService,
|
cacheService,
|
||||||
{} as MatchStatsRepository,
|
matchStatsRepository,
|
||||||
{} as MatchStatsService,
|
{} as MatchStatsService,
|
||||||
);
|
);
|
||||||
(service as unknown as { buildBaseDataset: jest.Mock }).buildBaseDataset =
|
(service as unknown as { resolveContext: jest.Mock }).resolveContext =
|
||||||
jest.fn().mockResolvedValue({
|
jest.fn().mockResolvedValue({
|
||||||
|
community: {},
|
||||||
season: {
|
season: {
|
||||||
id: 'all',
|
id: 'all',
|
||||||
label: '전체',
|
label: '전체',
|
||||||
startTime: null,
|
startedAt: null,
|
||||||
endTime: null,
|
endedAt: null,
|
||||||
},
|
},
|
||||||
matches: [],
|
from: null,
|
||||||
records: [participant],
|
to: null,
|
||||||
detailsByMatchId: new Map(),
|
|
||||||
});
|
});
|
||||||
|
const buildBaseDataset = jest.spyOn(
|
||||||
|
service as unknown as { buildBaseDataset: jest.Mock },
|
||||||
|
'buildBaseDataset',
|
||||||
|
);
|
||||||
|
|
||||||
const response = await service.getUserInternalHistory({
|
const response = await service.getUserInternalHistory({
|
||||||
communityId,
|
communityId,
|
||||||
@@ -236,9 +248,85 @@ describe('CommunityStatsService', () => {
|
|||||||
gameNumber: 2,
|
gameNumber: 2,
|
||||||
queueLabel: '내전',
|
queueLabel: '내전',
|
||||||
});
|
});
|
||||||
|
expect(matchStatsRepository.findUserRecordFragments).toHaveBeenCalledWith({
|
||||||
|
communityId,
|
||||||
|
userId,
|
||||||
|
fromGameCreation: undefined,
|
||||||
|
toGameCreation: undefined,
|
||||||
|
limit: 300,
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
matchStatsRepository.findDetailsByMatches.mock.calls[0][0].matchIds.map(
|
||||||
|
(id) => id.toString(),
|
||||||
|
),
|
||||||
|
).toEqual([matchId.toString()]);
|
||||||
|
expect(buildBaseDataset).not.toHaveBeenCalled();
|
||||||
expect(cacheService.set).toHaveBeenCalledWith(
|
expect(cacheService.set).toHaveBeenCalledWith(
|
||||||
expect.stringContaining(`userInternal:v2:${userId.toString()}`),
|
expect.stringContaining(`userInternal:v3:${userId.toString()}`),
|
||||||
response,
|
response,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('coalesces concurrent user internal history cache misses', async () => {
|
||||||
|
const communityId = new mongoose.Types.ObjectId();
|
||||||
|
const matchId = new mongoose.Types.ObjectId();
|
||||||
|
const userId = new mongoose.Types.ObjectId();
|
||||||
|
const participant = record(matchId);
|
||||||
|
participant.userId = userId.toString();
|
||||||
|
|
||||||
|
let resolveFragments:
|
||||||
|
| ((fragments: Array<{ matchId: mongoose.Types.ObjectId; records: InternalRecord[] }>) => void)
|
||||||
|
| undefined;
|
||||||
|
const fragmentsPromise = new Promise<
|
||||||
|
Array<{ matchId: mongoose.Types.ObjectId; records: InternalRecord[] }>
|
||||||
|
>((resolve) => {
|
||||||
|
resolveFragments = resolve;
|
||||||
|
});
|
||||||
|
const cacheService = {
|
||||||
|
get: jest.fn().mockResolvedValue(null),
|
||||||
|
set: jest.fn().mockResolvedValue(undefined),
|
||||||
|
} as unknown as jest.Mocked<CommunityStatsCacheService>;
|
||||||
|
const matchStatsRepository = {
|
||||||
|
findUserRecordFragments: jest.fn().mockReturnValue(fragmentsPromise),
|
||||||
|
findDetailsByMatches: jest.fn().mockResolvedValue([]),
|
||||||
|
} as unknown as jest.Mocked<MatchStatsRepository>;
|
||||||
|
const service = new CommunityStatsService(
|
||||||
|
{} as ConnectionService,
|
||||||
|
cacheService,
|
||||||
|
matchStatsRepository,
|
||||||
|
{} as MatchStatsService,
|
||||||
|
);
|
||||||
|
(service as unknown as { resolveContext: jest.Mock }).resolveContext =
|
||||||
|
jest.fn().mockResolvedValue({
|
||||||
|
community: {},
|
||||||
|
season: {
|
||||||
|
id: 'all',
|
||||||
|
label: '전체',
|
||||||
|
startedAt: null,
|
||||||
|
endedAt: null,
|
||||||
|
},
|
||||||
|
from: null,
|
||||||
|
to: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const first = service.getUserInternalHistory({
|
||||||
|
communityId,
|
||||||
|
userId,
|
||||||
|
filters: { limit: 5 },
|
||||||
|
});
|
||||||
|
const second = service.getUserInternalHistory({
|
||||||
|
communityId,
|
||||||
|
userId,
|
||||||
|
filters: { limit: 5 },
|
||||||
|
});
|
||||||
|
await Promise.resolve();
|
||||||
|
resolveFragments?.([{ matchId, records: [participant] }]);
|
||||||
|
const [firstResponse, secondResponse] = await Promise.all([first, second]);
|
||||||
|
|
||||||
|
expect(firstResponse).toBe(secondResponse);
|
||||||
|
expect(matchStatsRepository.findUserRecordFragments).toHaveBeenCalledTimes(
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
expect(cacheService.set).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -24,7 +24,11 @@ import type {
|
|||||||
TeamInsightsResponse,
|
TeamInsightsResponse,
|
||||||
UserInternalHistoryResponse,
|
UserInternalHistoryResponse,
|
||||||
} from './community-stats.types';
|
} from './community-stats.types';
|
||||||
import { MatchStatsRepository } from './match-stats.repository';
|
import {
|
||||||
|
MatchStatsDetailFragment,
|
||||||
|
MatchStatsRepository,
|
||||||
|
MatchStatsUserRecordFragment,
|
||||||
|
} from './match-stats.repository';
|
||||||
import { MatchStatsService } from './match-stats.service';
|
import { MatchStatsService } from './match-stats.service';
|
||||||
import {
|
import {
|
||||||
InternalGameDetail,
|
InternalGameDetail,
|
||||||
@@ -55,6 +59,10 @@ interface BaseDataset {
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class CommunityStatsService {
|
export class CommunityStatsService {
|
||||||
private readonly logger = new Logger(CommunityStatsService.name);
|
private readonly logger = new Logger(CommunityStatsService.name);
|
||||||
|
private readonly userInternalHistoryInFlight = new Map<
|
||||||
|
string,
|
||||||
|
Promise<UserInternalHistoryResponse>
|
||||||
|
>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly connectionService: ConnectionService,
|
private readonly connectionService: ConnectionService,
|
||||||
@@ -293,7 +301,7 @@ export class CommunityStatsService {
|
|||||||
filters: CommunityStatsFilters;
|
filters: CommunityStatsFilters;
|
||||||
}): Promise<UserInternalHistoryResponse> {
|
}): Promise<UserInternalHistoryResponse> {
|
||||||
const key = this.cacheKey(
|
const key = this.cacheKey(
|
||||||
`userInternal:v2:${params.userId.toString()}`,
|
`userInternal:v3:${params.userId.toString()}`,
|
||||||
params.communityId,
|
params.communityId,
|
||||||
params.filters,
|
params.filters,
|
||||||
);
|
);
|
||||||
@@ -301,38 +309,20 @@ export class CommunityStatsService {
|
|||||||
await this.cacheService.get<UserInternalHistoryResponse>(key);
|
await this.cacheService.get<UserInternalHistoryResponse>(key);
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
const dataset = await this.buildBaseDataset(
|
// Auction viewers can all request the same next player's internal history
|
||||||
params.communityId,
|
// at once. Coalesce local cache misses so one player change does one DB read.
|
||||||
params.filters,
|
const inFlight = this.userInternalHistoryInFlight.get(key);
|
||||||
);
|
if (inFlight) return inFlight;
|
||||||
const allUserRecords = dataset.records
|
|
||||||
.filter((record) => record.userId === params.userId.toString())
|
const promise = this.buildUserInternalHistoryResponse(params, key);
|
||||||
.sort((left, right) => right.gameCreation - left.gameCreation);
|
this.userInternalHistoryInFlight.set(key, promise);
|
||||||
const records = this.applyChampionFilter(allUserRecords, params.filters);
|
try {
|
||||||
const limit = this.clampLimit(params.filters.limit, 30, 50);
|
return await promise;
|
||||||
const cursor = params.filters.cursor;
|
} finally {
|
||||||
const page = records
|
if (this.userInternalHistoryInFlight.get(key) === promise) {
|
||||||
.filter((record) => !cursor || record.gameCreation < cursor)
|
this.userInternalHistoryInFlight.delete(key);
|
||||||
.slice(0, limit + 1);
|
}
|
||||||
const visible = page.slice(0, limit);
|
}
|
||||||
const response: UserInternalHistoryResponse = {
|
|
||||||
season: dataset.season,
|
|
||||||
championStats: this.buildUserChampionStats(
|
|
||||||
allUserRecords,
|
|
||||||
dataset.season,
|
|
||||||
),
|
|
||||||
summary: this.buildUserSummary(records, visible.length || limit),
|
|
||||||
matches: visible.map((record) => this.toRecentMatchView(record, dataset)),
|
|
||||||
nextCursor:
|
|
||||||
page.length > limit
|
|
||||||
? (visible[visible.length - 1]?.gameCreation ?? null)
|
|
||||||
: null,
|
|
||||||
lastFetchedAt: records[0]
|
|
||||||
? new Date(records[0].gameCreation).toISOString()
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
await this.cacheService.set(key, response);
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public invalidateCommunity(
|
public invalidateCommunity(
|
||||||
@@ -397,6 +387,120 @@ export class CommunityStatsService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async buildUserInternalHistoryResponse(
|
||||||
|
params: {
|
||||||
|
communityId: mongoose.Types.ObjectId;
|
||||||
|
userId: mongoose.Types.ObjectId;
|
||||||
|
filters: CommunityStatsFilters;
|
||||||
|
},
|
||||||
|
cacheKey: string,
|
||||||
|
): Promise<UserInternalHistoryResponse> {
|
||||||
|
const context = await this.resolveContext(
|
||||||
|
params.communityId,
|
||||||
|
params.filters,
|
||||||
|
);
|
||||||
|
const userId = params.userId.toString();
|
||||||
|
const fragments = await this.matchStatsRepository.findUserRecordFragments({
|
||||||
|
communityId: params.communityId,
|
||||||
|
userId: params.userId,
|
||||||
|
fromGameCreation: context.from?.getTime(),
|
||||||
|
toGameCreation: context.to?.getTime(),
|
||||||
|
limit: MAX_MATCH_SCAN,
|
||||||
|
});
|
||||||
|
const allUserRecords = this.extractUserRecords(
|
||||||
|
fragments,
|
||||||
|
userId,
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
const records = this.applyChampionFilter(allUserRecords, params.filters);
|
||||||
|
const limit = this.clampLimit(params.filters.limit, 30, 50);
|
||||||
|
const cursor = params.filters.cursor;
|
||||||
|
const page = records
|
||||||
|
.filter((record) => !cursor || record.gameCreation < cursor)
|
||||||
|
.slice(0, limit + 1);
|
||||||
|
const visible = page.slice(0, limit);
|
||||||
|
const detailsByMatchId = await this.loadVisibleDetails(
|
||||||
|
params.communityId,
|
||||||
|
visible,
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
const dataset: BaseDataset = {
|
||||||
|
season: context.season,
|
||||||
|
matches: [],
|
||||||
|
records: allUserRecords,
|
||||||
|
detailsByMatchId,
|
||||||
|
};
|
||||||
|
const response: UserInternalHistoryResponse = {
|
||||||
|
season: context.season,
|
||||||
|
championStats: this.buildUserChampionStats(
|
||||||
|
allUserRecords,
|
||||||
|
context.season,
|
||||||
|
),
|
||||||
|
summary: this.buildUserSummary(records, visible.length || limit),
|
||||||
|
matches: visible.map((record) => this.toRecentMatchView(record, dataset)),
|
||||||
|
nextCursor:
|
||||||
|
page.length > limit
|
||||||
|
? (visible[visible.length - 1]?.gameCreation ?? null)
|
||||||
|
: null,
|
||||||
|
lastFetchedAt: records[0]
|
||||||
|
? new Date(records[0].gameCreation).toISOString()
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
await this.cacheService.set(cacheKey, response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private extractUserRecords(
|
||||||
|
fragments: MatchStatsUserRecordFragment[],
|
||||||
|
userId: string,
|
||||||
|
context: CommunityStatsContext,
|
||||||
|
): InternalRecord[] {
|
||||||
|
return fragments
|
||||||
|
.flatMap((fragment) =>
|
||||||
|
(fragment.records as InternalRecord[]).filter(
|
||||||
|
(record) =>
|
||||||
|
record.userId === userId &&
|
||||||
|
this.isGameInRange(record.gameCreation, context),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.sort((left, right) => right.gameCreation - left.gameCreation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async loadVisibleDetails(
|
||||||
|
communityId: mongoose.Types.ObjectId,
|
||||||
|
visible: InternalRecord[],
|
||||||
|
context: CommunityStatsContext,
|
||||||
|
): Promise<Map<string, InternalGameDetail[]>> {
|
||||||
|
const matchIds = [
|
||||||
|
...new Set(
|
||||||
|
visible
|
||||||
|
.map((record) => record.matchId)
|
||||||
|
.filter((matchId) => mongoose.isValidObjectId(matchId)),
|
||||||
|
),
|
||||||
|
].map((matchId) => new mongoose.Types.ObjectId(matchId));
|
||||||
|
const fragments = await this.matchStatsRepository.findDetailsByMatches({
|
||||||
|
communityId,
|
||||||
|
matchIds,
|
||||||
|
});
|
||||||
|
return this.toDetailsByMatchId(fragments, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private toDetailsByMatchId(
|
||||||
|
fragments: MatchStatsDetailFragment[],
|
||||||
|
context: CommunityStatsContext,
|
||||||
|
): Map<string, InternalGameDetail[]> {
|
||||||
|
const detailsByMatchId = new Map<string, InternalGameDetail[]>();
|
||||||
|
for (const fragment of fragments) {
|
||||||
|
detailsByMatchId.set(
|
||||||
|
fragment.matchId.toString(),
|
||||||
|
(fragment.gameDetails as InternalGameDetail[]).filter((game) =>
|
||||||
|
this.isGameInRange(game.gameCreation, context),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return detailsByMatchId;
|
||||||
|
}
|
||||||
|
|
||||||
private async rebuildMissingOrStaleFragments(
|
private async rebuildMissingOrStaleFragments(
|
||||||
communityId: mongoose.Types.ObjectId,
|
communityId: mongoose.Types.ObjectId,
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
|
|||||||
@@ -3,6 +3,16 @@ import mongoose from 'mongoose';
|
|||||||
import { ConnectionService } from '../global/database/connection.service';
|
import { ConnectionService } from '../global/database/connection.service';
|
||||||
import type { MatchStats, MatchStatsDocument } from '../global/database/schema';
|
import type { MatchStats, MatchStatsDocument } from '../global/database/schema';
|
||||||
|
|
||||||
|
export type MatchStatsUserRecordFragment = Pick<
|
||||||
|
MatchStats,
|
||||||
|
'matchId' | 'records'
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type MatchStatsDetailFragment = Pick<
|
||||||
|
MatchStats,
|
||||||
|
'matchId' | 'gameDetails'
|
||||||
|
>;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MatchStatsRepository {
|
export class MatchStatsRepository {
|
||||||
constructor(private readonly connectionService: ConnectionService) {}
|
constructor(private readonly connectionService: ConnectionService) {}
|
||||||
@@ -68,6 +78,53 @@ export class MatchStatsRepository {
|
|||||||
toGameCreation?: number;
|
toGameCreation?: number;
|
||||||
}): Promise<MatchStatsDocument[]> {
|
}): Promise<MatchStatsDocument[]> {
|
||||||
const MatchStatsModel = await this.connectionService.getModel('matchStats');
|
const MatchStatsModel = await this.connectionService.getModel('matchStats');
|
||||||
|
const filter = this.buildUserFilter(params);
|
||||||
|
|
||||||
|
return MatchStatsModel.find(filter).sort({ scheduledAt: -1 }).exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findUserRecordFragments(params: {
|
||||||
|
communityId: mongoose.Types.ObjectId;
|
||||||
|
userId: mongoose.Types.ObjectId;
|
||||||
|
fromGameCreation?: number;
|
||||||
|
toGameCreation?: number;
|
||||||
|
limit?: number;
|
||||||
|
}): Promise<MatchStatsUserRecordFragment[]> {
|
||||||
|
const MatchStatsModel = await this.connectionService.getModel('matchStats');
|
||||||
|
const filter = this.buildUserFilter(params);
|
||||||
|
|
||||||
|
// User history only needs records for summaries/page selection; gameDetails
|
||||||
|
// are much larger and are fetched separately for the visible page.
|
||||||
|
return MatchStatsModel.find(filter)
|
||||||
|
.sort({ scheduledAt: -1 })
|
||||||
|
.limit(params.limit ?? 300)
|
||||||
|
.select({ _id: 0, matchId: 1, records: 1 })
|
||||||
|
.lean<MatchStatsUserRecordFragment[]>()
|
||||||
|
.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findDetailsByMatches(params: {
|
||||||
|
communityId: mongoose.Types.ObjectId;
|
||||||
|
matchIds: mongoose.Types.ObjectId[];
|
||||||
|
}): Promise<MatchStatsDetailFragment[]> {
|
||||||
|
if (params.matchIds.length === 0) return [];
|
||||||
|
|
||||||
|
const MatchStatsModel = await this.connectionService.getModel('matchStats');
|
||||||
|
return MatchStatsModel.find({
|
||||||
|
communityId: params.communityId,
|
||||||
|
matchId: { $in: params.matchIds },
|
||||||
|
})
|
||||||
|
.select({ _id: 0, matchId: 1, gameDetails: 1 })
|
||||||
|
.lean<MatchStatsDetailFragment[]>()
|
||||||
|
.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildUserFilter(params: {
|
||||||
|
communityId: mongoose.Types.ObjectId;
|
||||||
|
userId: mongoose.Types.ObjectId;
|
||||||
|
fromGameCreation?: number;
|
||||||
|
toGameCreation?: number;
|
||||||
|
}): Record<string, unknown> {
|
||||||
const filter: Record<string, unknown> = {
|
const filter: Record<string, unknown> = {
|
||||||
communityId: params.communityId,
|
communityId: params.communityId,
|
||||||
userIds: params.userId,
|
userIds: params.userId,
|
||||||
@@ -78,7 +135,6 @@ export class MatchStatsRepository {
|
|||||||
if (params.toGameCreation !== undefined) {
|
if (params.toGameCreation !== undefined) {
|
||||||
filter.gameCreationMin = { $lte: params.toGameCreation };
|
filter.gameCreationMin = { $lte: params.toGameCreation };
|
||||||
}
|
}
|
||||||
|
return filter;
|
||||||
return MatchStatsModel.find(filter).sort({ scheduledAt: -1 }).exec();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2371,6 +2371,7 @@ MatchStatsSchema.index({
|
|||||||
gameCreationMax: 1,
|
gameCreationMax: 1,
|
||||||
});
|
});
|
||||||
MatchStatsSchema.index({ communityId: 1, userIds: 1 });
|
MatchStatsSchema.index({ communityId: 1, userIds: 1 });
|
||||||
|
MatchStatsSchema.index({ communityId: 1, userIds: 1, scheduledAt: -1 });
|
||||||
MatchStatsSchema.index({ communityId: 1, championIds: 1 });
|
MatchStatsSchema.index({ communityId: 1, championIds: 1 });
|
||||||
|
|
||||||
export const tournamentModelDefinitions: ModelDefinition[] = [
|
export const tournamentModelDefinitions: ModelDefinition[] = [
|
||||||
|
|||||||
Reference in new issue
Block a user