From 4ec79daaf981627f6c7bc2127d2ec738ac7fbb05 Mon Sep 17 00:00:00 2001 From: Sergey Okatov Date: Tue, 7 May 2024 09:38:11 +0500 Subject: [PATCH] m7l2 concurrent db --- .../kotlin/repo/V2AdRepoBaseTest.kt | 1 + .../src/commonMain/kotlin/MkplAdProcessor.kt | 2 ++ .../kotlin/repo/AdRepoPrepareUpdate.kt | 1 + .../src/commonMain/kotlin/repo/CheckLock.kt | 20 +++++++++++++++++++ .../kotlin/repo/BizRepoUpdateTest.kt | 2 ++ 5 files changed, 26 insertions(+) create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/CheckLock.kt diff --git a/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/repo/V2AdRepoBaseTest.kt b/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/repo/V2AdRepoBaseTest.kt index ced81b8..f3108c1 100644 --- a/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/repo/V2AdRepoBaseTest.kt +++ b/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/repo/V2AdRepoBaseTest.kt @@ -102,6 +102,7 @@ abstract class V2AdRepoBaseTest { assertEquals(ad.description, responseObj.ad?.description) assertEquals(ad.adType, responseObj.ad?.adType) assertEquals(ad.visibility, responseObj.ad?.visibility) + assertEquals(uuidNew, responseObj.ad?.lock) } } @Test diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt index ca3354f..b7fb0b4 100644 --- a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt @@ -106,6 +106,7 @@ class MkplAdProcessor( chain { title = "Логика сохранения" repoRead("Чтение объявления из БД") + checkLock("Проверяем консистентность по оптимистичной блокировке") repoPrepareUpdate("Подготовка объекта для обновления") repoUpdate("Обновление объявления в БД") } @@ -133,6 +134,7 @@ class MkplAdProcessor( chain { title = "Логика удаления" repoRead("Чтение объявления из БД") + checkLock("Проверяем консистентность по оптимистичной блокировке") repoPrepareDelete("Подготовка объекта для удаления") repoDelete("Удаление объявления из БД") } diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/AdRepoPrepareUpdate.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/AdRepoPrepareUpdate.kt index f67da7c..aa6530e 100644 --- a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/AdRepoPrepareUpdate.kt +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/AdRepoPrepareUpdate.kt @@ -16,6 +16,7 @@ fun ICorChainDsl.repoPrepareUpdate(title: String) = worker { description = adValidated.description adType = adValidated.adType visibility = adValidated.visibility + lock = adValidated.lock } } } diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/CheckLock.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/CheckLock.kt new file mode 100644 index 0000000..dff4cfb --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/repo/CheckLock.kt @@ -0,0 +1,20 @@ +package ru.otus.otuskotlin.marketplace.biz.repo + +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.helpers.fail +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.repo.errorRepoConcurrency +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker + +fun ICorChainDsl.checkLock(title: String) = worker { + this.title = title + description = """ + Проверка оптимистичной блокировки. Если не равна сохраненной в БД, значит данные запроса устарели + и необходимо их обновить вручную + """.trimIndent() + on { state == MkplState.RUNNING && adValidated.lock != adRepoRead.lock } + handle { + fail(errorRepoConcurrency(adRepoRead, adValidated.lock).errors) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/repo/BizRepoUpdateTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/repo/BizRepoUpdateTest.kt index 3834e76..097af20 100644 --- a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/repo/BizRepoUpdateTest.kt +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/repo/BizRepoUpdateTest.kt @@ -22,6 +22,7 @@ class BizRepoUpdateTest { ownerId = userId, adType = MkplDealSide.DEMAND, visibility = MkplVisibility.VISIBLE_PUBLIC, + lock = MkplAdLock("123-234-abc-ABC"), ) private val repo = AdRepositoryMock( invokeReadAd = { @@ -37,6 +38,7 @@ class BizRepoUpdateTest { description = "xyz", adType = MkplDealSide.DEMAND, visibility = MkplVisibility.VISIBLE_TO_GROUP, + lock = MkplAdLock("123-234-abc-ABC"), ) ) }