mirror of
https://github.com/google/nomulus
synced 2026-01-07 14:05:44 +00:00
Use standard JVM shutdown hook (#2323)
This removes a dependency on the App Engine SDK. It also looks like (from the logs at least) that shutdown hooks registered the old way stopped working after the runtime is upgraded to Java 17. Also removed some random leftover dependencies on the App Engine SKD that are not needed any more.
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
|
||||
package google.registry.module;
|
||||
|
||||
import com.google.appengine.api.LifecycleManager;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.monitoring.metrics.MetricReporter;
|
||||
import dagger.Lazy;
|
||||
@@ -46,25 +45,26 @@ public class ServletBase extends HttpServlet {
|
||||
public void init() {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
|
||||
// If metric reporter failed to instantiate for any reason (bad keyring, bad json credential,
|
||||
// etc), we log the error but keep the main thread running. Also the shutdown hook will only be
|
||||
// registered if metric reporter starts up correctly.
|
||||
// If the metric reporter failed to instantiate for any reason (bad keyring, bad json
|
||||
// credential, etc.), we log the error but keep the main thread running. Also, the shutdown hook
|
||||
// will only be registered if the metric reporter starts up correctly.
|
||||
try {
|
||||
metricReporter.get().startAsync().awaitRunning(java.time.Duration.ofSeconds(10));
|
||||
logger.atInfo().log("Started up MetricReporter.");
|
||||
LifecycleManager.getInstance()
|
||||
.setShutdownHook(
|
||||
() -> {
|
||||
try {
|
||||
metricReporter
|
||||
.get()
|
||||
.stopAsync()
|
||||
.awaitTerminated(java.time.Duration.ofSeconds(10));
|
||||
logger.atInfo().log("Shut down MetricReporter.");
|
||||
} catch (TimeoutException e) {
|
||||
logger.atSevere().withCause(e).log("Failed to stop MetricReporter.");
|
||||
}
|
||||
});
|
||||
Runtime.getRuntime()
|
||||
.addShutdownHook(
|
||||
new Thread(
|
||||
() -> {
|
||||
try {
|
||||
metricReporter
|
||||
.get()
|
||||
.stopAsync()
|
||||
.awaitTerminated(java.time.Duration.ofSeconds(10));
|
||||
logger.atInfo().log("Shut down MetricReporter.");
|
||||
} catch (TimeoutException e) {
|
||||
logger.atSevere().withCause(e).log("Failed to stop MetricReporter.");
|
||||
}
|
||||
}));
|
||||
} catch (Exception e) {
|
||||
logger.atSevere().withCause(e).log("Failed to initialize MetricReporter.");
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import static google.registry.util.DomainNameUtils.canonicalizeHostname;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.google.api.client.http.HttpMethods;
|
||||
import com.google.appengine.api.urlfetch.HTTPResponse;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.config.RegistryConfig.Config;
|
||||
@@ -107,7 +106,7 @@ public class RdeReporter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshals IIRDEA XML result object from {@link HTTPResponse} payload.
|
||||
* Unmarshall IIRDEA XML result object from HTTP response payload.
|
||||
*
|
||||
* @see <a
|
||||
* href="http://tools.ietf.org/html/draft-lozano-icann-registry-interfaces-05#section-4.1">
|
||||
|
||||
@@ -24,8 +24,8 @@ import com.google.common.collect.ImmutableSortedSet;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import google.registry.model.server.Lock;
|
||||
import google.registry.util.AppEngineTimeLimiter;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.TimeLimiter;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -82,7 +82,7 @@ public class LockHandlerImpl implements LockHandler {
|
||||
DateTime startTime = clock.nowUtc();
|
||||
String sanitizedTld = Strings.emptyToNull(tld);
|
||||
try {
|
||||
return AppEngineTimeLimiter.create()
|
||||
return TimeLimiter.create()
|
||||
.callWithTimeout(
|
||||
new LockingCallable(callable, lockAcquirer, sanitizedTld, leaseLength, lockNames),
|
||||
leaseLength.minus(LOCK_TIMEOUT_FUDGE).getMillis(),
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
// Copyright 2018 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;
|
||||
|
||||
import com.google.api.services.appengine.v1.Appengine;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import google.registry.config.CredentialModule.LocalCredential;
|
||||
import google.registry.config.RegistryConfig.Config;
|
||||
import google.registry.util.GoogleCredentialsBundle;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/** Module providing the instance of {@link Appengine} to access App Engine Admin Api. */
|
||||
@Module
|
||||
public abstract class AppEngineAdminApiModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public static Appengine provideAppengine(
|
||||
@LocalCredential GoogleCredentialsBundle credentialsBundle,
|
||||
@Config("projectId") String projectId) {
|
||||
return new Appengine.Builder(
|
||||
credentialsBundle.getHttpTransport(),
|
||||
credentialsBundle.getJsonFactory(),
|
||||
credentialsBundle.getHttpRequestInitializer())
|
||||
.setApplicationName(projectId)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,6 @@ import javax.inject.Singleton;
|
||||
@Singleton
|
||||
@Component(
|
||||
modules = {
|
||||
AppEngineAdminApiModule.class,
|
||||
AuthModule.class,
|
||||
BatchModule.class,
|
||||
BigqueryModule.class,
|
||||
|
||||
@@ -40,7 +40,6 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.utils.SystemProperty;
|
||||
import com.google.cloud.storage.BlobId;
|
||||
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;
|
||||
import com.google.common.io.ByteSource;
|
||||
@@ -184,9 +183,6 @@ public class RdeUploadActionTest {
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() throws Exception {
|
||||
// Force "development" mode so we don't try to really connect to GCS.
|
||||
SystemProperty.environment.set(SystemProperty.Environment.Value.Development);
|
||||
|
||||
createTld("tld");
|
||||
gcsUtils.createFromBytes(GHOSTRYDE_FILE, Ghostryde.encode(DEPOSIT_XML.read(), encryptKey));
|
||||
gcsUtils.createFromBytes(GHOSTRYDE_R1_FILE, Ghostryde.encode(DEPOSIT_XML.read(), encryptKey));
|
||||
|
||||
@@ -1,146 +0,0 @@
|
||||
// Copyright 2018 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.testing;
|
||||
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.services.appengine.v1.Appengine;
|
||||
import com.google.api.services.appengine.v1.model.ListServicesResponse;
|
||||
import com.google.api.services.appengine.v1.model.ListVersionsResponse;
|
||||
import com.google.api.services.appengine.v1.model.ManualScaling;
|
||||
import com.google.api.services.appengine.v1.model.Service;
|
||||
import com.google.api.services.appengine.v1.model.TrafficSplit;
|
||||
import com.google.api.services.appengine.v1.model.Version;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import google.registry.model.Buildable;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Helper class to provide a builder to construct {@link Appengine} object for testing. */
|
||||
public class AppEngineAdminApiHelper extends ImmutableObject implements Buildable {
|
||||
|
||||
private Appengine appengine;
|
||||
private String appId = "domain-registry-test";
|
||||
private Multimap<String, String> liveVersionsMap = ImmutableMultimap.of();
|
||||
private Multimap<String, String> manualScalingVersionsMap = ImmutableMultimap.of();
|
||||
|
||||
/** Returns the {@link Appengine} object. */
|
||||
public Appengine getAppengine() {
|
||||
return appengine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder asBuilder() {
|
||||
return new Builder(clone(this));
|
||||
}
|
||||
|
||||
/** A builder for constructing {@link Appengine} object, since it is immutable. */
|
||||
public static class Builder extends Buildable.Builder<AppEngineAdminApiHelper> {
|
||||
public Builder() {}
|
||||
|
||||
private Builder(AppEngineAdminApiHelper instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public Builder setAppId(String appId) {
|
||||
getInstance().appId = appId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setLiveVersionsMap(Multimap<String, String> liveVersionsMap) {
|
||||
getInstance().liveVersionsMap = liveVersionsMap;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setManualScalingVersionsMap(Multimap<String, String> manualScalingVersionsMap) {
|
||||
getInstance().manualScalingVersionsMap = manualScalingVersionsMap;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppEngineAdminApiHelper build() {
|
||||
getInstance().appengine = mock(Appengine.class, RETURNS_DEEP_STUBS);
|
||||
|
||||
// Mockito cannot mock ListServicesResponse as it is a final class
|
||||
ListServicesResponse listServicesResponse = new ListServicesResponse();
|
||||
try {
|
||||
when((Object) getInstance().appengine.apps().services().list(getInstance().appId).execute())
|
||||
.thenReturn(listServicesResponse);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// Add all given live versions to mocked Appengine object
|
||||
java.util.List<Service> serviceList = new ArrayList<>();
|
||||
getInstance()
|
||||
.liveVersionsMap
|
||||
.asMap()
|
||||
.forEach(
|
||||
(serviceId, versionList) -> {
|
||||
Service service = new Service();
|
||||
TrafficSplit trafficSplit = new TrafficSplit();
|
||||
trafficSplit.setAllocations(
|
||||
versionList.stream()
|
||||
.collect(Collectors.toMap(version -> version, version -> 1.0)));
|
||||
|
||||
service.setId(serviceId);
|
||||
service.setSplit(trafficSplit);
|
||||
serviceList.add(service);
|
||||
});
|
||||
listServicesResponse.setServices(serviceList);
|
||||
|
||||
// Add all given manual scaling versions to mocked Appengine object
|
||||
getInstance()
|
||||
.manualScalingVersionsMap
|
||||
.asMap()
|
||||
.forEach(
|
||||
(service, versionList) -> {
|
||||
// Mockito cannot mock ListVersionsResponse as it is a final class
|
||||
ListVersionsResponse listVersionsResponse = new ListVersionsResponse();
|
||||
try {
|
||||
when((Object)
|
||||
getInstance()
|
||||
.appengine
|
||||
.apps()
|
||||
.services()
|
||||
.versions()
|
||||
.list(getInstance().appId, service)
|
||||
.execute())
|
||||
.thenReturn(listVersionsResponse);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
listVersionsResponse.setVersions(
|
||||
versionList.stream()
|
||||
.map(
|
||||
versionId -> {
|
||||
Version version = new Version();
|
||||
ManualScaling manualScaling = new ManualScaling();
|
||||
version.setManualScaling(manualScaling);
|
||||
version.setId(versionId);
|
||||
return version;
|
||||
})
|
||||
.collect(Collectors.toList()));
|
||||
});
|
||||
|
||||
return getInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user