mirror of
https://github.com/google/nomulus.git
synced 2025-05-02 21:17:50 +02:00
Datastore has a non-zero chance of failing on reads. A map-reduce with too many failures will eventually give up. As a result, any map-reduce that goes over a large number of datastore entities is almost guaranteed to fail. Since we expect to have a large number of EppResources, we make sure to wrap all datastore reads with some retrying mechanism to reduce the number of transient failures that propagate to Map-Reduce. This feature already existed for CommitLogManifestReader, we refactor the code to use the same retrying mechanism in EppResource readers. Also removed the transactNew around the reads because looking at the source - it doesn't actually do anything we need (doesn't retry on any failure other than concurrency failure) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=190633281
92 lines
3.4 KiB
Java
92 lines
3.4 KiB
Java
// Copyright 2017 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.mapreduce.inputs;
|
|
|
|
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
|
import static google.registry.model.ofy.ObjectifyService.ofy;
|
|
|
|
import com.google.appengine.api.datastore.Cursor;
|
|
import com.google.appengine.api.datastore.QueryResultIterator;
|
|
import com.google.appengine.tools.mapreduce.InputReader;
|
|
import com.google.common.collect.ImmutableSet;
|
|
import com.googlecode.objectify.Key;
|
|
import com.googlecode.objectify.cmd.Query;
|
|
import google.registry.model.EppResource;
|
|
import google.registry.model.index.EppResourceIndex;
|
|
import google.registry.model.index.EppResourceIndexBucket;
|
|
import google.registry.util.FormattingLogger;
|
|
import javax.annotation.Nullable;
|
|
|
|
/** Base class for {@link InputReader} classes that map over {@link EppResourceIndex}. */
|
|
abstract class EppResourceBaseReader<T> extends RetryingInputReader<EppResourceIndex, T> {
|
|
|
|
static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
|
|
|
/** Number of bytes in 1MB of memory, used for memory estimates. */
|
|
static final long ONE_MB = 1024 * 1024;
|
|
|
|
private static final long serialVersionUID = 7942584269402339168L;
|
|
|
|
/**
|
|
* The resource kinds to filter for.
|
|
*
|
|
* <p>This can be empty, or any of {"ContactResource", "HostResource", "DomainBase"}. It will
|
|
* never contain "EppResource", "DomainResource" or "DomainApplication" since these aren't
|
|
* actual kinds in Datastore.
|
|
*/
|
|
private final ImmutableSet<String> filterKinds;
|
|
|
|
private final Key<EppResourceIndexBucket> bucketKey;
|
|
private final long memoryEstimate;
|
|
|
|
EppResourceBaseReader(
|
|
Key<EppResourceIndexBucket> bucketKey,
|
|
long memoryEstimate,
|
|
ImmutableSet<String> filterKinds) {
|
|
this.bucketKey = bucketKey;
|
|
this.memoryEstimate = memoryEstimate;
|
|
this.filterKinds = filterKinds;
|
|
}
|
|
|
|
@Override
|
|
public QueryResultIterator<EppResourceIndex> getQueryIterator(@Nullable Cursor cursor) {
|
|
return startQueryAt(query(), cursor).iterator();
|
|
}
|
|
|
|
@Override
|
|
public int getTotal() {
|
|
return query().count();
|
|
}
|
|
|
|
/** Query for children of this bucket. */
|
|
Query<EppResourceIndex> query() {
|
|
Query<EppResourceIndex> query = ofy().load().type(EppResourceIndex.class).ancestor(bucketKey);
|
|
return filterKinds.isEmpty() ? query : query.filter("kind in", filterKinds);
|
|
}
|
|
|
|
/** Returns the estimated memory that will be used by this reader in bytes. */
|
|
@Override
|
|
public long estimateMemoryRequirement() {
|
|
return memoryEstimate;
|
|
}
|
|
|
|
static <R extends EppResource> ImmutableSet<String> varargsToKinds(
|
|
ImmutableSet<Class<? extends R>> resourceClasses) {
|
|
// Ignore EppResource when finding kinds, since it doesn't have one and doesn't imply filtering.
|
|
return resourceClasses.contains(EppResource.class)
|
|
? ImmutableSet.of()
|
|
: resourceClasses.stream().map(Key::getKind).collect(toImmutableSet());
|
|
}
|
|
}
|