Use standard java thread factory instead of the AppEngine flavor

With Java 8 in GAE standard environment, we can now use standard java thread factory to run the metric reporter in the background in daemon mode, which would not interfere with basic scaling idle timeout as App Engine thread would.

Because the thread is not created by ThreadManager, no App Engine APIs can be called from it. We therefore use GoogleCredential instead of AppIdentityCredential as HttpRequestInitializer, and NetHttpTransport instead of UlrFetchTransport as HttpTransport.

MetricReporter is lazy injected because it depends on jsonCredential retrieved from CloudKms, which is not available in a test environment, causing FrontendServletTest and BackendServletTest to fail.

Some minor re-formatting with google-java-format on edited files.

Lastly removed moe comments in import statement, which makes the linter unhappy.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=172896227
This commit is contained in:
jianglai 2017-10-20 09:58:14 -07:00
parent b01fa6b4c9
commit c702b4486c
8 changed files with 151 additions and 118 deletions

View file

@ -15,7 +15,7 @@
package google.registry.module.backend;
import com.google.appengine.api.LifecycleManager;
import com.google.appengine.api.LifecycleManager.ShutdownHook;
import dagger.Lazy;
import google.registry.monitoring.metrics.MetricReporter;
import google.registry.util.FormattingLogger;
import java.io.IOException;
@ -32,7 +32,7 @@ public final class BackendServlet extends HttpServlet {
private static final BackendComponent component = DaggerBackendComponent.create();
private static final BackendRequestHandler requestHandler = component.requestHandler();
private static final MetricReporter metricReporter = component.metricReporter();
private static final Lazy<MetricReporter> metricReporter = component.metricReporter();
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
@Override
@ -40,7 +40,7 @@ public final class BackendServlet extends HttpServlet {
Security.addProvider(new BouncyCastleProvider());
try {
metricReporter.startAsync().awaitRunning(10, TimeUnit.SECONDS);
metricReporter.get().startAsync().awaitRunning(10, TimeUnit.SECONDS);
logger.info("Started up MetricReporter");
} catch (TimeoutException timeoutException) {
logger.severefmt("Failed to initialize MetricReporter: %s", timeoutException);
@ -48,15 +48,12 @@ public final class BackendServlet extends HttpServlet {
LifecycleManager.getInstance()
.setShutdownHook(
new ShutdownHook() {
@Override
public void shutdown() {
try {
metricReporter.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
logger.info("Shut down MetricReporter");
} catch (TimeoutException timeoutException) {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException);
}
() -> {
try {
metricReporter.get().stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
logger.info("Shut down MetricReporter");
} catch (TimeoutException timeoutException) {
logger.severefmt("Failed to stop MetricReporter: %s", timeoutException);
}
});
}