Convert 3 classes from ofy -> tm (#1034)

* Convert 3 classes from ofy -> tm

Convert SyncGroupMembersAction, SyncRegistrarsSheet and
IcannReportingUploadAction and their test cases to use TransactionManager and
dual-test them so we know they work in jpa.

* Address comments in review

Address review comments and make the entire IcannReportingUploadAction run
transactional.

* reformatted.

* Remove duplicate loadByKey() method

Remove test method added in a recent PR.
This commit is contained in:
Michael Muller
2021-03-29 13:08:15 -04:00
committed by GitHub
parent 65e468f2bc
commit db26635825
6 changed files with 95 additions and 91 deletions
@@ -16,7 +16,6 @@ package google.registry.export;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.request.Action.Method.POST;
import static google.registry.util.CollectionUtils.nullToEmpty;
@@ -164,7 +163,7 @@ public final class SyncGroupMembersAction implements Runnable {
registrarsToSave.add(result.getKey().asBuilder().setContactsRequireSyncing(false).build());
}
}
tm().transactNew(() -> ofy().save().entities(registrarsToSave.build()));
tm().transactNew(() -> tm().updateAll(registrarsToSave.build()));
return errors;
}
@@ -17,7 +17,6 @@ package google.registry.export.sheet;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.common.Cursor.CursorType.SYNC_REGISTRAR_SHEET;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registrar.RegistrarContact.Type.ABUSE;
import static google.registry.model.registrar.RegistrarContact.Type.ADMIN;
import static google.registry.model.registrar.RegistrarContact.Type.BILLING;
@@ -26,6 +25,7 @@ import static google.registry.model.registrar.RegistrarContact.Type.MARKETING;
import static google.registry.model.registrar.RegistrarContact.Type.TECH;
import static google.registry.model.registrar.RegistrarContact.Type.WHOIS;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.base.Joiner;
@@ -40,6 +40,7 @@ import google.registry.model.registrar.RegistrarContact;
import google.registry.util.Clock;
import google.registry.util.DateTimeUtils;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.inject.Inject;
@@ -61,8 +62,10 @@ class SyncRegistrarsSheet {
* successfully completed, as measured by a cursor.
*/
boolean wereRegistrarsModified() {
Cursor cursor = ofy().load().key(Cursor.createGlobalKey(SYNC_REGISTRAR_SHEET)).now();
DateTime lastUpdateTime = (cursor == null) ? START_OF_TIME : cursor.getCursorTime();
Optional<Cursor> cursor =
transactIfJpaTm(
() -> tm().loadByKeyIfPresent(Cursor.createGlobalVKey(SYNC_REGISTRAR_SHEET)));
DateTime lastUpdateTime = !cursor.isPresent() ? START_OF_TIME : cursor.get().getCursorTime();
for (Registrar registrar : Registrar.loadAllCached()) {
if (DateTimeUtils.isAtOrAfter(registrar.getLastUpdateTime(), lastUpdateTime)) {
return true;
@@ -17,7 +17,6 @@ package google.registry.reporting.icann;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static google.registry.model.common.Cursor.getCursorTimeOrStartOfTime;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.request.Action.Method.POST;
import static javax.servlet.http.HttpServletResponse.SC_OK;
@@ -28,7 +27,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.ByteStreams;
import com.googlecode.objectify.Key;
import google.registry.config.RegistryConfig.Config;
import google.registry.gcs.GcsUtils;
import google.registry.model.common.Cursor;
@@ -36,6 +34,7 @@ import google.registry.model.common.Cursor.CursorType;
import google.registry.model.registry.Registries;
import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldType;
import google.registry.persistence.VKey;
import google.registry.request.Action;
import google.registry.request.HttpException.ServiceUnavailableException;
import google.registry.request.Response;
@@ -100,7 +99,7 @@ public final class IcannReportingUploadAction implements Runnable {
@Override
public void run() {
Callable<Void> lockRunner =
Runnable transactional =
() -> {
ImmutableMap.Builder<String, Boolean> reportSummaryBuilder = new ImmutableMap.Builder<>();
@@ -122,6 +121,11 @@ public final class IcannReportingUploadAction implements Runnable {
emailUploadResults(reportSummaryBuilder.build());
response.setStatus(SC_OK);
response.setContentType(PLAIN_TEXT_UTF_8);
};
Callable<Void> lockRunner =
() -> {
tm().transact(transactional);
return null;
};
@@ -185,7 +189,7 @@ public final class IcannReportingUploadAction implements Runnable {
cursorType,
cursorTime.withTimeAtStartOfDay().withDayOfMonth(1).plusMonths(1),
Registry.get(tldStr));
tm().transact(() -> tm().put(newCursor));
tm().put(newCursor);
}
}
@@ -204,16 +208,16 @@ public final class IcannReportingUploadAction implements Runnable {
ImmutableSet<Registry> registries = Registries.getTldEntitiesOfType(TldType.REAL);
ImmutableMap<Key<Cursor>, Registry> activityKeyMap =
ImmutableMap<VKey<? extends Cursor>, Registry> activityKeyMap =
loadKeyMap(registries, CursorType.ICANN_UPLOAD_ACTIVITY);
ImmutableMap<Key<Cursor>, Registry> transactionKeyMap =
ImmutableMap<VKey<? extends Cursor>, Registry> transactionKeyMap =
loadKeyMap(registries, CursorType.ICANN_UPLOAD_TX);
ImmutableSet.Builder<Key<Cursor>> keys = new ImmutableSet.Builder<>();
ImmutableSet.Builder<VKey<? extends Cursor>> keys = new ImmutableSet.Builder<>();
keys.addAll(activityKeyMap.keySet());
keys.addAll(transactionKeyMap.keySet());
Map<Key<Cursor>, Cursor> cursorMap = ofy().load().keys(keys.build());
Map<VKey<? extends Cursor>, Cursor> cursorMap = tm().loadByKeysIfPresent(keys.build());
ImmutableMap.Builder<Cursor, String> cursors = new ImmutableMap.Builder<>();
cursors.putAll(
defaultNullCursorsToNextMonthAndAddToMap(
@@ -224,9 +228,9 @@ public final class IcannReportingUploadAction implements Runnable {
return cursors.build();
}
private ImmutableMap<Key<Cursor>, Registry> loadKeyMap(
private ImmutableMap<VKey<? extends Cursor>, Registry> loadKeyMap(
ImmutableSet<Registry> registries, CursorType type) {
return Maps.uniqueIndex(registries, r -> Cursor.createKey(type, r));
return Maps.uniqueIndex(registries, r -> Cursor.createVKey(type, r.getTldStr()));
}
/**
@@ -235,7 +239,9 @@ public final class IcannReportingUploadAction implements Runnable {
* next month.
*/
private ImmutableMap<Cursor, String> defaultNullCursorsToNextMonthAndAddToMap(
Map<Key<Cursor>, Registry> keyMap, CursorType type, Map<Key<Cursor>, Cursor> cursorMap) {
Map<VKey<? extends Cursor>, Registry> keyMap,
CursorType type,
Map<VKey<? extends Cursor>, Cursor> cursorMap) {
ImmutableMap.Builder<Cursor, String> cursors = new ImmutableMap.Builder<>();
keyMap.forEach(
(key, registry) -> {
@@ -249,7 +255,7 @@ public final class IcannReportingUploadAction implements Runnable {
clock.nowUtc().withDayOfMonth(1).withTimeAtStartOfDay().plusMonths(1),
registry));
if (!cursorMap.containsValue(cursor)) {
tm().transact(() -> ofy().save().entity(cursor));
tm().put(cursor);
}
cursors.put(cursor, registry.getTldStr());
});
@@ -279,6 +285,7 @@ public final class IcannReportingUploadAction implements Runnable {
String.format(
"Report Filename - Upload status:\n%s",
reportSummary.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(
(e) ->
String.format("%s - %s", e.getKey(), e.getValue() ? "SUCCESS" : "FAILURE"))