1
0
mirror of https://github.com/google/nomulus synced 2026-01-04 04:04:22 +00:00

Add activeOrDeletedSince parameter to RefreshDnsForAllDomainsAction (#2556)

This commit is contained in:
Juan Celhay
2024-09-17 12:02:25 -04:00
committed by GitHub
parent b07769bdee
commit 7ee541e1b1
4 changed files with 115 additions and 7 deletions

View File

@@ -39,6 +39,7 @@ import javax.annotation.Nullable;
import javax.inject.Inject;
import org.apache.arrow.util.VisibleForTesting;
import org.apache.http.HttpStatus;
import org.joda.time.DateTime;
import org.joda.time.Duration;
/**
@@ -50,6 +51,10 @@ import org.joda.time.Duration;
*
* <p>You may pass in a {@code batchSize} for the batched read of domains from the database. This is
* recommended to be somewhere between 200 and 500. The default value is 250.
*
* <p>If {@code activeOrDeletedSince} is passed in the request, this action will enqueue DNS publish
* tasks on all domains with a deletion time equal or greater than the value provided, including
* domains that have since been deleted.
*/
@Action(
service = Action.Service.TOOLS,
@@ -80,17 +85,21 @@ public class RefreshDnsForAllDomainsAction implements Runnable {
private final Random random;
private final DateTime activeOrDeletedSince;
@Inject
RefreshDnsForAllDomainsAction(
Response response,
@Parameter(PARAM_TLDS) ImmutableSet<String> tlds,
@Parameter(PARAM_BATCH_SIZE) Optional<Integer> batchSize,
@Parameter("refreshQps") Optional<Integer> refreshQps,
@Parameter("activeOrDeletedSince") Optional<DateTime> activeOrDeletedSince,
Random random) {
this.response = response;
this.tlds = tlds;
this.batchSize = batchSize.orElse(DEFAULT_BATCH_SIZE);
this.refreshQps = refreshQps.orElse(DEFAULT_REFRESH_QPS);
this.activeOrDeletedSince = activeOrDeletedSince.orElse(END_OF_TIME);
this.random = random;
}
@@ -118,11 +127,11 @@ public class RefreshDnsForAllDomainsAction implements Runnable {
private Duration calculateSmear() {
Long activeDomains =
tm().query(
"SELECT COUNT(*) FROM Domain WHERE tld IN (:tlds) AND deletionTime ="
+ " :endOfTime",
"SELECT COUNT(*) FROM Domain WHERE tld IN (:tlds) AND deletionTime >="
+ " :activeOrDeletedSince",
Long.class)
.setParameter("tlds", tlds)
.setParameter("endOfTime", END_OF_TIME)
.setParameter("activeOrDeletedSince", activeOrDeletedSince)
.getSingleResult();
return Duration.standardSeconds(Math.max(activeDomains / refreshQps, 1));
}
@@ -131,12 +140,12 @@ public class RefreshDnsForAllDomainsAction implements Runnable {
String sql =
String.format(
"SELECT domainName FROM Domain WHERE tld IN (:tlds) AND"
+ " deletionTime = :endOfTime %s ORDER BY domainName ASC",
+ " deletionTime >= :activeOrDeletedSince %s ORDER BY domainName ASC",
lastInPreviousBatch.isPresent() ? "AND domainName > :lastInPreviousBatch" : "");
TypedQuery<String> query =
tm().query(sql, String.class)
.setParameter("tlds", tlds)
.setParameter("endOfTime", END_OF_TIME);
.setParameter("activeOrDeletedSince", activeOrDeletedSince);
lastInPreviousBatch.ifPresent(l -> query.setParameter("lastInPreviousBatch", l));
return query.setMaxResults(batchSize).getResultStream().collect(toImmutableList());
}

View File

@@ -16,6 +16,7 @@ package google.registry.tools.server;
import static com.google.common.base.Strings.emptyToNull;
import static google.registry.request.RequestParameters.extractIntParameter;
import static google.registry.request.RequestParameters.extractOptionalDatetimeParameter;
import static google.registry.request.RequestParameters.extractOptionalIntParameter;
import static google.registry.request.RequestParameters.extractOptionalParameter;
import static google.registry.request.RequestParameters.extractRequiredParameter;
@@ -26,6 +27,7 @@ import google.registry.request.Parameter;
import google.registry.tools.server.UpdateUserGroupAction.Mode;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
import org.joda.time.DateTime;
/** Dagger module for the tools package. */
@Module
@@ -75,6 +77,12 @@ public class ToolsServerModule {
return extractOptionalIntParameter(req, "refreshQps");
}
@Provides
@Parameter("activeOrDeletedSince")
static Optional<DateTime> provideDeletionTime(HttpServletRequest req) {
return extractOptionalDatetimeParameter(req, "activeOrDeletedSince");
}
@Provides
static Mode provideGroupUpdateMode(HttpServletRequest req) {
return Mode.valueOf(extractRequiredParameter(req, "groupUpdateMode"));

View File

@@ -56,7 +56,12 @@ public class RefreshDnsForAllDomainsActionTest {
createTld("bar");
action =
new RefreshDnsForAllDomainsAction(
response, ImmutableSet.of("bar"), Optional.of(10), Optional.empty(), new Random());
response,
ImmutableSet.of("bar"),
Optional.of(10),
Optional.empty(),
Optional.empty(),
new Random());
}
@Test
@@ -75,7 +80,12 @@ public class RefreshDnsForAllDomainsActionTest {
// Set batch size to 1 since each batch will be enqueud at the same time
action =
new RefreshDnsForAllDomainsAction(
response, ImmutableSet.of("bar"), Optional.of(1), Optional.of(7), new Random());
response,
ImmutableSet.of("bar"),
Optional.of(1),
Optional.of(7),
Optional.empty(),
new Random());
tm().transact(() -> action.refreshBatch(Optional.empty(), Duration.standardMinutes(1000)));
tm().transact(() -> action.refreshBatch(Optional.empty(), Duration.standardMinutes(1000)));
ImmutableList<DnsRefreshRequest> refreshRequests =
@@ -98,6 +108,28 @@ public class RefreshDnsForAllDomainsActionTest {
assertNoDnsRequestsExcept("foo.bar");
}
@Test
void test_runAction_refreshesDeletedDomain_whenActiveOrDeletedSinceIsProvided() throws Exception {
action =
new RefreshDnsForAllDomainsAction(
response,
ImmutableSet.of("bar"),
Optional.of(1),
Optional.of(7),
Optional.of(clock.nowUtc().minusYears(3)),
new Random());
persistActiveDomain("foo.bar");
persistDeletedDomain("deleted1.bar", clock.nowUtc().minusYears(1));
persistDeletedDomain("deleted3.bar", clock.nowUtc().minusYears(3));
persistDeletedDomain("deleted5.bar", clock.nowUtc().minusYears(5));
action.run();
assertDomainDnsRequestWithRequestTime("foo.bar", clock.nowUtc());
assertDomainDnsRequestWithRequestTime("deleted1.bar", clock.nowUtc());
assertDomainDnsRequestWithRequestTime("deleted3.bar", clock.nowUtc());
assertNoDnsRequestsExcept("foo.bar", "deleted1.bar", "deleted3.bar");
}
@Test
void test_runAction_ignoresDomainsOnOtherTlds() throws Exception {
createTld("baz");

View File

@@ -0,0 +1,59 @@
// 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.tools.server;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import google.registry.request.HttpException.BadRequestException;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link ToolsServerModule}. */
public class ToolsServerModuleTest {
private final HttpServletRequest request = mock(HttpServletRequest.class);
@Test
void test_provideDeletionTime() throws Exception {
when(request.getParameter("activeOrDeletedSince")).thenReturn("1991-07-01T00:00:00Z");
DateTime expected = DateTime.parse("1991-07-01T00:00:00Z");
Optional<DateTime> dateTimeParam = ToolsServerModule.provideDeletionTime(request);
assertThat(dateTimeParam).isEqualTo(Optional.of(expected));
}
@Test
void test_doesNotprovideDeletionTimeOnEmptyParam() throws Exception {
when(request.getParameter("activeOrDeletedSince")).thenReturn("");
assertThat(ToolsServerModule.provideDeletionTime(request)).isEqualTo(Optional.empty());
}
@Test
void test_provideDeletionTime_incorrectDateFormat_throwsBadRequestException() throws Exception {
when(request.getParameter("activeOrDeletedSince")).thenReturn("error404?");
BadRequestException thrown =
assertThrows(
BadRequestException.class, () -> ToolsServerModule.provideDeletionTime(request));
assertThat(thrown).hasMessageThat().contains("Bad ISO 8601 timestamp: activeOrDeletedSince");
}
}