감사 상세를 무손실 압축하고 이전 기수 정리 처리량을 개선한다
This commit is contained in:
1 parent
da79013226
commit
078b9aa205
23 files changed
+504
-95
No files matched your search
@@ -13,6 +13,7 @@ export * from './scenario/databaseUrl.js';
|
||||
export * from './scenario/mapLoader.js';
|
||||
export * from './scenario/scenarioSeeder.js';
|
||||
export * from './scenario/gameCancellation.js';
|
||||
export * from './playAudit/retention.js';
|
||||
export * from './turn/types.js';
|
||||
export * from './turn/worldLoader.js';
|
||||
export * from './turn/inMemoryWorld.js';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { GamePrisma, InputJsonValue } from '@sammo-ts/infra';
|
||||
import { encodeAuditDecisionSteps, type GamePrisma, type InputJsonValue } from '@sammo-ts/infra';
|
||||
import { auditPolicyHash } from './policy.js';
|
||||
import type { PendingAuditDecision } from './decision.js';
|
||||
|
||||
@@ -50,7 +50,9 @@ export const persistAuditDecisions = async (
|
||||
chunks.push({
|
||||
decisionId: decision.id,
|
||||
ordinal: start / AUDIT_DECISION_CHUNK_STEPS,
|
||||
steps: decision.steps.slice(start, start + AUDIT_DECISION_CHUNK_STEPS) as InputJsonValue,
|
||||
steps: encodeAuditDecisionSteps(
|
||||
decision.steps.slice(start, start + AUDIT_DECISION_CHUNK_STEPS) as InputJsonValue[]
|
||||
),
|
||||
});
|
||||
if (chunks.length === BATCH) {
|
||||
await tx.playAuditDecisionChunk.createMany({ data: chunks, skipDuplicates: true });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { asRecord } from '@sammo-ts/common';
|
||||
import type { GamePrismaClient } from '@sammo-ts/infra';
|
||||
import { GamePrisma, type GamePrismaClient } from '@sammo-ts/infra';
|
||||
|
||||
export const AUDIT_RETENTION_BATCH_SIZE = 200;
|
||||
export type AuditRetentionResult = { status: 'progress' | 'complete' | 'busy' | 'identityChanged'; deleted: number };
|
||||
@@ -13,6 +13,7 @@ export const prunePreviousAuditBatch = async (
|
||||
return db.$transaction(
|
||||
async (tx) => {
|
||||
await tx.$executeRaw`SET LOCAL statement_timeout = '2000ms'`;
|
||||
await tx.$executeRaw`SET LOCAL lock_timeout = '250ms'`;
|
||||
// seeder와 동일한 잠금이다. RESET을 기다리게 하지 않고 다음 batch에서 재시도한다.
|
||||
const [lock] = await tx.$queryRaw<{ locked: boolean }[]>`
|
||||
SELECT pg_try_advisory_xact_lock(hashtextextended(current_schema(), 0)) AS locked
|
||||
@@ -32,29 +33,43 @@ export const prunePreviousAuditBatch = async (
|
||||
});
|
||||
return { status: 'progress', deleted: deleted.count };
|
||||
}
|
||||
const [decision] = await tx.$queryRaw<{ id: string }[]>`
|
||||
SELECT id FROM play_audit_decision WHERE id = COALESCE(
|
||||
(SELECT id FROM play_audit_decision WHERE server_id < ${expectedServerId}
|
||||
ORDER BY server_id, general_id, tick, id LIMIT 1),
|
||||
(SELECT id FROM play_audit_decision WHERE server_id > ${expectedServerId}
|
||||
ORDER BY server_id, general_id, tick, id LIMIT 1)
|
||||
) FOR UPDATE
|
||||
// 이미 있는 (server_id,nation_id,tick,id) 인덱스 순서로 여러 부모를 잠근다.
|
||||
// chunk 총량과 빈 header 삭제량의 합도 BATCH 이하로 제한한다.
|
||||
let decisions = await tx.$queryRaw<{ id: string }[]>`
|
||||
SELECT id FROM play_audit_decision WHERE server_id < ${expectedServerId}
|
||||
ORDER BY server_id, nation_id, tick, id LIMIT ${AUDIT_RETENTION_BATCH_SIZE} FOR UPDATE
|
||||
`;
|
||||
if (decision) {
|
||||
const chunks = await tx.playAuditDecisionChunk.findMany({
|
||||
where: { decisionId: decision.id },
|
||||
orderBy: { ordinal: 'asc' },
|
||||
take: AUDIT_RETENTION_BATCH_SIZE,
|
||||
select: { ordinal: true },
|
||||
if (!decisions.length) {
|
||||
decisions = await tx.$queryRaw<{ id: string }[]>`
|
||||
SELECT id FROM play_audit_decision WHERE server_id > ${expectedServerId}
|
||||
ORDER BY server_id, nation_id, tick, id LIMIT ${AUDIT_RETENTION_BATCH_SIZE} FOR UPDATE
|
||||
`;
|
||||
}
|
||||
if (decisions.length) {
|
||||
const ids = decisions.map(({ id }) => id);
|
||||
const [chunks] = await tx.$queryRaw<{ deleted: number }[]>(GamePrisma.sql`
|
||||
WITH batch AS (
|
||||
SELECT decision_id, ordinal FROM play_audit_decision_chunk
|
||||
WHERE decision_id IN (${GamePrisma.join(ids)})
|
||||
ORDER BY decision_id, ordinal LIMIT ${AUDIT_RETENTION_BATCH_SIZE}
|
||||
), removed AS (
|
||||
DELETE FROM play_audit_decision_chunk c USING batch b
|
||||
WHERE c.decision_id=b.decision_id AND c.ordinal=b.ordinal RETURNING 1
|
||||
) SELECT count(*)::integer AS deleted FROM removed
|
||||
`);
|
||||
const count = chunks?.deleted ?? 0;
|
||||
const remaining = AUDIT_RETENTION_BATCH_SIZE - count;
|
||||
if (!remaining) return { status: 'progress', deleted: count };
|
||||
const empty = await tx.playAuditDecision.findMany({
|
||||
where: { id: { in: ids }, chunks: { none: {} } },
|
||||
select: { id: true },
|
||||
orderBy: { id: 'asc' },
|
||||
take: remaining,
|
||||
});
|
||||
if (chunks.length) {
|
||||
const deleted = await tx.playAuditDecisionChunk.deleteMany({
|
||||
where: { decisionId: decision.id, ordinal: { in: chunks.map((row) => row.ordinal) } },
|
||||
});
|
||||
return { status: 'progress', deleted: deleted.count };
|
||||
}
|
||||
await tx.playAuditDecision.delete({ where: { id: decision.id } });
|
||||
return { status: 'progress', deleted: 1 };
|
||||
const headers = await tx.playAuditDecision.deleteMany({
|
||||
where: { id: { in: empty.map(({ id }) => id) } },
|
||||
});
|
||||
return { status: 'progress', deleted: count + headers.count };
|
||||
}
|
||||
const policies = await tx.playAuditPolicy.findMany({
|
||||
where: { serverId: { not: expectedServerId } },
|
||||
|
||||
@@ -17,7 +17,7 @@ export const startAuditRetentionWorker = (options: {
|
||||
timer.unref();
|
||||
};
|
||||
const run = async () => {
|
||||
let delay = 1000;
|
||||
let delay = 100;
|
||||
try {
|
||||
const result = await options.prune();
|
||||
if (result.status === 'complete' || result.status === 'identityChanged') stopped = true;
|
||||
|
||||
Reference in new issue
Block a user