Cache Registrars in memory

This replaces the memcache caching, which we think is overall a bad idea.
We load all registrars at once instead of caching each as needed, so that
the loadAllCached() methods can be cached as well, and therefore will
always produce results consistent with loadByClientIdCached()'s view of the
registrar's values. All of our prod registrars together total 300k of data
right now, so this is hardly worth optimizing further, and in any case this
will likely reduce latency even further since most requests will be
served out of memory.

While I was in the Registrar file I standardized the error messages for incorrect
password and clientId length to be the same format, and cleaned up a few
random things I noticed in the code.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156151828
This commit is contained in:
cgoldfeder 2017-05-16 00:42:49 -07:00 committed by Ben McIlwain
parent 9a48aae107
commit c9d7e75946
17 changed files with 149 additions and 179 deletions

View file

@ -15,12 +15,13 @@
package google.registry.rdap;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.rdap.RdapUtils.getRegistrarByIanaIdentifier;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.primitives.Longs;
import com.google.re2j.Pattern;
import com.googlecode.objectify.Key;
import google.registry.model.contact.ContactResource;
@ -96,18 +97,14 @@ public class RdapEntityAction extends RdapActionBase {
OutputDataType.FULL);
}
}
try {
Long ianaIdentifier = Long.parseLong(pathSearchString);
Long ianaIdentifier = Longs.tryParse(pathSearchString);
if (ianaIdentifier != null) {
wasValidKey = true;
Registrar registrar = Iterables.getOnlyElement(
Registrar.loadByIanaIdentifierRange(ianaIdentifier, ianaIdentifier + 1, 1), null);
if ((registrar != null) && registrar.isActiveAndPubliclyVisible()) {
Optional<Registrar> registrar = getRegistrarByIanaIdentifier(ianaIdentifier);
if ((registrar.isPresent()) && registrar.get().isActiveAndPubliclyVisible()) {
return rdapJsonFormatter.makeRdapJsonForRegistrar(
registrar, true, rdapLinkBase, rdapWhoisServer, now, OutputDataType.FULL);
registrar.get(), true, rdapLinkBase, rdapWhoisServer, now, OutputDataType.FULL);
}
} catch (NumberFormatException e) {
// Although the search string was not a valid IANA identifier, it might still have been a
// valid ROID.
}
// At this point, we have failed to find either a contact or a registrar.
throw wasValidKey

View file

@ -16,14 +16,18 @@ package google.registry.rdap;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.rdap.RdapIcannStandardInformation.TRUNCATION_NOTICES;
import static google.registry.rdap.RdapUtils.getRegistrarByIanaIdentifier;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Longs;
import com.googlecode.objectify.Key;
import google.registry.config.RegistryConfig.Config;
import google.registry.model.contact.ContactResource;
@ -138,19 +142,16 @@ public class RdapEntitySearchAction extends RdapActionBase {
throw new UnprocessableEntityException("Suffixes not allowed in entity name searches");
}
// Get the registrar matches, depending on whether there's a wildcard.
ImmutableList<Registrar> registrarMatches;
if (!partialStringQuery.getHasWildcard()) {
Registrar registrar = Registrar.loadByName(partialStringQuery.getInitialString());
registrarMatches = (registrar == null)
? ImmutableList.<Registrar>of()
: ImmutableList.of(registrar);
} else {
// Fetch an additional registrar, so we can detect result set truncation.
registrarMatches = ImmutableList.copyOf(Registrar.loadByNameRange(
partialStringQuery.getInitialString(),
partialStringQuery.getNextInitialString(),
rdapResultSetMaxSize + 1));
}
ImmutableList<Registrar> registrarMatches =
FluentIterable.from(Registrar.loadAllCached())
.filter(
new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
return partialStringQuery.matches(registrar.getRegistrarName());
}})
.limit(rdapResultSetMaxSize + 1)
.toList();
// Get the contact matches and return the results, fetching an additional contact to detect
// truncation.
return makeSearchResults(
@ -169,7 +170,8 @@ public class RdapEntitySearchAction extends RdapActionBase {
.type(ContactResource.class)
.id(partialStringQuery.getInitialString())
.now();
ImmutableList<Registrar> registrars = getMatchingRegistrars(partialStringQuery);
ImmutableList<Registrar> registrars =
getMatchingRegistrars(partialStringQuery.getInitialString());
return makeSearchResults(
((contactResource == null) || !contactResource.getDeletionTime().isEqual(END_OF_TIME))
? ImmutableList.<ContactResource>of() : ImmutableList.of(contactResource),
@ -201,17 +203,15 @@ public class RdapEntitySearchAction extends RdapActionBase {
}
/** Looks up registrars by handle (i.e. IANA identifier). */
private ImmutableList<Registrar>
getMatchingRegistrars(final RdapSearchPattern partialStringQuery) {
Long ianaIdentifier;
try {
ianaIdentifier = Long.parseLong(partialStringQuery.getInitialString());
} catch (NumberFormatException e) {
return ImmutableList.of();
private ImmutableList<Registrar> getMatchingRegistrars(final String ianaIdentifierString) {
Long ianaIdentifier = Longs.tryParse(ianaIdentifierString);
if (ianaIdentifier != null) {
Optional<Registrar> registrar = getRegistrarByIanaIdentifier(ianaIdentifier);
if (registrar.isPresent()) {
return ImmutableList.of(registrar.get());
}
}
// Fetch an additional registrar to detect result set truncation.
return ImmutableList.copyOf(Registrar.loadByIanaIdentifierRange(
ianaIdentifier, ianaIdentifier + 1, rdapResultSetMaxSize + 1));
return ImmutableList.of();
}
/** Builds a JSON array of entity info maps based on the specified contacts and registrars. */

View file

@ -0,0 +1,38 @@
// 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.rdap;
import static com.google.common.collect.Iterables.tryFind;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import google.registry.model.registrar.Registrar;
/** Utility functions for RDAP. */
public final class RdapUtils {
private RdapUtils() {}
/** Looks up a registrar by its IANA identifier. */
static Optional<Registrar> getRegistrarByIanaIdentifier(final long ianaIdentifier) {
return tryFind(
Registrar.loadAllCached(),
new Predicate<Registrar>() {
@Override
public boolean apply(Registrar registrar) {
return registrar.getIanaIdentifier() == ianaIdentifier;
}});
}
}