Skip to content

Commit

Permalink
Rewrite date_nanos rolling upgrade test (#70855)
Browse files Browse the repository at this point in the history
This change rewrites the rolling upgrade yml test that was added for
date_nanos in Java. This is needed to allow extra logic when backporting to
7.x. date_nanos are not available in 6.x so we need to skip the upgraded test
based on the version of the old cluster.

Relates #70463
  • Loading branch information
jimczi authored Mar 26, 2021
1 parent 9a8c6fb commit 2468c41
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.support.XContentMapValues;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.equalTo;

/**
* Basic test that indexed documents survive the rolling restart. See
Expand Down Expand Up @@ -137,18 +143,7 @@ public void testAutoIdWithOpTypeCreate() throws IOException {
waitForGreen.addParameter("wait_for_nodes", "3");
client().performRequest(waitForGreen);

Version minNodeVersion = null;
Map<?, ?> response = entityAsMap(client().performRequest(new Request("GET", "_nodes")));
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
for (Map.Entry<?, ?> node : nodes.entrySet()) {
Map<?, ?> nodeInfo = (Map<?, ?>) node.getValue();
Version nodeVersion = Version.fromString(nodeInfo.get("version").toString());
if (minNodeVersion == null) {
minNodeVersion = nodeVersion;
} else if (nodeVersion.before(minNodeVersion)) {
minNodeVersion = nodeVersion;
}
}
Version minNodeVersion = minNodeVersion();

if (minNodeVersion.before(Version.V_7_5_0)) {
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(bulk));
Expand All @@ -171,6 +166,62 @@ public void testAutoIdWithOpTypeCreate() throws IOException {
}
}

public void testDateNanosFormatUpgrade() throws IOException {
final String indexName = "test_date_nanos";
switch (CLUSTER_TYPE) {
case OLD:
Request createIndex = new Request("PUT", "/" + indexName);
XContentBuilder mappings = XContentBuilder.builder(XContentType.JSON.xContent())
.startObject()
.startObject("mappings")
.startObject("properties")
.startObject("date")
.field("type", "date")
.endObject()
.startObject("date_nanos")
.field("type", "date_nanos")
.endObject()
.endObject()
.endObject()
.endObject();
createIndex.setJsonEntity(Strings.toString(mappings));
client().performRequest(createIndex);

Request index = new Request("POST", "/" + indexName + "/_doc/");
XContentBuilder doc = XContentBuilder.builder(XContentType.JSON.xContent())
.startObject()
.field("date", "2015-01-01T12:10:30.123456789Z")
.field("date_nanos", "2015-01-01T12:10:30.123456789Z")
.endObject();
index.addParameter("refresh", "true");
index.setJsonEntity(Strings.toString(doc));
client().performRequest(index);
break;

case UPGRADED:
Request search = new Request("POST", "/" + indexName + "/_search");
XContentBuilder query = XContentBuilder.builder(XContentType.JSON.xContent())
.startObject()
.array("fields", new String[] { "date", "date_nanos" })
.endObject();
search.setJsonEntity(Strings.toString(query));
Map<String, Object> response = entityAsMap(client().performRequest(search));

Map<?, ?> bestHit = (Map<?, ?>) ((List<?>) (XContentMapValues.extractValue("hits.hits", response))).get(0);
List<?> date = (List<?>) XContentMapValues.extractValue("fields.date", bestHit);
assertThat(date.size(), equalTo(1));
assertThat(date.get(0), equalTo("2015-01-01T12:10:30.123Z"));

List<?> dateNanos = (List<?>) XContentMapValues.extractValue("fields.date_nanos", bestHit);
assertThat(dateNanos.size(), equalTo(1));
assertThat(dateNanos.get(0), equalTo("2015-01-01T12:10:30.123456789Z"));
break;

default:
break;
}
}

private void bulk(String index, String valueSuffix, int count) throws IOException {
StringBuilder b = new StringBuilder();
for (int i = 0; i < count; i++) {
Expand All @@ -191,4 +242,20 @@ private void assertCount(String index, int count) throws IOException {
assertEquals("{\"hits\":{\"total\":" + count + "}}",
EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8));
}

private Version minNodeVersion() throws IOException {
Map<?, ?> response = entityAsMap(client().performRequest(new Request("GET", "_nodes")));
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
Version minNodeVersion = null;
for (Map.Entry<?, ?> node : nodes.entrySet()) {
Map<?, ?> nodeInfo = (Map<?, ?>) node.getValue();
Version nodeVersion = Version.fromString(nodeInfo.get("version").toString());
if (minNodeVersion == null) {
minNodeVersion = nodeVersion;
} else if (nodeVersion.before(minNodeVersion)) {
minNodeVersion = nodeVersion;
}
}
return minNodeVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,3 @@
- do:
indices.get_index_template:
name: my-it

---
"Verify date_nanos default format":
- do:
indices.create:
index: index_nanos
body:
mappings:
properties:
date:
type: date
date_nanos:
type: date_nanos

- do:
index:
index: index_nanos
id: 0
body:
date: "2015-01-01T12:10:30.123456789Z"
date_nanos: "2015-01-01T12:10:30.123456789Z"
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,3 @@
- match: {component_templates.0.component_template.template.settings: {index: {number_of_shards: '1', number_of_replicas: '0'}}}
- is_true: component_templates.0.component_template.template.mappings
- match: {component_templates.0.component_template.template.aliases: {aliasname: {}}}

---
"Verify date_nanos default format":
- do:
search:
index: index_nanos
body:
fields: ["date_nanos", "date"]

- match: { hits.total.value: 1 }
- match: { hits.hits.0._id: "0" }
- match: { hits.hits.0.fields.date: [ "2015-01-01T12:10:30.123Z" ] }
- match: { hits.hits.0.fields.date_nanos: [ "2015-01-01T12:10:30.123456789Z" ] }

0 comments on commit 2468c41

Please sign in to comment.