From c872a837ebbc180f09a471d2cb54c1cb43561243 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 11:31:36 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor:=20SnowflakeIdGenerateService?= =?UTF-8?q?=E3=82=92=E6=94=B9=E8=89=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../other/SnowflakeIdGenerateService.kt | 13 ++++++----- .../TwitterSnowflakeIdGenerateServiceTest.kt | 22 +++++++------------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt index 3b82b1cc..26f3564b 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt @@ -24,9 +24,9 @@ import java.time.Instant @Suppress("MagicNumber") open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateService { - var lastTimeStamp: Long = -1 - var sequenceId: Int = 0 - val mutex = Mutex() + private var lastTimeStamp: Long = -1 + private var sequenceId: Long = 0 + private val mutex = Mutex() @Throws(IllegalStateException::class) override suspend fun generateId(): Long { @@ -34,7 +34,6 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe var timestamp = getTime() if (timestamp < lastTimeStamp) { timestamp = wait(timestamp) - // throw IllegalStateException(" $lastTimeStamp $timestamp ${lastTimeStamp-timestamp} ") } if (timestamp == lastTimeStamp) { sequenceId++ @@ -46,7 +45,7 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe sequenceId = 0 } lastTimeStamp = timestamp - return@withLock (timestamp - baseTime).shl(22).or(1L.shl(12)).or(sequenceId.toLong()) + return@withLock (timestamp - baseTime).shl(22).or(1L.shl(12)).or(sequenceId) } } @@ -77,8 +76,10 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe override fun hashCode(): Int { var result = baseTime.hashCode() result = 31 * result + lastTimeStamp.hashCode() - result = 31 * result + sequenceId + result = 31 * result + sequenceId.hashCode() result = 31 * result + mutex.hashCode() return result } + + } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt index 98855ca8..0c7cb432 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/other/TwitterSnowflakeIdGenerateServiceTest.kt @@ -1,28 +1,22 @@ package dev.usbharu.hideout.core.infrastructure.other -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test class TwitterSnowflakeIdGenerateServiceTest { @Test fun noDuplicateTest() = runBlocking { - val mutex = Mutex() - val mutableListOf = mutableListOf() - coroutineScope { - repeat(500000) { - launch(Dispatchers.IO) { - val id = TwitterSnowflakeIdGenerateService.generateId() - mutex.withLock { - mutableListOf.add(id) - } + + val mutableListOf = coroutineScope { + (1..10000).map { + async { + TwitterSnowflakeIdGenerateService.generateId() } - } + }.awaitAll() } assertEquals(0, mutableListOf.size - mutableListOf.toSet().size) From 457b6a12bed6592aa127062815985daad8c90324 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 12:04:31 +0900 Subject: [PATCH 2/8] =?UTF-8?q?refactor:=20userdetail=E3=81=AEtoString?= =?UTF-8?q?=E3=82=92=E6=94=B9=E8=89=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/userdetails/UserDetail.kt | 10 ++++++++++ .../model/userdetails/UserDetailHashedPassword.kt | 6 +++++- .../core/domain/model/userdetails/UserDetailId.kt | 6 +++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt index 7cd33a5f..183ec3e6 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt @@ -42,6 +42,16 @@ class UserDetail( } override fun hashCode(): Int = id.hashCode() + override fun toString(): String { + return "UserDetail(" + + "id=$id, " + + "actorId=$actorId, " + + "password=$password, " + + "autoAcceptFolloweeFollowRequest=$autoAcceptFolloweeFollowRequest, " + + "lastMigration=$lastMigration, " + + "homeTimelineId=$homeTimelineId" + + ")" + } companion object { @Suppress("LongParameterList") diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailHashedPassword.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailHashedPassword.kt index f0dc4399..8e5edf56 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailHashedPassword.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailHashedPassword.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.userdetails @JvmInline -value class UserDetailHashedPassword(val password: String) +value class UserDetailHashedPassword(val password: String) { + override fun toString(): String { + return "[MASKED]" + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailId.kt index cc048546..0641cd6e 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetailId.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.userdetails @JvmInline -value class UserDetailId(val id: Long) +value class UserDetailId(val id: Long) { + override fun toString(): String { + return "UserDetailId(id=$id)" + } +} From 7362d20565d19d3b955b69d4eb3708b636d5fa9e Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 12:04:47 +0900 Subject: [PATCH 3/8] =?UTF-8?q?fix:=20=E8=B5=B7=E5=8B=95=E3=81=97=E3=81=AA?= =?UTF-8?q?=E3=81=8B=E3=81=A3=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/db/migration/V1__Init_DB.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql b/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql index dd9dfa5c..6ce43568 100644 --- a/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql +++ b/hideout-core/src/main/resources/db/migration/V1__Init_DB.sql @@ -101,10 +101,12 @@ create table if not exists media url varchar(255) not null unique, remote_url varchar(255) null unique, thumbnail_url varchar(255) null unique, - "type" varchar(100) not null, + "type" varchar(100) not null, blurhash varchar(255) null, mime_type varchar(255) not null, - description varchar(4000) null + description varchar(4000) null, + actor_id bigint not null, + constraint fk_media_actor_id__id foreign key (actor_id) references actors (id) on delete restrict on update restrict ); alter table actors From 9b60099ad9268c58094eded13a3500e291c02017 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 12:13:21 +0900 Subject: [PATCH 4/8] =?UTF-8?q?refactor:=20Thymeleaf=E3=81=AE=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=E3=82=92=E3=81=AA=E3=81=8F=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/templates/homeTimeline.html | 2 +- .../src/main/resources/templates/postById.html | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/hideout-core/src/main/resources/templates/homeTimeline.html b/hideout-core/src/main/resources/templates/homeTimeline.html index baac1330..60bc762b 100644 --- a/hideout-core/src/main/resources/templates/homeTimeline.html +++ b/hideout-core/src/main/resources/templates/homeTimeline.html @@ -6,7 +6,7 @@ \ No newline at end of file diff --git a/hideout-core/src/main/resources/templates/postById.html b/hideout-core/src/main/resources/templates/postById.html index 506f4c40..bbf5e358 100644 --- a/hideout-core/src/main/resources/templates/postById.html +++ b/hideout-core/src/main/resources/templates/postById.html @@ -20,24 +20,24 @@ From e4947ce5a931a51bb34735ee380f5fd8992f6d9e Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 12:22:31 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=E3=83=AD=E3=82=B0=E3=82=92?= =?UTF-8?q?=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/application/shared/AbstractApplicationService.kt | 3 ++- hideout-core/src/main/resources/log4j2.xml | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt index 40bfa6fc..9006f5fc 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/shared/AbstractApplicationService.kt @@ -30,7 +30,8 @@ abstract class AbstractApplicationService( val response = transaction.transaction { internalExecute(command, principal) } - logger.info("SUCCESS ${command::class.simpleName}") + + logger.info("SUCCESS $command $response") response } catch (e: CancellationException) { logger.debug("Coroutine canceled", e) diff --git a/hideout-core/src/main/resources/log4j2.xml b/hideout-core/src/main/resources/log4j2.xml index 978d338a..2f0d6625 100644 --- a/hideout-core/src/main/resources/log4j2.xml +++ b/hideout-core/src/main/resources/log4j2.xml @@ -6,13 +6,13 @@ - + - + - + From 0c9faa509b03cb904e450461a5ec4112a02b6170 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 12:57:33 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20mdc=E3=82=92=E5=87=BA=E5=8A=9B?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/MdcInterceptor.kt | 33 +++++++++++++++++++ hideout-core/src/main/resources/log4j2.xml | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt new file mode 100644 index 00000000..44b54057 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt @@ -0,0 +1,33 @@ +package dev.usbharu.hideout.core.infrastructure.springframework + +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.slf4j.MDC +import org.springframework.stereotype.Component +import org.springframework.web.servlet.AsyncHandlerInterceptor +import java.util.* + +@Component +class MdcInterceptor() : AsyncHandlerInterceptor { + override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { + MDC.put("request", UUID.randomUUID().toString()) + return true + } + + override fun afterConcurrentHandlingStarted( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any + ) { + MDC.remove("request") + } + + override fun afterCompletion( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any, + ex: Exception? + ) { + MDC.remove("request") + } +} \ No newline at end of file diff --git a/hideout-core/src/main/resources/log4j2.xml b/hideout-core/src/main/resources/log4j2.xml index 2f0d6625..7b5e5c96 100644 --- a/hideout-core/src/main/resources/log4j2.xml +++ b/hideout-core/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - + From eed5e8a75a0aa5c6cfaccf76ee109d70e3a620a5 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 24 Aug 2024 13:17:25 +0900 Subject: [PATCH 7/8] =?UTF-8?q?revert:=20MDC=E3=81=AB=E5=AF=BE=E5=BF=9C?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=AA=E3=81=8B=E3=81=A3=E3=81=9F=E3=81=AE?= =?UTF-8?q?=E3=81=A7=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/MdcInterceptor.kt | 33 ------------------- hideout-core/src/main/resources/log4j2.xml | 2 +- 2 files changed, 1 insertion(+), 34 deletions(-) delete mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt deleted file mode 100644 index 44b54057..00000000 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MdcInterceptor.kt +++ /dev/null @@ -1,33 +0,0 @@ -package dev.usbharu.hideout.core.infrastructure.springframework - -import jakarta.servlet.http.HttpServletRequest -import jakarta.servlet.http.HttpServletResponse -import org.slf4j.MDC -import org.springframework.stereotype.Component -import org.springframework.web.servlet.AsyncHandlerInterceptor -import java.util.* - -@Component -class MdcInterceptor() : AsyncHandlerInterceptor { - override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { - MDC.put("request", UUID.randomUUID().toString()) - return true - } - - override fun afterConcurrentHandlingStarted( - request: HttpServletRequest, - response: HttpServletResponse, - handler: Any - ) { - MDC.remove("request") - } - - override fun afterCompletion( - request: HttpServletRequest, - response: HttpServletResponse, - handler: Any, - ex: Exception? - ) { - MDC.remove("request") - } -} \ No newline at end of file diff --git a/hideout-core/src/main/resources/log4j2.xml b/hideout-core/src/main/resources/log4j2.xml index 7b5e5c96..a4436af8 100644 --- a/hideout-core/src/main/resources/log4j2.xml +++ b/hideout-core/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - + From ac5d6800e103bd6bb3a9ae68f2b8bfae825e2f74 Mon Sep 17 00:00:00 2001 From: usbharu Date: Sat, 24 Aug 2024 04:22:10 +0000 Subject: [PATCH 8/8] style: fix lint (CI) --- .../core/domain/model/userdetails/UserDetail.kt | 14 +++++++------- .../other/SnowflakeIdGenerateService.kt | 2 -- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt index 183ec3e6..a782b044 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/userdetails/UserDetail.kt @@ -44,13 +44,13 @@ class UserDetail( override fun hashCode(): Int = id.hashCode() override fun toString(): String { return "UserDetail(" + - "id=$id, " + - "actorId=$actorId, " + - "password=$password, " + - "autoAcceptFolloweeFollowRequest=$autoAcceptFolloweeFollowRequest, " + - "lastMigration=$lastMigration, " + - "homeTimelineId=$homeTimelineId" + - ")" + "id=$id, " + + "actorId=$actorId, " + + "password=$password, " + + "autoAcceptFolloweeFollowRequest=$autoAcceptFolloweeFollowRequest, " + + "lastMigration=$lastMigration, " + + "homeTimelineId=$homeTimelineId" + + ")" } companion object { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt index 26f3564b..a2d43add 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/other/SnowflakeIdGenerateService.kt @@ -80,6 +80,4 @@ open class SnowflakeIdGenerateService(private val baseTime: Long) : IdGenerateSe result = 31 * result + mutex.hashCode() return result } - - }