Explicitly set the shard size when saving the claims list

This allows us to get rid of the use of InjectRule in tests.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218221409
This commit is contained in:
jianglai 2018-10-22 13:21:06 -07:00
parent 6a560c18ce
commit 589e98a1db
2 changed files with 12 additions and 18 deletions

View file

@ -39,7 +39,6 @@ import google.registry.model.annotations.VirtualEntity;
import google.registry.model.common.CrossTldSingleton; import google.registry.model.common.CrossTldSingleton;
import google.registry.util.CollectionUtils; import google.registry.util.CollectionUtils;
import google.registry.util.Concurrent; import google.registry.util.Concurrent;
import google.registry.util.NonFinalForTesting;
import google.registry.util.Retrier; import google.registry.util.Retrier;
import google.registry.util.SystemSleeper; import google.registry.util.SystemSleeper;
import java.util.HashMap; import java.util.HashMap;
@ -69,10 +68,8 @@ import org.joda.time.DateTime;
@NotBackedUp(reason = Reason.EXTERNALLY_SOURCED) @NotBackedUp(reason = Reason.EXTERNALLY_SOURCED)
public class ClaimsListShard extends ImmutableObject { public class ClaimsListShard extends ImmutableObject {
/** The number of claims list entries to store per shard. Do not modify except for in tests. */ /** The number of claims list entries to store per shard. */
@VisibleForTesting private static final int SHARD_SIZE = 10000;
@NonFinalForTesting
static int shardSize = 10000;
@Id @Id
long id; long id;
@ -165,6 +162,11 @@ public class ClaimsListShard extends ImmutableObject {
* switching over to using them atomically, then deleting the old ones. * switching over to using them atomically, then deleting the old ones.
*/ */
public void save() { public void save() {
save(SHARD_SIZE);
}
@VisibleForTesting
void save(int shardSize) {
// Figure out what the next versionId should be based on which ones already exist. // Figure out what the next versionId should be based on which ones already exist.
final Key<ClaimsListRevision> oldRevision = getCurrentRevision(); final Key<ClaimsListRevision> oldRevision = getCurrentRevision();
final Key<ClaimsListRevision> parentKey = ClaimsListRevision.createKey(); final Key<ClaimsListRevision> parentKey = ClaimsListRevision.createKey();

View file

@ -26,12 +26,10 @@ import com.googlecode.objectify.Key;
import google.registry.model.tmch.ClaimsListShard.ClaimsListRevision; import google.registry.model.tmch.ClaimsListShard.ClaimsListRevision;
import google.registry.model.tmch.ClaimsListShard.UnshardedSaveException; import google.registry.model.tmch.ClaimsListShard.UnshardedSaveException;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import google.registry.testing.InjectRule;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -46,13 +44,7 @@ public class ClaimsListShardTest {
.withDatastore() .withDatastore()
.build(); .build();
@Rule private final int shardSize = 10;
public final InjectRule inject = new InjectRule();
@Before
public void before() {
inject.setStaticField(ClaimsListShard.class, "shardSize", 10);
}
@Test @Test
public void test_unshardedSaveFails() { public void test_unshardedSaveFails() {
@ -81,13 +73,13 @@ public class ClaimsListShardTest {
public void test_savesAndGets_withSharding() { public void test_savesAndGets_withSharding() {
// Create a ClaimsList that will need 4 shards to save. // Create a ClaimsList that will need 4 shards to save.
Map<String, String> labelsToKeys = new HashMap<>(); Map<String, String> labelsToKeys = new HashMap<>();
for (int i = 0; i <= ClaimsListShard.shardSize * 3; i++) { for (int i = 0; i <= shardSize * 3; i++) {
labelsToKeys.put(Integer.toString(i), Integer.toString(i)); labelsToKeys.put(Integer.toString(i), Integer.toString(i));
} }
DateTime now = DateTime.now(UTC); DateTime now = DateTime.now(UTC);
// Save it with sharding, and make sure that reloading it works. // Save it with sharding, and make sure that reloading it works.
ClaimsListShard unsharded = ClaimsListShard.create(now, ImmutableMap.copyOf(labelsToKeys)); ClaimsListShard unsharded = ClaimsListShard.create(now, ImmutableMap.copyOf(labelsToKeys));
unsharded.save(); unsharded.save(shardSize);
assertThat(ClaimsListShard.get().labelsToKeys).isEqualTo(unsharded.labelsToKeys); assertThat(ClaimsListShard.get().labelsToKeys).isEqualTo(unsharded.labelsToKeys);
List<ClaimsListShard> shards1 = ofy().load().type(ClaimsListShard.class).list(); List<ClaimsListShard> shards1 = ofy().load().type(ClaimsListShard.class).list();
assertThat(shards1).hasSize(4); assertThat(shards1).hasSize(4);
@ -97,11 +89,11 @@ public class ClaimsListShardTest {
// Create a smaller ClaimsList that will need only 2 shards to save. // Create a smaller ClaimsList that will need only 2 shards to save.
labelsToKeys = new HashMap<>(); labelsToKeys = new HashMap<>();
for (int i = 0; i <= ClaimsListShard.shardSize; i++) { for (int i = 0; i <= shardSize; i++) {
labelsToKeys.put(Integer.toString(i), Integer.toString(i)); labelsToKeys.put(Integer.toString(i), Integer.toString(i));
} }
unsharded = ClaimsListShard.create(now.plusDays(1), ImmutableMap.copyOf(labelsToKeys)); unsharded = ClaimsListShard.create(now.plusDays(1), ImmutableMap.copyOf(labelsToKeys));
unsharded.save(); unsharded.save(shardSize);
ofy().clearSessionCache(); ofy().clearSessionCache();
assertThat(ClaimsListShard.get().labelsToKeys).hasSize(unsharded.labelsToKeys.size()); assertThat(ClaimsListShard.get().labelsToKeys).hasSize(unsharded.labelsToKeys.size());
assertThat(ClaimsListShard.get().labelsToKeys).isEqualTo(unsharded.labelsToKeys); assertThat(ClaimsListShard.get().labelsToKeys).isEqualTo(unsharded.labelsToKeys);