Skip to content

Commit dca139c

Browse files
committed
Remove merging, add replicaRead test, update docs
1 parent 98af70b commit dca139c

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

misk-hibernate/src/main/kotlin/misk/hibernate/RealTransacter.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ private constructor(
156156
}
157157
return transactionWithRetriesInternal {
158158
replicaReadWithoutTransactionInternalSession { session ->
159+
// No need to target replica explicitly if the `@replica` is already set as the database
159160
if (config.type.isVitess && config.database != Destination.replica().toString()) {
160161
session.target(Destination.replica(), block)
161162
} else {
@@ -566,18 +567,14 @@ private constructor(
566567
true -> {
567568
useConnection { connection ->
568569
val previous = currentTarget(connection)
569-
// Merge Destinations to support composition (e.g., targeting a shard with TabletType not specified within a replica read)
570-
// This will prefer values from the new Destination when merging.
571-
val mergedDestination = previous.mergedWith(destination)
572-
val targetHasChanged = previous != mergedDestination
573-
570+
val targetHasChanged = previous != destination
574571
try {
575572
if (targetHasChanged) {
576573
logger.debug {
577-
"The new destination was updated from previous target. Destination target: $mergedDestination, " +
574+
"The new destination was updated from previous target. Destination target: $destination, " +
578575
"previous target: $previous"
579576
}
580-
use(connection, mergedDestination)
577+
use(connection, destination)
581578
}
582579
block(this)
583580
} catch (e: Exception) {
@@ -588,7 +585,7 @@ private constructor(
588585
use(connection, previous)
589586
} catch (e: Exception) {
590587
logger.error(e) {
591-
"Exception restoring destination, previous = $previous, destination = $mergedDestination, " +
588+
"Exception restoring destination, previous = $previous, destination = $destination, " +
592589
"cause = ${e.message}"
593590
}
594591
}

misk-hibernate/src/test/kotlin/misk/hibernate/vitess/VitessHibernateTransacterPoolIntegrationTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ class VitessHibernateTransacterPoolIntegrationTest {
124124
assertThat(readerTransacter.config().database).isEqualTo("@replica")
125125
}
126126

127+
@Test
128+
fun `replicaRead on reader transacter works`() {
129+
val movieId = writerTransacter.transaction { session ->
130+
val movie = DbMovie("Tenet", LocalDate.of(2020, 9, 3))
131+
session.save(movie)
132+
}
133+
134+
val replicaReadResult = readerTransacter.replicaRead { session ->
135+
queryFactory.newQuery<MovieQuery>()
136+
.id(movieId)
137+
.uniqueResult(session)
138+
}
139+
140+
assertThat(replicaReadResult).isNotNull
141+
assertThat(replicaReadResult!!.name).isEqualTo("Tenet")
142+
}
143+
127144
@Test
128145
fun `replicaRead on writer transacter also works`() {
129146
val movieId = writerTransacter.transaction { session ->

misk-hibernate/src/test/kotlin/misk/hibernate/vitess/VitessShardTargetIntegrationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class VitessShardTargetIntegrationTest {
199199
}
200200

201201
@Test
202-
fun `target(Destination) works in transactions`() {
202+
fun `target(destination) works in transactions`() {
203203
transacter.transaction { session ->
204204
session.target(Destination(shard = jp.shard(session), tabletType = TabletType.PRIMARY)) {
205205
assertThat(

0 commit comments

Comments
 (0)