Catch errors when attempting to delete entities through Objectify

This is currently erroring out on entities that fail to load properly through Objectify (e.g. because their entity type is no longer registered).  The proper thing to do is to catch the error, log it, and fall back to the raw Datastore operation, which will succeed.

The exact Exception this is designed to catch is:

com.google.apphosting.runtime.jetty9.JettyLogger warn: /_dr/admin/deleteEntity (JettyLogger.java:29)
java.lang.IllegalStateException: No registered subclass for discriminator 'DomainApplication'
	at com.googlecode.objectify.v4.impl.PolymorphicEntityMetadata.getConcrete(PolymorphicEntityMetadata.java:133)
	at com.googlecode.objectify.v4.impl.PolymorphicEntityMetadata.load(PolymorphicEntityMetadata.java:164)
	at com.googlecode.objectify.v4.impl.LoadEngine.load(LoadEngine.java:220)
	at com.googlecode.objectify.v4.impl.LoadEngine$1.nowUncached(LoadEngine.java:178)
	at com.googlecode.objectify.v4.impl.LoadEngine$1.nowUncached(LoadEngine.java:164)
	at com.googlecode.objectify.v4.util.ResultCache.now(ResultCache.java:30)
	at com.googlecode.objectify.v4.impl.Round$1.nowUncached(Round.java:73)
	at com.googlecode.objectify.v4.util.ResultCache.now(ResultCache.java:30)
	at com.googlecode.objectify.v4.LoadResult.now(LoadResult.java:25)
	at google.registry.tools.server.DeleteEntityAction.loadOfyEntity(DeleteEntityAction.java:103)
	at google.registry.tools.server.DeleteEntityAction.run(DeleteEntityAction.java:74)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=230737553
This commit is contained in:
mcilwain 2019-01-24 09:40:36 -08:00 committed by jianglai
parent acbd23fa64
commit 97c2049669
2 changed files with 20 additions and 2 deletions

View file

@ -99,8 +99,15 @@ public class DeleteEntityAction implements Runnable {
} }
private Optional<Object> loadOfyEntity(Key rawKey) { private Optional<Object> loadOfyEntity(Key rawKey) {
try {
EntityMetadata<Object> metadata = ofy().factory().getMetadata(rawKey.getKind()); EntityMetadata<Object> metadata = ofy().factory().getMetadata(rawKey.getKind());
return Optional.ofNullable(metadata == null ? null : ofy().load().key(create(rawKey)).now()); return Optional.ofNullable(metadata == null ? null : ofy().load().key(create(rawKey)).now());
} catch (Throwable e) {
logger.atWarning().withCause(e).log(
"Could not load entity with key %s using Objectify; falling back to raw Datastore.",
rawKey);
return Optional.empty();
}
} }
private Optional<Entity> loadRawEntity(Key rawKey) { private Optional<Entity> loadRawEntity(Key rawKey) {

View file

@ -66,6 +66,17 @@ public class DeleteEntityActionTest {
.isEqualTo("Deleted 0 raw entities and 1 registered entities"); .isEqualTo("Deleted 0 raw entities and 1 registered entities");
} }
@Test
public void test_deletePolymorphicEntity_fallbackSucceedsForUnregisteredType() {
Entity entity = new Entity("single", "raw");
entity.setIndexedProperty("^d", "UnregType");
getDatastoreService().put(entity);
action.rawKeys = KeyFactory.keyToString(entity.getKey());
action.run();
assertThat(response.getPayload())
.isEqualTo("Deleted 1 raw entities and 0 registered entities");
}
@Test @Test
public void test_deleteOneRawEntityAndOneRegisteredEntitySuccessfully() { public void test_deleteOneRawEntityAndOneRegisteredEntitySuccessfully() {
Entity entity = new Entity("first", "raw"); Entity entity = new Entity("first", "raw");