Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that BWC tests run successfully, even with types deprecation messages. #36511

Merged
merged 3 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import org.apache.http.util.EntityUtils;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.test.rest.TypesRemovalWarningsHandler;
import org.elasticsearch.rest.action.document.RestGetAction;
import org.elasticsearch.rest.action.search.RestExplainAction;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.CheckedFunction;
Expand Down Expand Up @@ -82,7 +82,6 @@ public void setIndex() throws IOException {
index = getTestName().toLowerCase(Locale.ROOT);
}


public void testSearch() throws Exception {
int count;
if (isRunningAgainstOldCluster()) {
Expand All @@ -96,7 +95,7 @@ public void testSearch() throws Exception {
}
{
mappingsAndSettings.startObject("mappings");
mappingsAndSettings.startObject("_doc");
mappingsAndSettings.startObject("doc");
mappingsAndSettings.startObject("properties");
{
mappingsAndSettings.startObject("string");
Expand Down Expand Up @@ -162,7 +161,7 @@ public void testNewReplicasWork() throws Exception {
}
{
mappingsAndSettings.startObject("mappings");
mappingsAndSettings.startObject("_doc");
mappingsAndSettings.startObject("doc");
mappingsAndSettings.startObject("properties");
{
mappingsAndSettings.startObject("field");
Expand Down Expand Up @@ -233,7 +232,7 @@ public void testAliasWithBadName() throws Exception {
}
{
mappingsAndSettings.startObject("mappings");
mappingsAndSettings.startObject("_doc");
mappingsAndSettings.startObject("doc");
mappingsAndSettings.startObject("properties");
{
mappingsAndSettings.startObject("key");
Expand Down Expand Up @@ -336,7 +335,7 @@ public void testShrink() throws IOException {
mappingsAndSettings.startObject();
{
mappingsAndSettings.startObject("mappings");
mappingsAndSettings.startObject("_doc");
mappingsAndSettings.startObject("doc");
mappingsAndSettings.startObject("properties");
{
mappingsAndSettings.startObject("field");
Expand Down Expand Up @@ -404,7 +403,7 @@ public void testShrinkAfterUpgrade() throws IOException {
mappingsAndSettings.startObject();
{
mappingsAndSettings.startObject("mappings");
mappingsAndSettings.startObject("_doc");
mappingsAndSettings.startObject("doc");
mappingsAndSettings.startObject("properties");
{
mappingsAndSettings.startObject("field");
Expand Down Expand Up @@ -492,7 +491,7 @@ public void testRollover() throws IOException {
bulk.append("{\"index\":{}}\n");
bulk.append("{\"test\":\"test\"}\n");
}
Request bulkRequest = new Request("POST", "/" + index + "_write/_doc/_bulk");
Request bulkRequest = new Request("POST", "/" + index + "_write/doc/_bulk");
bulkRequest.setJsonEntity(bulk.toString());
bulkRequest.addParameter("refresh", "");
assertThat(EntityUtils.toString(client().performRequest(bulkRequest).getEntity()), containsString("\"errors\":false"));
Expand Down Expand Up @@ -571,19 +570,15 @@ void assertAllSearchWorks(int count) throws IOException {
String type = (String) bestHit.get("_type");
String id = (String) bestHit.get("_id");

Request explanationRequest = new Request("GET", "/" + index + "/" + type + "/" + id + "/_explain");
explanationRequest.setJsonEntity("{ \"query\": { \"match_all\" : {} }}");

RequestOptions.Builder explanationOptions = RequestOptions.DEFAULT.toBuilder();
explanationOptions.setWarningsHandler(TypesRemovalWarningsHandler.INSTANCE);
explanationRequest.setOptions(explanationOptions);

String explanation = toStr(client().performRequest(explanationRequest));
Request explainRequest = new Request("GET", "/" + index + "/" + type + "/" + id + "/_explain");
explainRequest.setJsonEntity("{ \"query\": { \"match_all\" : {} }}");
explainRequest.setOptions(expectWarnings(RestExplainAction.TYPES_DEPRECATION_MESSAGE));
String explanation = toStr(client().performRequest(explainRequest));
assertFalse("Could not find payload boost in explanation\n" + explanation, explanation.contains("payloadBoost"));

// Make sure the query can run on the whole index
Request searchRequest = new Request("GET", "/" + index + "/_search");
searchRequest.setEntity(explanationRequest.getEntity());
searchRequest.setEntity(explainRequest.getEntity());
searchRequest.addParameter("explain", "true");
Map<?, ?> matchAllResponse = entityAsMap(client().performRequest(searchRequest));
assertNoFailures(matchAllResponse);
Expand Down Expand Up @@ -628,11 +623,13 @@ void assertRealtimeGetWorks() throws IOException {
Map<?, ?> hit = (Map<?, ?>) ((List<?>)(XContentMapValues.extractValue("hits.hits", searchResponse))).get(0);
String docId = (String) hit.get("_id");

Request updateRequest = new Request("POST", "/" + index + "/_doc/" + docId + "/_update");
Request updateRequest = new Request("POST", "/" + index + "/doc/" + docId + "/_update");
updateRequest.setJsonEntity("{ \"doc\" : { \"foo\": \"bar\"}}");
client().performRequest(updateRequest);

Map<String, Object> getRsp = entityAsMap(client().performRequest(new Request("GET", "/" + index + "/_doc/" + docId)));
Request getRequest = new Request("GET", "/" + index + "/doc/" + docId);
getRequest.setOptions(expectWarnings(RestGetAction.TYPES_DEPRECATION_MESSAGE));
Map<String, Object> getRsp = entityAsMap(client().performRequest(getRequest));
Map<?, ?> source = (Map<?, ?>) getRsp.get("_source");
assertTrue("doc does not contain 'foo' key: " + source, source.containsKey("foo"));

Expand Down Expand Up @@ -685,7 +682,7 @@ int extractTotalHits(Map<?, ?> response) {
* Tests that a single document survives. Super basic smoke test.
*/
public void testSingleDoc() throws IOException {
String docLocation = "/" + index + "/_doc/1";
String docLocation = "/" + index + "/doc/1";
String doc = "{\"test\": \"test\"}";

if (isRunningAgainstOldCluster()) {
Expand All @@ -694,7 +691,10 @@ public void testSingleDoc() throws IOException {
client().performRequest(createDoc);
}

assertThat(toStr(client().performRequest(new Request("GET", docLocation))), containsString(doc));

Request request = new Request("GET", docLocation);
request.setOptions(expectWarnings(RestGetAction.TYPES_DEPRECATION_MESSAGE));
assertThat(toStr(client().performRequest(request)), containsString(doc));
}

/**
Expand Down Expand Up @@ -872,7 +872,7 @@ public void testSnapshotRestore() throws IOException {
}
templateBuilder.endObject();
templateBuilder.startObject("mappings"); {
templateBuilder.startObject("_doc"); {
templateBuilder.startObject("doc"); {
templateBuilder.startObject("_source"); {
templateBuilder.field("enabled", true);
}
Expand Down Expand Up @@ -982,7 +982,7 @@ public void testSoftDeletes() throws Exception {
int numDocs = between(10, 100);
for (int i = 0; i < numDocs; i++) {
String doc = Strings.toString(JsonXContent.contentBuilder().startObject().field("field", "v1").endObject());
Request request = new Request("POST", "/" + index + "/_doc/" + i);
Request request = new Request("POST", "/" + index + "/doc/" + i);
request.setJsonEntity(doc);
client().performRequest(request);
if (rarely()) {
Expand All @@ -995,11 +995,11 @@ public void testSoftDeletes() throws Exception {
for (int i = 0; i < numDocs; i++) {
if (randomBoolean()) {
String doc = Strings.toString(JsonXContent.contentBuilder().startObject().field("field", "v2").endObject());
Request request = new Request("POST", "/" + index + "/_doc/" + i);
Request request = new Request("POST", "/" + index + "/doc/" + i);
request.setJsonEntity(doc);
client().performRequest(request);
} else if (randomBoolean()) {
client().performRequest(new Request("DELETE", "/" + index + "/_doc/" + i));
client().performRequest(new Request("DELETE", "/" + index + "/doc/" + i));
liveDocs--;
}
}
Expand Down Expand Up @@ -1065,7 +1065,7 @@ private void checkSnapshot(String snapshotName, int count, Version tookOnVersion
bulk.append("{\"index\":{\"_id\":\"").append(count + i).append("\"}}\n");
bulk.append("{\"test\":\"test\"}\n");
}
Request writeToRestoredRequest = new Request("POST", "/restored_" + index + "/_doc/_bulk");
Request writeToRestoredRequest = new Request("POST", "/restored_" + index + "/doc/_bulk");
writeToRestoredRequest.addParameter("refresh", "true");
writeToRestoredRequest.setJsonEntity(bulk.toString());
assertThat(EntityUtils.toString(client().performRequest(writeToRestoredRequest).getEntity()), containsString("\"errors\":false"));
Expand Down Expand Up @@ -1097,7 +1097,7 @@ private void checkSnapshot(String snapshotName, int count, Version tookOnVersion
expectedTemplate.put("index_patterns", singletonList("evil_*"));
}
expectedTemplate.put("settings", singletonMap("index", singletonMap("number_of_shards", "1")));
expectedTemplate.put("mappings", singletonMap("_doc", singletonMap("_source", singletonMap("enabled", true))));
expectedTemplate.put("mappings", singletonMap("doc", singletonMap("_source", singletonMap("enabled", true))));
expectedTemplate.put("order", 0);
Map<String, Object> aliases = new HashMap<>();
aliases.put("alias1", emptyMap());
Expand All @@ -1118,7 +1118,7 @@ private void indexRandomDocuments(int count, boolean flushAllowed, boolean saveI
logger.info("Indexing {} random documents", count);
for (int i = 0; i < count; i++) {
logger.debug("Indexing document [{}]", i);
Request createDocument = new Request("POST", "/" + index + "/_doc/" + i);
Request createDocument = new Request("POST", "/" + index + "/doc/" + i);
createDocument.setJsonEntity(Strings.toString(docSupplier.apply(i)));
client().performRequest(createDocument);
if (rarely()) {
Expand All @@ -1143,15 +1143,16 @@ private void saveInfoDocument(String type, String value) throws IOException {
infoDoc.field("value", value);
infoDoc.endObject();
// Only create the first version so we know how many documents are created when the index is first created
Request request = new Request("PUT", "/info/_doc/" + index + "_" + type);
Request request = new Request("PUT", "/info/doc/" + index + "_" + type);
request.addParameter("op_type", "create");
request.setJsonEntity(Strings.toString(infoDoc));
client().performRequest(request);
}

private String loadInfoDocument(String type) throws IOException {
Request request = new Request("GET", "/info/_doc/" + index + "_" + type);
Request request = new Request("GET", "/info/doc/" + index + "_" + type);
request.addParameter("filter_path", "_source");
request.setOptions(expectWarnings(RestGetAction.TYPES_DEPRECATION_MESSAGE));
String doc = toStr(client().performRequest(request));
Matcher m = Pattern.compile("\"value\":\"(.+)\"").matcher(doc);
assertTrue(doc, m.find());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.seqno.SeqNoStats;
import org.elasticsearch.rest.action.document.RestGetAction;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;

Expand All @@ -45,7 +46,7 @@ public class IndexingIT extends ESRestTestCase {
private int indexDocs(String index, final int idStart, final int numDocs) throws IOException {
for (int i = 0; i < numDocs; i++) {
final int id = idStart + i;
Request request = new Request("PUT", index + "/_doc/" + id);
Request request = new Request("PUT", index + "/doc/" + id);
request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}");
assertOK(client().performRequest(request));
}
Expand Down Expand Up @@ -284,8 +285,10 @@ private void assertCount(final String index, final String preference, final int
}

private void assertVersion(final String index, final int docId, final String preference, final int expectedVersion) throws IOException {
Request request = new Request("GET", index + "/_doc/" + docId);
Request request = new Request("GET", index + "/doc/" + docId);
request.addParameter("preference", preference);
request.setOptions(expectWarnings(RestGetAction.TYPES_DEPRECATION_MESSAGE));

final Response response = client().performRequest(request);
assertOK(response);
final int actualVersion = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("_version").toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public class RestGetAction extends BaseRestHandler {
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
LogManager.getLogger(RestGetAction.class));
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in " +
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in " +
"document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.";

public RestGetAction(final Settings settings, final RestController controller) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class RestExplainAction extends BaseRestHandler {
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
LogManager.getLogger(RestExplainAction.class));
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] " +
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] " +
"Specifying a type in explain requests is deprecated.";

public RestExplainAction(Settings settings, RestController controller) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public void testTermsQuery() throws Exception {
assertEquals(new MatchNoDocsQuery(), query);
}


static DirectoryReader openReaderWithNewType(String type, IndexWriter writer) throws IOException {
Document doc = new Document();
StringField typeField = new StringField(TypeFieldMapper.NAME, type, Store.NO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,18 @@ public static RequestOptions expectVersionSpecificWarnings(Consumer<VersionSensi
expectationsSetter.accept(warningsHandler);
builder.setWarningsHandler(warningsHandler);
return builder.build();
}
}

/**
* Creates request options designed to be used when making a call that can return warnings, for example a
* deprecated request. The options will ensure that the given warnings are returned if all nodes are on
* {@link Version#CURRENT} and will allow (but not require) the warnings if any node is running an older version.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change the javadoc to talk about Version.CURRENT otherwise the comment won't age well when we move to 8 and beyond

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* @param warnings The expected warnings.
*/
public static RequestOptions expectWarnings(String... warnings) {
return expectVersionSpecificWarnings(consumer -> consumer.current(warnings));
}

/**
* Construct an HttpHost from the given host and port
Expand Down

This file was deleted.

Loading