Skip to content

Commit

Permalink
Remove DeprecationLogger from route objects (elastic#52278)
Browse files Browse the repository at this point in the history
This commit removes the need for DeprecatedRoute and ReplacedRoute to
have an instance of a DeprecationLogger. Instead the RestController now
has a DeprecationLogger that will be used for all deprecated and
replaced route messages.

Relates elastic#51950
  • Loading branch information
jaymode committed Feb 12, 2020
1 parent 0c18893 commit 00e9d45
Show file tree
Hide file tree
Showing 113 changed files with 184 additions and 680 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public TestDeprecationHeaderRestAction(Settings settings) {
@Override
public List<DeprecatedRoute> deprecatedRoutes() {
return singletonList(
new DeprecatedRoute(GET, "/_test_cluster/deprecated_settings", DEPRECATED_ENDPOINT, deprecationLogger));
new DeprecatedRoute(GET, "/_test_cluster/deprecated_settings", DEPRECATED_ENDPOINT));
}

@Override
Expand Down
17 changes: 7 additions & 10 deletions server/src/main/java/org/elasticsearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
public class RestController implements HttpServerTransport.Dispatcher {

private static final Logger logger = LogManager.getLogger(RestController.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);

private final PathTrie<MethodHandlers> handlers = new PathTrie<>(RestUtils.REST_DECODER);

Expand Down Expand Up @@ -96,13 +97,11 @@ public RestController(Set<RestHeaderDefinition> headersToCopy, UnaryOperator<Res
* @param path Path to handle (e.g., "/{index}/{type}/_bulk")
* @param handler The handler to actually execute
* @param deprecationMessage The message to log and send as a header in the response
* @param logger The existing deprecation logger to use
*/
protected void registerAsDeprecatedHandler(RestRequest.Method method, String path, RestHandler handler,
String deprecationMessage, DeprecationLogger logger) {
protected void registerAsDeprecatedHandler(RestRequest.Method method, String path, RestHandler handler, String deprecationMessage) {
assert (handler instanceof DeprecationRestHandler) == false;

registerHandler(method, path, new DeprecationRestHandler(handler, deprecationMessage, logger));
registerHandler(method, path, new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger));
}

/**
Expand All @@ -128,17 +127,15 @@ protected void registerAsDeprecatedHandler(RestRequest.Method method, String pat
* @param handler The handler to actually execute
* @param deprecatedMethod GET, POST, etc.
* @param deprecatedPath <em>Deprecated</em> path to handle (e.g., "/_optimize")
* @param logger The existing deprecation logger to use
*/
protected void registerWithDeprecatedHandler(RestRequest.Method method, String path, RestHandler handler,
RestRequest.Method deprecatedMethod, String deprecatedPath,
DeprecationLogger logger) {
RestRequest.Method deprecatedMethod, String deprecatedPath) {
// e.g., [POST /_optimize] is deprecated! Use [POST /_forcemerge] instead.
final String deprecationMessage =
"[" + deprecatedMethod.name() + " " + deprecatedPath + "] is deprecated! Use [" + method.name() + " " + path + "] instead.";

registerHandler(method, path, handler);
registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage, logger);
registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage);
}

/**
Expand All @@ -164,9 +161,9 @@ protected void registerHandler(RestRequest.Method method, String path, RestHandl
public void registerHandler(final RestHandler restHandler) {
restHandler.routes().forEach(route -> registerHandler(route.getMethod(), route.getPath(), restHandler));
restHandler.deprecatedRoutes().forEach(route ->
registerAsDeprecatedHandler(route.getMethod(), route.getPath(), restHandler, route.getDeprecationMessage(), route.getLogger()));
registerAsDeprecatedHandler(route.getMethod(), route.getPath(), restHandler, route.getDeprecationMessage()));
restHandler.replacedRoutes().forEach(route -> registerWithDeprecatedHandler(route.getMethod(), route.getPath(),
restHandler, route.getDeprecatedMethod(), route.getDeprecatedPath(), route.getLogger()));
restHandler, route.getDeprecatedMethod(), route.getDeprecatedPath()));
}

@Override
Expand Down
17 changes: 2 additions & 15 deletions server/src/main/java/org/elasticsearch/rest/RestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.rest;

import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.rest.RestRequest.Method;

Expand Down Expand Up @@ -115,21 +114,15 @@ public Method getMethod() {
class DeprecatedRoute extends Route {

private final String deprecationMessage;
private final DeprecationLogger logger;

public DeprecatedRoute(Method method, String path, String deprecationMessage, DeprecationLogger logger) {
public DeprecatedRoute(Method method, String path, String deprecationMessage) {
super(method, path);
this.deprecationMessage = deprecationMessage;
this.logger = logger;
}

public String getDeprecationMessage() {
return deprecationMessage;
}

public DeprecationLogger getLogger() {
return logger;
}
}

/**
Expand All @@ -140,13 +133,11 @@ class ReplacedRoute extends Route {

private final String deprecatedPath;
private final Method deprecatedMethod;
private final DeprecationLogger logger;

public ReplacedRoute(Method method, String path, Method deprecatedMethod, String deprecatedPath, DeprecationLogger logger) {
public ReplacedRoute(Method method, String path, Method deprecatedMethod, String deprecatedPath) {
super(method, path);
this.deprecatedMethod = deprecatedMethod;
this.deprecatedPath = deprecatedPath;
this.logger = logger;
}

public String getDeprecatedPath() {
Expand All @@ -156,9 +147,5 @@ public String getDeprecatedPath() {
public Method getDeprecatedMethod() {
return deprecatedMethod;
}

public DeprecationLogger getLogger() {
return logger;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress;
Expand Down Expand Up @@ -179,12 +178,11 @@ public void testRegisterAsDeprecatedHandler() {
String path = "/_" + randomAlphaOfLengthBetween(1, 6);
RestHandler handler = mock(RestHandler.class);
String deprecationMessage = randomAlphaOfLengthBetween(1, 10);
DeprecationLogger logger = mock(DeprecationLogger.class);

// don't want to test everything -- just that it actually wraps the handler
doCallRealMethod().when(controller).registerAsDeprecatedHandler(method, path, handler, deprecationMessage, logger);
doCallRealMethod().when(controller).registerAsDeprecatedHandler(method, path, handler, deprecationMessage);

controller.registerAsDeprecatedHandler(method, path, handler, deprecationMessage, logger);
controller.registerAsDeprecatedHandler(method, path, handler, deprecationMessage);

verify(controller).registerHandler(eq(method), eq(path), any(DeprecationRestHandler.class));
}
Expand All @@ -197,18 +195,17 @@ public void testRegisterWithDeprecatedHandler() {
final RestHandler handler = mock(RestHandler.class);
final RestRequest.Method deprecatedMethod = randomFrom(RestRequest.Method.values());
final String deprecatedPath = "/_" + randomAlphaOfLengthBetween(1, 6);
final DeprecationLogger logger = mock(DeprecationLogger.class);

final String deprecationMessage = "[" + deprecatedMethod.name() + " " + deprecatedPath + "] is deprecated! Use [" +
method.name() + " " + path + "] instead.";

// don't want to test everything -- just that it actually wraps the handlers
doCallRealMethod().when(controller).registerWithDeprecatedHandler(method, path, handler, deprecatedMethod, deprecatedPath, logger);
doCallRealMethod().when(controller).registerWithDeprecatedHandler(method, path, handler, deprecatedMethod, deprecatedPath);

controller.registerWithDeprecatedHandler(method, path, handler, deprecatedMethod, deprecatedPath, logger);
controller.registerWithDeprecatedHandler(method, path, handler, deprecatedMethod, deprecatedPath);

verify(controller).registerHandler(method, path, handler);
verify(controller).registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage, logger);
verify(controller).registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage);
}

public void testRegisterSecondMethodWithDifferentNamedWildcard() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
Expand All @@ -23,8 +21,6 @@

public class RestDeleteLicenseAction extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestDeleteLicenseAction.class));

RestDeleteLicenseAction() {}

@Override
Expand All @@ -34,7 +30,7 @@ public List<Route> routes() {

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(DELETE, "/_license", DELETE, URI_BASE + "/license", deprecationLogger));
return singletonList(new ReplacedRoute(DELETE, "/_license", DELETE, URI_BASE + "/license"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.core.XPackClient;
Expand All @@ -21,8 +19,6 @@

public class RestGetBasicStatus extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetBasicStatus.class));

RestGetBasicStatus() {}

@Override
Expand All @@ -32,7 +28,7 @@ public List<Route> routes() {

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(GET, "/_license/basic_status", GET, URI_BASE + "/license/basic_status", deprecationLogger));
return singletonList(new ReplacedRoute(GET, "/_license/basic_status", GET, URI_BASE + "/license/basic_status"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
Expand All @@ -31,8 +29,6 @@

public class RestGetLicenseAction extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetLicenseAction.class));

RestGetLicenseAction() {}

@Override
Expand All @@ -42,7 +38,7 @@ public List<Route> routes() {

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(GET, "/_license", GET, URI_BASE + "/license", deprecationLogger));
return singletonList(new ReplacedRoute(GET, "/_license", GET, URI_BASE + "/license"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.core.XPackClient;
Expand All @@ -21,8 +19,6 @@

public class RestGetTrialStatus extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetTrialStatus.class));

RestGetTrialStatus() {}

@Override
Expand All @@ -32,7 +28,7 @@ public List<Route> routes() {

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(GET, "/_license/trial_status", GET, URI_BASE + "/license/trial_status", deprecationLogger));
return singletonList(new ReplacedRoute(GET, "/_license/trial_status", GET, URI_BASE + "/license/trial_status"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestStatusToXContentListener;
import org.elasticsearch.xpack.core.XPackClient;
Expand All @@ -22,8 +20,6 @@

public class RestPostStartBasicLicense extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestPostStartBasicLicense.class));

RestPostStartBasicLicense() {}

@Override
Expand All @@ -33,7 +29,7 @@ public List<Route> routes() {

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(POST, "/_license/start_basic", POST, URI_BASE + "/license/start_basic", deprecationLogger));
return singletonList(new ReplacedRoute(POST, "/_license/start_basic", POST, URI_BASE + "/license/start_basic"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
Expand All @@ -26,8 +24,6 @@

public class RestPostStartTrialLicense extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestPostStartTrialLicense.class));

RestPostStartTrialLicense() {}

@Override
Expand All @@ -37,7 +33,7 @@ public List<Route> routes() {

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(POST, "/_license/start_trial", POST, URI_BASE + "/license/start_trial", deprecationLogger));
return singletonList(new ReplacedRoute(POST, "/_license/start_trial", POST, URI_BASE + "/license/start_trial"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.elasticsearch.license;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.core.XPackClient;
Expand All @@ -22,8 +20,6 @@

public class RestPutLicenseAction extends XPackRestHandler {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestPutLicenseAction.class));

RestPutLicenseAction() {}

@Override
Expand All @@ -35,8 +31,8 @@ public List<Route> routes() {
public List<ReplacedRoute> replacedRoutes() {
return unmodifiableList(asList(
// TODO: remove POST endpoint?
new ReplacedRoute(POST, "/_license", POST, URI_BASE + "/license", deprecationLogger),
new ReplacedRoute(PUT, "/_license", PUT, URI_BASE + "/license", deprecationLogger)));
new ReplacedRoute(POST, "/_license", POST, URI_BASE + "/license"),
new ReplacedRoute(PUT, "/_license", PUT, URI_BASE + "/license")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@
*/
public class RestGetCertificateInfoAction extends BaseRestHandler {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(RestGetCertificateInfoAction.class));

@Override
public List<Route> routes() {
return Collections.emptyList();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return singletonList(new ReplacedRoute(GET, "/_ssl/certificates", GET, "/_xpack/ssl/certificates", deprecationLogger));
return singletonList(new ReplacedRoute(GET, "/_ssl/certificates", GET, "/_xpack/ssl/certificates"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public List<Route> routes() {
@Override
public List<ReplacedRoute> replacedRoutes() {
return unmodifiableList(asList(
new ReplacedRoute(GET, "/_migration/deprecations", GET, "/_xpack/migration/deprecations", deprecationLogger),
new ReplacedRoute(
GET, "/{index}/_migration/deprecations", GET, "/{index}/_xpack/migration/deprecations", deprecationLogger)));
new ReplacedRoute(GET, "/_migration/deprecations", GET, "/_xpack/migration/deprecations"),
new ReplacedRoute(GET, "/{index}/_migration/deprecations", GET, "/{index}/_xpack/migration/deprecations")));
}

@Override
Expand Down
Loading

0 comments on commit 00e9d45

Please sign in to comment.