Elasticsearch: Encapsulate supported versions in an enum
Supported versions are encapsulated in a new enum, ElasticVersion, that provides a method to translate from the version string returned by the Elasticsearch server. Translation of the version works on prefix matching, with granularity at the X.Y level. For example the version strings "2.4.0" and "2.4.6" will both resolve to the V2_4 enum value. This assumes that Elasticsearch has backwards compatibility between all minor versions in the X.Y series, i.e. "2.4.0" and "2.4.6" should both be equally compatible. If this turns out to not be the case, further support can be added later. ElasticVersionManager is renamed to ElasticIndexVersionManager to avoid confusion with the ElasticVersion enum. Change-Id: I4dfe443f0e5d26652433334cef6be8c2727e9317
This commit is contained in:
@@ -61,6 +61,7 @@ junit_tests(
|
||||
"//gerrit-server:query_tests_code",
|
||||
"//gerrit-server:server",
|
||||
"//gerrit-server:testutil",
|
||||
"//lib:truth",
|
||||
"//lib/guice",
|
||||
"//lib/httpcomponents:httpcore",
|
||||
"//lib/jgit/org.eclipse.jgit:jgit",
|
||||
|
@@ -59,6 +59,6 @@ public class ElasticIndexModule extends AbstractIndexModule {
|
||||
|
||||
@Override
|
||||
protected Class<? extends AbstractVersionManager> getVersionManager() {
|
||||
return ElasticVersionManager.class;
|
||||
return ElasticIndexVersionManager.class;
|
||||
}
|
||||
}
|
||||
|
@@ -31,14 +31,15 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Singleton
|
||||
public class ElasticVersionManager extends AbstractVersionManager implements LifecycleListener {
|
||||
private static final Logger log = LoggerFactory.getLogger(ElasticVersionManager.class);
|
||||
public class ElasticIndexVersionManager extends AbstractVersionManager
|
||||
implements LifecycleListener {
|
||||
private static final Logger log = LoggerFactory.getLogger(ElasticIndexVersionManager.class);
|
||||
|
||||
private final String prefix;
|
||||
private final ElasticIndexVersionDiscovery versionDiscovery;
|
||||
|
||||
@Inject
|
||||
ElasticVersionManager(
|
||||
ElasticIndexVersionManager(
|
||||
ElasticConfiguration cfg,
|
||||
SitePaths sitePaths,
|
||||
Collection<IndexDefinition<?, ?, ?>> defs,
|
@@ -67,8 +67,8 @@ class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListen
|
||||
synchronized (this) {
|
||||
if (client == null) {
|
||||
client = build();
|
||||
String version = getVersion();
|
||||
log.info("Connected to Elasticsearch version {}", version);
|
||||
ElasticVersion version = getVersion();
|
||||
log.info("Elasticsearch integration version {}", version);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,20 +102,23 @@ class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListen
|
||||
}
|
||||
}
|
||||
|
||||
private String getVersion() throws ElasticException {
|
||||
private ElasticVersion getVersion() throws ElasticException {
|
||||
try {
|
||||
Response response = client.performRequest("GET", "");
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
|
||||
throw new FailedToGetVersion(statusLine);
|
||||
}
|
||||
return new JsonParser()
|
||||
.parse(AbstractElasticIndex.getContent(response))
|
||||
.getAsJsonObject()
|
||||
.get("version")
|
||||
.getAsJsonObject()
|
||||
.get("number")
|
||||
.getAsString();
|
||||
String version =
|
||||
new JsonParser()
|
||||
.parse(AbstractElasticIndex.getContent(response))
|
||||
.getAsJsonObject()
|
||||
.get("version")
|
||||
.getAsJsonObject()
|
||||
.get("number")
|
||||
.getAsString();
|
||||
log.info("Connected to Elasticsearch version {}", version);
|
||||
return ElasticVersion.forVersion(version);
|
||||
} catch (IOException e) {
|
||||
throw new FailedToGetVersion(e);
|
||||
}
|
||||
|
@@ -0,0 +1,60 @@
|
||||
// Copyright (C) 2018 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.elasticsearch;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public enum ElasticVersion {
|
||||
V2_4("2.4.*"),
|
||||
V5_6("5.6.*"),
|
||||
V6_2("6.2.*");
|
||||
|
||||
private final String version;
|
||||
private final Pattern pattern;
|
||||
|
||||
private ElasticVersion(String version) {
|
||||
this.version = version;
|
||||
this.pattern = Pattern.compile(version);
|
||||
}
|
||||
|
||||
public static class InvalidVersion extends ElasticException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
InvalidVersion(String version) {
|
||||
super(
|
||||
String.format(
|
||||
"Invalid version: [%s]. Supported versions: %s", version, supportedVersions()));
|
||||
}
|
||||
}
|
||||
|
||||
public static ElasticVersion forVersion(String version) throws InvalidVersion {
|
||||
for (ElasticVersion value : ElasticVersion.values()) {
|
||||
if (value.pattern.matcher(version).matches()) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
throw new InvalidVersion(version);
|
||||
}
|
||||
|
||||
public static String supportedVersions() {
|
||||
return Joiner.on(", ").join(ElasticVersion.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return version;
|
||||
}
|
||||
}
|
@@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.elasticsearch.testing;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gerrit.elasticsearch.ElasticVersion;
|
||||
import java.util.Set;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.junit.internal.AssumptionViolatedException;
|
||||
@@ -24,13 +25,7 @@ import org.testcontainers.containers.GenericContainer;
|
||||
public class ElasticContainer<SELF extends ElasticContainer<SELF>> extends GenericContainer<SELF> {
|
||||
private static final int ELASTICSEARCH_DEFAULT_PORT = 9200;
|
||||
|
||||
public enum Version {
|
||||
V2,
|
||||
V5,
|
||||
V6
|
||||
}
|
||||
|
||||
public static ElasticContainer<?> createAndStart(Version version) {
|
||||
public static ElasticContainer<?> createAndStart(ElasticVersion version) {
|
||||
// Assumption violation is not natively supported by Testcontainers.
|
||||
// See https://github.com/testcontainers/testcontainers-java/issues/343
|
||||
try {
|
||||
@@ -43,22 +38,22 @@ public class ElasticContainer<SELF extends ElasticContainer<SELF>> extends Gener
|
||||
}
|
||||
|
||||
public static ElasticContainer<?> createAndStart() {
|
||||
return createAndStart(Version.V2);
|
||||
return createAndStart(ElasticVersion.V2_4);
|
||||
}
|
||||
|
||||
private static String getImageName(Version version) {
|
||||
private static String getImageName(ElasticVersion version) {
|
||||
switch (version) {
|
||||
case V2:
|
||||
case V2_4:
|
||||
return "elasticsearch:2.4.6-alpine";
|
||||
case V5:
|
||||
case V5_6:
|
||||
return "elasticsearch:5.6.9-alpine";
|
||||
case V6:
|
||||
case V6_2:
|
||||
return "docker.elastic.co/elasticsearch/elasticsearch:6.2.4";
|
||||
}
|
||||
throw new IllegalStateException("Unsupported version: " + version.name());
|
||||
throw new IllegalStateException("No tests for version: " + version.name());
|
||||
}
|
||||
|
||||
private ElasticContainer(Version version) {
|
||||
private ElasticContainer(ElasticVersion version) {
|
||||
super(getImageName(version));
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,45 @@
|
||||
// Copyright (C) 2018 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.elasticsearch;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
public class ElasticVersionTest {
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void supportedVersion() throws Exception {
|
||||
assertThat(ElasticVersion.forVersion("2.4.0")).isEqualTo(ElasticVersion.V2_4);
|
||||
assertThat(ElasticVersion.forVersion("2.4.6")).isEqualTo(ElasticVersion.V2_4);
|
||||
|
||||
assertThat(ElasticVersion.forVersion("5.6.0")).isEqualTo(ElasticVersion.V5_6);
|
||||
assertThat(ElasticVersion.forVersion("5.6.9")).isEqualTo(ElasticVersion.V5_6);
|
||||
|
||||
assertThat(ElasticVersion.forVersion("6.2.0")).isEqualTo(ElasticVersion.V6_2);
|
||||
assertThat(ElasticVersion.forVersion("6.2.4")).isEqualTo(ElasticVersion.V6_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsupportedVersion() throws Exception {
|
||||
exception.expect(ElasticVersion.InvalidVersion.class);
|
||||
exception.expectMessage(
|
||||
"Invalid version: [4.0.0]. Supported versions: " + ElasticVersion.supportedVersions());
|
||||
ElasticVersion.forVersion("4.0.0");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user