Skip to content

Commit 10f5ffb

Browse files
committed
HLRC: Ban LoggingDeprecationHandler (#32756)
LoggingDeprecationHandler requires log4j2 but we don't require log4j2 in the client. This bans LoggingDeprecationHandler and removes all uses of it in the high level client. Closes #32151
1 parent 5bb20f5 commit 10f5ffb

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
import org.elasticsearch.common.bytes.BytesReference;
9696
import org.elasticsearch.common.lucene.uid.Versions;
9797
import org.elasticsearch.common.unit.TimeValue;
98-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
98+
import org.elasticsearch.common.xcontent.DeprecationHandler;
9999
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
100100
import org.elasticsearch.common.xcontent.ToXContent;
101101
import org.elasticsearch.common.xcontent.XContent;
@@ -425,8 +425,14 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
425425
BytesReference indexSource = indexRequest.source();
426426
XContentType indexXContentType = indexRequest.getContentType();
427427

428-
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
429-
LoggingDeprecationHandler.INSTANCE, indexSource, indexXContentType)) {
428+
try (XContentParser parser = XContentHelper.createParser(
429+
/*
430+
* EMPTY and THROW are fine here because we just call
431+
* copyCurrentStructure which doesn't touch the
432+
* registry or deprecation.
433+
*/
434+
NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
435+
indexSource, indexXContentType)) {
430436
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
431437
builder.copyCurrentStructure(parser);
432438
source = BytesReference.bytes(builder).toBytesRef();

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.elasticsearch.common.CheckedFunction;
6060
import org.elasticsearch.common.ParseField;
6161
import org.elasticsearch.common.xcontent.ContextParser;
62-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
62+
import org.elasticsearch.common.xcontent.DeprecationHandler;
6363
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
6464
import org.elasticsearch.common.xcontent.XContentParser;
6565
import org.elasticsearch.common.xcontent.XContentType;
@@ -1402,8 +1402,7 @@ protected final <Resp> Resp parseEntity(final HttpEntity entity,
14021402
if (xContentType == null) {
14031403
throw new IllegalStateException("Unsupported Content-Type: " + entity.getContentType().getValue());
14041404
}
1405-
try (XContentParser parser = xContentType.xContent().createParser(registry,
1406-
LoggingDeprecationHandler.INSTANCE, entity.getContent())) {
1405+
try (XContentParser parser = xContentType.xContent().createParser(registry, DEPRECATION_HANDLER, entity.getContent())) {
14071406
return entityParser.apply(parser);
14081407
}
14091408
}
@@ -1421,6 +1420,19 @@ static boolean convertExistsResponse(Response response) {
14211420
return response.getStatusLine().getStatusCode() == 200;
14221421
}
14231422

1423+
/**
1424+
* Ignores deprecation warnings. This is appropriate because it is only
1425+
* used to parse responses from Elasticsearch. Any deprecation warnings
1426+
* emitted there just mean that you are talking to an old version of
1427+
* Elasticsearch. There isn't anything you can do about the deprecation.
1428+
*/
1429+
private static final DeprecationHandler DEPRECATION_HANDLER = new DeprecationHandler() {
1430+
@Override
1431+
public void usedDeprecatedName(String usedName, String modernName) {}
1432+
@Override
1433+
public void usedDeprecatedField(String usedName, String replacedWith) {}
1434+
};
1435+
14241436
static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
14251437
Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();
14261438
map.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));

client/rest-high-level/src/main/resources/forbidden/rest-high-level-signatures.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ org.apache.http.entity.ContentType#create(java.lang.String)
1919
org.apache.http.entity.ContentType#create(java.lang.String,java.lang.String)
2020
org.apache.http.entity.ContentType#create(java.lang.String,java.nio.charset.Charset)
2121
org.apache.http.entity.ContentType#create(java.lang.String,org.apache.http.NameValuePair[])
22+
23+
@defaultMessage We can't rely on log4j2 being on the classpath so don't log deprecations!
24+
org.elasticsearch.common.xcontent.LoggingDeprecationHandler

client/rest-high-level/src/test/java/org/elasticsearch/client/SyncedFlushResponseTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.elasticsearch.cluster.routing.ShardRoutingState;
2525
import org.elasticsearch.cluster.routing.TestShardRouting;
2626
import org.elasticsearch.common.bytes.BytesReference;
27-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
27+
import org.elasticsearch.common.xcontent.DeprecationHandler;
2828
import org.elasticsearch.common.xcontent.ToXContent;
2929
import org.elasticsearch.common.xcontent.XContentBuilder;
3030
import org.elasticsearch.common.xcontent.XContentParser;
@@ -63,7 +63,7 @@ public void testXContentSerialization() throws IOException {
6363
.xContent()
6464
.createParser(
6565
xContentRegistry(),
66-
LoggingDeprecationHandler.INSTANCE,
66+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
6767
BytesReference.bytes(serverResponsebuilder).streamInput()
6868
).map()
6969
);
@@ -74,7 +74,7 @@ public void testXContentSerialization() throws IOException {
7474
.xContent()
7575
.createParser(
7676
xContentRegistry(),
77-
LoggingDeprecationHandler.INSTANCE,
77+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
7878
BytesReference.bytes(clientResponsebuilder).streamInput()
7979
)
8080
.map()
@@ -94,7 +94,9 @@ public void testXContentDeserialization() throws IOException {
9494
.contentType()
9595
.xContent()
9696
.createParser(
97-
xContentRegistry(), LoggingDeprecationHandler.INSTANCE, BytesReference.bytes(builder).streamInput()
97+
xContentRegistry(),
98+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
99+
BytesReference.bytes(builder).streamInput()
98100
);
99101
SyncedFlushResponse originalResponse = plan.clientResult;
100102
SyncedFlushResponse parsedResponse = SyncedFlushResponse.fromXContent(parser);
@@ -175,7 +177,8 @@ TestPlan createTestPlan() throws IOException {
175177
.contentType()
176178
.xContent()
177179
.createParser(
178-
xContentRegistry(), LoggingDeprecationHandler.INSTANCE,
180+
xContentRegistry(),
181+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
179182
BytesReference.bytes(builder).streamInput()
180183
)
181184
.map();

0 commit comments

Comments
 (0)