Make DnsWriter truly atomic

Right now - if there's an error during DnsWriter.publish*, all the publish from
before that error will be committed, while all the publish after that error
will not.

More than that - in some writers partial publishes can be committed, depending
on implementation.

This defines a new contract that publish* are only committed when .commit is
called. That way any error will simply mean no publish is committed.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=165708063
This commit is contained in:
guyben 2017-08-18 08:27:34 -07:00 committed by Ben McIlwain
parent fcb554947c
commit d5ac03aae4
8 changed files with 204 additions and 90 deletions

View file

@ -93,29 +93,30 @@ public final class PublishDnsUpdatesAction implements Runnable, Callable<Void> {
/** Steps through the domain and host refreshes contained in the parameters and processes them. */
private void processBatch() {
try (DnsWriter writer = dnsWriterProxy.getByClassNameForTld(dnsWriter, tld)) {
for (String domain : nullToEmpty(domains)) {
if (!DomainNameUtils.isUnder(
InternetDomainName.from(domain), InternetDomainName.from(tld))) {
dnsMetrics.incrementPublishDomainRequests(tld, Status.REJECTED);
logger.severefmt("%s: skipping domain %s not under tld", tld, domain);
} else {
dnsMetrics.incrementPublishDomainRequests(tld, Status.ACCEPTED);
writer.publishDomain(domain);
logger.infofmt("%s: published domain %s", tld, domain);
}
}
for (String host : nullToEmpty(hosts)) {
if (!DomainNameUtils.isUnder(
InternetDomainName.from(host), InternetDomainName.from(tld))) {
dnsMetrics.incrementPublishHostRequests(tld, Status.REJECTED);
logger.severefmt("%s: skipping host %s not under tld", tld, host);
} else {
dnsMetrics.incrementPublishHostRequests(tld, Status.ACCEPTED);
writer.publishHost(host);
logger.infofmt("%s: published host %s", tld, host);
}
DnsWriter writer = dnsWriterProxy.getByClassNameForTld(dnsWriter, tld);
for (String domain : nullToEmpty(domains)) {
if (!DomainNameUtils.isUnder(
InternetDomainName.from(domain), InternetDomainName.from(tld))) {
dnsMetrics.incrementPublishDomainRequests(tld, Status.REJECTED);
logger.severefmt("%s: skipping domain %s not under tld", tld, domain);
} else {
dnsMetrics.incrementPublishDomainRequests(tld, Status.ACCEPTED);
writer.publishDomain(domain);
logger.infofmt("%s: published domain %s", tld, domain);
}
}
for (String host : nullToEmpty(hosts)) {
if (!DomainNameUtils.isUnder(
InternetDomainName.from(host), InternetDomainName.from(tld))) {
dnsMetrics.incrementPublishHostRequests(tld, Status.REJECTED);
logger.severefmt("%s: skipping host %s not under tld", tld, host);
} else {
dnsMetrics.incrementPublishHostRequests(tld, Status.ACCEPTED);
writer.publishHost(host);
logger.infofmt("%s: published host %s", tld, host);
}
}
// If we got here it means we managed to stage the entire batch without any errors.
writer.commit();
}
}