Skip to content

Compatible version of typed Index And Get API #53228

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

Merged
merged 28 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a4f80ca
rest index action - 283 nofix
pgomulka Mar 5, 2020
c99b818
Index and Get and Infra
pgomulka Mar 6, 2020
c9c0e55
minor tests
pgomulka Mar 6, 2020
258542e
compile fixees
pgomulka Mar 6, 2020
548fbd9
Merge branch 'compat_rest_api' into compat/type-index-get
pgomulka Mar 9, 2020
0dbea8a
disable testing conventions
pgomulka Mar 9, 2020
4cf6bc1
assertions and todo for header fix
pgomulka Mar 10, 2020
3ac22b1
more tests and cleanup
pgomulka Mar 10, 2020
38721fe
Merge remote-tracking branch 'upstream/compat_rest_api' into compat/t…
jakelandis Mar 11, 2020
f2db19f
introduce a module to house the REST code
jakelandis Mar 11, 2020
b83b4ce
fix preCommit
jakelandis Mar 11, 2020
cf1d340
Merge pull request #18 from jakelandis/introduce_module
pgomulka Mar 11, 2020
5c4a02d
move restindex compatible handlers to rest-compatibility module. 228 …
pgomulka Mar 11, 2020
0ef7e18
moving test classes and compat related code to separate v7 module
pgomulka Mar 11, 2020
835ce56
test class rename and return 400 when compatible header not present
pgomulka Mar 13, 2020
f440231
clean up deprecation warnings and remove use of consumers
pgomulka Mar 13, 2020
84f1dde
v7 compatible actions warnings tests
pgomulka Mar 13, 2020
d106d1b
rename tests and enable them
pgomulka Mar 16, 2020
cf61bbd
rename isV7Compatible method
pgomulka Mar 16, 2020
f00f438
checkstyle
pgomulka Mar 16, 2020
ae1799f
Merge branch 'master' into compat/type-index-get
pgomulka Mar 16, 2020
a0fce89
Revert "Merge branch 'master' into compat/type-index-get"
pgomulka Mar 16, 2020
7e55744
Merge branch 'compat_rest_api' into compat/type-index-get
pgomulka Mar 16, 2020
00fe62d
Revert "Revert "Merge branch 'master' into compat/type-index-get""
pgomulka Mar 16, 2020
a6f0b9a
use locale with toLowerCase
pgomulka Mar 17, 2020
4eff534
spotless
pgomulka Mar 17, 2020
2d4161c
imports and disable integ tests as there are none
pgomulka Mar 17, 2020
6830f97
fix tests
pgomulka Mar 17, 2020
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ subprojects {
':distribution:tools:keystore-cli',
':distribution:tools:launchers',
':distribution:tools:plugin-cli',
':modules:rest-compatibility',
':qa:os',
':qa:wildfly',
':x-pack:plugin:autoscaling',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public interface HumanReadableTransformer {
*/
private boolean humanReadable = false;

private byte compatibleMajorVersion;

/**
* Constructs a new builder using the provided XContent and an OutputStream. Make sure
* to call {@link #close()} when the builder is done with.
Expand Down Expand Up @@ -998,6 +1000,16 @@ public XContentBuilder copyCurrentStructure(XContentParser parser) throws IOExce
return this;
}

public XContentBuilder setCompatibleMajorVersion(byte compatibleMajorVersion){
this.compatibleMajorVersion = compatibleMajorVersion;
return this;
}

public byte getCompatibleMajorVersion() {
return compatibleMajorVersion;
}


@Override
public void flush() throws IOException {
generator.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* The content type of {@link org.elasticsearch.common.xcontent.XContent}.
Expand Down Expand Up @@ -114,6 +116,10 @@ public XContent xContent() {
}
};

private static final Pattern COMPATIBLE_API_HEADER_PATTERN = Pattern.compile(
"application/(vnd.elasticsearch\\+)?([^;]+)(\\s*;\\s*compatible-with=(\\d+))?",
Pattern.CASE_INSENSITIVE);

/**
* Accepts either a format string, which is equivalent to {@link XContentType#shortName()} or a media type that optionally has
* parameters and attempts to match the value to an {@link XContentType}. The comparisons are done in lower case format and this method
Expand Down Expand Up @@ -142,7 +148,9 @@ public static XContentType fromMediaTypeOrFormat(String mediaType) {
* The provided media type should not include any parameters. This method is suitable for parsing part of the {@code Content-Type}
* HTTP header. This method will return {@code null} if no match is found
*/
public static XContentType fromMediaType(String mediaType) {
public static XContentType fromMediaType(String mediaTypeHeaderValue) {
String mediaType = parseMediaType(mediaTypeHeaderValue);

final String lowercaseMediaType = Objects.requireNonNull(mediaType, "mediaType cannot be null").toLowerCase(Locale.ROOT);
for (XContentType type : values()) {
if (type.mediaTypeWithoutParameters().equals(lowercaseMediaType)) {
Expand All @@ -157,6 +165,27 @@ public static XContentType fromMediaType(String mediaType) {
return null;
}

public static String parseMediaType(String mediaType) {
if (mediaType != null) {
Matcher matcher = COMPATIBLE_API_HEADER_PATTERN.matcher(mediaType);
if (matcher.find()) {
return "application/" + matcher.group(2).toLowerCase(Locale.ROOT);
}
}

return mediaType;
}

public static String parseVersion(String mediaType){
if(mediaType != null){
Matcher matcher = COMPATIBLE_API_HEADER_PATTERN.matcher(mediaType);
if (matcher.find() && "vnd.elasticsearch+".equalsIgnoreCase(matcher.group(1))) {

return matcher.group(4);
}
}
return null;
}
private static boolean isSameMediaTypeOrFormatAs(String stringType, XContentType type) {
return type.mediaTypeWithoutParameters().equalsIgnoreCase(stringType) ||
stringType.toLowerCase(Locale.ROOT).startsWith(type.mediaTypeWithoutParameters().toLowerCase(Locale.ROOT) + ";") ||
Expand Down
26 changes: 26 additions & 0 deletions modules/rest-compatibility/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/

esplugin {
description 'Adds a compatiblity layer for the prior major versions REST API'
classname 'org.elasticsearch.rest.compat.RestCompatPlugin'
}

integTest.enabled = false
Copy link
Member

Choose a reason for hiding this comment

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

I see tests in this PR; why are they disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they should be enabled. fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually there are no integTests, but there are unit tests so integTests.enabled = false tests.enabled=true

test.enabled = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.rest.compat;

import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.compat.version7.RestGetActionV7;
import org.elasticsearch.rest.compat.version7.RestIndexActionV7;

import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

public class RestCompatPlugin extends Plugin implements ActionPlugin {

@Override
public List<RestHandler> getRestHandlers(
Settings settings,
RestController restController,
ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings,
SettingsFilter settingsFilter,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster
) {
if (Version.CURRENT.major == 8) {
return List.of(
new RestGetActionV7(),
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't we never have more than one major versions worth of compat in a single version? If that is the case, why do we need to version the class names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a narrow window where 2 versions can be present.
Let's say we released version 8 (or a feature freeze) and we are now already developing ES9 and there will be features compatible with v8.
We won't be able to remove the v7 compat features instantly so for some time there will be both.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am +1 on using the version in the class for this reason, and for the obviousness they provide. While (arguably) ugly, these classes are special in that they are not intended to be maintained, rather they are intended to be deleted after a some point in time. Having the version in the name makes is more obvious and helps to prevent accidental modifications (like from a simple class search) and makes it really obvious if these things exist for too long.

new RestIndexActionV7.CompatibleRestIndexAction(),
new RestIndexActionV7.CompatibleCreateHandler(),
new RestIndexActionV7.CompatibleAutoIdHandler(nodesInCluster)
);
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.rest.compat.version7;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.Version;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.document.RestGetAction;

import java.io.IOException;
import java.util.List;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.HEAD;

public class RestGetActionV7 extends RestGetAction {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetAction.class));
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in "
+ "document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.";

@Override
public List<Route> routes() {
assert Version.CURRENT.major == 8 : "REST API compatibility for version 7 is only supported on version 8";

return List.of(new Route(GET, "/{index}/{type}/{id}"), new Route(HEAD, "/{index}/{type}/{id}"));
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient client) throws IOException {
deprecationLogger.deprecatedAndMaybeLog("get_with_types", TYPES_DEPRECATION_MESSAGE);
request.param("type");
return super.prepareRequest(request, client);
}

@Override
public boolean compatibilityRequired() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.rest.compat.version7;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.Version;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.document.RestIndexAction;

import java.io.IOException;
import java.util.List;
import java.util.function.Supplier;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;

public class RestIndexActionV7 {
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in document "
+ "index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, "
+ "or /{index}/_create/{id}).";
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestIndexAction.class));

private static void logDeprecationMessage() {
deprecationLogger.deprecatedAndMaybeLog("index_with_types", TYPES_DEPRECATION_MESSAGE);
}

public static class CompatibleRestIndexAction extends RestIndexAction {
@Override
public List<Route> routes() {
assert Version.CURRENT.major == 8 : "REST API compatilbity for version 7 is only supported on version 8";

return List.of(new Route(POST, "/{index}/{type}/{id}"), new Route(PUT, "/{index}/{type}/{id}"));
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient client) throws IOException {
logDeprecationMessage();
request.param("type");
return super.prepareRequest(request, client);
}

@Override
public boolean compatibilityRequired() {
return true;
}
}

public static class CompatibleCreateHandler extends RestIndexAction.CreateHandler {
@Override
public List<Route> routes() {
return unmodifiableList(
asList(new Route(POST, "/{index}/{type}/{id}/_create"), new Route(PUT, "/{index}/{type}/{id}/_create"))
);
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient client) throws IOException {
logDeprecationMessage();
request.param("type");
return super.prepareRequest(request, client);
}

@Override
public boolean compatibilityRequired() {
return true;
}
}

public static final class CompatibleAutoIdHandler extends RestIndexAction.AutoIdHandler {

public CompatibleAutoIdHandler(Supplier<DiscoveryNodes> nodesInCluster) {
super(nodesInCluster);
}

@Override
public List<Route> routes() {
return singletonList(new Route(POST, "/{index}/{type}"));
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient client) throws IOException {
logDeprecationMessage();
request.param("type");
return super.prepareRequest(request, client);
}

@Override
public boolean compatibilityRequired() {
return true;
}
}
}
Loading