Add support for performing checks on Gerrit start up

This allows to do checks on Gerrit startup and abort the Gerrit startup
if issues are found. E.g. this allows us to do validation on the Gerrit
config, which is needed by a follow-up change.

The startup checks are done after all lifecycle listeners have been
invoked so that logging is already available and the reason for a
failing startup can be written to the error log.

All implementations of StartupCheck should be bound in
StartupChecks.Module.

Change-Id: I941dad2c926130a1f9c5a1a363f7e1eff0dab304
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-01-31 12:16:04 +01:00
parent ad9e2a7e00
commit 97f74f081d
5 changed files with 114 additions and 0 deletions

View File

@@ -47,6 +47,7 @@ import com.google.gerrit.pgm.util.ErrorLogFile;
import com.google.gerrit.pgm.util.LogFileCompressor;
import com.google.gerrit.pgm.util.RuntimeShutdown;
import com.google.gerrit.pgm.util.SiteProgram;
import com.google.gerrit.server.StartupChecks;
import com.google.gerrit.server.account.InternalAccountDirectory;
import com.google.gerrit.server.cache.h2.DefaultCacheFactory;
import com.google.gerrit.server.change.ChangeCleanupRunner;
@@ -379,6 +380,7 @@ public class Daemon extends SiteProgram {
modules.add(new PluginRestApiModule());
modules.add(new RestCacheAdminModule());
modules.add(new GpgModule(config));
modules.add(new StartupChecks.Module());
if (MoreObjects.firstNonNull(httpd, true)) {
modules.add(new CanonicalWebUrlModule() {
@Override

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2017 The Android Open Source Project
//
// 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 com.google.gerrit.server;
import com.google.gerrit.extensions.events.LifecycleListener;
/**
* Check executed on Gerrit startup.
*/
public interface StartupCheck {
/**
* Performs Gerrit startup check, can abort startup by throwing
* {@link StartupException}.
* <p>
* Called on Gerrit startup after all {@link LifecycleListener} have been
* invoked.
*
* @throws StartupException thrown if Gerrit startup should be aborted
*/
void check() throws StartupException;
}

View File

@@ -0,0 +1,50 @@
// Copyright (C) 2017 The Android Open Source Project
//
// 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 com.google.gerrit.server;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class StartupChecks implements LifecycleListener {
public static class Module extends LifecycleModule {
@Override
protected void configure() {
DynamicSet.setOf(binder(), StartupCheck.class);
listener().to(StartupChecks.class);
}
}
private final DynamicSet<StartupCheck> startupChecks;
@Inject
StartupChecks(DynamicSet<StartupCheck> startupChecks) {
this.startupChecks = startupChecks;
}
@Override
public void start() throws StartupException {
for (StartupCheck startupCheck : startupChecks) {
startupCheck.check();
}
}
@Override
public void stop() {
}
}

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2017 The Android Open Source Project
//
// 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 com.google.gerrit.server;
public class StartupException extends RuntimeException {
private static final long serialVersionUID = 1L;
public StartupException(String message) {
super(message);
}
public StartupException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -32,6 +32,7 @@ import com.google.gerrit.lucene.LuceneIndexModule;
import com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker;
import com.google.gerrit.pgm.util.LogFileCompressor;
import com.google.gerrit.server.LibModuleLoader;
import com.google.gerrit.server.StartupChecks;
import com.google.gerrit.server.account.InternalAccountDirectory;
import com.google.gerrit.server.cache.h2.DefaultCacheFactory;
import com.google.gerrit.server.change.ChangeCleanupRunner;
@@ -319,6 +320,7 @@ public class WebAppInitializer extends GuiceServletContextListener
modules.add(new PluginRestApiModule());
modules.add(new RestCacheAdminModule());
modules.add(new GpgModule(config));
modules.add(new StartupChecks.Module());
// Index module shutdown must happen before work queue shutdown, otherwise
// work queue can get stuck waiting on index futures that will never return.