From 9852004a36480c7146b9e9591ddf03f2371b9542 Mon Sep 17 00:00:00 2001 From: "oura.a" Date: Wed, 27 Dec 2023 02:01:24 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20636:=20=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=82=BB=E3=83=B3=E3=82=B9=E7=99=BA=E8=A1=8C=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=8C=E9=81=85=E3=81=84=E5=95=8F=E9=A1=8C=E3=81=AE=E8=A7=A3?= =?UTF-8?q?=E6=B1=BA=E3=81=8A=E3=82=88=E3=81=B3=E3=83=88=E3=83=A9=E3=83=B3?= =?UTF-8?q?=E3=82=B6=E3=82=AF=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=8C=E5=8A=B9?= =?UTF-8?q?=E3=81=84=E3=81=A6=E3=81=AA=E3=81=91=E3=82=8C=E3=81=B0=E5=8A=B9?= =?UTF-8?q?=E3=81=8F=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task3243: ライセンス発行処理が遅い問題の解決およびトランザクションが効いてなければ効くよう修正する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/3243) ライセンス発行処理が重複して実行できてしまう不具合を修正しました。 ■修正内容 ・ライセンス注文テーブルの情報を取得する際に、行ロックを取得するよう修正 ・ライセンス注文テーブルに、「注文元アカウントID、POナンバー」の2カラムを対象とするインデックスを作成 ・以前に外部キー制約をつけた際、自動で作成されていたインデックスを削除 ■ロックについて 共有ロックと排他ロックがある ・共有ロック:共有ロック取得中でも、他のトランザクションが共有ロックを取得できる        排他ロックは取得できない ・排他ロック:排他ロック取得中は、他のトランザクションは共有ロック・排他ロック共に取得できない 今回の修正では、デフォルト設定で共有ロックを取得していた箇所を、明示的に排他ロックを取得するようにした。 ■行ロックについて ・インデックス行に対してロックをかけている  →インデックスが作成されていない、検索条件にヒットしないなどでうまく動かない   例)インデックスが作成されていないと、テーブル全体のロックとなってしまう ・上記の都合で検索条件が範囲指定のものにロックをかける際は注意が必要。(今回は一意指定なので問題なし) ■SQLiteを使ったユニットテストが`pessimistic_write`に対応していない件について `process.env.NODE_ENV`の値を参照(テスト実行中は`test`、ビルドした環境で動かすと`undifind`)し、 テスト実行の場合`pessimistic_write`を付与しないようクエリを修正した。 ## レビューポイント インデックスについて懸念点があるか? ## UIの変更 なし ## 動作確認状況 ローカルで以下を確認 ■発行処理について ・同じ注文に対し複数タブで発行処理を実行し、後発の処理が「ライセンス発行済みエラー」となることを確認 ・同一アカウントからの異なるPOナンバーの注文を同時に発行し、行ロックによる待ちが発生せず並列に処理されることを確認 ・別アカウントからの同一POナンバーの注文を同時に発行し、行ロックによる待ちが発生せず並列に処理されることを確認 ■インデックスについて 同一アカウントからの異なるPOナンバーの注文を同時に発行 ・インデックスを作成している状態で、行ロックによる待ちが発生せず並列に処理されることを確認 ・インデックスを削除した状態で、行ロックによる待ちが発生することを確認 ・migrate:up/downが正しく動作することを確認 ## 補足 以前のアカウント削除PBIで一時的に設定した外部キー制約の作成時に、自動でインデックスも作成されていたようです。 必要ないインデックスはどこかで削除する必要があるかと思っています。 --- .../db/migrations/050-add-index-licenses_order.sql | 7 +++++++ dictation_server/src/constants/index.ts | 13 +++++++++++++ .../licenses/licenses.repository.service.ts | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 dictation_server/db/migrations/050-add-index-licenses_order.sql diff --git a/dictation_server/db/migrations/050-add-index-licenses_order.sql b/dictation_server/db/migrations/050-add-index-licenses_order.sql new file mode 100644 index 0000000..006bde4 --- /dev/null +++ b/dictation_server/db/migrations/050-add-index-licenses_order.sql @@ -0,0 +1,7 @@ +-- +migrate Up +ALTER TABLE `license_orders` ADD INDEX `idx_from_account_id_and_po_number` (from_account_id,po_number); +ALTER TABLE `license_orders` DROP INDEX `license_orders_fk_from_account_id`; + +-- +migrate Down +ALTER TABLE `license_orders` DROP INDEX `idx_from_account_id_and_po_number`; +ALTER TABLE `license_orders` ADD INDEX `license_orders_fk_from_account_id` (from_account_id); \ No newline at end of file diff --git a/dictation_server/src/constants/index.ts b/dictation_server/src/constants/index.ts index 439c7ed..4c63064 100644 --- a/dictation_server/src/constants/index.ts +++ b/dictation_server/src/constants/index.ts @@ -295,3 +295,16 @@ export const TERM_TYPE = { * @const {string} */ export const USER_AUDIO_FORMAT = 'DS2(QP)'; + +/** + * ユニットテスト実行をしている場合のNODE_ENVの値 + * @const {string[]} + */ +export const NODE_ENV_TEST = 'test'; + +/** + * SQLのlockOptionの種類 + */ +export const DB_LOCK_MODE = { + PESSIMISTIC_WRITE: 'pessimistic_write', +}; diff --git a/dictation_server/src/repositories/licenses/licenses.repository.service.ts b/dictation_server/src/repositories/licenses/licenses.repository.service.ts index 988e8c1..2004be3 100644 --- a/dictation_server/src/repositories/licenses/licenses.repository.service.ts +++ b/dictation_server/src/repositories/licenses/licenses.repository.service.ts @@ -1,5 +1,5 @@ import { Injectable, Logger } from '@nestjs/common'; -import { DataSource, EntityManager, In, Not } from 'typeorm'; +import { DataSource, In } from 'typeorm'; import { LicenseOrder, License, @@ -9,9 +9,11 @@ import { } from './entity/license.entity'; import { CARD_LICENSE_LENGTH, + DB_LOCK_MODE, LICENSE_ALLOCATED_STATUS, LICENSE_ISSUE_STATUS, LICENSE_TYPE, + NODE_ENV_TEST, SWITCH_FROM_TYPE, TIERS, } from '../../constants'; @@ -415,15 +417,23 @@ export class LicensesRepositoryService { const licenseOrderRepo = entityManager.getRepository(LicenseOrder); const licenseRepo = entityManager.getRepository(License); + let lockOption = {}; + // テスト環境の場合は悲観的ロックを行わない + if (process.env.NODE_ENV !== NODE_ENV_TEST) { + lockOption = { mode: DB_LOCK_MODE.PESSIMISTIC_WRITE }; + } + const issuingOrder = await licenseOrderRepo.findOne({ where: { from_account_id: orderedAccountId, po_number: poNumber, }, comment: `${context.getTrackingId()}_${new Date().toUTCString()}`, + + ...lockOption, }); - // 注文が存在しない場合、エラー if (!issuingOrder) { + // 注文が存在しない場合、エラー throw new OrderNotFoundError(`No order found for PONumber:${poNumber}`); } // 既に発行済みの注文の場合、エラー