mirror of
https://github.com/google/nomulus.git
synced 2025-07-03 01:33:29 +02:00
Extract multiple commit prevention in DNS writers into a base class
This still retains the DnsWriter interface itself for better integration with Dagger and to preserve the option of having a DNS writer that does not have this requirement (e.g. because it is idempotent). This also makes the commit check thread-safe, which is a nice-to-have. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=168451114
This commit is contained in:
parent
f6e0d5fa0c
commit
51298aeabb
4 changed files with 49 additions and 32 deletions
36
java/google/registry/dns/writer/BaseDnsWriter.java
Normal file
36
java/google/registry/dns/writer/BaseDnsWriter.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
// 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.dns.writer;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* A base implementation of {@link DnsWriter} that protects against multiple calls to commit().
|
||||
*/
|
||||
public abstract class BaseDnsWriter implements DnsWriter {
|
||||
|
||||
private final AtomicBoolean committedAlready = new AtomicBoolean(false);
|
||||
|
||||
@Override
|
||||
public final void commit() {
|
||||
checkState(committedAlready.compareAndSet(false, true), "commit() has already been called");
|
||||
commitUnchecked();
|
||||
}
|
||||
|
||||
/** Commits DNS updates. This can never be called more than once. */
|
||||
protected abstract void commitUnchecked();
|
||||
}
|
|
@ -14,8 +14,6 @@
|
|||
|
||||
package google.registry.dns.writer;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
@ -27,7 +25,7 @@ import javax.inject.Inject;
|
|||
*
|
||||
* <p>All this class does is write its displeasure to the logs.
|
||||
*/
|
||||
public final class VoidDnsWriter implements DnsWriter {
|
||||
public final class VoidDnsWriter extends BaseDnsWriter {
|
||||
|
||||
/**
|
||||
* The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change
|
||||
|
@ -37,8 +35,6 @@ public final class VoidDnsWriter implements DnsWriter {
|
|||
|
||||
private static final Logger logger = Logger.getLogger(VoidDnsWriter.class.getName());
|
||||
|
||||
private boolean committed = false;
|
||||
|
||||
private final Set<String> names = new HashSet<>();
|
||||
|
||||
@Inject
|
||||
|
@ -55,10 +51,7 @@ public final class VoidDnsWriter implements DnsWriter {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void commit() {
|
||||
checkState(!committed, "commit() has already been called");
|
||||
committed = true;
|
||||
|
||||
protected void commitUnchecked() {
|
||||
logger.warning("Ignoring DNS zone updates! No DnsWriterFactory implementation specified!\n"
|
||||
+ Joiner.on('\n').join(names));
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
package google.registry.dns.writer.clouddns;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static google.registry.model.EppResourceUtils.loadByForeignKey;
|
||||
|
||||
import com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo;
|
||||
|
@ -33,6 +32,7 @@ import com.google.common.collect.ImmutableSet.Builder;
|
|||
import com.google.common.net.InternetDomainName;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import google.registry.config.RegistryConfig.Config;
|
||||
import google.registry.dns.writer.BaseDnsWriter;
|
||||
import google.registry.dns.writer.DnsWriter;
|
||||
import google.registry.dns.writer.DnsWriterZone;
|
||||
import google.registry.model.domain.DomainResource;
|
||||
|
@ -59,7 +59,7 @@ import org.joda.time.Duration;
|
|||
*
|
||||
* @see <a href="https://cloud.google.com/dns/docs/">Google Cloud DNS Documentation</a>
|
||||
*/
|
||||
public class CloudDnsWriter implements DnsWriter {
|
||||
public class CloudDnsWriter extends BaseDnsWriter {
|
||||
|
||||
/**
|
||||
* The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change
|
||||
|
@ -85,8 +85,6 @@ public class CloudDnsWriter implements DnsWriter {
|
|||
private final ImmutableMap.Builder<String, ImmutableSet<ResourceRecordSet>>
|
||||
desiredRecordsBuilder = new ImmutableMap.Builder<>();
|
||||
|
||||
private boolean committed = false;
|
||||
|
||||
@Inject
|
||||
CloudDnsWriter(
|
||||
Dns dnsConnection,
|
||||
|
@ -273,15 +271,9 @@ public class CloudDnsWriter implements DnsWriter {
|
|||
* representation built via this writer.
|
||||
*/
|
||||
@Override
|
||||
public void commit() {
|
||||
checkState(!committed, "commit() has already been called");
|
||||
committed = true;
|
||||
commit(desiredRecordsBuilder.build());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void commit(ImmutableMap<String, ImmutableSet<ResourceRecordSet>> desiredRecords) {
|
||||
retrier.callWithRetry(getMutateZoneCallback(desiredRecords), ZoneStateException.class);
|
||||
protected void commitUnchecked() {
|
||||
retrier.callWithRetry(
|
||||
getMutateZoneCallback(desiredRecordsBuilder.build()), ZoneStateException.class);
|
||||
logger.info("Wrote to Cloud DNS");
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
|
||||
package google.registry.dns.writer.dnsupdate;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.common.base.Verify.verify;
|
||||
import static com.google.common.collect.Sets.intersection;
|
||||
import static com.google.common.collect.Sets.union;
|
||||
|
@ -26,7 +25,7 @@ import com.google.common.collect.ImmutableList;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.net.InternetDomainName;
|
||||
import google.registry.config.RegistryConfig.Config;
|
||||
import google.registry.dns.writer.DnsWriter;
|
||||
import google.registry.dns.writer.BaseDnsWriter;
|
||||
import google.registry.dns.writer.DnsWriterZone;
|
||||
import google.registry.model.domain.DomainResource;
|
||||
import google.registry.model.domain.secdns.DelegationSignerData;
|
||||
|
@ -76,7 +75,7 @@ import org.xbill.DNS.Update;
|
|||
* message, as required by RFC 2136. Care must be taken to make sure the SOA serial number does not
|
||||
* go backwards if the entire TLD (zone) is "reset" to empty and republished.
|
||||
*/
|
||||
public class DnsUpdateWriter implements DnsWriter {
|
||||
public class DnsUpdateWriter extends BaseDnsWriter {
|
||||
|
||||
/**
|
||||
* The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change
|
||||
|
@ -92,12 +91,12 @@ public class DnsUpdateWriter implements DnsWriter {
|
|||
private final Update update;
|
||||
private final String zoneName;
|
||||
|
||||
private boolean committed = false;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param dnsTimeToLive TTL used for any created resource records
|
||||
* @param dnsDefaultATtl TTL used for any created resource records
|
||||
* @param dnsDefaultNsTtl TTL used for any created nameserver records
|
||||
* @param dnsDefaultDsTtl TTL used for any created DS records
|
||||
* @param transport the transport used to send/receive the UPDATE messages
|
||||
* @param clock a source of time
|
||||
*/
|
||||
|
@ -168,10 +167,7 @@ public class DnsUpdateWriter implements DnsWriter {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void commit() {
|
||||
checkState(!committed, "commit() has already been called");
|
||||
committed = true;
|
||||
|
||||
protected void commitUnchecked() {
|
||||
try {
|
||||
Message response = transport.send(update);
|
||||
verify(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue