diff --git a/Documentation/config-gerrit.txt b/Documentation/config-gerrit.txt index d342990ec5..9ba5a0d581 100644 --- a/Documentation/config-gerrit.txt +++ b/Documentation/config-gerrit.txt @@ -2224,6 +2224,12 @@ Path prefix for PolyGerrit's static resources if using a CDN. Path for PolyGerrit's favicon after link:#gerrit.canonicalWebUrl[default URL], including icon name and extension (.ico should be used). +[[gerrit.instanceId]]gerrit.instanceId:: ++ +Optional identifier for this Gerrit instance. +Used to identify a specific instance within a group of Gerrit instances with the +same `serverId` (i.e.: a Gerrit cluster). +Unlike `instanceName` this value is not available in the email templates. [[gerrit.instanceName]]gerrit.instanceName:: + diff --git a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java index 8df9518650..89cc724242 100644 --- a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java +++ b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java @@ -108,6 +108,7 @@ import com.google.gerrit.server.change.RevisionResource; import com.google.gerrit.server.config.AllProjectsName; import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.CanonicalWebUrl; +import com.google.gerrit.server.config.GerritInstanceId; import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.PluginConfigFactory; import com.google.gerrit.server.config.SitePaths; @@ -245,6 +246,7 @@ public abstract class AbstractDaemonTest { @Inject @CanonicalWebUrl protected Provider canonicalWebUrl; @Inject @GerritPersonIdent protected Provider serverIdent; @Inject @GerritServerConfig protected Config cfg; + @Inject @GerritInstanceId @Nullable protected String instanceId; @Inject protected AcceptanceTestRequestScope atrScope; @Inject protected AccountCache accountCache; @Inject protected AccountCreator accountCreator; diff --git a/java/com/google/gerrit/pgm/Daemon.java b/java/com/google/gerrit/pgm/Daemon.java index 8905ee5ea1..034e042e8e 100644 --- a/java/com/google/gerrit/pgm/Daemon.java +++ b/java/com/google/gerrit/pgm/Daemon.java @@ -72,6 +72,7 @@ import com.google.gerrit.server.config.CanonicalWebUrlProvider; import com.google.gerrit.server.config.DefaultUrlFormatter; import com.google.gerrit.server.config.DownloadConfig; import com.google.gerrit.server.config.GerritGlobalModule; +import com.google.gerrit.server.config.GerritInstanceIdModule; import com.google.gerrit.server.config.GerritInstanceNameModule; import com.google.gerrit.server.config.GerritOptions; import com.google.gerrit.server.config.GerritRuntime; @@ -446,6 +447,7 @@ public class Daemon extends SiteProgram { modules.add(new GpgModule(config)); modules.add(new StartupChecks.Module()); modules.add(new GerritInstanceNameModule()); + modules.add(new GerritInstanceIdModule()); if (MoreObjects.firstNonNull(httpd, true)) { modules.add( new CanonicalWebUrlModule() { diff --git a/java/com/google/gerrit/server/config/GerritInstanceId.java b/java/com/google/gerrit/server/config/GerritInstanceId.java new file mode 100644 index 0000000000..dba7eac55e --- /dev/null +++ b/java/com/google/gerrit/server/config/GerritInstanceId.java @@ -0,0 +1,30 @@ +// Copyright (C) 2020 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.config; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import com.google.inject.BindingAnnotation; +import java.lang.annotation.Retention; + +/** + * Marker on a {@link String} holding the instance id for this server. + * + *

Note that the String may be null, if the administrator has not configured the value. Clients + * must handle such cases explicitly. + */ +@Retention(RUNTIME) +@BindingAnnotation +public @interface GerritInstanceId {} diff --git a/java/com/google/gerrit/server/config/GerritInstanceIdModule.java b/java/com/google/gerrit/server/config/GerritInstanceIdModule.java new file mode 100644 index 0000000000..33af5286cf --- /dev/null +++ b/java/com/google/gerrit/server/config/GerritInstanceIdModule.java @@ -0,0 +1,30 @@ +// Copyright (C) 2020 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.config; + +import static com.google.inject.Scopes.SINGLETON; + +import com.google.inject.AbstractModule; + +/** Supports binding the {@link GerritInstanceId} annotation. */ +public class GerritInstanceIdModule extends AbstractModule { + @Override + protected void configure() { + bind(String.class) + .annotatedWith(GerritInstanceId.class) + .toProvider(GerritInstanceIdProvider.class) + .in(SINGLETON); + } +} diff --git a/java/com/google/gerrit/server/config/GerritInstanceIdProvider.java b/java/com/google/gerrit/server/config/GerritInstanceIdProvider.java new file mode 100644 index 0000000000..891ca7668f --- /dev/null +++ b/java/com/google/gerrit/server/config/GerritInstanceIdProvider.java @@ -0,0 +1,36 @@ +// Copyright (C) 2020 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.config; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import org.eclipse.jgit.lib.Config; + +/** Provides {@link GerritInstanceId} from {@code gerrit.instanceId}. */ +@Singleton +public class GerritInstanceIdProvider implements Provider { + private final String instanceId; + + @Inject + public GerritInstanceIdProvider(@GerritServerConfig Config cfg) { + instanceId = cfg.getString("gerrit", null, "instanceId"); + } + + @Override + public String get() { + return instanceId; + } +} diff --git a/java/com/google/gerrit/testing/InMemoryModule.java b/java/com/google/gerrit/testing/InMemoryModule.java index baccbf9de4..8800463b5b 100644 --- a/java/com/google/gerrit/testing/InMemoryModule.java +++ b/java/com/google/gerrit/testing/InMemoryModule.java @@ -49,6 +49,7 @@ import com.google.gerrit.server.config.CanonicalWebUrlModule; import com.google.gerrit.server.config.CanonicalWebUrlProvider; import com.google.gerrit.server.config.DefaultUrlFormatter; import com.google.gerrit.server.config.GerritGlobalModule; +import com.google.gerrit.server.config.GerritInstanceIdModule; import com.google.gerrit.server.config.GerritInstanceNameModule; import com.google.gerrit.server.config.GerritOptions; import com.google.gerrit.server.config.GerritRuntime; @@ -189,6 +190,7 @@ public class InMemoryModule extends FactoryModule { install(new InMemorySchemaModule()); install(NoSshKeyCache.module()); install(new GerritInstanceNameModule()); + install(new GerritInstanceIdModule()); install( new CanonicalWebUrlModule() { @Override diff --git a/javatests/com/google/gerrit/acceptance/config/GerritInstanceIdIT.java b/javatests/com/google/gerrit/acceptance/config/GerritInstanceIdIT.java new file mode 100644 index 0000000000..0dd6a83e84 --- /dev/null +++ b/javatests/com/google/gerrit/acceptance/config/GerritInstanceIdIT.java @@ -0,0 +1,34 @@ +// Copyright (C) 2020 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.acceptance.config; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.gerrit.acceptance.AbstractDaemonTest; +import org.junit.Test; + +public class GerritInstanceIdIT extends AbstractDaemonTest { + + @Test + @GerritConfig(name = "gerrit.instanceId", value = "testInstanceId") + public void shouldReturnInstanceIdWhenDefined() { + assertThat(instanceId).isEqualTo("testInstanceId"); + } + + @Test + public void shouldReturnNullWhenNotDefined() { + assertThat(instanceId).isNull(); + } +}