1
0
mirror of https://github.com/google/nomulus synced 2026-07-20 15:02:30 +00:00

Add a DelegatingReplicaJpaTransactionManager to handle multiple replicas (#3005)

This will allow us to spread the load across multiple Postgres replica
instances which should help with latency and stability.
This commit is contained in:
gbrodman
2026-04-10 16:46:06 -04:00
committed by GitHub
parent 4a1d0609f3
commit b78d12e73f
12 changed files with 609 additions and 25 deletions
@@ -120,6 +120,23 @@ public class SecretManagerKeyringUpdaterTest {
verifyPersistedSecret("sql-replica-conn-name", name);
}
@Test
void sqlReplicaConnectionNames() {
String names = "name1\nname2";
updater.setSqlReplicaConnectionNames(names).update();
assertThat(keyring.getSqlReplicaConnectionNames()).containsExactly("name1", "name2").inOrder();
verifyPersistedSecret("sql-replica-conn-names", names);
}
@Test
void sqlReplicaConnectionNames_fallback() {
String name = "name";
updater.setSqlReplicaConnectionName(name).update();
assertThat(keyring.getSqlReplicaConnectionNames()).containsExactly(name);
}
@Test
void marksdbDnlLoginAndPassword() {
String secret = "marksdbDnlLoginAndPassword";
@@ -0,0 +1,135 @@
// Copyright 2026 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.persistence.transaction;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import java.util.Random;
import java.util.concurrent.Callable;
import org.junit.jupiter.api.Test;
/** Tests for {@link DelegatingReplicaJpaTransactionManager}. */
public class DelegatingReplicaJpaTransactionManagerTest {
private JpaTransactionManager replica1 = mock(JpaTransactionManager.class);
private JpaTransactionManager replica2 = mock(JpaTransactionManager.class);
private Random random = mock(Random.class);
private DelegatingReplicaJpaTransactionManager transactionManager =
new DelegatingReplicaJpaTransactionManager(ImmutableList.of(replica1, replica2), random);
@Test
void testGetReplica_rotates() {
when(random.nextInt(2)).thenReturn(0).thenReturn(1);
transactionManager.loadByKey(null);
verify(replica1).loadByKey(null);
transactionManager.loadByKey(null);
verify(replica2).loadByKey(null);
}
@Test
void testTransact_usesSameReplica() throws Exception {
when(random.nextInt(2)).thenReturn(1);
when(replica2.transact(any(), any(), anyBoolean()))
.thenAnswer(
invocation -> {
Callable<Object> work = invocation.getArgument(1);
return work.call();
});
transactionManager.transact(
() -> {
transactionManager.loadByKey(null);
return null;
});
verify(replica2).transact(any(), any(), anyBoolean());
// The loadByKey inside the transact should also use replica2.
verify(replica2).loadByKey(null);
// And it should NOT have called random again for the nested call.
verify(random).nextInt(2);
}
@Test
void testTransactNoRetry_usesSameReplica() throws Exception {
when(random.nextInt(2)).thenReturn(0);
when(replica1.transactNoRetry(any(), any(), anyBoolean()))
.thenAnswer(
invocation -> {
Callable<Object> work = invocation.getArgument(1);
return work.call();
});
transactionManager.transactNoRetry(
() -> {
transactionManager.loadByKey(null);
return null;
});
verify(replica1).transactNoRetry(any(), any(), anyBoolean());
verify(replica1).loadByKey(null);
verify(random).nextInt(2);
}
@Test
void testReTransactNoRetry_usesSameReplica() throws Exception {
when(random.nextInt(2)).thenReturn(0);
when(replica1.reTransact(any(Callable.class)))
.thenAnswer(
invocation -> {
Callable<Object> work = invocation.getArgument(0);
return work.call();
});
transactionManager.reTransact(
() -> {
transactionManager.loadByKey(null);
return null;
});
verify(replica1).reTransact(any(Callable.class));
verify(replica1).loadByKey(null);
verify(random).nextInt(2);
}
@Test
void testInTransaction() {
when(random.nextInt(2)).thenReturn(0);
when(replica1.inTransaction()).thenReturn(true);
// Not in transaction yet
assertThat(transactionManager.inTransaction()).isFalse();
transactionManager.transact(
() -> {
assertThat(transactionManager.inTransaction()).isTrue();
return null;
});
}
@Test
void testTeardown_tearsDownAllReplicas() {
transactionManager.teardown();
verify(replica1).teardown();
verify(replica2).teardown();
}
}
@@ -19,6 +19,7 @@ import static google.registry.keyring.api.PgpHelper.KeyRequirement.SIGN;
import static google.registry.testing.TestDataHelper.loadBytes;
import static google.registry.testing.TestDataHelper.loadFile;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteSource;
import dagger.Module;
import dagger.Provides;
@@ -57,7 +58,8 @@ public final class FakeKeyringModule {
private static final String MARKSDB_SMDRL_LOGIN_AND_PASSWORD = "smdrl:yolo";
private static final String BSA_API_KEY = "bsaapikey";
private static final String SQL_PRIMARY_CONNECTION = "project:primary-region:primary-name";
private static final String SQL_REPLICA_CONNECTION = "project:replica-region:replica-name";
private static final String SQL_REPLICA_CONNECTION_1 = "project:replica-region:replica-name";
private static final String SQL_REPLICA_CONNECTION_2 = "project:replica-region:replica-name-2";
@Provides
public Keyring get() {
@@ -160,7 +162,12 @@ public final class FakeKeyringModule {
@Override
public String getSqlReplicaConnectionName() {
return SQL_REPLICA_CONNECTION;
return SQL_REPLICA_CONNECTION_1;
}
@Override
public ImmutableList<String> getSqlReplicaConnectionNames() {
return ImmutableList.of(SQL_REPLICA_CONNECTION_1, SQL_REPLICA_CONNECTION_2);
}
@Override