mirror of
https://github.com/google/nomulus
synced 2026-09-06 16:17:13 +00:00
Add tm().reTransact() methods and refactor away some inner transactions (#2125)
In the future, reTransact() will be the only way to initiate a transaction that doesn't fail when called inside an outer wrapping transaction (when wrapped, it's a no-op). It should be used sparingly, with a preference towards refactoring the code to move transactions outwards (which this PR also contains). Note that this PR includes some potential efficiency gains caused by existing poor use of transactions. E.g. in the file RefreshDnsAction, the existing code was using two separate transactions to refresh the DNS for domains and hosts (one is hidden in loadAndVerifyExistence(), whereas now as of this PR it has a single wrapping transaction to do so.
This commit is contained in:
@@ -15,11 +15,11 @@
|
||||
package google.registry.model.tmch;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.truth.Truth8;
|
||||
import google.registry.persistence.transaction.JpaTestExtensions;
|
||||
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationWithCoverageExtension;
|
||||
import google.registry.testing.FakeClock;
|
||||
@@ -113,17 +113,21 @@ public class ClaimsListDaoTest {
|
||||
tm().transact(() -> tm().insert(claimsList));
|
||||
ClaimsList fromDatabase = ClaimsListDao.get();
|
||||
// At first, we haven't loaded any entries
|
||||
assertThat(fromDatabase.claimKeyCache.getIfPresent("label1")).isNull();
|
||||
Truth8.assertThat(fromDatabase.getClaimKey("label1")).hasValue("key1");
|
||||
assertThat(tm().transact(() -> fromDatabase.claimKeyCache.getIfPresent("label1"))).isNull();
|
||||
assertThat(tm().transact(() -> fromDatabase.getClaimKey("label1"))).hasValue("key1");
|
||||
// After retrieval, the key exists
|
||||
Truth8.assertThat(fromDatabase.claimKeyCache.getIfPresent("label1")).hasValue("key1");
|
||||
assertThat(fromDatabase.claimKeyCache.getIfPresent("label2")).isNull();
|
||||
assertThat(tm().transact(() -> fromDatabase.claimKeyCache.getIfPresent("label1")))
|
||||
.hasValue("key1");
|
||||
assertThat(tm().transact(() -> fromDatabase.claimKeyCache.getIfPresent("label2"))).isNull();
|
||||
// Loading labels-to-keys should still work
|
||||
assertThat(fromDatabase.getLabelsToKeys()).containsExactly("label1", "key1", "label2", "key2");
|
||||
assertThat(tm().transact(() -> fromDatabase.getLabelsToKeys()))
|
||||
.containsExactly("label1", "key1", "label2", "key2");
|
||||
// We should also cache nonexistent values
|
||||
assertThat(fromDatabase.claimKeyCache.getIfPresent("nonexistent")).isNull();
|
||||
Truth8.assertThat(fromDatabase.getClaimKey("nonexistent")).isEmpty();
|
||||
Truth8.assertThat(fromDatabase.claimKeyCache.getIfPresent("nonexistent")).isEmpty();
|
||||
assertThat(tm().transact(() -> fromDatabase.claimKeyCache.getIfPresent("nonexistent")))
|
||||
.isNull();
|
||||
assertThat(tm().transact(() -> fromDatabase.getClaimKey("nonexistent"))).isEmpty();
|
||||
assertThat(tm().transact(() -> fromDatabase.claimKeyCache.getIfPresent("nonexistent")))
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
private void assertClaimsListEquals(ClaimsList left, ClaimsList right) {
|
||||
|
||||
+10
@@ -116,6 +116,11 @@ public class ReplicaSimulatingJpaTransactionManager implements JpaTransactionMan
|
||||
isolationLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T reTransact(Supplier<T> work) {
|
||||
return transact(work);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T transact(Supplier<T> work) {
|
||||
return transact(work, null);
|
||||
@@ -141,6 +146,11 @@ public class ReplicaSimulatingJpaTransactionManager implements JpaTransactionMan
|
||||
isolationLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reTransact(Runnable work) {
|
||||
transact(work);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transact(Runnable work) {
|
||||
transact(work, null);
|
||||
|
||||
@@ -17,6 +17,7 @@ package google.registry.tmch;
|
||||
import static com.google.common.collect.ImmutableList.toImmutableList;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -41,7 +42,8 @@ class TmchDnlActionTest extends TmchActionTestCase {
|
||||
|
||||
@Test
|
||||
void testDnl() throws Exception {
|
||||
assertThat(ClaimsListDao.get().getClaimKey("xn----7sbejwbn3axu3d")).isEmpty();
|
||||
assertThat(tm().transact(() -> ClaimsListDao.get().getClaimKey("xn----7sbejwbn3axu3d")))
|
||||
.isEmpty();
|
||||
when(httpUrlConnection.getInputStream())
|
||||
.thenReturn(new ByteArrayInputStream(TmchTestData.loadBytes("dnl/dnl-latest.csv").read()))
|
||||
.thenReturn(new ByteArrayInputStream(TmchTestData.loadBytes("dnl/dnl-latest.sig").read()));
|
||||
@@ -54,8 +56,8 @@ class TmchDnlActionTest extends TmchActionTestCase {
|
||||
ClaimsList claimsList = ClaimsListDao.get();
|
||||
assertThat(claimsList.getTmdbGenerationTime())
|
||||
.isEqualTo(DateTime.parse("2013-11-24T23:15:37.4Z"));
|
||||
assertThat(claimsList.getClaimKey("xn----7sbejwbn3axu3d"))
|
||||
assertThat(tm().transact(() -> claimsList.getClaimKey("xn----7sbejwbn3axu3d")))
|
||||
.hasValue("2013112500/7/4/8/dIHW0DiuybvhdP8kIz");
|
||||
assertThat(claimsList.getClaimKey("lolcat")).isEmpty();
|
||||
assertThat(tm().transact(() -> claimsList.getClaimKey("lolcat"))).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package google.registry.tools;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import google.registry.model.tmch.ClaimsList;
|
||||
@@ -46,11 +47,11 @@ class UploadClaimsListCommandTest extends CommandTestCase<UploadClaimsListComman
|
||||
ClaimsList claimsList = ClaimsListDao.get();
|
||||
assertThat(claimsList.getTmdbGenerationTime())
|
||||
.isEqualTo(DateTime.parse("2012-08-16T00:00:00.0Z"));
|
||||
assertThat(claimsList.getClaimKey("example"))
|
||||
assertThat(tm().transact(() -> claimsList.getClaimKey("example")))
|
||||
.hasValue("2013041500/2/6/9/rJ1NrDO92vDsAzf7EQzgjX4R0000000001");
|
||||
assertThat(claimsList.getClaimKey("another-example"))
|
||||
assertThat(tm().transact(() -> claimsList.getClaimKey("another-example")))
|
||||
.hasValue("2013041500/6/A/5/alJAqG2vI2BmCv5PfUvuDkf40000000002");
|
||||
assertThat(claimsList.getClaimKey("anotherexample"))
|
||||
assertThat(tm().transact(() -> claimsList.getClaimKey("anotherexample")))
|
||||
.hasValue("2013041500/A/C/7/rHdC4wnrWRvPY6nneCVtQhFj0000000003");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user