From e0a678f0c439ab30aaa8508a932d4888557a04a6 Mon Sep 17 00:00:00 2001
From: Alpar Torok <torokalpar@gmail.com>
Date: Thu, 29 Nov 2018 08:41:39 +0200
Subject: [PATCH] Remove version.qualified from MainResponse (#35412)

The fully qualified version will be returned as `version.number`
---
 .../elasticsearch/client/PingAndInfoIT.java   |  2 +-
 docs/plugins/discovery-azure-classic.asciidoc |  3 +--
 .../setup/install/check-running.asciidoc      |  3 +--
 .../reindex/remote/RemoteResponseParsers.java |  6 ++++-
 .../RemoteScrollableHitSourceTests.java       |  8 +++----
 .../test/resources/packaging/utils/utils.bash | 10 +++++++--
 .../main/java/org/elasticsearch/Build.java    |  2 +-
 .../main/java/org/elasticsearch/Version.java  |  3 +++
 .../action/main/MainResponse.java             | 22 +++++++++++++++----
 .../java/org/elasticsearch/BuildTests.java    |  2 +-
 .../action/main/MainResponseTests.java        |  7 +++---
 .../exporter/http/VersionHttpResource.java    |  6 ++++-
 12 files changed, 51 insertions(+), 23 deletions(-)

diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java
index ba27bdd3a5b..af230a8c13b 100644
--- a/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java
+++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java
@@ -52,7 +52,7 @@ public class PingAndInfoIT extends ESRestHighLevelClientTestCase {
         assertEquals(versionMap.get("build_hash"), info.getBuild().shortHash());
         assertEquals(versionMap.get("build_date"), info.getBuild().date());
         assertEquals(versionMap.get("build_snapshot"), info.getBuild().isSnapshot());
-        assertEquals(versionMap.get("number"), info.getVersion().toString());
+        assertTrue(versionMap.get("number").toString().startsWith(info.getVersion().toString()));
         assertEquals(versionMap.get("lucene_version"), info.getVersion().luceneVersion.toString());
     }
 
diff --git a/docs/plugins/discovery-azure-classic.asciidoc b/docs/plugins/discovery-azure-classic.asciidoc
index 1f973567284..30a8ee9c43d 100644
--- a/docs/plugins/discovery-azure-classic.asciidoc
+++ b/docs/plugins/discovery-azure-classic.asciidoc
@@ -371,13 +371,12 @@ This command should give you a JSON result:
   "cluster_name" : "elasticsearch",
   "cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA",
   "version" : {
-    "number" : "{version}",
+    "number" : "{version_qualified}",
     "build_flavor" : "{build_flavor}",
     "build_type" : "zip",
     "build_hash" : "f27399d",
     "build_date" : "2016-03-30T09:51:41.449Z",
     "build_snapshot" : false,
-    "qualified" : "{version_qualified}",
     "lucene_version" : "{lucene_version}",
     "minimum_wire_compatibility_version" : "1.2.3",
     "minimum_index_compatibility_version" : "1.2.3"
diff --git a/docs/reference/setup/install/check-running.asciidoc b/docs/reference/setup/install/check-running.asciidoc
index 663442a456c..150252ecc5d 100644
--- a/docs/reference/setup/install/check-running.asciidoc
+++ b/docs/reference/setup/install/check-running.asciidoc
@@ -18,13 +18,12 @@ which should give you a response something like this:
   "cluster_name" : "elasticsearch",
   "cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA",
   "version" : {
-    "number" : "{version}",
+    "number" : "{version_qualified}",
     "build_flavor" : "{build_flavor}",
     "build_type" : "zip",
     "build_hash" : "f27399d",
     "build_date" : "2016-03-30T09:51:41.449Z",
     "build_snapshot" : false,
-    "qualified" : "{version_qualified}",
     "lucene_version" : "{lucene_version}",
     "minimum_wire_compatibility_version" : "1.2.3",
     "minimum_index_compatibility_version" : "1.2.3"
diff --git a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/remote/RemoteResponseParsers.java b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/remote/RemoteResponseParsers.java
index 4834c6f9ce0..30f598f8f70 100644
--- a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/remote/RemoteResponseParsers.java
+++ b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/remote/RemoteResponseParsers.java
@@ -268,7 +268,11 @@ final class RemoteResponseParsers {
             "/", true, a -> (Version) a[0]);
     static {
         ConstructingObjectParser<Version, XContentType> versionParser = new ConstructingObjectParser<>(
-                "version", true, a -> Version.fromString((String) a[0]));
+                "version", true, a -> Version.fromString(
+                    ((String) a[0])
+                        .replace("-SNAPSHOT", "")
+                        .replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "")
+        ));
         versionParser.declareString(constructorArg(), new ParseField("number"));
         MAIN_ACTION_PARSER.declareObject(constructorArg(), versionParser, new ParseField("version"));
     }
diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/remote/RemoteScrollableHitSourceTests.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/remote/RemoteScrollableHitSourceTests.java
index d3d3cefea45..559d211b7ed 100644
--- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/remote/RemoteScrollableHitSourceTests.java
+++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/remote/RemoteScrollableHitSourceTests.java
@@ -150,15 +150,15 @@ public class RemoteScrollableHitSourceTests extends ESTestCase {
         assertTrue(called.get());
         called.set(false);
         sourceWithMockedRemoteCall(false, ContentType.APPLICATION_JSON, "main/5_0_0_alpha_3.json").lookupRemoteVersion(v -> {
-            // V_5_0_0_alpha3
-            assertEquals(Version.fromId(5000003), v);
+            // assert for V_5_0_0 (no qualifier) since we no longer consider qualifier in Version since 7
+            assertEquals(Version.fromId(5000099), v);
             called.set(true);
         });
         assertTrue(called.get());
         called.set(false);
         sourceWithMockedRemoteCall(false, ContentType.APPLICATION_JSON, "main/with_unknown_fields.json").lookupRemoteVersion(v -> {
-            // V_5_0_0_alpha3
-            assertEquals(Version.fromId(5000003), v);
+            // V_5_0_0 since we no longer consider qualifier in Version
+            assertEquals(Version.fromId(5000099), v);
             called.set(true);
         });
         assertTrue(called.get());
diff --git a/qa/vagrant/src/test/resources/packaging/utils/utils.bash b/qa/vagrant/src/test/resources/packaging/utils/utils.bash
index e54e913aea1..18363a5ac62 100644
--- a/qa/vagrant/src/test/resources/packaging/utils/utils.bash
+++ b/qa/vagrant/src/test/resources/packaging/utils/utils.bash
@@ -516,13 +516,19 @@ wait_for_elasticsearch_status() {
 # $1 - expected version
 check_elasticsearch_version() {
     local version=$1
-    local versionToCheck=$(echo $version | sed -e 's/-SNAPSHOT//' | sed -e 's/-\(alpha\|beta\|rc\)[0-9]//')
+    local versionToCheck
+    local major=$(echo ${version} | cut -d. -f1 )
+    if [ $major -ge 7 ] ; then
+        versionToCheck=$version
+    else
+        versionToCheck=$(echo ${version} | sed -e 's/-SNAPSHOT//')
+    fi
 
     run curl -s localhost:9200
     [ "$status" -eq 0 ]
 
     echo $output | grep \"number\"\ :\ \"$versionToCheck\" || {
-        echo "Installed an unexpected version:"
+        echo "Expected $versionToCheck but installed an unexpected version:"
         curl -s localhost:9200
         false
     }
diff --git a/server/src/main/java/org/elasticsearch/Build.java b/server/src/main/java/org/elasticsearch/Build.java
index 706d5095dee..9e659bb4145 100644
--- a/server/src/main/java/org/elasticsearch/Build.java
+++ b/server/src/main/java/org/elasticsearch/Build.java
@@ -139,7 +139,7 @@ public class Build {
             // not running from the official elasticsearch jar file (unit tests, IDE, uber client jar, shadiness)
             shortHash = "Unknown";
             date = "Unknown";
-            version = "Unknown";
+            version = Version.CURRENT.toString();
             final String buildSnapshot = System.getProperty("build.snapshot");
             if (buildSnapshot != null) {
                 try {
diff --git a/server/src/main/java/org/elasticsearch/Version.java b/server/src/main/java/org/elasticsearch/Version.java
index 2c58bbca708..e08a8d4b327 100644
--- a/server/src/main/java/org/elasticsearch/Version.java
+++ b/server/src/main/java/org/elasticsearch/Version.java
@@ -253,6 +253,9 @@ public class Version implements Comparable<Version>, ToXContentFragment {
             if (rawMajor >= 5 && snapshot) { // we don't support snapshot as part of the version here anymore
                 throw new IllegalArgumentException("illegal version format - snapshots are only supported until version 2.x");
             }
+            if (rawMajor >=7 && parts.length == 4) { // we don't support qualifier as part of the version anymore
+                throw new IllegalArgumentException("illegal version format - qualifiers are only supported until version 6.x");
+            }
             final int betaOffset = rawMajor < 5 ? 0 : 25;
             //we reverse the version id calculation based on some assumption as we can't reliably reverse the modulo
             final int major = rawMajor * 1000000;
diff --git a/server/src/main/java/org/elasticsearch/action/main/MainResponse.java b/server/src/main/java/org/elasticsearch/action/main/MainResponse.java
index 4b06a2d0613..38d78fdc0c1 100644
--- a/server/src/main/java/org/elasticsearch/action/main/MainResponse.java
+++ b/server/src/main/java/org/elasticsearch/action/main/MainResponse.java
@@ -107,13 +107,12 @@ public class MainResponse extends ActionResponse implements ToXContentObject {
         builder.field("cluster_name", clusterName.value());
         builder.field("cluster_uuid", clusterUuid);
         builder.startObject("version")
-            .field("number", version.toString())
+            .field("number", build.getQualifiedVersion())
             .field("build_flavor", build.flavor().displayName())
             .field("build_type", build.type().displayName())
             .field("build_hash", build.shortHash())
             .field("build_date", build.date())
             .field("build_snapshot", build.isSnapshot())
-            .field("qualified", build.getQualifiedVersion())
             .field("lucene_version", version.luceneVersion.toString())
             .field("minimum_wire_compatibility_version", version.minimumCompatibilityVersion().toString())
             .field("minimum_index_compatibility_version", version.minimumIndexCompatibilityVersion().toString())
@@ -141,9 +140,13 @@ public class MainResponse extends ActionResponse implements ToXContentObject {
                             (String) value.get("build_hash"),
                             (String) value.get("build_date"),
                             (boolean) value.get("build_snapshot"),
-                            (String) value.get("qualified")
+                            (String) value.get("number")
                     );
-            response.version = Version.fromString((String) value.get("number"));
+            response.version = Version.fromString(
+                ((String) value.get("number"))
+                    .replace("-SNAPSHOT", "")
+                    .replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "")
+            );
         }, (parser, context) -> parser.map(), new ParseField("version"));
     }
 
@@ -171,4 +174,15 @@ public class MainResponse extends ActionResponse implements ToXContentObject {
     public int hashCode() {
         return Objects.hash(nodeName, version, clusterUuid, build, clusterName);
     }
+
+    @Override
+    public String toString() {
+        return "MainResponse{" +
+            "nodeName='" + nodeName + '\'' +
+            ", version=" + version +
+            ", clusterName=" + clusterName +
+            ", clusterUuid='" + clusterUuid + '\'' +
+            ", build=" + build +
+            '}';
+    }
 }
diff --git a/server/src/test/java/org/elasticsearch/BuildTests.java b/server/src/test/java/org/elasticsearch/BuildTests.java
index 04119bbc0f0..1f99a1f4542 100644
--- a/server/src/test/java/org/elasticsearch/BuildTests.java
+++ b/server/src/test/java/org/elasticsearch/BuildTests.java
@@ -111,7 +111,7 @@ public class BuildTests extends ESTestCase {
         assertNotEquals(build, differentSnapshot);
 
         Build differentVersion = new Build(
-            build.flavor(), build.type(), build.shortHash(), build.date(), build.isSnapshot(), "7.0.0"
+            build.flavor(), build.type(), build.shortHash(), build.date(), build.isSnapshot(), "1.2.3"
         );
         assertNotEquals(build, differentVersion);
     }
diff --git a/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java b/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java
index 319c2920d7c..1dff130fb98 100644
--- a/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java
+++ b/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java
@@ -41,11 +41,11 @@ public class MainResponseTests extends AbstractStreamableXContentTestCase<MainRe
         ClusterName clusterName = new ClusterName(randomAlphaOfLength(10));
         String nodeName = randomAlphaOfLength(10);
         final String date = new Date(randomNonNegativeLong()).toString();
+        Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_0_1, Version.CURRENT);
         Build build = new Build(
             Build.Flavor.UNKNOWN, Build.Type.UNKNOWN, randomAlphaOfLength(8), date, randomBoolean(),
-            randomAlphaOfLength(12)
+            version.toString()
         );
-        Version version = VersionUtils.randomVersion(random());
         return new MainResponse(nodeName, version, clusterName, clusterUuid , build);
     }
 
@@ -75,13 +75,12 @@ public class MainResponseTests extends AbstractStreamableXContentTestCase<MainRe
                 + "\"cluster_name\":\"clusterName\","
                 + "\"cluster_uuid\":\"" + clusterUUID + "\","
                 + "\"version\":{"
-                    + "\"number\":\"" + version.toString() + "\","
+                    + "\"number\":\"" + build.getQualifiedVersion() + "\","
                     + "\"build_flavor\":\"" + current.flavor().displayName() + "\","
                     + "\"build_type\":\"" + current.type().displayName() + "\","
                     + "\"build_hash\":\"" + current.shortHash() + "\","
                     + "\"build_date\":\"" + current.date() + "\","
                     + "\"build_snapshot\":" + current.isSnapshot() + ","
-                    + "\"qualified\":\"" + current.getQualifiedVersion() + "\","
                     + "\"lucene_version\":\"" + version.luceneVersion.toString() + "\","
                     + "\"minimum_wire_compatibility_version\":\"" + version.minimumCompatibilityVersion().toString() + "\","
                     + "\"minimum_index_compatibility_version\":\"" + version.minimumIndexCompatibilityVersion().toString() + "\"},"
diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java
index 52ac37ef9b7..ef89f8d62e6 100644
--- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java
+++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java
@@ -96,7 +96,11 @@ public class VersionHttpResource extends HttpResource {
         // the response should be filtered to just '{"version":{"number":"xyz"}}', so this is cheap and guaranteed
         @SuppressWarnings("unchecked")
         final String versionNumber = (String) ((Map<String, Object>) map.get("version")).get("number");
-        final Version version = Version.fromString(versionNumber);
+        final Version version = Version.fromString(
+            versionNumber
+                .replace("-SNAPSHOT", "")
+                .replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "")
+        );
 
         if (version.onOrAfter(minimumVersion)) {
             logger.debug("version [{}] >= [{}] and supported for [{}]", version, minimumVersion, resourceOwnerName);