Make nomulus list_cursors command faster by batching loads

They're all in the same entity group anyway (the cross-TLD one), so they can be
loaded in a single call instead of individually.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201364854
This commit is contained in:
mcilwain 2018-06-20 09:24:38 -07:00 committed by Ben McIlwain
parent 6f2e663b72
commit a5cc359813
2 changed files with 30 additions and 29 deletions

View file

@ -14,56 +14,59 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.google.common.collect.Ordering; import com.googlecode.objectify.Key;
import google.registry.model.common.Cursor; import google.registry.model.common.Cursor;
import google.registry.model.common.Cursor.CursorType; import google.registry.model.common.Cursor.CursorType;
import google.registry.model.registry.Registries; import google.registry.model.registry.Registries;
import google.registry.model.registry.Registry; import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldType; import google.registry.model.registry.Registry.TldType;
import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.Command.RemoteApiCommand;
import java.util.ArrayList; import java.util.Map;
import java.util.List;
/** Lists {@link Cursor} timestamps used by locking rolling cursor tasks, like in RDE. */ /** Lists {@link Cursor} timestamps used by locking rolling cursor tasks, like in RDE. */
@Parameters(separators = " =", commandDescription = "Lists cursor timestamps used by LRC tasks") @Parameters(separators = " =", commandDescription = "Lists cursor timestamps used by LRC tasks")
final class ListCursorsCommand implements RemoteApiCommand { final class ListCursorsCommand implements RemoteApiCommand {
@Parameter( @Parameter(names = "--type", description = "Which cursor to list.", required = true)
names = "--type",
description = "Which cursor to list.",
required = true)
private CursorType cursorType; private CursorType cursorType;
@Parameter( @Parameter(
names = "--tld_type", names = "--tld_type",
description = "Filter TLDs of a certain type (REAL or TEST.)") description = "Filter TLDs of a certain type (REAL or TEST; defaults to REAL).")
private TldType filterTldType = TldType.REAL; private TldType filterTldType = TldType.REAL;
@Parameter( @Parameter(
names = "--escrow_enabled", names = "--escrow_enabled",
description = "Filter TLDs to only include those with RDE escrow enabled.") description = "Filter TLDs to only include those with RDE escrow enabled; defaults to false.")
private boolean filterEscrowEnabled; private boolean filterEscrowEnabled = false;
@Override @Override
public void run() { public void run() {
List<String> lines = new ArrayList<>(); Map<Registry, Key<Cursor>> registries =
for (String tld : Registries.getTlds()) { Registries.getTlds()
Registry registry = Registry.get(tld); .stream()
if (filterTldType != registry.getTldType()) { .map(Registry::get)
continue; .filter(r -> r.getTldType() == filterTldType)
} .filter(r -> !filterEscrowEnabled || r.getEscrowEnabled())
if (filterEscrowEnabled && !registry.getEscrowEnabled()) { .collect(toImmutableMap(r -> r, r -> Cursor.createKey(cursorType, r)));
continue; Map<Key<Cursor>, Cursor> cursors = ofy().load().keys(registries.values());
} registries
Cursor cursor = ofy().load().key(Cursor.createKey(cursorType, registry)).now(); .entrySet()
lines.add(String.format("%-25s%s", cursor != null ? cursor.getCursorTime() : "absent", tld)); .stream()
} .map(e -> renderLine(e.getKey().getTldStr(), e.getValue(), cursors))
for (String line : Ordering.natural().sortedCopy(lines)) { .sorted()
System.out.println(line); .forEach(System.out::println);
} }
private static String renderLine(
String tld, Key<Cursor> cursorKey, Map<Key<Cursor>, Cursor> cursors) {
return String.format(
"%-25s%s",
cursors.containsKey(cursorKey) ? cursors.get(cursorKey).getCursorTime() : "(absent)", tld);
} }
} }

View file

@ -42,9 +42,7 @@ public class ListCursorsCommandTest extends CommandTestCase<ListCursorsCommand>
Cursor.create(CursorType.BRDA, DateTime.parse("1984-12-18TZ"), Registry.get("bar"))); Cursor.create(CursorType.BRDA, DateTime.parse("1984-12-18TZ"), Registry.get("bar")));
runCommand("--type=BRDA"); runCommand("--type=BRDA");
assertThat(getStdoutAsLines()) assertThat(getStdoutAsLines())
.containsExactly( .containsExactly("(absent) foo", "1984-12-18T00:00:00.000Z bar")
"1984-12-18T00:00:00.000Z bar",
"absent foo")
.inOrder(); .inOrder();
} }
@ -65,6 +63,6 @@ public class ListCursorsCommandTest extends CommandTestCase<ListCursorsCommand>
createTlds("foo", "bar"); createTlds("foo", "bar");
persistResource(Registry.get("bar").asBuilder().setEscrowEnabled(true).build()); persistResource(Registry.get("bar").asBuilder().setEscrowEnabled(true).build());
runCommand("--type=BRDA", "--escrow_enabled"); runCommand("--type=BRDA", "--escrow_enabled");
assertThat(getStdoutAsLines()).containsExactly("absent bar"); assertThat(getStdoutAsLines()).containsExactly("(absent) bar");
} }
} }