Skip to content

Commit 5bcc6fc

Browse files
authored
Remove DeprecationLogger from route objects (#52285)
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 #51950 Backport of #52278
1 parent 8c930a9 commit 5bcc6fc

File tree

113 files changed

+184
-682
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+184
-682
lines changed

qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestDeprecationHeaderRestAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public TestDeprecationHeaderRestAction(Settings settings) {
8484
@Override
8585
public List<DeprecatedRoute> deprecatedRoutes() {
8686
return singletonList(
87-
new DeprecatedRoute(GET, "/_test_cluster/deprecated_settings", DEPRECATED_ENDPOINT, deprecationLogger));
87+
new DeprecatedRoute(GET, "/_test_cluster/deprecated_settings", DEPRECATED_ENDPOINT));
8888
}
8989

9090
@Override

server/src/main/java/org/elasticsearch/rest/RestController.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
public class RestController implements HttpServerTransport.Dispatcher {
6565

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

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

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

105-
registerHandler(method, path, new DeprecationRestHandler(handler, deprecationMessage, logger));
104+
registerHandler(method, path, new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger));
106105
}
107106

108107
/**
@@ -128,17 +127,15 @@ protected void registerAsDeprecatedHandler(RestRequest.Method method, String pat
128127
* @param handler The handler to actually execute
129128
* @param deprecatedMethod GET, POST, etc.
130129
* @param deprecatedPath <em>Deprecated</em> path to handle (e.g., "/_optimize")
131-
* @param logger The existing deprecation logger to use
132130
*/
133131
protected void registerWithDeprecatedHandler(RestRequest.Method method, String path, RestHandler handler,
134-
RestRequest.Method deprecatedMethod, String deprecatedPath,
135-
DeprecationLogger logger) {
132+
RestRequest.Method deprecatedMethod, String deprecatedPath) {
136133
// e.g., [POST /_optimize] is deprecated! Use [POST /_forcemerge] instead.
137134
final String deprecationMessage =
138135
"[" + deprecatedMethod.name() + " " + deprecatedPath + "] is deprecated! Use [" + method.name() + " " + path + "] instead.";
139136

140137
registerHandler(method, path, handler);
141-
registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage, logger);
138+
registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage);
142139
}
143140

144141
/**
@@ -164,9 +161,9 @@ protected void registerHandler(RestRequest.Method method, String path, RestHandl
164161
public void registerHandler(final RestHandler restHandler) {
165162
restHandler.routes().forEach(route -> registerHandler(route.getMethod(), route.getPath(), restHandler));
166163
restHandler.deprecatedRoutes().forEach(route ->
167-
registerAsDeprecatedHandler(route.getMethod(), route.getPath(), restHandler, route.getDeprecationMessage(), route.getLogger()));
164+
registerAsDeprecatedHandler(route.getMethod(), route.getPath(), restHandler, route.getDeprecationMessage()));
168165
restHandler.replacedRoutes().forEach(route -> registerWithDeprecatedHandler(route.getMethod(), route.getPath(),
169-
restHandler, route.getDeprecatedMethod(), route.getDeprecatedPath(), route.getLogger()));
166+
restHandler, route.getDeprecatedMethod(), route.getDeprecatedPath()));
170167
}
171168

172169
@Override

server/src/main/java/org/elasticsearch/rest/RestHandler.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.elasticsearch.rest;
2121

2222
import org.elasticsearch.client.node.NodeClient;
23-
import org.elasticsearch.common.logging.DeprecationLogger;
2423
import org.elasticsearch.common.xcontent.XContent;
2524
import org.elasticsearch.rest.RestRequest.Method;
2625

@@ -115,21 +114,15 @@ public Method getMethod() {
115114
class DeprecatedRoute extends Route {
116115

117116
private final String deprecationMessage;
118-
private final DeprecationLogger logger;
119117

120-
public DeprecatedRoute(Method method, String path, String deprecationMessage, DeprecationLogger logger) {
118+
public DeprecatedRoute(Method method, String path, String deprecationMessage) {
121119
super(method, path);
122120
this.deprecationMessage = deprecationMessage;
123-
this.logger = logger;
124121
}
125122

126123
public String getDeprecationMessage() {
127124
return deprecationMessage;
128125
}
129-
130-
public DeprecationLogger getLogger() {
131-
return logger;
132-
}
133126
}
134127

135128
/**
@@ -140,13 +133,11 @@ class ReplacedRoute extends Route {
140133

141134
private final String deprecatedPath;
142135
private final Method deprecatedMethod;
143-
private final DeprecationLogger logger;
144136

145-
public ReplacedRoute(Method method, String path, Method deprecatedMethod, String deprecatedPath, DeprecationLogger logger) {
137+
public ReplacedRoute(Method method, String path, Method deprecatedMethod, String deprecatedPath) {
146138
super(method, path);
147139
this.deprecatedMethod = deprecatedMethod;
148140
this.deprecatedPath = deprecatedPath;
149-
this.logger = logger;
150141
}
151142

152143
public String getDeprecatedPath() {
@@ -156,9 +147,5 @@ public String getDeprecatedPath() {
156147
public Method getDeprecatedMethod() {
157148
return deprecatedMethod;
158149
}
159-
160-
public DeprecationLogger getLogger() {
161-
return logger;
162-
}
163150
}
164151
}

server/src/test/java/org/elasticsearch/rest/RestControllerTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.common.bytes.BytesArray;
2525
import org.elasticsearch.common.bytes.BytesReference;
2626
import org.elasticsearch.common.component.AbstractLifecycleComponent;
27-
import org.elasticsearch.common.logging.DeprecationLogger;
2827
import org.elasticsearch.common.settings.ClusterSettings;
2928
import org.elasticsearch.common.settings.Settings;
3029
import org.elasticsearch.common.transport.BoundTransportAddress;
@@ -179,12 +178,11 @@ public void testRegisterAsDeprecatedHandler() {
179178
String path = "/_" + randomAlphaOfLengthBetween(1, 6);
180179
RestHandler handler = mock(RestHandler.class);
181180
String deprecationMessage = randomAlphaOfLengthBetween(1, 10);
182-
DeprecationLogger logger = mock(DeprecationLogger.class);
183181

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

187-
controller.registerAsDeprecatedHandler(method, path, handler, deprecationMessage, logger);
185+
controller.registerAsDeprecatedHandler(method, path, handler, deprecationMessage);
188186

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

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

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

208-
controller.registerWithDeprecatedHandler(method, path, handler, deprecatedMethod, deprecatedPath, logger);
205+
controller.registerWithDeprecatedHandler(method, path, handler, deprecatedMethod, deprecatedPath);
209206

210207
verify(controller).registerHandler(method, path, handler);
211-
verify(controller).registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage, logger);
208+
verify(controller).registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage);
212209
}
213210

214211
public void testRegisterSecondMethodWithDifferentNamedWildcard() {

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestDeleteLicenseAction.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
1210
import org.elasticsearch.rest.RestRequest;
1311
import org.elasticsearch.rest.action.RestToXContentListener;
@@ -23,8 +21,6 @@
2321

2422
public class RestDeleteLicenseAction extends XPackRestHandler {
2523

26-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestDeleteLicenseAction.class));
27-
2824
RestDeleteLicenseAction() {}
2925

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

3531
@Override
3632
public List<ReplacedRoute> replacedRoutes() {
37-
return singletonList(new ReplacedRoute(DELETE, "/_license", DELETE, URI_BASE + "/license", deprecationLogger));
33+
return singletonList(new ReplacedRoute(DELETE, "/_license", DELETE, URI_BASE + "/license"));
3834
}
3935

4036
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.rest.RestRequest;
1210
import org.elasticsearch.rest.action.RestToXContentListener;
1311
import org.elasticsearch.xpack.core.XPackClient;
@@ -21,8 +19,6 @@
2119

2220
public class RestGetBasicStatus extends XPackRestHandler {
2321

24-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetBasicStatus.class));
25-
2622
RestGetBasicStatus() {}
2723

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

3329
@Override
3430
public List<ReplacedRoute> replacedRoutes() {
35-
return singletonList(new ReplacedRoute(GET, "/_license/basic_status", GET, URI_BASE + "/license/basic_status", deprecationLogger));
31+
return singletonList(new ReplacedRoute(GET, "/_license/basic_status", GET, URI_BASE + "/license/basic_status"));
3632
}
3733

3834
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetLicenseAction.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.common.xcontent.ToXContent;
1210
import org.elasticsearch.common.xcontent.XContentBuilder;
1311
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
@@ -31,8 +29,6 @@
3129

3230
public class RestGetLicenseAction extends XPackRestHandler {
3331

34-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetLicenseAction.class));
35-
3632
RestGetLicenseAction() {}
3733

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

4339
@Override
4440
public List<ReplacedRoute> replacedRoutes() {
45-
return singletonList(new ReplacedRoute(GET, "/_license", GET, URI_BASE + "/license", deprecationLogger));
41+
return singletonList(new ReplacedRoute(GET, "/_license", GET, URI_BASE + "/license"));
4642
}
4743

4844
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.rest.RestRequest;
1210
import org.elasticsearch.rest.action.RestToXContentListener;
1311
import org.elasticsearch.xpack.core.XPackClient;
@@ -21,8 +19,6 @@
2119

2220
public class RestGetTrialStatus extends XPackRestHandler {
2321

24-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestGetTrialStatus.class));
25-
2622
RestGetTrialStatus() {}
2723

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

3329
@Override
3430
public List<ReplacedRoute> replacedRoutes() {
35-
return singletonList(new ReplacedRoute(GET, "/_license/trial_status", GET, URI_BASE + "/license/trial_status", deprecationLogger));
31+
return singletonList(new ReplacedRoute(GET, "/_license/trial_status", GET, URI_BASE + "/license/trial_status"));
3632
}
3733

3834
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.rest.RestRequest;
1210
import org.elasticsearch.rest.action.RestStatusToXContentListener;
1311
import org.elasticsearch.xpack.core.XPackClient;
@@ -22,8 +20,6 @@
2220

2321
public class RestPostStartBasicLicense extends XPackRestHandler {
2422

25-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestPostStartBasicLicense.class));
26-
2723
RestPostStartBasicLicense() {}
2824

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

3430
@Override
3531
public List<ReplacedRoute> replacedRoutes() {
36-
return singletonList(new ReplacedRoute(POST, "/_license/start_basic", POST, URI_BASE + "/license/start_basic", deprecationLogger));
32+
return singletonList(new ReplacedRoute(POST, "/_license/start_basic", POST, URI_BASE + "/license/start_basic"));
3733
}
3834

3935
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartTrialLicense.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.common.xcontent.XContentBuilder;
1210
import org.elasticsearch.rest.BytesRestResponse;
1311
import org.elasticsearch.rest.RestRequest;
@@ -26,8 +24,6 @@
2624

2725
public class RestPostStartTrialLicense extends XPackRestHandler {
2826

29-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestPostStartTrialLicense.class));
30-
3127
RestPostStartTrialLicense() {}
3228

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

3834
@Override
3935
public List<ReplacedRoute> replacedRoutes() {
40-
return singletonList(new ReplacedRoute(POST, "/_license/start_trial", POST, URI_BASE + "/license/start_trial", deprecationLogger));
36+
return singletonList(new ReplacedRoute(POST, "/_license/start_trial", POST, URI_BASE + "/license/start_trial"));
4137
}
4238

4339
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
package org.elasticsearch.license;
88

9-
import org.apache.logging.log4j.LogManager;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.rest.RestRequest;
1210
import org.elasticsearch.rest.action.RestToXContentListener;
1311
import org.elasticsearch.xpack.core.XPackClient;
@@ -22,8 +20,6 @@
2220

2321
public class RestPutLicenseAction extends XPackRestHandler {
2422

25-
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestPutLicenseAction.class));
26-
2723
RestPutLicenseAction() {}
2824

2925
@Override
@@ -35,8 +31,8 @@ public List<Route> routes() {
3531
public List<ReplacedRoute> replacedRoutes() {
3632
return unmodifiableList(asList(
3733
// TODO: remove POST endpoint?
38-
new ReplacedRoute(POST, "/_license", POST, URI_BASE + "/license", deprecationLogger),
39-
new ReplacedRoute(PUT, "/_license", PUT, URI_BASE + "/license", deprecationLogger)));
34+
new ReplacedRoute(POST, "/_license", POST, URI_BASE + "/license"),
35+
new ReplacedRoute(PUT, "/_license", PUT, URI_BASE + "/license")));
4036
}
4137

4238
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/rest/RestGetCertificateInfoAction.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.core.ssl.rest;
77

8-
import org.apache.logging.log4j.LogManager;
98
import org.elasticsearch.client.node.NodeClient;
10-
import org.elasticsearch.common.logging.DeprecationLogger;
119
import org.elasticsearch.common.xcontent.XContentBuilder;
1210
import org.elasticsearch.rest.BaseRestHandler;
1311
import org.elasticsearch.rest.BytesRestResponse;
@@ -30,17 +28,14 @@
3028
*/
3129
public class RestGetCertificateInfoAction extends BaseRestHandler {
3230

33-
private static final DeprecationLogger deprecationLogger =
34-
new DeprecationLogger(LogManager.getLogger(RestGetCertificateInfoAction.class));
35-
3631
@Override
3732
public List<Route> routes() {
3833
return Collections.emptyList();
3934
}
4035

4136
@Override
4237
public List<ReplacedRoute> replacedRoutes() {
43-
return singletonList(new ReplacedRoute(GET, "/_ssl/certificates", GET, "/_xpack/ssl/certificates", deprecationLogger));
38+
return singletonList(new ReplacedRoute(GET, "/_ssl/certificates", GET, "/_xpack/ssl/certificates"));
4439
}
4540

4641
@Override

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/RestDeprecationInfoAction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public List<Route> routes() {
3737
@Override
3838
public List<ReplacedRoute> replacedRoutes() {
3939
return unmodifiableList(asList(
40-
new ReplacedRoute(GET, "/_migration/deprecations", GET, "/_xpack/migration/deprecations", deprecationLogger),
41-
new ReplacedRoute(
42-
GET, "/{index}/_migration/deprecations", GET, "/{index}/_xpack/migration/deprecations", deprecationLogger)));
40+
new ReplacedRoute(GET, "/_migration/deprecations", GET, "/_xpack/migration/deprecations"),
41+
new ReplacedRoute(GET, "/{index}/_migration/deprecations", GET, "/{index}/_xpack/migration/deprecations")));
4342
}
4443

4544
@Override

0 commit comments

Comments
 (0)